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 | },
|
25 |
|
26 | schema: []
|
27 | },
|
28 |
|
29 | create(context) {
|
30 |
|
31 | //--------------------------------------------------------------------------
|
32 | // Helpers
|
33 | //--------------------------------------------------------------------------
|
34 |
|
35 | /**
|
36 | * Check if the parameters are been shadowed
|
37 | * @param {Object} scope current scope
|
38 | * @param {string} name parameter name
|
39 | * @returns {boolean} True is its been shadowed
|
40 | */
|
41 | function paramIsShadowing(scope, name) {
|
42 | return astUtils.getVariableByName(scope, name) !== null;
|
43 | }
|
44 |
|
45 | //--------------------------------------------------------------------------
|
46 | // Public API
|
47 | //--------------------------------------------------------------------------
|
48 |
|
49 | return {
|
50 |
|
51 | CatchClause(node) {
|
52 | let scope = context.getScope();
|
53 |
|
54 | // When blockBindings is enabled, CatchClause creates its own scope
|
55 | // so start from one upper scope to exclude the current node
|
56 | if (scope.block === node) {
|
57 | scope = scope.upper;
|
58 | }
|
59 |
|
60 | if (paramIsShadowing(scope, node.param.name)) {
|
61 | context.report(node, "Value of '{{name}}' may be overwritten in IE 8 and earlier.",
|
62 | { name: node.param.name });
|
63 | }
|
64 | }
|
65 | };
|
66 |
|
67 | }
|
68 | };
|