fix(parser): report TS1035 for quoted module names (#25305)

Quoted module names are only valid in ambient contexts in TypeScript, but we were accepting them in regular TypeScript files.

This reports TS1035 when a quoted module name appears outside a declaration file or ambient declaration. The conformance snapshots now cover both TypeScript cases, and the old invalid codegen fixture has been replaced with a valid internal module declaration.
This commit is contained in:
camc314 committed 2026-08-05 10:21:52 +00:00
1 parent 1cf7ddea46
commit 4d723caaff
10 files changed
+51 -14

No files matched your search

@@ -112,9 +112,9 @@ enum A { a, 'b' }
----------
enum A{a,"b"}
########## 26
module 'a'
module a {}
----------
module "a";
module a{}
########## 27
declare module 'a'
----------
@@ -173,9 +173,9 @@ enum A {
}
########## 26
module 'a'
module a {}
----------
module 'a';
module a {}
########## 27
declare module 'a'
+1 -1
View File
@@ -101,7 +101,7 @@ fn ts() {
"class A {private static readonly prop: string}",
"interface A { a: string, 'b': number, 'c'(): void }",
"enum A { a, 'b' }",
"module 'a'",
"module a {}",
"declare module 'a'",
"a = x!;",
"b = (x as y);",
+4
View File
@@ -328,6 +328,10 @@ parser_diagnostics! {
.with_label(span)
};
quoted_module_name_only_allowed_in_ambient_module(span: Span) => {
ts_error("1035", "Only ambient modules can use quoted names.").with_label(span)
};
declaration_single_statement(span: Span) => {
OxcDiagnostic::error("Declaration cannot appear in a single-statement context")
.with_help("Wrap this declaration in a block statement")
+3
View File
@@ -409,6 +409,9 @@ impl<'a, C: Config> ParserImpl<'a, C> {
modifiers: &Modifiers,
) -> ArenaBox<'a, TSModuleDeclaration<'a>> {
let id = TSModuleDeclarationName::StringLiteral(self.parse_literal_string());
if !self.ctx.has_ambient() {
self.error(diagnostics::quoted_module_name_only_allowed_in_ambient_module(id.span()));
}
let body = if self.at(Kind::LCurly) {
// External module body (`declare module "x" {}`); `import`/`export` are allowed here.
let block = self.parse_ts_module_block(/* in_ts_namespace_body */ false);
+18 -1
View File
@@ -2,7 +2,7 @@ commit: 06b6eae3
parser_babel Summary:
AST Parsed : 2234/2234 (100.00%)
Positive Passed: 2219/2234 (99.33%)
Positive Passed: 2218/2234 (99.28%)
Negative Passed: 1727/1742 (99.14%)
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/cast/unparenthesized-assert-and-assign/input.ts
@@ -248,6 +248,23 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
· ──
╰────
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/module-namespace/head/input.ts
× TS(1035): Only ambient modules can use quoted names.
╭─[babel/packages/babel-parser/test/fixtures/typescript/module-namespace/head/input.ts:3:8]
2 │ namespace M.N.O {}
3 │ module "m" {}
· ───
4 │ module 'n' {}
╰────
× TS(1035): Only ambient modules can use quoted names.
╭─[babel/packages/babel-parser/test/fixtures/typescript/module-namespace/head/input.ts:4:8]
3 │ module "m" {}
4 │ module 'n' {}
· ───
╰────
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-in-nested-module/input.ts
× TS(2435): Ambient modules cannot be nested in other modules or namespaces.
@@ -3,7 +3,7 @@ commit: b465fdbf
parser_typescript Summary:
AST Parsed : 9779/9779 (100.00%)
Positive Passed: 9779/9779 (100.00%)
Negative Passed: 1645/2640 (62.31%)
Negative Passed: 1647/2640 (62.39%)
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDeclaration3.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDeclaration4.ts
@@ -776,8 +776,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/qualifiedMod
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/quickinfoTypeAtReturnPositionsInaccurate.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/quotedModuleNameMustBeAmbient.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/reboundIdentifierOnImportAlias.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/recursiveBaseCheck3.ts
@@ -1742,8 +1740,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ec
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ModuleDeclarations/parserModuleDeclaration1.d.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ModuleDeclarations/parserModuleDeclaration1.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ModuleDeclarations/parserModuleDeclaration10.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ModuleDeclarations/parserModuleDeclaration2.d.ts
@@ -11295,6 +11291,13 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/wit
4 │
╰────
× TS(1035): Only ambient modules can use quoted names.
╭─[typescript/tests/cases/compiler/quotedModuleNameMustBeAmbient.ts:1:8]
1 │ module 'M' {}
· ───
2 │
╰────
× Expected `,` or `)` but found `!`
╭─[typescript/tests/cases/compiler/reachabilityChecksNoCrash1.ts:1:54]
1 │ export async function arrayFromAsync<T>(asyncIterable!: AsyncIterable<T>): Promise<T[]> {
@@ -26814,6 +26817,13 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/wit
· ────
╰────
× TS(1035): Only ambient modules can use quoted names.
╭─[typescript/tests/cases/conformance/parser/ecmascript5/ModuleDeclarations/parserModuleDeclaration1.ts:1:8]
1 │ module "Foo" {
· ─────
2 │ }
╰────
× TS(1038): A 'declare' modifier cannot be used in an already ambient context.
╭─[typescript/tests/cases/conformance/parser/ecmascript5/ModuleDeclarations/parserModuleDeclaration3.ts:2:3]
1 │ declare namespace M {
@@ -286,6 +286,8 @@ after transform: ScopeId(0): ["A"]
rebuilt : ScopeId(0): []
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/module-namespace/head/input.ts
Only ambient modules can use quoted names.
Only ambient modules can use quoted names.
Ambient modules cannot be nested in other modules or namespaces.
Ambient modules cannot be nested in other modules or namespaces.
Bindings mismatch:
@@ -1,4 +1,4 @@
ts compatibility: 624/659 (94.69%), 15 files skipped
ts compatibility: 624/659 (94.69%), 16 files skipped
# Failed
@@ -55,5 +55,6 @@ ts compatibility: 624/659 (94.69%), 15 files skipped
- typescript/definite/definite.ts
- typescript/definite/without-annotation.ts
- typescript/interface2/module.ts
- typescript/module/keyword.ts
- typescript/namespace/invalid-await.ts
- typescript/type-parameters-arguments/const.ts
@@ -1,6 +1,6 @@
commit: 06b6eae3
Passed: 771/1164
Passed: 769/1162
# All Passed:
* babel-plugin-transform-logical-assignment-operators
@@ -1055,7 +1055,7 @@ x Output mismatch
x Output mismatch
# babel-plugin-transform-typescript (115/157)
# babel-plugin-transform-typescript (113/155)
* class/accessor-allowDeclareFields-false/input.ts
x TS(18010): An accessibility modifier cannot be used with a private