no-shadow-restricted-names.js
1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @fileoverview Disallow shadowing of NaN, undefined, and Infinity (ES5 section 15.1.1)
* @author Michael Ficarra
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: "disallow identifiers from shadowing restricted names",
category: "Variables",
recommended: false
},
schema: []
},
create(context) {
const RESTRICTED = ["undefined", "NaN", "Infinity", "arguments", "eval"];
/**
* Check if the node name is present inside the restricted list
* @param {ASTNode} id id to evaluate
* @returns {void}
* @private
*/
function checkForViolation(id) {
if (RESTRICTED.indexOf(id.name) > -1) {
context.report({
node: id,
message: "Shadowing of global property '{{idName}}'.",
data: {
idName: id.name
}
});
}
}
return {
VariableDeclarator(node) {
checkForViolation(node.id);
},
ArrowFunctionExpression(node) {
[].map.call(node.params, checkForViolation);
},
FunctionExpression(node) {
if (node.id) {
checkForViolation(node.id);
}
[].map.call(node.params, checkForViolation);
},
FunctionDeclaration(node) {
if (node.id) {
checkForViolation(node.id);
[].map.call(node.params, checkForViolation);
}
},
CatchClause(node) {
checkForViolation(node.param);
}
};
}
};