]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for no-prototype-built-ins | |
3 | * @author Andrew Levine | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/no-prototype-builtins"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
609c276f | 18 | |
eb39fafa DC |
19 | const ruleTester = new RuleTester(); |
20 | ||
6f036462 TL |
21 | ruleTester.run("no-prototype-builtins", rule, { |
22 | valid: [ | |
23 | "Object.prototype.hasOwnProperty.call(foo, 'bar')", | |
24 | "Object.prototype.isPrototypeOf.call(foo, 'bar')", | |
25 | "Object.prototype.propertyIsEnumerable.call(foo, 'bar')", | |
26 | "Object.prototype.hasOwnProperty.apply(foo, ['bar'])", | |
27 | "Object.prototype.isPrototypeOf.apply(foo, ['bar'])", | |
28 | "Object.prototype.propertyIsEnumerable.apply(foo, ['bar'])", | |
29 | "foo.hasOwnProperty", | |
30 | "foo.hasOwnProperty.bar()", | |
31 | "foo(hasOwnProperty)", | |
32 | "hasOwnProperty(foo, 'bar')", | |
33 | "isPrototypeOf(foo, 'bar')", | |
34 | "propertyIsEnumerable(foo, 'bar')", | |
35 | "({}.hasOwnProperty.call(foo, 'bar'))", | |
36 | "({}.isPrototypeOf.call(foo, 'bar'))", | |
37 | "({}.propertyIsEnumerable.call(foo, 'bar'))", | |
38 | "({}.hasOwnProperty.apply(foo, ['bar']))", | |
39 | "({}.isPrototypeOf.apply(foo, ['bar']))", | |
40 | "({}.propertyIsEnumerable.apply(foo, ['bar']))", | |
41 | "foo[hasOwnProperty]('bar')", | |
42 | "foo['HasOwnProperty']('bar')", | |
43 | { code: "foo[`isPrototypeOff`]('bar')", parserOptions: { ecmaVersion: 2015 } }, | |
44 | { code: "foo?.['propertyIsEnumerabl']('bar')", parserOptions: { ecmaVersion: 2020 } }, | |
45 | "foo[1]('bar')", | |
46 | "foo[null]('bar')", | |
609c276f | 47 | { code: "class C { #hasOwnProperty; foo() { obj.#hasOwnProperty('bar'); } }", parserOptions: { ecmaVersion: 2022 } }, |
eb39fafa | 48 | |
6f036462 TL |
49 | // out of scope for this rule |
50 | "foo['hasOwn' + 'Property']('bar')", | |
51 | { code: "foo[`hasOwnProperty${''}`]('bar')", parserOptions: { ecmaVersion: 2015 } } | |
52 | ], | |
eb39fafa | 53 | |
6f036462 TL |
54 | invalid: [ |
55 | { | |
56 | code: "foo.hasOwnProperty('bar')", | |
57 | errors: [{ | |
58 | line: 1, | |
59 | column: 5, | |
60 | endLine: 1, | |
61 | endColumn: 19, | |
62 | messageId: "prototypeBuildIn", | |
63 | data: { prop: "hasOwnProperty" }, | |
64 | type: "CallExpression" | |
65 | }] | |
66 | }, | |
67 | { | |
68 | code: "foo.isPrototypeOf('bar')", | |
69 | errors: [{ | |
70 | line: 1, | |
71 | column: 5, | |
72 | endLine: 1, | |
73 | endColumn: 18, | |
74 | messageId: "prototypeBuildIn", | |
75 | data: { prop: "isPrototypeOf" }, | |
76 | type: "CallExpression" | |
77 | }] | |
78 | }, | |
79 | { | |
80 | code: "foo.propertyIsEnumerable('bar')", | |
81 | errors: [{ | |
82 | line: 1, | |
83 | column: 5, | |
84 | endLine: 1, | |
85 | endColumn: 25, | |
86 | messageId: "prototypeBuildIn", | |
87 | data: { prop: "propertyIsEnumerable" } | |
88 | }] | |
89 | }, | |
90 | { | |
91 | code: "foo.bar.hasOwnProperty('bar')", | |
92 | errors: [{ | |
93 | line: 1, | |
94 | column: 9, | |
95 | endLine: 1, | |
96 | endColumn: 23, | |
97 | messageId: "prototypeBuildIn", | |
98 | data: { prop: "hasOwnProperty" }, | |
99 | type: "CallExpression" | |
100 | }] | |
101 | }, | |
102 | { | |
103 | code: "foo.bar.baz.isPrototypeOf('bar')", | |
104 | errors: [{ | |
105 | line: 1, | |
106 | column: 13, | |
107 | endLine: 1, | |
108 | endColumn: 26, | |
109 | messageId: "prototypeBuildIn", | |
110 | data: { prop: "isPrototypeOf" }, | |
111 | type: "CallExpression" | |
112 | }] | |
113 | }, | |
114 | { | |
115 | code: "foo['hasOwnProperty']('bar')", | |
116 | errors: [{ | |
117 | line: 1, | |
118 | column: 5, | |
119 | endLine: 1, | |
120 | endColumn: 21, | |
121 | messageId: "prototypeBuildIn", | |
122 | data: { prop: "hasOwnProperty" }, | |
123 | type: "CallExpression" | |
124 | }] | |
125 | }, | |
126 | { | |
127 | code: "foo[`isPrototypeOf`]('bar').baz", | |
128 | parserOptions: { ecmaVersion: 2015 }, | |
129 | errors: [{ | |
130 | line: 1, | |
131 | column: 5, | |
132 | endLine: 1, | |
133 | endColumn: 20, | |
134 | messageId: "prototypeBuildIn", | |
135 | data: { prop: "isPrototypeOf" }, | |
136 | type: "CallExpression" | |
137 | }] | |
138 | }, | |
139 | { | |
140 | code: String.raw`foo.bar["propertyIsEnumerable"]('baz')`, | |
141 | errors: [{ | |
142 | line: 1, | |
143 | column: 9, | |
144 | endLine: 1, | |
145 | endColumn: 31, | |
146 | messageId: "prototypeBuildIn", | |
147 | data: { prop: "propertyIsEnumerable" }, | |
148 | type: "CallExpression" | |
149 | }] | |
150 | }, | |
151 | ||
152 | // Optional chaining | |
153 | { | |
154 | code: "foo?.hasOwnProperty('bar')", | |
155 | parserOptions: { ecmaVersion: 2020 }, | |
156 | errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }] | |
157 | }, | |
158 | { | |
159 | code: "(foo?.hasOwnProperty)('bar')", | |
160 | parserOptions: { ecmaVersion: 2020 }, | |
161 | errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }] | |
162 | }, | |
163 | { | |
164 | code: "foo?.['hasOwnProperty']('bar')", | |
165 | parserOptions: { ecmaVersion: 2020 }, | |
166 | errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }] | |
167 | }, | |
168 | { | |
169 | code: "(foo?.[`hasOwnProperty`])('bar')", | |
170 | parserOptions: { ecmaVersion: 2020 }, | |
171 | errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }] | |
172 | } | |
173 | ] | |
eb39fafa | 174 | }); |