Skip to content

Commit 96a3047

Browse files
committed
Update for new syntax
1 parent 0eba61c commit 96a3047

28 files changed

+213
-37
lines changed

src/compiler/binder.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,10 @@ namespace ts {
13971397
}
13981398

13991399
function bindGlobalModuleExportDeclaration(node: GlobalModuleExportDeclaration) {
1400+
if (!file.externalModuleIndicator) {
1401+
file.bindDiagnostics.push(createDiagnosticForNode(node, Diagnostics.Global_module_exports_may_only_appear_in_module_files));
1402+
return;
1403+
}
14001404
file.symbol.globalExports = file.symbol.globalExports || {};
14011405
declareSymbol(file.symbol.globalExports, file.symbol, node, SymbolFlags.Alias, SymbolFlags.AliasExcludes);
14021406
}

src/compiler/checker.ts

+19
Original file line numberDiff line numberDiff line change
@@ -15201,6 +15201,23 @@ namespace ts {
1520115201
}
1520215202
}
1520315203

15204+
function checkGlobalModuleExportDeclaration(node: GlobalModuleExportDeclaration) {
15205+
if (node.modifiers && node.modifiers.length) {
15206+
error(node, Diagnostics.Modifiers_cannot_appear_here);
15207+
}
15208+
15209+
if (node.parent.kind !== SyntaxKind.SourceFile) {
15210+
error(node, Diagnostics.Global_module_exports_may_only_appear_at_top_level);
15211+
}
15212+
else {
15213+
const parent = node.parent as SourceFile;
15214+
// Note: missing external module indicator case checked in binder
15215+
if (parent.externalModuleIndicator && !parent.isDeclarationFile) {
15216+
error(node, Diagnostics.Global_module_exports_may_only_appear_in_declaration_files);
15217+
}
15218+
}
15219+
15220+
}
1520415221

