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