/
no-prototype-builtins.js
174 lines (165 loc) · 5.85 KB
/
no-prototype-builtins.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* @fileoverview Tests for no-prototype-built-ins
* @author Andrew Levine
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-prototype-builtins"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("no-prototype-builtins", rule, {
valid: [
"Object.prototype.hasOwnProperty.call(foo, 'bar')",
"Object.prototype.isPrototypeOf.call(foo, 'bar')",
"Object.prototype.propertyIsEnumerable.call(foo, 'bar')",
"Object.prototype.hasOwnProperty.apply(foo, ['bar'])",
"Object.prototype.isPrototypeOf.apply(foo, ['bar'])",
"Object.prototype.propertyIsEnumerable.apply(foo, ['bar'])",
"foo.hasOwnProperty",
"foo.hasOwnProperty.bar()",
"foo(hasOwnProperty)",
"hasOwnProperty(foo, 'bar')",
"isPrototypeOf(foo, 'bar')",
"propertyIsEnumerable(foo, 'bar')",
"({}.hasOwnProperty.call(foo, 'bar'))",
"({}.isPrototypeOf.call(foo, 'bar'))",
"({}.propertyIsEnumerable.call(foo, 'bar'))",
"({}.hasOwnProperty.apply(foo, ['bar']))",
"({}.isPrototypeOf.apply(foo, ['bar']))",
"({}.propertyIsEnumerable.apply(foo, ['bar']))",
"foo[hasOwnProperty]('bar')",
"foo['HasOwnProperty']('bar')",
{ code: "foo[`isPrototypeOff`]('bar')", parserOptions: { ecmaVersion: 2015 } },
{ code: "foo?.['propertyIsEnumerable']('bar')", parserOptions: { ecmaVersion: 2020 } },
"foo[1]('bar')",
"foo[null]('bar')",
{ code: "class C { #hasOwnProperty; foo() { obj.#hasOwnProperty('bar'); } }", parserOptions: { ecmaVersion: 2022 } },
// out of scope for this rule
"foo['hasOwn' + 'Property']('bar')",
{ code: "foo[`hasOwnProperty${''}`]('bar')", parserOptions: { ecmaVersion: 2015 } }
],
invalid: [
{
code: "foo.hasOwnProperty('bar')",
errors: [{
line: 1,
column: 5,
endLine: 1,
endColumn: 19,
messageId: "prototypeBuildIn",
data: { prop: "hasOwnProperty" },
type: "CallExpression"
}]
},
{
code: "foo.isPrototypeOf('bar')",
errors: [{
line: 1,
column: 5,
endLine: 1,
endColumn: 18,
messageId: "prototypeBuildIn",
data: { prop: "isPrototypeOf" },
type: "CallExpression"
}]
},
{
code: "foo.propertyIsEnumerable('bar')",
errors: [{
line: 1,
column: 5,
endLine: 1,
endColumn: 25,
messageId: "prototypeBuildIn",
data: { prop: "propertyIsEnumerable" }
}]
},
{
code: "foo.bar.hasOwnProperty('bar')",
errors: [{
line: 1,
column: 9,
endLine: 1,
endColumn: 23,
messageId: "prototypeBuildIn",
data: { prop: "hasOwnProperty" },
type: "CallExpression"
}]
},
{
code: "foo.bar.baz.isPrototypeOf('bar')",
errors: [{
line: 1,
column: 13,
endLine: 1,
endColumn: 26,
messageId: "prototypeBuildIn",
data: { prop: "isPrototypeOf" },
type: "CallExpression"
}]
},
{
code: "foo['hasOwnProperty']('bar')",
errors: [{
line: 1,
column: 5,
endLine: 1,
endColumn: 21,
messageId: "prototypeBuildIn",
data: { prop: "hasOwnProperty" },
type: "CallExpression"
}]
},
{
code: "foo[`isPrototypeOf`]('bar').baz",
parserOptions: { ecmaVersion: 2015 },
errors: [{
line: 1,
column: 5,
endLine: 1,
endColumn: 20,
messageId: "prototypeBuildIn",
data: { prop: "isPrototypeOf" },
type: "CallExpression"
}]
},
{
code: String.raw`foo.bar["propertyIsEnumerable"]('baz')`,
errors: [{
line: 1,
column: 9,
endLine: 1,
endColumn: 31,
messageId: "prototypeBuildIn",
data: { prop: "propertyIsEnumerable" },
type: "CallExpression"
}]
},
// Optional chaining
{
code: "foo?.hasOwnProperty('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }]
},
{
code: "(foo?.hasOwnProperty)('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }]
},
{
code: "foo?.['hasOwnProperty']('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }]
},
{
code: "(foo?.[`hasOwnProperty`])('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }]
}
]
});