From e352a59efd442664fd5164f456e8c1c77fa5c73c Mon Sep 17 00:00:00 2001 From: Jesse Chen Date: Mon, 30 Oct 2023 11:15:04 +0800 Subject: [PATCH] fix: add diagnose to statement of namespace --- src/diagnosticMessages.json | 1 + src/parser.ts | 9 +++++++-- tests/parser/namespace.ts | 2 ++ tests/parser/namespace.ts.fixture.ts | 6 ++++-- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/diagnosticMessages.json b/src/diagnosticMessages.json index 752ec4550d..e15946207a 100644 --- a/src/diagnosticMessages.json +++ b/src/diagnosticMessages.json @@ -194,6 +194,7 @@ "Cannot extend a class '{0}'. Class constructor is marked as private.": 2675, "The 'this' types of each signature are incompatible.": 2685, "Namespace '{0}' has no exported member '{1}'.": 2694, + "Namespace can only have declarations.": 2695, "Required type parameters may not follow optional type parameters.": 2706, "Duplicate property '{0}'.": 2718, "Property '{0}' is missing in type '{1}' but required in type '{2}'.": 2741, diff --git a/src/parser.ts b/src/parser.ts index 3bcee57681..58050e837e 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -398,9 +398,14 @@ export class Parser extends DiagnosticEmitter { tn.range(declareStart, declareEnd), "declare" ); // recoverable } - if (!namespace) { + if (namespace) { + this.error( + DiagnosticCode.Namespace_can_only_have_declarations, + tn.range(startPos) + ); + } else { statement = this.parseStatement(tn, true); - } // TODO: else? + } } break; } diff --git a/tests/parser/namespace.ts b/tests/parser/namespace.ts index 63fb767f7d..93ee68a315 100644 --- a/tests/parser/namespace.ts +++ b/tests/parser/namespace.ts @@ -1,7 +1,9 @@ +let outerVar:i32 = 0; declare namespace A { namespace B { export namespace C { var aVar: i32; + outerVar = 42; // 2695: Namespace can only have declarations. const aConst: i32; const aConstInvalid: i32 = 0; // 1039: Initializers are not allowed in ambient contexts. function aFunc(): void; diff --git a/tests/parser/namespace.ts.fixture.ts b/tests/parser/namespace.ts.fixture.ts index 8ef853742f..bcce2c0167 100644 --- a/tests/parser/namespace.ts.fixture.ts +++ b/tests/parser/namespace.ts.fixture.ts @@ -1,3 +1,4 @@ +let outerVar: i32 = 0; declare namespace A { namespace B { export namespace C { @@ -14,5 +15,6 @@ declare namespace A { } } } -// ERROR 1039: "Initializers are not allowed in ambient contexts." in namespace.ts(6,32+1) -// ERROR 1183: "An implementation cannot be declared in ambient contexts." in namespace.ts(8,37+1) +// ERROR 2695: "Namespace can only have declarations." in namespace.ts(6,7+0) +// ERROR 1039: "Initializers are not allowed in ambient contexts." in namespace.ts(8,32+1) +// ERROR 1183: "An implementation cannot be declared in ambient contexts." in namespace.ts(10,37+1)