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 | ;
|
8 |
|
9 | //------------------------------------------------------------------------------
|
10 | // Requirements
|
11 | //------------------------------------------------------------------------------
|
12 |
|
13 | const astUtils = require("./utils/ast-utils");
|
14 |
|
15 | //------------------------------------------------------------------------------
|
16 | // Rule Definition
|
17 | //------------------------------------------------------------------------------
|
18 |
|
19 | module.exports = {
|
20 | meta: {
|
21 | type: "suggestion",
|
22 |
|
23 | docs: {
|
24 | description: "disallow `catch` clause parameters from shadowing variables in the outer scope",
|
25 | category: "Variables",
|
26 | recommended: false,
|
27 | url: "https://eslint.org/docs/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 | //--------------------------------------------------------------------------
|
43 | // Helpers
|
44 | //--------------------------------------------------------------------------
|
45 |
|
46 | /**
|
47 | * Check if the parameters are been shadowed
|
48 | * @param {Object} scope current scope
|
49 | * @param {string} name parameter name
|
50 | * @returns {boolean} True is its been shadowed
|
51 | */
|
52 | function paramIsShadowing(scope, name) {
|
53 | return astUtils.getVariableByName(scope, name) !== null;
|
54 | }
|
55 |
|
56 | //--------------------------------------------------------------------------
|
57 | // Public API
|
58 | //--------------------------------------------------------------------------
|
59 |
|
60 | return {
|
61 |
|
62 | "CatchClause[param!=null]"(node) {
|
63 | let scope = context.getScope();
|
64 |
|
65 | /*
|
66 | * When ecmaVersion >= 6, CatchClause creates its own scope
|
67 | * so start from one upper scope to exclude the current node
|
68 | */
|
69 | if (scope.block === node) {
|
70 | scope = scope.upper;
|
71 | }
|
72 |
|
73 | if (paramIsShadowing(scope, node.param.name)) {
|
74 | context.report({ node, messageId: "mutable", data: { name: node.param.name } });
|
75 | }
|
76 | }
|
77 | };
|
78 |
|
79 | }
|
80 | };
|