Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 0 additions & 4 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5013,10 +5013,6 @@ func (c *Checker) checkModuleDeclaration(node *ast.Node) {
}
if ast.IsIdentifier(node.Name()) {
c.checkCollisionsForDeclarationName(node, node.Name())
if node.AsModuleDeclaration().Keyword == ast.KindModuleKeyword {
tokenRange := getNonModifierTokenRangeOfNode(node)
c.suggestionDiagnostics.Add(ast.NewDiagnostic(ast.GetSourceFileOfNode(node), tokenRange, diagnostics.A_namespace_declaration_should_not_be_declared_using_the_module_keyword_Please_use_the_namespace_keyword_instead))
}
}
c.checkExportsOnMergedDeclarations(node)
symbol := c.getSymbolOfDeclaration(node)
Expand Down
8 changes: 0 additions & 8 deletions internal/checker/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1262,14 +1262,6 @@ func minAndMax[T any](slice []T, getValue func(value T) int) (int, int) {
return minValue, maxValue
}

func getNonModifierTokenRangeOfNode(node *ast.Node) core.TextRange {
pos := node.Pos()
if last := ast.FindLastVisibleNode(node.ModifierNodes()); last != nil {
pos = last.Pos()
}
return scanner.GetRangeOfTokenAtPosition(ast.GetSourceFileOfNode(node), pos)
}

