Skip to content

Commit 20580f7

Browse files
DonIsaacBoshen
authored andcommitted
feat(sema/check): ts setters cannot have initializers
1 parent c391f1a commit 20580f7

File tree

5 files changed

+78
-48
lines changed

5 files changed

+78
-48
lines changed

crates/oxc_linter/src/rules/eslint/no_setter_return.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,5 +292,7 @@ fn test() {
292292
// ("(Object?.defineProperty)(foo, 'bar', { set(val) { return 1; } })", None),
293293
];
294294

295-
Tester::new(NoSetterReturn::NAME, NoSetterReturn::PLUGIN, pass, fail).test_and_snapshot();
295+
Tester::new(NoSetterReturn::NAME, NoSetterReturn::PLUGIN, pass, fail)
296+
.change_rule_path_extension("js")
297+
.test_and_snapshot();
296298
}

crates/oxc_linter/src/snapshots/eslint_no_setter_return.snap

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,253 +2,253 @@
22
source: crates/oxc_linter/src/tester.rs
33
---
44
eslint(no-setter-return): Setter cannot return a value
5-
╭─[no_setter_return.tsx:1:16]
5+
╭─[no_setter_return.js:1:16]
66
1 │ ({ set a(val){ return val + 1; } })
77
· ───────────────
88
╰────
99

1010
eslint(no-setter-return): Setter cannot return a value
11-
╭─[no_setter_return.tsx:1:17]
11+
╭─[no_setter_return.js:1:17]
1212
1 │ ({ set a(val) { return 1; } })
1313
· ─────────
1414
╰────
1515

1616
eslint(no-setter-return): Setter cannot return a value
17-
╭─[no_setter_return.tsx:1:24]
17+
╭─[no_setter_return.js:1:24]
1818
1class A { set a(val) { return 1; } }
1919
· ─────────
2020
╰────
2121

2222
eslint(no-setter-return): Setter cannot return a value
23-
╭─[no_setter_return.tsx:1:31]
23+
╭─[no_setter_return.js:1:31]
2424
1class A { static set a(val) { return 1; } }
2525
· ─────────
2626
╰────
2727

2828
eslint(no-setter-return): Setter cannot return a value
29-
╭─[no_setter_return.tsx:1:23]
29+
╭─[no_setter_return.js:1:23]
3030
1 │ (class { set a(val) { return 1; } })
3131
· ─────────
3232
╰────
3333

3434
eslint(no-setter-return): Setter cannot return a value
35-
╭─[no_setter_return.tsx:1:17]
35+
╭─[no_setter_return.js:1:17]
3636
1 │ ({ set a(val) { return val; } })
3737
· ───────────
3838
╰────
3939

4040
eslint(no-setter-return): Setter cannot return a value
41-
╭─[no_setter_return.tsx:1:24]
41+
╭─[no_setter_return.js:1:24]
4242
1class A { set a(val) { return undefined; } }
4343
· ─────────────────
4444
╰────
4545

4646
eslint(no-setter-return): Setter cannot return a value
47-
╭─[no_setter_return.tsx:1:23]
47+
╭─[no_setter_return.js:1:23]
4848
1 │ (class { set a(val) { return null; } })
4949
· ────────────
5050
╰────
5151

5252
eslint(no-setter-return): Setter cannot return a value
53-
╭─[no_setter_return.tsx:1:17]
53+
╭─[no_setter_return.js:1:17]
5454
1 │ ({ set a(val) { return x + y; } })
5555
· ─────────────
5656
╰────
5757

5858
eslint(no-setter-return): Setter cannot return a value
59-
╭─[no_setter_return.tsx:1:24]
59+
╭─[no_setter_return.js:1:24]
6060
1class A { set a(val) { return foo(); } }
6161
· ─────────────
6262
╰────
6363

6464
eslint(no-setter-return): Setter cannot return a value
65-
╭─[no_setter_return.tsx:1:23]
65+
╭─[no_setter_return.js:1:23]
6666
1 │ (class { set a(val) { return this._a; } })
6767
· ───────────────
6868
╰────
6969

7070
eslint(no-setter-return): Setter cannot return a value
71-
╭─[no_setter_return.tsx:1:17]
71+
╭─[no_setter_return.js:1:17]
7272
1 │ ({ set a(val) { return this.a; } })
7373
· ──────────────
7474
╰────
7575

