Skip to content

Commit

Permalink
fix: add diagnose for non-declarative statements in namespace (#2765)
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseCodeBones authored Nov 2, 2023
1 parent 145cbc6 commit 0b8abe4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
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;
Expand Down
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 {
Expand All @@ -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)

0 comments on commit 0b8abe4

Please sign in to comment.