Skip to content

Commit e352a59

Browse files
author
Jesse Chen
committed
fix: add diagnose to statement of namespace
1 parent c22c078 commit e352a59

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

Diff for: src/diagnosticMessages.json

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
"Cannot extend a class '{0}'. Class constructor is marked as private.": 2675,
195195
"The 'this' types of each signature are incompatible.": 2685,
196196
"Namespace '{0}' has no exported member '{1}'.": 2694,
197+
"Namespace can only have declarations.": 2695,
197198
"Required type parameters may not follow optional type parameters.": 2706,
198199
"Duplicate property '{0}'.": 2718,
199200
"Property '{0}' is missing in type '{1}' but required in type '{2}'.": 2741,

Diff for: src/parser.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,14 @@ export class Parser extends DiagnosticEmitter {
398398
tn.range(declareStart, declareEnd), "declare"
399399
); // recoverable
400400
}
401-
if (!namespace) {
401+
if (namespace) {
402+
this.error(
403+
DiagnosticCode.Namespace_can_only_have_declarations,
404+
tn.range(startPos)
405+
);
406+
} else {
402407
statement = this.parseStatement(tn, true);
403-
} // TODO: else?
408+
}
404409
}
405410
break;
406411
}

Diff for: tests/parser/namespace.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
let outerVar:i32 = 0;
12
declare namespace A {
23
namespace B {
34
export namespace C {
45
var aVar: i32;
6+
outerVar = 42; // 2695: Namespace can only have declarations.
57
const aConst: i32;
68
const aConstInvalid: i32 = 0; // 1039: Initializers are not allowed in ambient contexts.
79
function aFunc(): void;

Diff for: tests/parser/namespace.ts.fixture.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
let outerVar: i32 = 0;
12
declare namespace A {
23
namespace B {
34
export namespace C {
@@ -14,5 +15,6 @@ declare namespace A {
1415
}
1516
}
1617
}
17-
// ERROR 1039: "Initializers are not allowed in ambient contexts." in namespace.ts(6,32+1)
18-
// ERROR 1183: "An implementation cannot be declared in ambient contexts." in namespace.ts(8,37+1)
18+
// ERROR 2695: "Namespace can only have declarations." in namespace.ts(6,7+0)
19+
// ERROR 1039: "Initializers are not allowed in ambient contexts." in namespace.ts(8,32+1)
20+
// ERROR 1183: "An implementation cannot be declared in ambient contexts." in namespace.ts(10,37+1)

0 commit comments

Comments
 (0)