type FeatureMapEntry struct {
lib string
props []string
Expand Down
17 changes: 11 additions & 6 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2082,19 +2082,20 @@ func (p *Parser) parseEnumDeclaration(pos int, jsdoc jsdocScannerInfo, modifiers
}

func (p *Parser) parseModuleDeclaration(pos int, jsdoc jsdocScannerInfo, modifiers *ast.ModifierList) *ast.Statement {
keyword := ast.KindModuleKeyword
if p.token == ast.KindGlobalKeyword {
// global augmentation
return p.parseAmbientExternalModuleDeclaration(pos, jsdoc, modifiers)
} else if p.parseOptional(ast.KindNamespaceKeyword) {
keyword = ast.KindNamespaceKeyword
// namespace keyword always produces a namespace declaration
return p.parseModuleOrNamespaceDeclaration(pos, jsdoc, modifiers, false /*nested*/, false /*isIllegalModuleKeyword*/)
} else {
p.parseExpected(ast.KindModuleKeyword)
if p.token == ast.KindStringLiteral {
return p.parseAmbientExternalModuleDeclaration(pos, jsdoc, modifiers)
}
// `module X {}` is illegal; use `namespace` instead
return p.parseModuleOrNamespaceDeclaration(pos, jsdoc, modifiers, false /*nested*/, true /*isIllegalModuleKeyword*/)
}
return p.parseModuleOrNamespaceDeclaration(pos, jsdoc, modifiers, false /*nested*/, keyword)
}

func (p *Parser) parseAmbientExternalModuleDeclaration(pos int, jsdoc jsdocScannerInfo, modifiers *ast.ModifierList) *ast.Node {
Expand Down Expand Up @@ -2133,25 +2134,29 @@ func (p *Parser) parseModuleBlock() *ast.Node {
return p.finishNode(p.factory.NewModuleBlock(statements), pos)
}

func (p *Parser) parseModuleOrNamespaceDeclaration(pos int, jsdoc jsdocScannerInfo, modifiers *ast.ModifierList, nested bool, keyword ast.Kind) *ast.Node {
func (p *Parser) parseModuleOrNamespaceDeclaration(pos int, jsdoc jsdocScannerInfo, modifiers *ast.ModifierList, nested bool, isIllegalModuleKeyword bool) *ast.Node {
saveHasAwaitIdentifier := p.statementHasAwaitIdentifier
var name *ast.Node
if nested {
name = p.parseIdentifierName()
} else {
name = p.parseIdentifier()
}
if isIllegalModuleKeyword && !nested {
errorStart := scanner.SkipTrivia(p.sourceText, name.Pos())
p.parseErrorAt(errorStart, name.End(), diagnostics.A_namespace_declaration_should_not_be_declared_using_the_module_keyword_Please_use_the_namespace_keyword_instead)
Comment thread
jakebailey marked this conversation as resolved.
}
var body *ast.Node
if p.parseOptional(ast.KindDotToken) {
implicitExport := p.factory.NewModifier(ast.KindExportKeyword)
implicitExport.Loc = core.NewTextRange(p.nodePos(), p.nodePos())
implicitExport.Flags = ast.NodeFlagsReparsed
implicitModifiers := p.newModifierList(implicitExport.Loc, p.nodeSlicePool.NewSlice1(implicitExport))
body = p.parseModuleOrNamespaceDeclaration(p.nodePos(), 0 /*jsdoc*/, implicitModifiers, true /*nested*/, keyword)
body = p.parseModuleOrNamespaceDeclaration(p.nodePos(), 0 /*jsdoc*/, implicitModifiers, true /*nested*/, isIllegalModuleKeyword)
} else {
body = p.parseModuleBlock()
}
result := p.finishNode(p.factory.NewModuleDeclaration(modifiers, keyword, name, body), pos)
result := p.finishNode(p.factory.NewModuleDeclaration(modifiers, ast.KindNamespaceKeyword, name, body), pos)
p.withJSDoc(result, jsdoc)
p.checkJSSyntax(result)
p.statementHasAwaitIdentifier = saveHasAwaitIdentifier
Expand Down
2 changes: 0 additions & 2 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,6 @@ func TestEmit(t *testing.T) {
{title: "EnumDeclaration#1", input: `enum a{}`, output: "enum a {\n}"},
{title: "EnumDeclaration#2", input: `enum a{b}`, output: "enum a {\n b\n}"},
{title: "EnumDeclaration#3", input: `enum a{b=c}`, output: "enum a {\n b = c\n}"},
{title: "ModuleDeclaration#1", input: `module a{}`, output: "module a { }"},
{title: "ModuleDeclaration#2", input: `module a.b{}`, output: "module a.b { }"},
{title: "ModuleDeclaration#3", input: `module "a";`, output: "module \"a\";"},
{title: "ModuleDeclaration#4", input: `module "a"{}`, output: "module \"a\" { }"},
{title: "ModuleDeclaration#5", input: `namespace a{}`, output: "namespace a { }"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,40 @@ reachabilityChecks11.ts(69,5): error TS7027: Unreachable code detected.
// while (true);
var x = 1;

module A {
namespace A {
while (true);
let x;
~~~~~~
!!! error TS7027: Unreachable code detected.
}

module A1 {
namespace A1 {
do {} while(true);
module A {
namespace A {
interface F {}
}
}

module A2 {
namespace A2 {
while (true);
module A {
~~~~~~~~~~
namespace A {
~~~~~~~~~~~~~
var x = 1;
~~~~~~~~~~~~~~~~~~
}
~~~~~
!!! error TS7027: Unreachable code detected.
}

module A3 {
namespace A3 {
while (true);
type T = string;
}

module A4 {
namespace A4 {
while (true);
module A {
~~~~~~~~~~
namespace A {
~~~~~~~~~~~~~
const enum E { X }
~~~~~~~~~~~~~~~~~~~~~~~~~~
}
Expand Down Expand Up @@ -73,9 +73,9 @@ reachabilityChecks11.ts(69,5): error TS7027: Unreachable code detected.
!!! error TS7027: Unreachable code detected.
}

module B {
namespace B {
for (; ;);
module C {
namespace C {
}
}

Expand Down
20 changes: 10 additions & 10 deletions testdata/baselines/reference/compiler/reachabilityChecks11.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@
// while (true);
var x = 1;

module A {
namespace A {
while (true);
let x;
}

module A1 {
namespace A1 {
do {} while(true);
module A {
namespace A {
interface F {}
}
}

module A2 {
namespace A2 {
while (true);
module A {
namespace A {
var x = 1;
}
}

module A3 {
namespace A3 {
while (true);
type T = string;
}

module A4 {
namespace A4 {
while (true);
module A {
namespace A {
const enum E { X }
}
}
Expand All @@ -51,9 +51,9 @@ function f2() {
}
}

module B {
namespace B {
for (; ;);
module C {
namespace C {
}
}

Expand Down
24 changes: 12 additions & 12 deletions testdata/baselines/reference/compiler/reachabilityChecks11.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,55 @@
var x = 1;
>x : Symbol(x, Decl(reachabilityChecks11.ts, 1, 3))

module A {
namespace A {
>A : Symbol(A, Decl(reachabilityChecks11.ts, 1, 10))

while (true);
let x;
>x : Symbol(x, Decl(reachabilityChecks11.ts, 5, 7))
}

module A1 {
namespace A1 {
>A1 : Symbol(A1, Decl(reachabilityChecks11.ts, 6, 1))

do {} while(true);
module A {
namespace A {
>A : Symbol(A, Decl(reachabilityChecks11.ts, 9, 22))

interface F {}
>F : Symbol(F, Decl(reachabilityChecks11.ts, 10, 14))
>F : Symbol(F, Decl(reachabilityChecks11.ts, 10, 17))
}
}

module A2 {
namespace A2 {
>A2 : Symbol(A2, Decl(reachabilityChecks11.ts, 13, 1))

while (true);
module A {
namespace A {
>A : Symbol(A, Decl(reachabilityChecks11.ts, 16, 17))

var x = 1;
>x : Symbol(x, Decl(reachabilityChecks11.ts, 18, 11))
}
}

module A3 {
namespace A3 {
>A3 : Symbol(A3, Decl(reachabilityChecks11.ts, 20, 1))

while (true);
type T = string;
>T : Symbol(T, Decl(reachabilityChecks11.ts, 23, 17))
}

module A4 {
namespace A4 {
>A4 : Symbol(A4, Decl(reachabilityChecks11.ts, 25, 1))

while (true);
module A {
namespace A {
>A : Symbol(A, Decl(reachabilityChecks11.ts, 28, 17))

const enum E { X }
>E : Symbol(E, Decl(reachabilityChecks11.ts, 29, 14))
>E : Symbol(E, Decl(reachabilityChecks11.ts, 29, 17))
>X : Symbol(E.X, Decl(reachabilityChecks11.ts, 30, 22))
}
}
Expand Down Expand Up @@ -84,11 +84,11 @@ function f2() {
}
}

module B {
namespace B {
>B : Symbol(B, Decl(reachabilityChecks11.ts, 48, 1))

for (; ;);
module C {
namespace C {
>C : Symbol(C, Decl(reachabilityChecks11.ts, 51, 14))
}
}
Expand Down
20 changes: 10 additions & 10 deletions testdata/baselines/reference/compiler/reachabilityChecks11.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var x = 1;
>x : number
>1 : 1

module A {
namespace A {
>A : typeof A

while (true);
Expand All @@ -16,24 +16,24 @@ module A {
>x : any
}

module A1 {
namespace A1 {
>A1 : typeof A1

do {} while(true);
>true : true

module A {
namespace A {
interface F {}
}
}

module A2 {
namespace A2 {
>A2 : typeof A2

while (true);
>true : true

module A {
namespace A {
>A : typeof A

var x = 1;
Expand All @@ -42,7 +42,7 @@ module A2 {
}
}

module A3 {
namespace A3 {
>A3 : typeof A3

while (true);
Expand All @@ -52,13 +52,13 @@ module A3 {
>T : string
}

module A4 {
namespace A4 {
>A4 : typeof A4

while (true);
>true : true

module A {
namespace A {
const enum E { X }
>E : E
>X : E.X
Expand Down Expand Up @@ -93,11 +93,11 @@ function f2() {
}
}

module B {
namespace B {
>B : typeof B

for (; ;);
module C {
namespace C {
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
escapedIdentifiers.ts(25,16): error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
escapedIdentifiers.ts(37,12): error TS2564: Property 'foo1' has no initializer and is not definitely assigned in the constructor.
escapedIdentifiers.ts(40,12): error TS2564: Property 'foo2' has no initializer and is not definitely assigned in the constructor.


==== escapedIdentifiers.ts (2 errors) ====
==== escapedIdentifiers.ts (3 errors) ====
/*
0 .. \u0030
9 .. \u0039
Expand All @@ -28,6 +29,8 @@ escapedIdentifiers.ts(40,12): error TS2564: Property 'foo2' has no initializer a
export var baz1: number;
}
declare module moduleType\u0032 {
~~~~~~~~~~~~~~~~
!!! error TS1540: A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.
export var baz2: number;
}

Expand Down
Loading