Skip to content

Commit eecf7ba

Browse files
committed
Support 'namespace' declarations for internal modules
1 parent 218e101 commit eecf7ba

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

Diff for: src/compiler/parser.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -3704,6 +3704,7 @@ module ts {
37043704
return !isConstEnum;
37053705
case SyntaxKind.InterfaceKeyword:
37063706
case SyntaxKind.ModuleKeyword:
3707+
case SyntaxKind.NamespaceKeyword:
37073708
case SyntaxKind.EnumKeyword:
37083709
case SyntaxKind.TypeKeyword:
37093710
// When followed by an identifier, these do not start a statement but might
@@ -4371,14 +4372,14 @@ module ts {
43714372
return finishNode(node);
43724373
}
43734374

4374-
function parseInternalModuleTail(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray, flags: NodeFlags): ModuleDeclaration {
4375+
function parseModuleOrNamespaceDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray, flags: NodeFlags): ModuleDeclaration {
43754376
let node = <ModuleDeclaration>createNode(SyntaxKind.ModuleDeclaration, fullStart);
43764377
node.decorators = decorators;
43774378
setModifiers(node, modifiers);
43784379
node.flags |= flags;
43794380
node.name = parseIdentifier();
43804381
node.body = parseOptional(SyntaxKind.DotToken)
4381-
? parseInternalModuleTail(getNodePos(), /*decorators*/ undefined, /*modifiers:*/undefined, NodeFlags.Export)
4382+
? parseModuleOrNamespaceDeclaration(getNodePos(), /*decorators*/ undefined, /*modifiers:*/undefined, NodeFlags.Export)
43824383
: parseModuleBlock();
43834384
return finishNode(node);
43844385
}
@@ -4393,10 +4394,17 @@ module ts {
43934394
}
43944395

43954396
function parseModuleDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): ModuleDeclaration {
4396-
parseExpected(SyntaxKind.ModuleKeyword);
4397-
return token === SyntaxKind.StringLiteral
4398-
? parseAmbientExternalModuleDeclaration(fullStart, decorators, modifiers)
4399-
: parseInternalModuleTail(fullStart, decorators, modifiers, modifiers ? modifiers.flags : 0);
4397+
let flags = modifiers ? modifiers.flags : 0;
4398+
if (parseOptional(SyntaxKind.NamespaceKeyword)) {
4399+
flags |= NodeFlags.Namespace;
4400+
}
4401+
else {
4402+
parseExpected(SyntaxKind.ModuleKeyword);
4403+
if (token === SyntaxKind.StringLiteral) {
4404+
return parseAmbientExternalModuleDeclaration(fullStart, decorators, modifiers);
4405+
}
4406+
}
4407+
return parseModuleOrNamespaceDeclaration(fullStart, decorators, modifiers, flags);
44004408
}
44014409

