Skip to content

Commit ca1e432

Browse files
axetroyfisker
andauthored
no-accessor-recursion: Fix exception when used in CommonJS (#2574)
Co-authored-by: fisker <[email protected]>
1 parent 762670f commit ca1e432

File tree

2 files changed

+160
-137
lines changed

2 files changed

+160
-137
lines changed

rules/no-accessor-recursion.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Check if a property is a valid getter or setter.
3838
@param {import('estree').Property | import('estree').MethodDefinition} property
3939
*/
4040
const isValidProperty = property =>
41-
['Property', 'MethodDefinition'].includes(property.type)
41+
['Property', 'MethodDefinition'].includes(property?.type)
4242
&& !property.computed
4343
&& ['set', 'get'].includes(property.kind)
4444
&& isIdentifier(property.key);

test/no-accessor-recursion.js

+159-136
Original file line numberDiff line numberDiff line change
@@ -1,158 +1,161 @@
11
import outdent from 'outdent';
2-
import {getTester, parsers} from './utils/test.js';
2+
import {getTester, parsers, avoidTestTitleConflict} from './utils/test.js';
33

44
const {test} = getTester(import.meta);
55

6-
test.snapshot({
7-
valid: [
8-
'function foo () { this.bar }',
9-
'function foo () { this.foo }',
10-
'function foo (value) { this.bar = value }',
11-
'this.foo = foo',
12-
outdent`
13-
{
14-
this.foo = foo;
6+
const validCases = [
7+
'console.log(this)',
8+
'function foo () { this.bar }',
9+
'function foo () { this.foo }',
10+
'function foo (value) { this.bar = value }',
11+
'this.foo = foo',
12+
outdent`
13+
{
14+
this.foo = foo;
15+
}
16+
`,
17+
'this.foo = function () { this.foo }',
18+
'const foo = () => this.foo',
19+
outdent`
20+
const foo = {
21+
bar() {
22+
this.bar = void 0;
23+
return this.bar;
1524
}
16-
`,
17-
'this.foo = function () { this.foo }',
18-
'const foo = () => this.foo',
19-
outdent`
20-
const foo = {
21-
bar() {
22-
this.bar = void 0;
23-
return this.bar;
24-
}
25-
};
26-
`,
27-
outdent`
28-
class Foo {
29-
foo() {
30-
this.foo = void 0;
31-
return this.foo;
32-
}
25+
};
26+
`,
27+
outdent`
28+
class Foo {
29+
foo() {
30+
this.foo = void 0;
31+
return this.foo;
3332
}
34-
`,
35-
// Deep property setter
36-
outdent`
37-
class Foo {
38-
set bar(value) {
39-
this.bar.baz = value;
40-
}
33+
}
34+
`,
35+
// Deep property setter
36+
outdent`
37+
class Foo {
38+
set bar(value) {
39+
this.bar.baz = value;
4140
}
42-
`,
43-
// Define this to alias
44-
outdent`
45-
class Foo {
46-
get bar() {
47-
const self = this;
48-
return self.bar;
49-
}
41+
}
42+
`,
43+
// Define this to alias
44+
outdent`
45+
class Foo {
46+
get bar() {
47+
const self = this;
48+
return self.bar;
5049
}
51-
`,
52-
outdent`
53-
class Foo {
54-
set bar(value) {
55-
const self = this;
56-
return self.bar = value;
57-
}
50+
}
51+
`,
52+
outdent`
53+
class Foo {
54+
set bar(value) {
55+
const self = this;
56+
return self.bar = value;
5857
}
59-
`,
60-
outdent`
61-
const foo = {
62-
get bar() {
63-
return this._bar;
58+
}
59+
`,
60+
outdent`
61+
const foo = {
62+
get bar() {
63+
return this._bar;
64+
}
65+
};
66+
`,
67+
// Access this in function scope
68+
outdent`
69+
const foo = {
70+
get bar() {
71+
function baz() {
72+
return this.bar;
6473
}
65-
};
66-
`,
67-
// Access this in function scope
68-
outdent`
69-
const foo = {
70-
get bar() {
71-
function baz() {
74+
}
75+
};
76+
`,
77+
// Nest getter
78+
outdent`
79+
const foo = {
80+
get bar() {
81+
const qux = {
82+
get quux () {
7283
return this.bar;
7384
}
7485
}
75-
};
76-
`,
77-
// Nest getter
78-
outdent`
79-
const foo = {
80-
get bar() {
81-
const qux = {
82-
get quux () {
83-
return this.bar;
84-
}
85-
}
86-
}
87-
};
88-
`,
89-
// Test computed property
90-
outdent`
91-
const foo = {
92-
get bar() {
93-
return this[bar];
94-
}
95-
};
96-
`,
97-
outdent`
98-
const foo = {
99-
get [bar]() {
100-
return this.bar;
101-
}
102-
};
103-
`,
104-
// Setter access in the right of AssignmentExpression
105-
outdent`
106-
const foo = {
107-
set bar(value) {
108-
a = this.bar;
109-
}
110-
};
111-
`,
112-
// Private field without recursion access
113-
outdent`
114-
class Foo{
115-
get bar() {
116-
return this.#bar;
117-
}
86+
}
87+
};
88+
`,
89+
// Test computed property
90+
outdent`
91+
const foo = {
92+
get bar() {
93+
return this[bar];
94+
}
95+
};
96+
`,
97+
outdent`
98+
const foo = {
99+
get [bar]() {
100+
return this.bar;
101+
}
102+
};
103+
`,
104+
// Setter access in the right of AssignmentExpression
105+
outdent`
106+
const foo = {
107+
set bar(value) {
108+
a = this.bar;
109+
}
110+
};
111+
`,
112+
// Private field without recursion access
113+
outdent`
114+
class Foo{
115+
get bar() {
116+
return this.#bar;
117+
}
118118
119-
get #bar() {
120-
return 0;
121-
}
119+
get #bar() {
120+
return 0;
122121
}
123-
`,
124-
// Destructuring assignment with computed property
125-
outdent`
126-
class Foo{
127-
get bar() {
128-
const {[bar]: bar} = this;
129-
}
122+
}
123+
`,
124+
// Destructuring assignment with computed property
125+
outdent`
126+
class Foo{
127+
get bar() {
128+
const {[bar]: bar} = this;
130129
}
131-
`,
132-
// Static block
133-
outdent`
134-
const foo = {
135-
get bar() {
136-
class Foo {
137-
static {
138-
this.bar
139-
}
130+
}
131+
`,
132+
// Static block
133+
outdent`
134+
const foo = {
135+
get bar() {
136+
class Foo {
137+
static {
138+
this.bar
140139
}
141140
}
142-
};
143-
`,
144-
// Static property
145-
outdent`
146-
const foo = {
147-
get bar() {
148-
class Foo {
149-
bar = 1;
150-
baz = this.bar;
151-
}
141+
}
142+
};
143+
`,
144+
// Static property
145+
outdent`
146+
const foo = {
147+
get bar() {
148+
class Foo {
149+
bar = 1;
150+
baz = this.bar;
152151
}
153-
};
154-
`,
155-
],
152+
}
153+
};
154+
`,
155+
];
156+
157+
test.snapshot({
158+
valid: validCases,
156159
invalid: [
157160
// Getter
158161
outdent`
@@ -307,3 +310,23 @@ test.snapshot({
307310
`,
308311
],
309312
});
313+
314+
test.snapshot(avoidTestTitleConflict({
315+
testerOptions: {
316+
languageOptions: {
317+
sourceType: 'commonjs',
318+
},
319+
},
320+
valid: validCases,
321+
invalid: [],
322+
}, 'commonjs'));
323+
324+
test.snapshot(avoidTestTitleConflict({
325+
testerOptions: {
326+
languageOptions: {
327+
sourceType: 'script',
328+
},
329+
},
330+
valid: validCases,
331+
invalid: [],
332+
}, 'script'));

0 commit comments

Comments
 (0)