Skip to content

Commit a121aca

Browse files
authored
Allow isNodeMatchesNameOrPath to match super and MetaProperty (#2762)
1 parent e9e7f48 commit a121aca

File tree

4 files changed

+130
-7
lines changed

4 files changed

+130
-7
lines changed

rules/utils/is-node-matches.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,32 @@ export function isNodeMatchesNameOrPath(node, nameOrPath) {
1717
return (
1818
(node.type === 'Identifier' && node.name === name)
1919
|| (name === 'this' && node.type === 'ThisExpression')
20+
|| (name === 'super' && node.type === 'Super')
2021
);
2122
}
2223

2324
if (
24-
node.type !== 'MemberExpression'
25-
|| node.optional
26-
|| node.computed
27-
|| node.property.type !== 'Identifier'
28-
|| node.property.name !== name
25+
index === 1
26+
&& node.type === 'MetaProperty'
27+
&& node.property.type === 'Identifier'
28+
&& node.property.name === name
2929
) {
30-
return false;
30+
node = node.meta;
31+
continue;
32+
}
33+
34+
if (
35+
node.type === 'MemberExpression'
36+
&& !node.optional
37+
&& !node.computed
38+
&& node.property.type === 'Identifier'
39+
&& node.property.name === name
40+
) {
41+
node = node.object;
42+
continue;
3143
}
3244

33-
node = node.object;
45+
return false;
3446
}
3547
}
3648

test/prefer-structured-clone.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,27 @@ test.snapshot({
7171
code: 'my.cloneDeep(foo,)',
7272
options: [{functions: ['my.cloneDeep']}],
7373
},
74+
// Coverage for `isNodeMatchesNameOrPath`
75+
{
76+
code: outdent`
77+
class A {
78+
constructor() {
79+
this.a = new.target.cloneDeep(foo);
80+
this.b = import.meta.cloneDeep(foo);
81+
}
82+
}
83+
`,
84+
options: [{functions: ['new.target.cloneDeep']}],
85+
},
86+
{
87+
code: outdent`
88+
class A {
89+
constructor() {
90+
this.a = super.cloneDeep(foo);
91+
}
92+
}
93+
`,
94+
options: [{functions: ['super.cloneDeep']}],
95+
},
7496
],
7597
});

test/snapshots/prefer-structured-clone.js.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,92 @@ Generated by [AVA](https://avajs.dev).
299299
Suggestion 1/1: Switch to \`structuredClone(…)\`.␊
300300
1 | structuredClone(foo,)␊
301301
`
302+
303+
## invalid(6): class A { constructor() { this.a = new.target.cloneDeep(foo); this.b = import.meta.cloneDeep(foo); } }
304+
305+
> Input
306+
307+
`␊
308+
1 | class A {␊
309+
2 | constructor() {␊
310+
3 | this.a = new.target.cloneDeep(foo);␊
311+
4 | this.b = import.meta.cloneDeep(foo);␊
312+
5 | }␊
313+
6 | }␊
314+
`
315+
316+
> Options
317+
318+
`␊
319+
[␊
320+
{␊
321+
"functions": [␊
322+
"new.target.cloneDeep"␊
323+
]␊
324+
}␊
325+
]␊
326+
`
327+
328+
> Error 1/1
329+
330+
`␊
331+
1 | class A {␊
332+
2 | constructor() {␊
333+
> 3 | this.a = new.target.cloneDeep(foo);␊
334+
| ^^^^^^^^^^^^^^^^^^^^ Prefer \`structuredClone(…)\` over \`new.target.cloneDeep(…)\` to create a deep clone.␊
335+
4 | this.b = import.meta.cloneDeep(foo);␊
336+
5 | }␊
337+
6 | }␊
338+
339+
--------------------------------------------------------------------------------␊
340+
Suggestion 1/1: Switch to \`structuredClone(…)\`.␊
341+
1 | class A {␊
342+
2 | constructor() {␊
343+
3 | this.a = structuredClone(foo);␊
344+
4 | this.b = import.meta.cloneDeep(foo);␊
345+
5 | }␊
346+
6 | }␊
347+
`
348+
349+
## invalid(7): class A { constructor() { this.a = super.cloneDeep(foo); } }
350+
351+
> Input
352+
353+
`␊
354+
1 | class A {␊
355+
2 | constructor() {␊
356+
3 | this.a = super.cloneDeep(foo);␊
357+
4 | }␊
358+
5 | }␊
359+
`
360+
361+
> Options
362+
363+
`␊
364+
[␊
365+
{␊
366+
"functions": [␊
367+
"super.cloneDeep"␊
368+
]␊
369+
}␊
370+
]␊
371+
`
372+
373+
> Error 1/1
374+
375+
`␊
376+
1 | class A {␊
377+
2 | constructor() {␊
378+
> 3 | this.a = super.cloneDeep(foo);␊
379+
| ^^^^^^^^^^^^^^^ Prefer \`structuredClone(…)\` over \`super.cloneDeep(…)\` to create a deep clone.␊
380+
4 | }␊
381+
5 | }␊
382+
383+
--------------------------------------------------------------------------------␊
384+
Suggestion 1/1: Switch to \`structuredClone(…)\`.␊
385+
1 | class A {␊
386+
2 | constructor() {␊
387+
3 | this.a = structuredClone(foo);␊
388+
4 | }␊
389+
5 | }␊
390+
`
229 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)