UNPKG

1.87 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag variable leak in CatchClauses in IE 8 and earlier
3 * @author Ian Christian Myers
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12var astUtils = require("../ast-utils");
13
14//------------------------------------------------------------------------------
15// Rule Definition
16//------------------------------------------------------------------------------
17
18module.exports = function(context) {
19
20 //--------------------------------------------------------------------------
21 // Helpers
22 //--------------------------------------------------------------------------
23
24 /**
25 * Check if the parameters are been shadowed
26 * @param {object} scope current scope
27 * @param {string} name parameter name
28 * @returns {boolean} True is its been shadowed
29 */
30 function paramIsShadowing(scope, name) {
31 return astUtils.getVariableByName(scope, name) !== null;
32 }
33
34 //--------------------------------------------------------------------------
35 // Public API
36 //--------------------------------------------------------------------------
37
38 return {
39
40 "CatchClause": function(node) {
41 var scope = context.getScope();
42
43 // When blockBindings is enabled, CatchClause creates its own scope
44 // so start from one upper scope to exclude the current node
45 if (scope.block === node) {
46 scope = scope.upper;
47 }
48
49 if (paramIsShadowing(scope, node.param.name)) {
50 context.report(node, "Value of '{{name}}' may be overwritten in IE 8 and earlier.",
51 { name: node.param.name });
52 }
53 }
54 };
55
56};
57
58module.exports.schema = [];