feat(parser): error on ambient class accessor implementations (#18592)

This commit is contained in:
camc314 committed 2026-01-28 11:18:54 +00:00
1 parent 6b8a5ae022
commit 8670b18229
6 files changed
+249 -22

No files matched your search

@@ -144,7 +144,7 @@ fn test() {
use crate::tester::Tester;
let pass = vec![
"declare abstract class C { foo() {} get new();bar();}",
"declare abstract class C { foo(); get new();bar();}",
"class C { constructor();}",
"const foo = class { constructor();};",
"const foo = class { new(): X;};",
+6
View File
@@ -738,6 +738,12 @@ impl<'a> ParserImpl<'a> {
}
}
}
if self.ctx.has_ambient()
&& let Some(body) = &method.value.body
{
self.error(diagnostics::implementation_in_ambient(Span::empty(body.span.start)));
}
}
fn check_method_definition_accessor(&mut self, method: &MethodDefinition<'a>) {
@@ -176,6 +176,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts:17:19]
16 │ declare class DeclaredClass extends BaseClass {
17 │ override test() {}
· ▲
18 │ }
╰────
× Identifier `show` has already been declared
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts:2:12]
1 │ class MyClass extends BaseClass {
@@ -12510,6 +12518,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
╰────
help: Allowed modifiers are: private, protected, public, static, abstract, override, async
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/declare-method/input.ts:2:17]
1 │ class A {
2 │ declare foo() {}
· ▲
3 │ }
╰────
× Expected a semicolon or an implicit semicolon after a statement, but found none
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/declare-new-line-abstract/input.ts:1:8]
1 │ declare abstract
@@ -12634,6 +12650,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
╰────
help: Allowed modifiers are: private, protected, public, static, abstract, override, async
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/generator-method-with-modifiers/input.ts:7:16]
6 │ readonly *e() {}
7 │ declare *f() {}
· ▲
8 │ protected *g() {}
╰────
× TS(1244): Abstract methods can only appear within an abstract class.
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/generator-method-with-modifiers/input.ts:5:13]
4 │ static *c() {}
@@ -12779,6 +12803,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
╰────
help: Allowed modifiers are: private, protected, public, static, abstract, override, async
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/optional-generator-method-with-invalid-modifiers/input.ts:4:17]
3 │ readonly *e?() { }
4 │ declare *f?() { }
· ▲
5 │ }
╰────
× TS(1245): Method 'd?.d' cannot have an implementation because it is marked abstract.
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/optional-generator-method-with-invalid-modifiers/input.ts:8:14]
7 │ class A {
@@ -12805,6 +12837,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
╰────
help: Allowed modifiers are: private, protected, public, static, abstract, override, async
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/optional-generator-method-with-invalid-modifiers/input.ts:10:22]
9 │ readonly *[e?.e]?() { }
10 │ declare *[f?.f]?() { }
· ▲
11 │ }
╰────
× TS(1244): Abstract methods can only appear within an abstract class.
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/optional-generator-method-with-invalid-modifiers/input.ts:2:13]
1 │ class C {
@@ -733,6 +733,14 @@ Negative Passed: 134/134 (100.00%)
╰────
help: Allowed modifiers are: private, protected, public, static, abstract, override, async
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[misc/fail/oxc-11713-24.ts:2:20]
1 │ class Foo {
2 │ declare method() {}
· ▲
3 │ }
╰────
× TS(1024): 'readonly' modifier can only appear on a property declaration or index signature.
╭─[misc/fail/oxc-11713-25.ts:2:3]
1 │ class Foo {
+193 -21
View File
@@ -3,7 +3,7 @@ commit: 7f6a8467
parser_typescript Summary:
AST Parsed : 9842/9843 (99.99%)
Positive Passed: 9837/9843 (99.94%)
Negative Passed: 1500/2555 (58.71%)
Negative Passed: 1510/2555 (59.10%)
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDeclaration3.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDeclaration4.ts
@@ -14,8 +14,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDecl
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/accessorsInAmbientContext.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/allowSyntheticDefaultImports10.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ambientEnum1.ts
@@ -28,8 +26,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ambientExter
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ambientExternalModuleWithRelativeModuleName.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ambientGetters.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ambientStatement1.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/amdDependencyCommentName4.ts
@@ -182,8 +178,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constantEnum
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constructorOverloads3.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constructorOverloads6.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constructorOverloads7.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constructorsWithSpecializedSignatures.ts
@@ -362,8 +356,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/exportAssign
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/exportDeclarationsInAmbientNamespaces2.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/exportDeclareClass1.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/exportDefaultAlias_excludesEverything.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/exportDefaultClassAndValue.ts
@@ -410,8 +402,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/extendedInte
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/externSemantics.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/externSyntax.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/externalModuleImmutableBindings.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/externalModuleRefernceResolutionOrderInImportDeclaration.ts
@@ -630,8 +620,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/mergedDeclar
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/mergedDeclarations3.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/methodInAmbientClass1.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/missingDomElement_UsingDomLib.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/missingDomElements.ts
@@ -1466,8 +1454,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/externalM
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/externalModules/importsImplicitlyReadonly.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/externalModules/initializersInDeclarations.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/externalModules/multipleExportDefault1.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/externalModules/multipleExportDefault2.ts
@@ -1738,10 +1724,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ec
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors3.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors5.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors6.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/Accessors/parserGetAccessorWithTypeParameters1.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/Accessors/parserSetAccessorWithTypeAnnotation1.ts
@@ -1752,8 +1734,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ec
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ClassDeclarations/parserClassDeclaration12.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ClassDeclarations/parserClassDeclaration18.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ClassDeclarations/parserClassDeclaration7.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName4.ts
@@ -2524,6 +2504,70 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
╰────
help: Add a function body (`{}`).
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/accessorsInAmbientContext.ts:3:17]
2 │ class C {
3 │ get X() { return 1; }
· ▲
4 │ set X(v) { }
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/accessorsInAmbientContext.ts:4:18]
3 │ get X() { return 1; }
4 │ set X(v) { }
· ▲
5 │
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/accessorsInAmbientContext.ts:6:24]
5 │
6 │ static get Y() { return 1; }
· ▲
7 │ static set Y(v) { }
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/accessorsInAmbientContext.ts:7:25]
6 │ static get Y() { return 1; }
7 │ static set Y(v) { }
· ▲
8 │ }
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/accessorsInAmbientContext.ts:12:13]
11 │ declare class C {
12 │ get X() { return 1; }
· ▲
13 │ set X(v) { }
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/accessorsInAmbientContext.ts:13:14]
12 │ get X() { return 1; }
13 │ set X(v) { }
· ▲
14 │
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/accessorsInAmbientContext.ts:15:20]
14 │
15 │ static get Y() { return 1; }
· ▲
16 │ static set Y(v) { }
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/accessorsInAmbientContext.ts:16:21]
15 │ static get Y() { return 1; }
16 │ static set Y(v) { }
· ▲
17 │ }
╰────
× Unexpected token
╭─[typescript/tests/cases/compiler/aliasErrors.ts:13:12]
12 │ import m2 = no.mod;
@@ -2572,6 +2616,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
5 │ }
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/ambientGetters.ts:6:18]
5 │ declare class B {
6 │ get length() { return 0; }
· ▲
7 │ }
╰────
× Expected `;` but found `:`
╭─[typescript/tests/cases/compiler/ambientPropertyDeclarationInJs.ts:6:17]
5 │
@@ -4518,6 +4570,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
8 │
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/constructorOverloads6.ts:4:25]
3 │ constructor(n: number);
4 │ constructor(x: any) {
· ▲
5 │
╰────
× Multiple constructor implementations are not allowed.
╭─[typescript/tests/cases/compiler/constructorOverloads8.ts:2:5]
1 │ class C {
@@ -5992,6 +6052,22 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
14 │ }
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/exportDeclareClass1.ts:2:21]
1 │ export declare class eaC {
2 │ static tF() { };
· ▲
3 │ static tsF(param:any) { };
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/exportDeclareClass1.ts:3:31]
2 │ static tF() { };
3 │ static tsF(param:any) { };
· ▲
4 │ };
╰────
× Cannot use `await` as an identifier in an async context
╭─[typescript/tests/cases/compiler/exportDefaultAsyncFunction2.ts:2:17]
1 │ export function async<T>(...args: any[]): any { }
@@ -6104,6 +6180,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
╰────
help: Try inserting a semicolon here
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/externSyntax.ts:8:20]
7 │ public f();
8 │ public g() { } // error body
· ▲
9 │ }
╰────
× Expected `,` or `)` but found `=>`
╭─[typescript/tests/cases/compiler/fatarrowfunctionsErrors.ts:2:8]
1 │ foo((...Far:any[])=>{return 0;})
@@ -7788,6 +7872,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
2 │ const b = !@#!@#!@#!
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/compiler/methodInAmbientClass1.ts:2:20]
1 │ declare class Foo {
2 │ fn(): boolean {
· ▲
3 │ }
╰────
× Identifier `baz` has already been declared
╭─[typescript/tests/cases/compiler/mismatchedClassConstructorVariable.ts:1:5]
1 │ var baz: foo;
@@ -12238,6 +12330,30 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
21 │
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/conformance/ambient/ambientErrors.ts:39:23]
38 │ y = 4;
39 │ constructor() { }
· ▲
40 │ fn() { }
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/conformance/ambient/ambientErrors.ts:40:14]
39 │ constructor() { }
40 │ fn() { }
· ▲
41 │ static sfn() { }
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/conformance/ambient/ambientErrors.ts:41:22]
40 │ fn() { }
41 │ static sfn() { }
· ▲
42 │ }
╰────
× TS(2309): An export assignment cannot be used in a module with other exported elements
╭─[typescript/tests/cases/conformance/ambient/ambientErrors.ts:57:5]
56 │ export var q;
@@ -12788,6 +12904,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
9 │ }
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractDeclarations.d.ts:2:28]
1 │ declare abstract class A {
2 │ abstract constructor() {}
· ▲
3 │ }
╰────
× TS(1242): 'abstract' modifier can only appear on a class, method, or property declaration.
╭─[typescript/tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractDeclarations.d.ts:2:14]
1 │ declare abstract class A {
@@ -15105,6 +15229,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
╰────
help: Allowed modifiers are: private, protected, public, static, abstract, override, async
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/conformance/classes/propertyMemberDeclarations/derivedUninitializedPropertyDeclaration.ts:15:17]
14 │ class BOther extends A {
15 │ declare m() { return 2 } // not allowed on methods
· ▲
16 │ declare nonce: any; // ok, even though it's not in the base
╰────
× TS(2391): Function implementation is missing or not immediately following the declaration.
╭─[typescript/tests/cases/conformance/classes/propertyMemberDeclarations/memberFunctionDeclarations/memberFunctionOverloadMixingStaticAndInstance.ts:3:12]
2 │ foo();
@@ -19675,6 +19807,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
· ─
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/conformance/externalModules/initializersInDeclarations.ts:6:16]
5 │ "some prop" = 42;
6 │ fn(): boolean {
· ▲
7 │ return false;
╰────
× Expected `as` but found `from`
╭─[typescript/tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithAMD.ts:1:10]
1 │ import * from Zero from "./0"
@@ -21146,6 +21286,22 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
╰────
help: No modifiers are allowed here.
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors5.ts:2:13]
1 │ declare class C {
2 │ get foo() { return 0; }
· ▲
3 │ }
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors6.ts:2:14]
1 │ declare class C {
2 │ set foo(v) { }
· ▲
3 │ }
╰────
× A 'get' accessor must not have any formal parameters.
╭─[typescript/tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors7.ts:1:18]
1 │ var v = { get foo(v: number) { } };
@@ -21294,6 +21450,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
3 │ constructor() { }
╰────
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/conformance/parser/ecmascript5/ClassDeclarations/parserClassDeclaration18.ts:4:25]
3 │ constructor(n: number);
4 │ constructor(x: any) {
· ▲
5 │ }
╰────
× TS(1175): 'implements' clause already seen
╭─[typescript/tests/cases/conformance/parser/ecmascript5/ClassDeclarations/parserClassDeclaration2.ts:1:9]
1 │ class C implements A implements B {
@@ -22590,6 +22754,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
╰────
help: Allowed modifiers are: private, protected, public, static, abstract, override, async
× TS(1183): An implementation cannot be declared in ambient contexts.
╭─[typescript/tests/cases/conformance/parser/ecmascript5/MemberFunctionDeclarations/parserMemberFunctionDeclaration5.ts:2:19]
1 │ class C {
2 │ declare Foo() { }
· ▲
3 │ }
╰────
× Identifier `public` has already been declared
╭─[typescript/tests/cases/conformance/parser/ecmascript5/MemberFunctionDeclarations/parserMemberFunctionDeclarationAmbiguities1.ts:2:3]
1 │ class C {
@@ -367,6 +367,7 @@ Function implementation is missing or not immediately following the declaration.
Function implementation is missing or not immediately following the declaration.
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts
An implementation cannot be declared in ambient contexts.
Identifier `show` has already been declared
Identifier `size` has already been declared