Skip to content

Commit 00b22b0

Browse files
authored
Move module keyword check back to checker (#3034)
1 parent 651b520 commit 00b22b0

9 files changed

Lines changed: 82 additions & 76 deletions

File tree

internal/checker/checker.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5010,6 +5010,9 @@ func (c *Checker) checkModuleDeclaration(node *ast.Node) {
50105010
}
50115011
if ast.IsIdentifier(node.Name()) {
50125012
c.checkCollisionsForDeclarationName(node, node.Name())
5013+
if node.AsModuleDeclaration().Keyword == ast.KindModuleKeyword {
5014+
c.error(node.Name(), diagnostics.A_namespace_declaration_should_not_be_declared_using_the_module_keyword_Please_use_the_namespace_keyword_instead)
5015+
}
50135016
}
50145017
c.checkExportsOnMergedDeclarations(node)
50155018
symbol := c.getSymbolOfDeclaration(node)

internal/parser/parser.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,20 +2083,19 @@ func (p *Parser) parseEnumDeclaration(pos int, jsdoc jsdocScannerInfo, modifiers
20832083
}
20842084

20852085
func (p *Parser) parseModuleDeclaration(pos int, jsdoc jsdocScannerInfo, modifiers *ast.ModifierList) *ast.Statement {
2086+
keyword := ast.KindModuleKeyword
20862087
if p.token == ast.KindGlobalKeyword {
20872088
// global augmentation
20882089
return p.parseAmbientExternalModuleDeclaration(pos, jsdoc, modifiers)
20892090
} else if p.parseOptional(ast.KindNamespaceKeyword) {
2090-
// namespace keyword always produces a namespace declaration
2091-
return p.parseModuleOrNamespaceDeclaration(pos, jsdoc, modifiers, false /*nested*/, false /*isIllegalModuleKeyword*/)
2091+
keyword = ast.KindNamespaceKeyword
20922092
} else {
20932093
p.parseExpected(ast.KindModuleKeyword)
20942094
if p.token == ast.KindStringLiteral {
20952095
return p.parseAmbientExternalModuleDeclaration(pos, jsdoc, modifiers)
20962096
}
2097-
// `module X {}` is illegal; use `namespace` instead
2098-
return p.parseModuleOrNamespaceDeclaration(pos, jsdoc, modifiers, false /*nested*/, true /*isIllegalModuleKeyword*/)
20992097
}
2098+
return p.parseModuleOrNamespaceDeclaration(pos, jsdoc, modifiers, false /*nested*/, keyword)
21002099
}
21012100

21022101
func (p *Parser) parseAmbientExternalModuleDeclaration(pos int, jsdoc jsdocScannerInfo, modifiers *ast.ModifierList) *ast.Node {
@@ -2135,29 +2134,25 @@ func (p *Parser) parseModuleBlock() *ast.Node {
21352134
return p.finishNode(p.factory.NewModuleBlock(statements), pos)
21362135
}
21372136

2138-
func (p *Parser) parseModuleOrNamespaceDeclaration(pos int, jsdoc jsdocScannerInfo, modifiers *ast.ModifierList, nested bool, isIllegalModuleKeyword bool) *ast.Node {
2137+
func (p *Parser) parseModuleOrNamespaceDeclaration(pos int, jsdoc jsdocScannerInfo, modifiers *ast.ModifierList, nested bool, keyword ast.Kind) *ast.Node {
21392138
saveHasAwaitIdentifier := p.statementHasAwaitIdentifier
21402139
var name *ast.Node
21412140
if nested {
21422141
name = p.parseIdentifierName()
21432142
} else {
21442143
name = p.parseIdentifier()
21452144
}
2146-
if isIllegalModuleKeyword && !nested {
2147-
errorStart := scanner.SkipTrivia(p.sourceText, name.Pos())
2148-
p.parseErrorAt(errorStart, name.End(), diagnostics.A_namespace_declaration_should_not_be_declared_using_the_module_keyword_Please_use_the_namespace_keyword_instead)
2149-
}
21502145
var body *ast.Node
21512146
if p.parseOptional(ast.KindDotToken) {
21522147
implicitExport := p.factory.NewModifier(ast.KindExportKeyword)
21532148
implicitExport.Loc = core.NewTextRange(p.nodePos(), p.nodePos())
21542149
implicitExport.Flags = ast.NodeFlagsReparsed
21552150
implicitModifiers := p.newModifierList(implicitExport.Loc, p.nodeSlicePool.NewSlice1(implicitExport))
2156-
body = p.parseModuleOrNamespaceDeclaration(p.nodePos(), 0 /*jsdoc*/, implicitModifiers, true /*nested*/, isIllegalModuleKeyword)
2151+
body = p.parseModuleOrNamespaceDeclaration(p.nodePos(), 0 /*jsdoc*/, implicitModifiers, true /*nested*/, keyword)
21572152
} else {
21582153
body = p.parseModuleBlock()
21592154
}
2160-
result := p.finishNode(p.factory.NewModuleDeclaration(modifiers, ast.KindNamespaceKeyword, name, body), pos)
2155+
result := p.finishNode(p.factory.NewModuleDeclaration(modifiers, keyword, name, body), pos)
21612156
p.withJSDoc(result, jsdoc)
21622157
p.checkJSSyntax(result)
21632158
p.statementHasAwaitIdentifier = saveHasAwaitIdentifier

internal/printer/printer_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ func TestEmit(t *testing.T) {
255255
{title: "EnumDeclaration#1", input: `enum a{}`, output: "enum a {\n}"},
256256
{title: "EnumDeclaration#2", input: `enum a{b}`, output: "enum a {\n b\n}"},
257257
{title: "EnumDeclaration#3", input: `enum a{b=c}`, output: "enum a {\n b = c\n}"},
258+
{title: "ModuleDeclaration#1", input: `module a{}`, output: "module a { }"},
259+
{title: "ModuleDeclaration#2", input: `module a.b{}`, output: "module a.b { }"},
258260
{title: "ModuleDeclaration#3", input: `module "a";`, output: "module \"a\";"},
259261
{title: "ModuleDeclaration#4", input: `module "a"{}`, output: "module \"a\" { }"},
260262
{title: "ModuleDeclaration#5", input: `namespace a{}`, output: "namespace a { }"},
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/compiler/moduleKeywordSkipLibCheck.ts] ////
2+
3+
//// [decl.d.ts]
4+
declare module Foo {
5+
export function bar(): void;
6+
}
7+
8+
//// [main.ts]
9+
Foo.bar();
10+
11+
12+
//// [main.js]
13+
"use strict";
14+
Foo.bar();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/moduleKeywordSkipLibCheck.ts] ////
2+
3+
=== decl.d.ts ===
4+
declare module Foo {
5+
>Foo : Symbol(Foo, Decl(decl.d.ts, 0, 0))
6+
7+
export function bar(): void;
8+
>bar : Symbol(bar, Decl(decl.d.ts, 0, 20))
9+
}
10+
11+
=== main.ts ===
12+
Foo.bar();
13+
>Foo.bar : Symbol(Foo.bar, Decl(decl.d.ts, 0, 20))
14+
>Foo : Symbol(Foo, Decl(decl.d.ts, 0, 0))
15+
>bar : Symbol(Foo.bar, Decl(decl.d.ts, 0, 20))
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/moduleKeywordSkipLibCheck.ts] ////
2+
3+
=== decl.d.ts ===
4+
declare module Foo {
5+
>Foo : typeof Foo
6+
7+
export function bar(): void;
8+
>bar : () => void
9+
}
10+
11+
=== main.ts ===
12+
Foo.bar();
13+
>Foo.bar() : void
14+
>Foo.bar : () => void
15+
>Foo : typeof Foo
16+
>bar : () => void
17+

testdata/baselines/reference/submodule/compiler/moduleKeywordDeprecated.errors.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
decl.d.ts(1,16): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
22
decl.d.ts(2,16): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
3+
decl.d.ts(2,20): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
34
decl.d.ts(7,15): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
45
decl.d.ts(8,15): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
6+
decl.d.ts(8,24): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
57
foo.ts(2,8): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
68
foo.ts(3,8): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
9+
foo.ts(3,12): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
710
foo.ts(4,16): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
811
foo.ts(5,16): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
12+
foo.ts(5,21): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
913

1014

11-
==== foo.ts (4 errors) ====
15+
==== foo.ts (6 errors) ====
1216
// Error
1317
module notok { }
1418
~~~~~
1519
!!! error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
1620
module not.ok { }
1721
~~~
22+
!!! error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
23+
~~
1824
!!! error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
1925
declare module bad { }
2026
~~~
2127
!!! error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
2228
declare module also.bad { }
2329
~~~~
2430
!!! error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
31+
~~~
32+
!!! error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
2533

2634
// Still the only way to do it
2735
declare module "good" { }
2836

29-
==== decl.d.ts (4 errors) ====
37+
==== decl.d.ts (6 errors) ====
3038
declare module foo { }
3139
~~~
3240
!!! error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
3341
declare module foo.bar { }
3442
~~~
3543
!!! error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
44+
~~~
45+
!!! error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
3646

3747
// Still the only way to do it
3848
declare module "alsogood" { }
@@ -43,4 +53,6 @@ foo.ts(5,16): error TS1540: A 'namespace' declaration should not be declared usi
4353
export module exported.sub { }
4454
~~~~~~~~
4555
!!! error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
56+
~~~
57+
!!! error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
4658

testdata/baselines/reference/submodule/compiler/moduleKeywordDeprecated.errors.txt.diff

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @strict: true
2+
// @skipLibCheck: true
3+
4+
// @filename: decl.d.ts
5+
declare module Foo {
6+
export function bar(): void;
7+
}
8+
9+
// @filename: main.ts
10+
Foo.bar();

0 commit comments

Comments
 (0)