/
no-shadow-restricted-names.js
127 lines (122 loc) · 6.31 KB
/
no-shadow-restricted-names.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* @fileoverview Disallow shadowing of NaN, undefined, and Infinity (ES5 section 15.1.1)
* @author Michael Ficarra
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-shadow-restricted-names"),
RuleTester = require("../../../lib/testers/rule-tester");
const ruleTester = new RuleTester();
ruleTester.run("no-shadow-restricted-names", rule, {
valid: [
"function foo(bar){ var baz; }",
"!function foo(bar){ var baz; }",
"!function(bar){ var baz; }",
"try {} catch(e) {}",
{ code: "export default function() {}", parserOptions: { sourceType: "module" } },
{
code: "try {} catch {}",
parserOptions: { ecmaVersion: 2019 }
},
"var undefined;",
"var undefined; doSomething(undefined);",
"var undefined; var undefined;",
{
code: "let undefined",
parserOptions: { ecmaVersion: 2015 }
}
],
invalid: [
{
code: "function NaN(NaN) { var NaN; !function NaN(NaN) { try {} catch(NaN) {} }; }",
errors: [
{ message: "Shadowing of global property 'NaN'.", type: "Identifier" },
{ message: "Shadowing of global property 'NaN'.", type: "Identifier" },
{ message: "Shadowing of global property 'NaN'.", type: "Identifier" },
{ message: "Shadowing of global property 'NaN'.", type: "Identifier" },
{ message: "Shadowing of global property 'NaN'.", type: "Identifier" },
{ message: "Shadowing of global property 'NaN'.", type: "Identifier" }
]
},
{
code: "function undefined(undefined) { !function undefined(undefined) { try {} catch(undefined) {} }; }",
errors: [
{ message: "Shadowing of global property 'undefined'.", type: "Identifier" },
{ message: "Shadowing of global property 'undefined'.", type: "Identifier" },
{ message: "Shadowing of global property 'undefined'.", type: "Identifier" },
{ message: "Shadowing of global property 'undefined'.", type: "Identifier" },
{ message: "Shadowing of global property 'undefined'.", type: "Identifier" }
]
},
{
code: "function Infinity(Infinity) { var Infinity; !function Infinity(Infinity) { try {} catch(Infinity) {} }; }",
errors: [
{ message: "Shadowing of global property 'Infinity'.", type: "Identifier" },
{ message: "Shadowing of global property 'Infinity'.", type: "Identifier" },
{ message: "Shadowing of global property 'Infinity'.", type: "Identifier" },
{ message: "Shadowing of global property 'Infinity'.", type: "Identifier" },
{ message: "Shadowing of global property 'Infinity'.", type: "Identifier" },
{ message: "Shadowing of global property 'Infinity'.", type: "Identifier" }
]
},
{
code: "function arguments(arguments) { var arguments; !function arguments(arguments) { try {} catch(arguments) {} }; }",
errors: [
{ message: "Shadowing of global property 'arguments'.", type: "Identifier" },
{ message: "Shadowing of global property 'arguments'.", type: "Identifier" },
{ message: "Shadowing of global property 'arguments'.", type: "Identifier" },
{ message: "Shadowing of global property 'arguments'.", type: "Identifier" },
{ message: "Shadowing of global property 'arguments'.", type: "Identifier" },
{ message: "Shadowing of global property 'arguments'.", type: "Identifier" }
]
},
{
code: "function eval(eval) { var eval; !function eval(eval) { try {} catch(eval) {} }; }",
errors: [
{ message: "Shadowing of global property 'eval'.", type: "Identifier" },
{ message: "Shadowing of global property 'eval'.", type: "Identifier" },
{ message: "Shadowing of global property 'eval'.", type: "Identifier" },
{ message: "Shadowing of global property 'eval'.", type: "Identifier" },
{ message: "Shadowing of global property 'eval'.", type: "Identifier" },
{ message: "Shadowing of global property 'eval'.", type: "Identifier" }
]
},
{
code: "var eval = (eval) => { var eval; !function eval(eval) { try {} catch(eval) {} }; }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ message: "Shadowing of global property 'eval'.", type: "Identifier" },
{ message: "Shadowing of global property 'eval'.", type: "Identifier" },
{ message: "Shadowing of global property 'eval'.", type: "Identifier" },
{ message: "Shadowing of global property 'eval'.", type: "Identifier" },
{ message: "Shadowing of global property 'eval'.", type: "Identifier" },
{ message: "Shadowing of global property 'eval'.", type: "Identifier" }
]
},
{
code: "var [undefined] = [1]",
parserOptions: { ecmaVersion: 6 },
errors: [
{ message: "Shadowing of global property 'undefined'.", type: "Identifier" }
]
},
{
code: "var {undefined} = obj; var {a: undefined} = obj; var {a: {b: {undefined}}} = obj; var {a, ...undefined} = obj;",
parserOptions: { ecmaVersion: 9 },
errors: [
{ message: "Shadowing of global property 'undefined'.", type: "Identifier" },
{ message: "Shadowing of global property 'undefined'.", type: "Identifier" },
{ message: "Shadowing of global property 'undefined'.", type: "Identifier" },
{ message: "Shadowing of global property 'undefined'.", type: "Identifier" }
]
},
{
code: "var undefined; undefined = 5;",
errors: [
{ message: "Shadowing of global property 'undefined'.", type: "Identifier" }
]
}
]
});