Skip to content

fix: add diagnose for non-declarative statements in namespace #2765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/diagnosticMessages.json
Original file line number Diff line number Diff line change
@@ -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,
9 changes: 7 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 2 additions & 0 deletions tests/parser/namespace.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 4 additions & 2 deletions tests/parser/namespace.ts.fixture.ts
Original file line number Diff line number Diff line change
@@ -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)