[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Rule to flag variable leak in CatchClauses in IE 8 and earlier
|
---|
| 3 | * @author Ian Christian Myers
|
---|
| 4 | * @deprecated in ESLint v5.1.0
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | "use strict";
|
---|
| 8 |
|
---|
| 9 | //------------------------------------------------------------------------------
|
---|
| 10 | // Requirements
|
---|
| 11 | //------------------------------------------------------------------------------
|
---|
| 12 |
|
---|
| 13 | const astUtils = require("./utils/ast-utils");
|
---|
| 14 |
|
---|
| 15 | //------------------------------------------------------------------------------
|
---|
| 16 | // Rule Definition
|
---|
| 17 | //------------------------------------------------------------------------------
|
---|
| 18 |
|
---|
| 19 | /** @type {import('../shared/types').Rule} */
|
---|
| 20 | module.exports = {
|
---|
| 21 | meta: {
|
---|
| 22 | type: "suggestion",
|
---|
| 23 |
|
---|
| 24 | docs: {
|
---|
| 25 | description: "Disallow `catch` clause parameters from shadowing variables in the outer scope",
|
---|
| 26 | recommended: false,
|
---|
| 27 | url: "https://eslint.org/docs/latest/rules/no-catch-shadow"
|
---|
| 28 | },
|
---|
| 29 |
|
---|
| 30 | replacedBy: ["no-shadow"],
|
---|
| 31 |
|
---|
| 32 | deprecated: true,
|
---|
| 33 | schema: [],
|
---|
| 34 |
|
---|
| 35 | messages: {
|
---|
| 36 | mutable: "Value of '{{name}}' may be overwritten in IE 8 and earlier."
|
---|
| 37 | }
|
---|
| 38 | },
|
---|
| 39 |
|
---|
| 40 | create(context) {
|
---|
| 41 |
|
---|
| 42 | const sourceCode = context.sourceCode;
|
---|
| 43 |
|
---|
| 44 | //--------------------------------------------------------------------------
|
---|
| 45 | // Helpers
|
---|
| 46 | //--------------------------------------------------------------------------
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * Check if the parameters are been shadowed
|
---|
| 50 | * @param {Object} scope current scope
|
---|
| 51 | * @param {string} name parameter name
|
---|
| 52 | * @returns {boolean} True is its been shadowed
|
---|
| 53 | */
|
---|
| 54 | function paramIsShadowing(scope, name) {
|
---|
| 55 | return astUtils.getVariableByName(scope, name) !== null;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | //--------------------------------------------------------------------------
|
---|
| 59 | // Public API
|
---|
| 60 | //--------------------------------------------------------------------------
|
---|
| 61 |
|
---|
| 62 | return {
|
---|
| 63 |
|
---|
| 64 | "CatchClause[param!=null]"(node) {
|
---|
| 65 | let scope = sourceCode.getScope(node);
|
---|
| 66 |
|
---|
| 67 | /*
|
---|
| 68 | * When ecmaVersion >= 6, CatchClause creates its own scope
|
---|
| 69 | * so start from one upper scope to exclude the current node
|
---|
| 70 | */
|
---|
| 71 | if (scope.block === node) {
|
---|
| 72 | scope = scope.upper;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | if (paramIsShadowing(scope, node.param.name)) {
|
---|
| 76 | context.report({ node, messageId: "mutable", data: { name: node.param.name } });
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | };
|
---|
| 80 |
|
---|
| 81 | }
|
---|
| 82 | };
|
---|