44024410
function isExternalModuleReference() {
@@ -4631,6 +4639,7 @@ module ts {
46314639
// Not true keywords so ensure an identifier follows or is string literal or asterisk or open brace
46324640
return lookAhead(nextTokenCanFollowImportKeyword);
46334641
case SyntaxKind.ModuleKeyword:
4642+
case SyntaxKind.NamespaceKeyword:
46344643
// Not a true keyword so ensure an identifier or string literal follows
46354644
return lookAhead(nextTokenIsIdentifierOrKeywordOrStringLiteral);
46364645
case SyntaxKind.ExportKeyword:
@@ -4715,6 +4724,7 @@ module ts {
47154724
case SyntaxKind.EnumKeyword:
47164725
return parseEnumDeclaration(fullStart, decorators, modifiers);
47174726
case SyntaxKind.ModuleKeyword:
4727+
case SyntaxKind.NamespaceKeyword:
47184728
return parseModuleDeclaration(fullStart, decorators, modifiers);
47194729
case SyntaxKind.ImportKeyword:
47204730
return parseImportDeclarationOrImportEqualsDeclaration(fullStart, decorators, modifiers);

Diff for: src/compiler/scanner.ts

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ module ts {
7676
"interface": SyntaxKind.InterfaceKeyword,
7777
"let": SyntaxKind.LetKeyword,
7878
"module": SyntaxKind.ModuleKeyword,
79+
"namespace": SyntaxKind.NamespaceKeyword,
7980
"new": SyntaxKind.NewKeyword,
8081
"null": SyntaxKind.NullKeyword,
8182
"number": SyntaxKind.NumberKeyword,

Diff for: src/compiler/types.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ module ts {
138138
DeclareKeyword,
139139
GetKeyword,
140140
ModuleKeyword,
141+
NamespaceKeyword,
141142
RequireKeyword,
142143
NumberKeyword,
143144
SetKeyword,
@@ -312,8 +313,9 @@ module ts {
312313
DeclarationFile = 0x00000800, // Node is a .d.ts file
313314
Let = 0x00001000, // Variable declaration
314315
Const = 0x00002000, // Variable declaration
315-
OctalLiteral = 0x00004000,
316-
ExportContext = 0x00008000, // Export context (initialized by binding)
316+
OctalLiteral = 0x00004000, // Octal numeric literal
317+
Namespace = 0x00008000, // Namespace declaration
318+
ExportContext = 0x00010000, // Export context (initialized by binding)
317319

318320
Modifier = Export | Ambient | Public | Private | Protected | Static | Default,
319321
AccessibilityModifier = Public | Private | Protected,

Diff for: src/services/formatting/rules.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ module ts.formatting {
312312
this.NoSpaceAfterModuleImport = new Rule(RuleDescriptor.create2(Shared.TokenRange.FromTokens([SyntaxKind.ModuleKeyword, SyntaxKind.RequireKeyword]), SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
313313

314314
// Add a space around certain TypeScript keywords
315-
this.SpaceAfterCertainTypeScriptKeywords = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.ClassKeyword, SyntaxKind.DeclareKeyword, SyntaxKind.EnumKeyword, SyntaxKind.ExportKeyword, SyntaxKind.ExtendsKeyword, SyntaxKind.GetKeyword, SyntaxKind.ImplementsKeyword, SyntaxKind.ImportKeyword, SyntaxKind.InterfaceKeyword, SyntaxKind.ModuleKeyword, SyntaxKind.PrivateKeyword, SyntaxKind.PublicKeyword, SyntaxKind.SetKeyword, SyntaxKind.StaticKeyword]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
315+
this.SpaceAfterCertainTypeScriptKeywords = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.ClassKeyword, SyntaxKind.DeclareKeyword, SyntaxKind.EnumKeyword, SyntaxKind.ExportKeyword, SyntaxKind.ExtendsKeyword, SyntaxKind.GetKeyword, SyntaxKind.ImplementsKeyword, SyntaxKind.ImportKeyword, SyntaxKind.InterfaceKeyword, SyntaxKind.ModuleKeyword, SyntaxKind.NamespaceKeyword, SyntaxKind.PrivateKeyword, SyntaxKind.PublicKeyword, SyntaxKind.SetKeyword, SyntaxKind.StaticKeyword]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
316316
this.SpaceBeforeCertainTypeScriptKeywords = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.ExtendsKeyword, SyntaxKind.ImplementsKeyword])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
317317

318318
// Treat string literals in module names as identifiers, and add a space between the literal and the opening Brace braces, e.g.: module "m2" {

Diff for: src/services/services.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2991,7 +2991,8 @@ module ts {
29912991
case SyntaxKind.OpenBracketToken:
29922992
return containingNodeKind === SyntaxKind.ArrayLiteralExpression; // [ |
29932993

2994-
case SyntaxKind.ModuleKeyword: // module |
2994+
case SyntaxKind.ModuleKeyword: // module |
2995+
case SyntaxKind.NamespaceKeyword: // namespace |
29952996
return true;
29962997

29972998
case SyntaxKind.DotToken:
@@ -3644,7 +3645,9 @@ module ts {
36443645
}
36453646
if (symbolFlags & SymbolFlags.Module) {
36463647
addNewLineIfDisplayPartsExist();
3647-
displayParts.push(keywordPart(SyntaxKind.ModuleKeyword));
3648+
let declaration = <ModuleDeclaration>getDeclarationOfKind(symbol, SyntaxKind.ModuleDeclaration);
3649+
let isNamespace = declaration && declaration.name && declaration.name.kind === SyntaxKind.Identifier;
3650+
displayParts.push(keywordPart(isNamespace ? SyntaxKind.NamespaceKeyword : SyntaxKind.ModuleKeyword));
36483651
displayParts.push(spacePart());
36493652
addFullSymbolName(symbol);
36503653
}

0 commit comments

Comments
 (0)