7676
eslint(no-setter-return): Setter cannot return a value
77-
╭─[no_setter_return.tsx:1:28]
77+
╭─[no_setter_return.js:1:28]
7878
1 │ ({ set a(val) { if (foo) { return 1; }; } })
7979
· ─────────
8080
╰────
8181

8282
eslint(no-setter-return): Setter cannot return a value
83-
╭─[no_setter_return.tsx:1:30]
83+
╭─[no_setter_return.js:1:30]
8484
1class A { set a(val) { try { return 1; } catch(e) {} } }
8585
· ─────────
8686
╰────
8787

8888
eslint(no-setter-return): Setter cannot return a value
89-
╭─[no_setter_return.tsx:1:57]
89+
╭─[no_setter_return.js:1:57]
9090
1 │ (class { set a(val) { while (foo){ if (bar) break; else return 1; } } })
9191
· ─────────
9292
╰────
9393

9494
eslint(no-setter-return): Setter cannot return a value
95-
╭─[no_setter_return.tsx:1:17]
95+
╭─[no_setter_return.js:1:17]
9696
1 │ ({ set a(val) { return 1; }, set b(val) { return 1; } })
9797
· ─────────
9898
╰────
9999

100100
eslint(no-setter-return): Setter cannot return a value
101-
╭─[no_setter_return.tsx:1:43]
101+
╭─[no_setter_return.js:1:43]
102102
1 │ ({ set a(val) { return 1; }, set b(val) { return 1; } })
103103
· ─────────
104104
╰────
105105

106106
eslint(no-setter-return): Setter cannot return a value
107-
╭─[no_setter_return.tsx:1:24]
107+
╭─[no_setter_return.js:1:24]
108108
1class A { set a(val) { return 1; } set b(val) { return 1; } }
109109
· ─────────
110110
╰────
111111

112112
eslint(no-setter-return): Setter cannot return a value
113-
╭─[no_setter_return.tsx:1:49]
113+
╭─[no_setter_return.js:1:49]
114114
1class A { set a(val) { return 1; } set b(val) { return 1; } }
115115
· ─────────
116116
╰────
117117

118118
eslint(no-setter-return): Setter cannot return a value
119-
╭─[no_setter_return.tsx:1:23]
119+
╭─[no_setter_return.js:1:23]
120120
1 │ (class { set a(val) { return 1; } static set b(val) { return 1; } })
121121
· ─────────
122122
╰────
123123

124124
eslint(no-setter-return): Setter cannot return a value
125-
╭─[no_setter_return.tsx:1:55]
125+
╭─[no_setter_return.js:1:55]
126126
1 │ (class { set a(val) { return 1; } static set b(val) { return 1; } })
127127
· ─────────
128128
╰────
129129

130130
eslint(no-setter-return): Setter cannot return a value
131-
╭─[no_setter_return.tsx:1:27]
131+
╭─[no_setter_return.js:1:27]
132132
1 │ ({ set a(val) { if(val) { return 1; } else { return 2 }; } })
133133
· ─────────
134134
╰────
135135

136136
eslint(no-setter-return): Setter cannot return a value
137-
╭─[no_setter_return.tsx:1:46]
137+
╭─[no_setter_return.js:1:46]
138138
1 │ ({ set a(val) { if(val) { return 1; } else { return 2 }; } })
139139
· ────────
140140
╰────
141141

142142
eslint(no-setter-return): Setter cannot return a value
143-
╭─[no_setter_return.tsx:1:46]
143+
╭─[no_setter_return.js:1:46]
144144
1class A { set a(val) { switch(val) { case 1: return x; case 2: return y; default: return z } } }
145145
· ─────────
146146
╰────
147147

148148
eslint(no-setter-return): Setter cannot return a value
149-
╭─[no_setter_return.tsx:1:64]
149+
╭─[no_setter_return.js:1:64]
150150
1class A { set a(val) { switch(val) { case 1: return x; case 2: return y; default: return z } } }
151151
· ─────────
152152
╰────
153153

154154
eslint(no-setter-return): Setter cannot return a value
155-
╭─[no_setter_return.tsx:1:83]
155+
╭─[no_setter_return.js:1:83]
156156
1class A { set a(val) { switch(val) { case 1: return x; case 2: return y; default: return z } } }
157157
· ────────
158158
╰────
159159