1520515222
function checkSourceElement(node: Node): void {
1520615223
if (!node) {
@@ -15318,6 +15335,8 @@ namespace ts {
1531815335
return checkExportDeclaration(<ExportDeclaration>node);
1531915336
case SyntaxKind.ExportAssignment:
1532015337
return checkExportAssignment(<ExportAssignment>node);
15338+
case SyntaxKind.GlobalModuleExportDeclaration:
15339+
return checkGlobalModuleExportDeclaration(<GlobalModuleExportDeclaration>node);
1532115340
case SyntaxKind.EmptyStatement:
1532215341
checkGrammarStatementInAmbientContext(node);
1532315342
return;

src/compiler/diagnosticMessages.json

+12
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,18 @@
831831
"category": "Error",
832832
"code": 1313
833833
},
834+
"Global module exports may only appear in module files.": {
835+
"category": "Error",
836+
"code": 1314
837+
},
838+
"Global module exports may only appear in declaration files.": {
839+
"category": "Error",
840+
"code": 1315
841+
},
842+
"Global module exports may only appear at top level.": {
843+
"category": "Error",
844+
"code": 1316
845+
},
834846
"Duplicate identifier '{0}'.": {
835847
"category": "Error",
836848
"code": 2300

src/compiler/parser.ts

+15-18
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ namespace ts {
11461146
if (token === SyntaxKind.DefaultKeyword) {
11471147
return lookAhead(nextTokenIsClassOrFunction);
11481148
}
1149-
return token !== SyntaxKind.AsteriskToken && token !== SyntaxKind.OpenBraceToken && canFollowModifier();
1149+
return token !== SyntaxKind.AsteriskToken && token !== SyntaxKind.AsKeyword && token !== SyntaxKind.OpenBraceToken && canFollowModifier();
11501150
}
11511151
if (token === SyntaxKind.DefaultKeyword) {
11521152
return nextTokenIsClassOrFunction();
@@ -4431,7 +4431,8 @@ namespace ts {
44314431
case SyntaxKind.ExportKeyword:
44324432
nextToken();
44334433
if (token === SyntaxKind.EqualsToken || token === SyntaxKind.AsteriskToken ||
4434-
token === SyntaxKind.OpenBraceToken || token === SyntaxKind.DefaultKeyword) {
4434+
token === SyntaxKind.OpenBraceToken || token === SyntaxKind.DefaultKeyword ||
4435+
token === SyntaxKind.AsKeyword) {
44354436
return true;
44364437
}
44374438
continue;
@@ -4608,22 +4609,23 @@ namespace ts {
46084609
case SyntaxKind.EnumKeyword:
46094610
return parseEnumDeclaration(fullStart, decorators, modifiers);
46104611
case SyntaxKind.GlobalKeyword:
4611-
if (lookAhead(isGlobalModuleExportDeclaration)) {
4612-
return parseGlobalModuleExportDeclaration(fullStart, decorators, modifiers);
4613-
}
4614-
else {
4615-
return parseModuleDeclaration(fullStart, decorators, modifiers);
4616-
}
4612+
return parseModuleDeclaration(fullStart, decorators, modifiers);
46174613
case SyntaxKind.ModuleKeyword:
46184614
case SyntaxKind.NamespaceKeyword:
46194615
return parseModuleDeclaration(fullStart, decorators, modifiers);
46204616
case SyntaxKind.ImportKeyword:
46214617
return parseImportDeclarationOrImportEqualsDeclaration(fullStart, decorators, modifiers);
46224618
case SyntaxKind.ExportKeyword:
46234619
nextToken();
4624-
return token === SyntaxKind.DefaultKeyword || token === SyntaxKind.EqualsToken ?
4625-
parseExportAssignment(fullStart, decorators, modifiers) :
4626-
parseExportDeclaration(fullStart, decorators, modifiers);
4620+
switch (token) {
4621+
case SyntaxKind.DefaultKeyword:
4622+
case SyntaxKind.EqualsToken:
4623+
return parseExportAssignment(fullStart, decorators, modifiers);
4624+
case SyntaxKind.AsKeyword:
4625+
return parseGlobalModuleExportDeclaration(fullStart, decorators, modifiers);
4626+
default:
4627+
return parseExportDeclaration(fullStart, decorators, modifiers);
4628+
}
46274629
default:
46284630
if (decorators || modifiers) {
46294631
// We reached this point because we encountered decorators and/or modifiers and assumed a declaration
@@ -4637,11 +4639,6 @@ namespace ts {
46374639
}
46384640
}
46394641

4640-
function isGlobalModuleExportDeclaration() {
4641-
nextToken();
4642-
return token === SyntaxKind.ExportKeyword;
4643-
}
4644-
46454642
function nextTokenIsIdentifierOrStringLiteralOnSameLine() {
46464643
nextToken();
46474644
return !scanner.hasPrecedingLineBreak() && (isIdentifier() || token === SyntaxKind.StringLiteral);
@@ -5301,8 +5298,8 @@ namespace ts {
53015298
const exportDeclaration = <GlobalModuleExportDeclaration>createNode(SyntaxKind.GlobalModuleExportDeclaration, fullStart);
53025299
exportDeclaration.decorators = decorators;
53035300
exportDeclaration.modifiers = modifiers;
5304-
parseExpected(SyntaxKind.GlobalKeyword);
5305-
parseExpected(SyntaxKind.ExportKeyword);
5301+
parseExpected(SyntaxKind.AsKeyword);
5302+
parseExpected(SyntaxKind.NamespaceKeyword);
53065303

53075304
exportDeclaration.name = parseIdentifier();
53085305

src/compiler/program.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ namespace ts {
12821282
}
12831283

12841284
function processRootFile(fileName: string, isDefaultLib: boolean) {
1285-
processSourceFile(normalizePath(fileName), isDefaultLib, true);
1285+
processSourceFile(normalizePath(fileName), isDefaultLib, /*isReference*/ true);
12861286
}
12871287

12881288
function fileReferenceIsEqualTo(a: FileReference, b: FileReference): boolean {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
tests/cases/conformance/externalModules/err1.d.ts(3,1): error TS1314: Global module exports may only appear in module files.
2+
tests/cases/conformance/externalModules/err2.d.ts(3,2): error TS1314: Global module exports may only appear in module files.
3+
tests/cases/conformance/externalModules/err2.d.ts(3,2): error TS1316: Global module exports may only appear at top level.
4+
tests/cases/conformance/externalModules/err3.d.ts(3,1): error TS1184: Modifiers cannot appear here.
5+
tests/cases/conformance/externalModules/err3.d.ts(4,1): error TS1184: Modifiers cannot appear here.
6+
tests/cases/conformance/externalModules/err3.d.ts(5,1): error TS1184: Modifiers cannot appear here.
7+
tests/cases/conformance/externalModules/err3.d.ts(6,7): error TS1134: Variable declaration expected.
8+
tests/cases/conformance/externalModules/err4.d.ts(3,2): error TS1316: Global module exports may only appear at top level.
9+
tests/cases/conformance/externalModules/err5.ts(3,1): error TS1315: Global module exports may only appear in declaration files.
10+
11+
12+
==== tests/cases/conformance/externalModules/err1.d.ts (1 errors) ====
13+
14+
// Illegal, can't be in script file
15+
export as namespace Foo;
16+
~~~~~~~~~~~~~~~~~~~~~~~~
17+
!!! error TS1314: Global module exports may only appear in module files.
18+
19+
==== tests/cases/conformance/externalModules/err2.d.ts (2 errors) ====
20+
// Illegal, can't be in external ambient module
21+
declare module "Foo" {
22+
export as namespace Bar;
23+
~~~~~~~~~~~~~~~~~~~~~~~~
24+
!!! error TS1314: Global module exports may only appear in module files.
25+
~~~~~~~~~~~~~~~~~~~~~~~~
26+
!!! error TS1316: Global module exports may only appear at top level.
27+
}
28+
29+
==== tests/cases/conformance/externalModules/err3.d.ts (4 errors) ====
30+
// Illegal, can't have modifiers
31+
export var p;
32+
static export as namespace oo1;
33+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34+
!!! error TS1184: Modifiers cannot appear here.
35+
declare export as namespace oo2;
36+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37+
!!! error TS1184: Modifiers cannot appear here.
38+
public export as namespace oo3;
39+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40+
!!! error TS1184: Modifiers cannot appear here.
41+
const export as namespace oo4;
42+
~~~~~~
43+
!!! error TS1134: Variable declaration expected.
44+
45+
==== tests/cases/conformance/externalModules/err4.d.ts (1 errors) ====
46+
// Illegal, must be at top-level
47+
export namespace B {
48+
export as namespace C1;
49+
~~~~~~~~~~~~~~~~~~~~~~~
50+
!!! error TS1316: Global module exports may only appear at top level.
51+
}
52+
53+
==== tests/cases/conformance/externalModules/err5.ts (1 errors) ====
54+
// Illegal, may not appear in implementation files
55+
export var v;
56+
export as namespace C2;
57+
~~~~~~~~~~~~~~~~~~~~~~~
58+
!!! error TS1315: Global module exports may only appear in declaration files.
59+
60+
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [tests/cases/conformance/externalModules/umd-errors.ts] ////
2+
3+
//// [err1.d.ts]
4+
5+
// Illegal, can't be in script file
6+
export as namespace Foo;
7+
8+
//// [err2.d.ts]
9+
// Illegal, can't be in external ambient module
10+
declare module "Foo" {
11+
export as namespace Bar;
12+
}
13+
14+
//// [err3.d.ts]
15+
// Illegal, can't have modifiers
16+
export var p;
17+
static export as namespace oo1;
18+
declare export as namespace oo2;
19+
public export as namespace oo3;
20+
const export as namespace oo4;
21+
22+
//// [err4.d.ts]
23+
// Illegal, must be at top-level
24+
export namespace B {
25+
export as namespace C1;
26+
}
27+
28+
//// [err5.ts]
29+
// Illegal, may not appear in implementation files
30+
export var v;
31+
export as namespace C2;
32+
33+
34+
35+
//// [err5.js]
36+
"use strict";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/externalModules/err5.d.ts ===
2+
// Illegal, may not appear in implementation files
3+
export var v;
4+
>v : Symbol(v, Decl(err5.d.ts, 1, 10))
5+
6+
export as namespace C;
7+
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/conformance/externalModules/err5.d.ts ===
2+
// Illegal, may not appear in implementation files
3+
export var v;
4+
>v : any
5+
6+
export as namespace C;
7+
>C : any
8+
9+

tests/baselines/reference/umd1.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
export var x: number;
66
export function fn(): void;
77
export interface Thing { n: typeof x }
8-
declare global export Foo;
8+
export as namespace Foo;
99

1010
//// [a.ts]
1111
/// <reference path="foo.d.ts" />

tests/baselines/reference/umd1.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ export interface Thing { n: typeof x }
2929
>n : Symbol(n, Decl(foo.d.ts, 3, 24))
3030
>x : Symbol(x, Decl(foo.d.ts, 1, 10))
3131

32-
declare global export Foo;
32+
export as namespace Foo;
3333

tests/baselines/reference/umd1.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ export interface Thing { n: typeof x }
3030
>n : number
3131
>x : number
3232

33-
declare global export Foo;
33+
export as namespace Foo;
3434
>Foo : any
3535

tests/baselines/reference/umd2.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ tests/cases/conformance/externalModules/a.ts(2,8): error TS2503: Cannot find nam
1515

1616
export var x: number;
1717
export function fn(): void;
18-
declare global export Foo;
18+
export as namespace Foo;
1919

tests/baselines/reference/umd2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
export var x: number;
66
export function fn(): void;
7-
declare global export Foo;
7+
export as namespace Foo;
88

99
//// [a.ts]
1010
Foo.fn();

tests/baselines/reference/umd3.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
export var x: number;
66
export function fn(): void;
77
export interface Thing { n: typeof x }
8-
declare global export Foo;
8+
export as namespace Foo;
99

1010
//// [a.ts]
1111
import * as Foo from './foo';

tests/baselines/reference/umd3.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ export interface Thing { n: typeof x }
3131
>n : Symbol(n, Decl(foo.d.ts, 3, 24))
3232
>x : Symbol(x, Decl(foo.d.ts, 1, 10))
3333

34-
declare global export Foo;
34+
export as namespace Foo;
3535

tests/baselines/reference/umd3.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ export interface Thing { n: typeof x }
3232
>n : number
3333
>x : number
3434

35-
declare global export Foo;
35+
export as namespace Foo;
3636
>Foo : any
3737

tests/baselines/reference/umd4.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
export var x: number;
66
export function fn(): void;
77
export interface Thing { n: typeof x }
8-
declare global export Foo;
8+
export as namespace Foo;
99

1010
//// [a.ts]
1111
import * as Bar from './foo';

tests/baselines/reference/umd4.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ export interface Thing { n: typeof x }
3131
>n : Symbol(n, Decl(foo.d.ts, 3, 24))
3232
>x : Symbol(x, Decl(foo.d.ts, 1, 10))
3333

34-
declare global export Foo;
34+
export as namespace Foo;
3535

tests/baselines/reference/umd4.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ export interface Thing { n: typeof x }
3232
>n : number
3333
>x : number
3434

35-
declare global export Foo;
35+
export as namespace Foo;
3636
>Foo : any
3737

tests/baselines/reference/umd5.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ tests/cases/conformance/externalModules/a.ts(6,9): error TS2304: Cannot find nam
1616
export var x: number;
1717
export function fn(): void;
1818
export interface Thing { n: typeof x }
19-
declare global export Foo;
19+
export as namespace Foo;
2020

tests/baselines/reference/umd5.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
export var x: number;
66
export function fn(): void;
77
export interface Thing { n: typeof x }
8-
declare global export Foo;
8+
export as namespace Foo;
99

1010
//// [a.ts]
1111
import * as Bar from './foo';

0 commit comments

Comments
 (0)