Skip to content

Commit f9b0edf

Browse files
committed
feat(parser): error on ambient class accessor implementations
1 parent 14fabec commit f9b0edf

5 files changed

Lines changed: 249 additions & 21 deletions

File tree

crates/oxc_parser/src/js/class.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,12 @@ impl<'a> ParserImpl<'a> {
738738
}
739739
}
740740
}
741+
742+
if self.ctx.has_ambient()
743+
&& let Some(body) = &method.value.body
744+
{
745+
self.error(diagnostics::implementation_in_ambient(Span::empty(body.span.start)));
746+
}
741747
}
742748

743749
fn check_method_definition_accessor(&mut self, method: &MethodDefinition<'a>) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare namespace M {
2+
class C {
3+
get X() { return 1; }
4+
set X(v) { }
5+
6+
static get Y() { return 1; }
7+
static set Y(v) { }
8+
}
9+
}

tasks/coverage/snapshots/parser_babel.snap

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
176176

177177
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts
178178

179+
× TS(1183): An implementation cannot be declared in ambient contexts.
180+
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts:17:19]
181+
16 │ declare class DeclaredClass extends BaseClass {
182+
17 │ override test() {}
183+
· ▲
184+
18 │ }
185+
╰────
186+
179187
× Identifier `show` has already been declared
180188
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts:2:12]
181189
1 │ class MyClass extends BaseClass {
@@ -12510,6 +12518,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
1251012518
╰────
1251112519
help: Allowed modifiers are: private, protected, public, static, abstract, override, async
1251212520

12521+
× TS(1183): An implementation cannot be declared in ambient contexts.
12522+
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/declare-method/input.ts:2:17]
12523+
1 │ class A {
12524+
2 │ declare foo() {}
12525+
· ▲
12526+
3 │ }
12527+
╰────
12528+
1251312529
× Expected a semicolon or an implicit semicolon after a statement, but found none
1251412530
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/declare-new-line-abstract/input.ts:1:8]
1251512531
1 │ declare abstract
@@ -12634,6 +12650,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
1263412650
╰────
1263512651
help: Allowed modifiers are: private, protected, public, static, abstract, override, async
1263612652

12653+
× TS(1183): An implementation cannot be declared in ambient contexts.
12654+
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/generator-method-with-modifiers/input.ts:7:16]
12655+
6 │ readonly *e() {}
12656+
7 │ declare *f() {}
12657+
· ▲
12658+
8 │ protected *g() {}
12659+
╰────
12660+
1263712661
× TS(1244): Abstract methods can only appear within an abstract class.
1263812662
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/generator-method-with-modifiers/input.ts:5:13]
1263912663
4 │ static *c() {}
@@ -12779,6 +12803,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
1277912803
╰────
1278012804
help: Allowed modifiers are: private, protected, public, static, abstract, override, async
1278112805

12806+
× TS(1183): An implementation cannot be declared in ambient contexts.
12807+
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/optional-generator-method-with-invalid-modifiers/input.ts:4:17]
12808+
3 │ readonly *e?() { }
12809+
4 │ declare *f?() { }
12810+
· ▲
12811+
5 │ }
12812+
╰────
12813+
1278212814
× TS(1245): Method 'd?.d' cannot have an implementation because it is marked abstract.
1278312815
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/optional-generator-method-with-invalid-modifiers/input.ts:8:14]
1278412816
7 │ class A {
@@ -12805,6 +12837,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
1280512837
╰────
1280612838
help: Allowed modifiers are: private, protected, public, static, abstract, override, async
1280712839

12840+
× TS(1183): An implementation cannot be declared in ambient contexts.
12841+
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/optional-generator-method-with-invalid-modifiers/input.ts:10:22]
12842+
9 │ readonly *[e?.e]?() { }
12843+
10 │ declare *[f?.f]?() { }
12844+
· ▲
12845+
11 │ }
12846+
╰────
12847+
1280812848
× TS(1244): Abstract methods can only appear within an abstract class.
1280912849
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/optional-generator-method-with-invalid-modifiers/input.ts:2:13]
1281012850
1 │ class C {

0 commit comments

Comments
 (0)