160160
eslint(no-setter-return): Setter cannot return a value
161-
╭─[no_setter_return.tsx:1:62]
161+
╭─[no_setter_return.js:1:62]
162162
1 │ (class { static set a(val) { if (val > 0) { this._val = val; return val; } return false; } })
163163
· ───────────
164164
╰────
165165

166166
eslint(no-setter-return): Setter cannot return a value
167-
╭─[no_setter_return.tsx:1:76]
167+
╭─[no_setter_return.js:1:76]
168168
1 │ (class { static set a(val) { if (val > 0) { this._val = val; return val; } return false; } })
169169
· ─────────────
170170
╰────
171171

172172
eslint(no-setter-return): Setter cannot return a value
173-
╭─[no_setter_return.tsx:1:27]
173+
╭─[no_setter_return.js:1:27]
174174
1 │ ({ set a(val) { if(val) { return 1; } else { return; }; } })
175175
· ─────────
176176
╰────
177177

178178
eslint(no-setter-return): Setter cannot return a value
179-
╭─[no_setter_return.tsx:1:46]
179+
╭─[no_setter_return.js:1:46]
180180
1class A { set a(val) { switch(val) { case 1: return x; case 2: return; default: return z } } }
181181
· ─────────
182182
╰────
183183

184184
eslint(no-setter-return): Setter cannot return a value
185-
╭─[no_setter_return.tsx:1:81]
185+
╭─[no_setter_return.js:1:81]
186186
1class A { set a(val) { switch(val) { case 1: return x; case 2: return; default: return z } } }
187187
· ────────
188188
╰────
189189

190190
eslint(no-setter-return): Setter cannot return a value
191-
╭─[no_setter_return.tsx:1:72]
191+
╭─[no_setter_return.js:1:72]
192192
1 │ (class { static set a(val) { if (val > 0) { this._val = val; return; } return false; } })
193193
· ─────────────
194194
╰────
195195

196196
eslint(no-setter-return): Setter cannot return a value
197-
╭─[no_setter_return.tsx:1:32]
197+
╭─[no_setter_return.js:1:32]
198198
1 │ ({ set a(val) { function b(){} return b(); } })
199199
· ───────────
200200
╰────
201201

202202
eslint(no-setter-return): Setter cannot return a value
203-
╭─[no_setter_return.tsx:1:24]
203+
╭─[no_setter_return.js:1:24]
204204
1class A { set a(val) { return () => {}; } }
205205
· ────────────────
206206
╰────
207207

208208
eslint(no-setter-return): Setter cannot return a value
209-
╭─[no_setter_return.tsx:1:49]
209+
╭─[no_setter_return.js:1:49]
210210
1 │ (class { set a(val) { function b(){ return 1; } return 2; } })
211211
· ─────────
212212
╰────
213213

214214
eslint(no-setter-return): Setter cannot return a value
215-
╭─[no_setter_return.tsx:1:41]
215+
╭─[no_setter_return.js:1:41]
216216
1 │ ({ set a(val) { function b(){ return; } return 1; } })
217217
· ─────────
218218
╰────
219219

220220
eslint(no-setter-return): Setter cannot return a value
221-
╭─[no_setter_return.tsx:1:58]
221+
╭─[no_setter_return.js:1:58]
222222
1class A { set a(val) { var x = function() { return 1; }; return 2; } }
223223
· ─────────
224224
╰────
225225

226226
eslint(no-setter-return): Setter cannot return a value
227-
╭─[no_setter_return.tsx:1:50]
227+
╭─[no_setter_return.js:1:50]
228228
1 │ (class { set a(val) { var x = () => { return; }; return 2; } })
229229
· ─────────
230230
╰────
231231

232232
eslint(no-setter-return): Setter cannot return a value
233-
╭─[no_setter_return.tsx:1:33]
233+
╭─[no_setter_return.js:1:33]
234234
1function f(){}; ({ set a(val) { return 1; } });
235235
· ─────────
236236
╰────
237237

238238
eslint(no-setter-return): Setter cannot return a value
239-
╭─[no_setter_return.tsx:1:44]
239+
╭─[no_setter_return.js:1:44]
240240
1x = function f(){}; class A { set a(val) { return 1; } };
241241
· ─────────
242242
╰────
243243

