UNPKG

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