1
1
use oxc_ast:: { AstKind , ast:: Expression } ;
2
2
use oxc_diagnostics:: OxcDiagnostic ;
3
3
use oxc_macros:: declare_oxc_lint;
4
- use oxc_semantic:: IsGlobalReference ;
5
4
use oxc_span:: Span ;
6
5
7
6
use crate :: { AstNode , context:: LintContext , rule:: Rule } ;
@@ -66,7 +65,8 @@ impl Rule for NoObjectConstructor {
66
65
return ;
67
66
} ;
68
67
69
- if ident. is_global_reference_name ( "Object" , ctx. symbols ( ) )
68
+ if ident. name == "Object"
69
+ && ctx. is_reference_to_global_variable ( ident)
70
70
&& arguments. len ( ) == 0
71
71
&& type_parameters. is_none ( )
72
72
{
@@ -80,98 +80,189 @@ fn test() {
80
80
use crate :: tester:: Tester ;
81
81
82
82
let pass = vec ! [
83
- "new Object(x)" ,
84
- "Object(x)" ,
85
- "new globalThis.Object" ,
86
- "const createObject = Object => new Object()" ,
87
- "var Object; new Object;" ,
88
- // Disabled because the eslint-test uses languageOptions: { globals: { Object: "off" } }
89
- // "new Object()",
83
+ ( "new Object(x)" , None , None ) ,
84
+ ( "Object(x)" , None , None ) ,
85
+ ( "new globalThis.Object" , None , None ) ,
86
+ ( "const createObject = Object => new Object()" , None , None ) ,
87
+ ( "var Object; new Object;" , None , None ) ,
88
+ ( "new Object()" , None , Some ( serde_json:: json!( { "globals" : { "Object" : "off" } } ) ) ) ,
90
89
] ;
91
90
92
91
let fail = vec ! [
93
- "new Object" ,
94
- "Object()" ,
95
- "const fn = () => Object();" ,
96
- "Object() instanceof Object;" ,
97
- "const obj = Object?.();" ,
98
- "(new Object() instanceof Object);" ,
92
+ ( "new Object" , None , None ) ,
93
+ ( "Object()" , None , None ) ,
94
+ ( "const fn = () => Object();" , None , None ) ,
95
+ ( "Object() instanceof Object;" , None , None ) ,
96
+ ( "const obj = Object?.();" , None , None ) ,
97
+ ( "(new Object() instanceof Object);" , None , None ) ,
99
98
// Semicolon required before `({})` to compensate for ASI
100
- "Object()" ,
101
- "foo()
99
+ ( "Object()" , None , None ) ,
100
+ (
101
+ "foo()
102
102
Object()" ,
103
- "var yield = bar.yield
103
+ None ,
104
+ None ,
105
+ ) ,
106
+ (
107
+ "var yield = bar.yield
104
108
Object()" ,
105
- "var foo = { bar: baz }
109
+ None ,
110
+ None ,
111
+ ) ,
112
+ (
113
+ "var foo = { bar: baz }
106
114
Object()" ,
107
- "<foo />
115
+ None ,
116
+ None ,
117
+ ) ,
118
+ (
119
+ "<foo />
108
120
Object()" ,
109
- "<foo></foo>
121
+ None ,
122
+ None ,
123
+ ) ,
124
+ (
125
+ "<foo></foo>
110
126
Object()" ,
127
+ None ,
128
+ None ,
129
+ ) ,
111
130
// No semicolon required before `({})` because ASI does not occur
112
- "Object()" ,
113
- "{}
114
- Object()" ,
115
- "function foo() {}
116
- Object()" ,
117
- "class Foo {}
118
- Object()" ,
119
- "foo: Object();" ,
120
- "foo();Object();" ,
121
- "{ Object(); }" ,
122
- "if (a) Object();" ,
123
- "if (a); else Object();" ,
124
- "while (a) Object();" ,
125
- "do Object(); while (a);" ,
126
- "for (let i = 0; i < 10; i++) Object();" ,
127
- "for (const prop in obj) Object();" ,
128
- "for (const element of iterable) Object();" ,
129
- "with (obj) Object();" ,
131
+ ( "Object()" , None , None ) ,
132
+ (
133
+ "{}
134
+ Object()" ,
135
+ None ,
136
+ None ,
137
+ ) ,
138
+ (
139
+ "function foo() {}
140
+ Object()" ,
141
+ None ,
142
+ None ,
143
+ ) ,
144
+ (
145
+ "class Foo {}
146
+ Object()" ,
147
+ None ,
148
+ None ,
149
+ ) ,
150
+ ( "foo: Object();" , None , None ) ,
151
+ ( "foo();Object();" , None , None ) ,
152
+ ( "{ Object(); }" , None , None ) ,
153
+ ( "if (a) Object();" , None , None ) ,
154
+ ( "if (a); else Object();" , None , None ) ,
155
+ ( "while (a) Object();" , None , None ) ,
156
+ ( "do Object(); while (a);" , None , None ) ,
157
+ ( "for (let i = 0; i < 10; i++) Object();" , None , None ) ,
158
+ ( "for (const prop in obj) Object();" , None , None ) ,
159
+ ( "for (const element of iterable) Object();" , None , None ) ,
160
+ ( "with (obj) Object();" , None , None ) ,
130
161
// No semicolon required before `({})` because ASI still occurs
131
- "const foo = () => {}
162
+ (
163
+ "const foo = () => {}
132
164
Object()" ,
133
- "a++
165
+ None ,
166
+ None ,
167
+ ) ,
168
+ (
169
+ "a++
134
170
Object()" ,
135
- "a--
171
+ None ,
172
+ None ,
173
+ ) ,
174
+ (
175
+ "a--
136
176
Object()" ,
137
- "function foo() {
177
+ None ,
178
+ None ,
179
+ ) ,
180
+ (
181
+ "function foo() {
138
182
return
139
183
Object();
140
184
}" ,
141
- "function * foo() {
185
+ None ,
186
+ None ,
187
+ ) ,
188
+ (
189
+ "function * foo() {
142
190
yield
143
191
Object();
144
192
}" ,
145
- "do {} while (a) Object()" ,
146
- "debugger
193
+ None ,
194
+ None ,
195
+ ) ,
196
+ ( "do {} while (a) Object()" , None , None ) ,
197
+ (
198
+ "debugger
147
199
Object()" ,
148
- "for (;;) {
200
+ None ,
201
+ None ,
202
+ ) ,
203
+ (
204
+ "for (;;) {
149
205
break
150
206
Object()
151
207
}" ,
152
- r"for (;;) {
208
+ None ,
209
+ None ,
210
+ ) ,
211
+ (
212
+ r"for (;;) {
153
213
continue
154
214
Object()
155
215
}" ,
156
- "foo: break foo
216
+ None ,
217
+ None ,
218
+ ) ,
219
+ (
220
+ "foo: break foo
157
221
Object()" ,
158
- "foo: while (true) continue foo
222
+ None ,
223
+ None ,
224
+ ) ,
225
+ (
226
+ "foo: while (true) continue foo
159
227
Object()" ,
160
- "const foo = bar
228
+ None ,
229
+ None ,
230
+ ) ,
231
+ (
232
+ "const foo = bar
161
233
export { foo }
162
234
Object()" ,
163
- "export { foo } from 'bar'
235
+ None ,
236
+ None ,
237
+ ) ,
238
+ (
239
+ "export { foo } from 'bar'
164
240
Object()" ,
165
- r"export * as foo from 'bar'
241
+ None ,
242
+ None ,
243
+ ) ,
244
+ (
245
+ r"export * as foo from 'bar'
166
246
Object()" ,
167
- "import foo from 'bar
247
+ None ,
248
+ None ,
249
+ ) ,
250
+ (
251
+ "import foo from 'bar
168
252
Object()" ,
169
- "var yield = 5;
253
+ None ,
254
+ None ,
255
+ ) ,
256
+ (
257
+ "var yield = 5;
170
258
yield: while (foo) {
171
259
if (bar)
172
260
break yield
173
261
new Object();
174
262
}" ,
263
+ None ,
264
+ None ,
265
+ ) ,
175
266
] ;
176
267
177
268
Tester :: new ( NoObjectConstructor :: NAME , NoObjectConstructor :: PLUGIN , pass, fail)
0 commit comments