244244
eslint(no-setter-return): Setter cannot return a value
245-
╭─[no_setter_return.tsx:1:40]
245+
╭─[no_setter_return.js:1:40]
246246
1x = () => {}; A = class { set a(val) { return 1; } };
247247
· ─────────
248248
╰────
249249

250250
eslint(no-setter-return): Setter cannot return a value
251-
╭─[no_setter_return.tsx:1:25]
251+
╭─[no_setter_return.js:1:25]
252252
1return; ({ set a(val) { return 1; } }); return 2;
253253
· ─────────
254254
╰────

crates/oxc_parser/src/diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,10 @@ pub fn setter_with_parameters(span: Span) -> OxcDiagnostic {
732732
pub fn setter_with_rest_parameter(span: Span) -> OxcDiagnostic {
733733
OxcDiagnostic::error("A 'set' accessor cannot have rest parameter.").with_label(span)
734734
}
735+
#[cold]
736+
pub fn setter_with_assignment_pattern(span: Span) -> OxcDiagnostic {
737+
OxcDiagnostic::error("A 'set' accessor cannot have an initializer.").with_label(span)
738+
}
735739

736740
#[cold]
737741
pub fn getter_parameters(span: Span) -> OxcDiagnostic {

crates/oxc_parser/src/js/class.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,10 @@ impl<'a> ParserImpl<'a> {
605605
self.error(diagnostics::setter_with_rest_parameter(rest.span));
606606
} else if function.params.parameters_count() != 1 {
607607
self.error(diagnostics::setter_with_parameters(function.params.span));
608+
} else if self.is_ts
609+
&& function.params.items.first().unwrap().pattern.kind.is_assignment_pattern()
610+
{
611+
self.error(diagnostics::setter_with_assignment_pattern(function.params.span));
608612
}
609613
}
610614

tasks/coverage/snapshots/parser_typescript.snap

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ commit: 81c95189
33
parser_typescript Summary:
44
AST Parsed : 6530/6537 (99.89%)
55
Positive Passed: 6519/6537 (99.72%)
6-
Negative Passed: 1401/5763 (24.31%)
6+
Negative Passed: 1403/5763 (24.34%)
77
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration24.ts
88

99
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ExportAssignment7.ts
@@ -36,8 +36,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/accessorDecl
3636

3737
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts
3838

39-
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/accessorWithInitializer.ts
40-
4139
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/accessorsInAmbientContext.ts
4240

4341
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts
@@ -7356,8 +7354,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ec
73567354

73577355
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration1.ts
73587356

7359-
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration16.ts
7360-
73617357
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration17.ts
73627358

73637359
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration2.ts
@@ -9313,6 +9309,22 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
93139309
4 │ }
93149310
╰────
93159311

9312+
× A 'set' accessor cannot have an initializer.
9313+
╭─[typescript/tests/cases/compiler/accessorWithInitializer.ts:2:10]
9314+
1 │ class C {
9315+
2 │ set X(v = 0) { }
9316+
· ───────
9317+
3 │ static set X(v2 = 0) { }
9318+
╰────
9319+
9320+
× A 'set' accessor cannot have an initializer.
9321+
╭─[typescript/tests/cases/compiler/accessorWithInitializer.ts:3:17]
9322+
2 │ set X(v = 0) { }
9323+
3 │ static set X(v2 = 0) { }
9324+
· ────────
9325+
4 │ }
9326+
╰────
9327+
93169328
× A 'set' accessor cannot have rest parameter.
93179329
╭─[typescript/tests/cases/compiler/accessorWithRestParam.ts:2:11]
93189330
1 │ class C {
@@ -27496,6 +27508,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
2749627508
3 │ }
2749727509
╰────
2749827510

27511+
× A 'set' accessor cannot have an initializer.
27512+
╭─[typescript/tests/cases/conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration16.ts:2:11]
27513+
1 │ class C {
27514+
2 │ set Foo(a = 1) { }
27515+
· ───────
27516+
3 │ }
27517+
╰────
27518+
2749927519
× A 'set' accessor cannot have rest parameter.
2750027520
╭─[typescript/tests/cases/conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration18.ts:2:12]
2750127521
1 │ class C {

0 commit comments

Comments
 (0)