Skip to content

Commit 27292e4

Browse files
committed
Merge pull request #8678 from RyanCavanaugh/fix8255
Tweak UMD / global semantics
2 parents 92d465d + 91b8f20 commit 27292e4

11 files changed

+36
-29
lines changed

src/compiler/binder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,8 +1764,8 @@ namespace ts {
17641764
case SyntaxKind.ImportSpecifier:
17651765
case SyntaxKind.ExportSpecifier:
17661766
return declareSymbolAndAddToSymbolTable(<Declaration>node, SymbolFlags.Alias, SymbolFlags.AliasExcludes);
1767-
case SyntaxKind.GlobalModuleExportDeclaration:
1768-
return bindGlobalModuleExportDeclaration(<GlobalModuleExportDeclaration>node);
1767+
case SyntaxKind.NamespaceExportDeclaration:
1768+
return bindNamespaceExportDeclaration(<NamespaceExportDeclaration>node);
17691769
case SyntaxKind.ImportClause:
17701770
return bindImportClause(<ImportClause>node);
17711771
case SyntaxKind.ExportDeclaration:
@@ -1815,7 +1815,7 @@ namespace ts {
18151815
}
18161816
}
18171817

1818-
function bindGlobalModuleExportDeclaration(node: GlobalModuleExportDeclaration) {
1818+
function bindNamespaceExportDeclaration(node: NamespaceExportDeclaration) {
18191819
if (node.modifiers && node.modifiers.length) {
18201820
file.bindDiagnostics.push(createDiagnosticForNode(node, Diagnostics.Modifiers_cannot_appear_here));
18211821
}

src/compiler/checker.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ namespace ts {
639639
let propertyWithInvalidInitializer: Node;
640640
const errorLocation = location;
641641
let grandparent: Node;
642+
let isInExternalModule = false;
642643

643644
loop: while (location) {
644645
// Locals of a source file are not in scope (because they get merged into the global symbol table)
@@ -686,6 +687,7 @@ namespace ts {
686687
switch (location.kind) {
687688
case SyntaxKind.SourceFile:
688689
if (!isExternalOrCommonJsModule(<SourceFile>location)) break;
690+
isInExternalModule = true;
689691
case SyntaxKind.ModuleDeclaration:
690692
const moduleExports = getSymbolOfNode(location).exports;
691693
if (location.kind === SyntaxKind.SourceFile || isAmbientModule(location)) {
@@ -879,6 +881,14 @@ namespace ts {
879881
checkResolvedBlockScopedVariable(exportOrLocalSymbol, errorLocation);
880882
}
881883
}
884+
885+
// If we're in an external module, we can't reference symbols created from UMD export declarations
886+
if (result && isInExternalModule) {
887+
const decls = result.declarations;
888+
if (decls && decls.length === 1 && decls[0].kind === SyntaxKind.NamespaceExportDeclaration) {
889+
error(errorLocation, Diagnostics.Identifier_0_must_be_imported_from_a_module, name);
890+
}
891+
}
882892
}
883893
return result;
884894
}
@@ -1076,7 +1086,7 @@ namespace ts {
10761086
return getExternalModuleMember(<ImportDeclaration>node.parent.parent.parent, node);
10771087
}
10781088

1079-
function getTargetOfGlobalModuleExportDeclaration(node: GlobalModuleExportDeclaration): Symbol {
1089+
function getTargetOfGlobalModuleExportDeclaration(node: NamespaceExportDeclaration): Symbol {
10801090
return resolveExternalModuleSymbol(node.parent.symbol);
10811091
}
10821092

@@ -1104,8 +1114,8 @@ namespace ts {
11041114
return getTargetOfExportSpecifier(<ExportSpecifier>node);
11051115
case SyntaxKind.ExportAssignment:
11061116
return getTargetOfExportAssignment(<ExportAssignment>node);
1107-
case SyntaxKind.GlobalModuleExportDeclaration:
1108-
return getTargetOfGlobalModuleExportDeclaration(<GlobalModuleExportDeclaration>node);
1117+
case SyntaxKind.NamespaceExportDeclaration:
1118+
return getTargetOfGlobalModuleExportDeclaration(<NamespaceExportDeclaration>node);
11091119
}
11101120
}
11111121

@@ -6374,7 +6384,7 @@ namespace ts {
63746384
if (kind === SignatureKind.Construct && sourceSignatures.length && targetSignatures.length) {
63756385
if (isAbstractConstructorType(source) && !isAbstractConstructorType(target)) {
63766386
// An abstract constructor type is not assignable to a non-abstract constructor type
6377-
// as it would otherwise be possible to new an abstract class. Note that the assignability
6387+
// as it would otherwise be possible to new an abstract class. Note that the assignability
63786388
// check we perform for an extends clause excludes construct signatures from the target,
63796389
// so this check never proceeds.
63806390
if (reportErrors) {
@@ -12878,7 +12888,7 @@ namespace ts {
1287812888
if (checkIfTypePredicateVariableIsDeclaredInBindingPattern(
1287912889
<BindingPattern>name,
1288012890
predicateVariableNode,
12881-
predicateVariableName)) {
12891+
predicateVariableName)) {
1288212892
return true;
1288312893
}
1288412894
}
@@ -17497,7 +17507,7 @@ namespace ts {
1749717507
if (file.moduleAugmentations.length) {
1749817508
(augmentations || (augmentations = [])).push(file.moduleAugmentations);
1749917509
}
17500-
if (file.wasReferenced && file.symbol && file.symbol.globalExports) {
17510+
if (file.symbol && file.symbol.globalExports) {
1750117511
mergeSymbolTable(globals, file.symbol.globalExports);
1750217512
}
1750317513
});

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,10 @@
19231923
"category": "Error",
19241924
"code": 2685
19251925
},
1926+
"Identifier '{0}' must be imported from a module": {
1927+
"category": "Error",
1928+
"code": 2686
1929+
},
19261930
"Import declaration '{0}' is using private name '{1}'.": {
19271931
"category": "Error",
19281932
"code": 4000

src/compiler/parser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ namespace ts {
303303
case SyntaxKind.ImportClause:
304304
return visitNode(cbNode, (<ImportClause>node).name) ||
305305
visitNode(cbNode, (<ImportClause>node).namedBindings);
306-
case SyntaxKind.GlobalModuleExportDeclaration:
307-
return visitNode(cbNode, (<GlobalModuleExportDeclaration>node).name);
306+
case SyntaxKind.NamespaceExportDeclaration:
307+
return visitNode(cbNode, (<NamespaceExportDeclaration>node).name);
308308

309309
case SyntaxKind.NamespaceImport:
310310
return visitNode(cbNode, (<NamespaceImport>node).name);
@@ -5344,8 +5344,8 @@ namespace ts {
53445344
return nextToken() === SyntaxKind.SlashToken;
53455345
}
53465346

5347-
function parseGlobalModuleExportDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): GlobalModuleExportDeclaration {
5348-
const exportDeclaration = <GlobalModuleExportDeclaration>createNode(SyntaxKind.GlobalModuleExportDeclaration, fullStart);
5347+
function parseGlobalModuleExportDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): NamespaceExportDeclaration {
5348+
const exportDeclaration = <NamespaceExportDeclaration>createNode(SyntaxKind.NamespaceExportDeclaration, fullStart);
53495349
exportDeclaration.decorators = decorators;
53505350
exportDeclaration.modifiers = modifiers;
53515351
parseExpected(SyntaxKind.AsKeyword);

src/compiler/program.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,10 +1762,6 @@ namespace ts {
17621762
reportFileNamesDifferOnlyInCasingError(fileName, file.fileName, refFile, refPos, refEnd);
17631763
}
17641764

1765-
if (file) {
1766-
file.wasReferenced = file.wasReferenced || isReference;
1767-
}
1768-
17691765
return file;
17701766
}
17711767

@@ -1782,7 +1778,6 @@ namespace ts {
17821778

17831779
filesByName.set(path, file);
17841780
if (file) {
1785-
file.wasReferenced = file.wasReferenced || isReference;
17861781
file.path = path;
17871782

17881783
if (host.useCaseSensitiveFileNames()) {

src/compiler/types.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ namespace ts {
277277
ModuleDeclaration,
278278
ModuleBlock,
279279
CaseBlock,
280-
GlobalModuleExportDeclaration,
280+
NamespaceExportDeclaration,
281281
ImportEqualsDeclaration,
282282
ImportDeclaration,
283283
ImportClause,
@@ -1341,8 +1341,8 @@ namespace ts {
13411341
name: Identifier;
13421342
}
13431343

1344-
// @kind(SyntaxKind.GlobalModuleImport)
1345-
export interface GlobalModuleExportDeclaration extends DeclarationStatement {
1344+
// @kind(SyntaxKind.NamespaceExportDeclaration)
1345+
export interface NamespaceExportDeclaration extends DeclarationStatement {
13461346
name: Identifier;
13471347
moduleReference: LiteralLikeNode;
13481348
}
@@ -1599,8 +1599,6 @@ namespace ts {
15991599
/* @internal */ externalModuleIndicator: Node;
16001600
// The first node that causes this file to be a CommonJS module
16011601
/* @internal */ commonJsModuleIndicator: Node;
1602-
// True if the file was a root file in a compilation or a /// reference targets
1603-
/* @internal */ wasReferenced?: boolean;
16041602

16051603
/* @internal */ identifiers: Map<string>;
16061604
/* @internal */ nodeCount: number;

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ namespace ts {
16431643
// export default ...
16441644
export function isAliasSymbolDeclaration(node: Node): boolean {
16451645
return node.kind === SyntaxKind.ImportEqualsDeclaration ||
1646-
node.kind === SyntaxKind.GlobalModuleExportDeclaration ||
1646+
node.kind === SyntaxKind.NamespaceExportDeclaration ||
16471647
node.kind === SyntaxKind.ImportClause && !!(<ImportClause>node).name ||
16481648
node.kind === SyntaxKind.NamespaceImport ||
16491649
node.kind === SyntaxKind.ImportSpecifier ||

tests/baselines/reference/umd-augmentation-3.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export = M2D;
4444
>M2D : Symbol(M2D, Decl(index.d.ts, 3, 13))
4545

4646
declare namespace M2D {
47-
>M2D : Symbol(, Decl(index.d.ts, 3, 13), Decl(math2d-augment.d.ts, 0, 33))
47+
>M2D : Symbol(Math2d, Decl(index.d.ts, 3, 13), Decl(math2d-augment.d.ts, 0, 33))
4848

4949
interface Point {
5050
>Point : Symbol(Point, Decl(index.d.ts, 5, 23))

tests/baselines/reference/umd-augmentation-3.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export = M2D;
5454
>M2D : typeof M2D
5555

5656
declare namespace M2D {
57-
>M2D : typeof
57+
>M2D : typeof Math2d
5858

5959
interface Point {
6060
>Point : Point

tests/baselines/reference/umd5.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/externalModules/a.ts(6,9): error TS2304: Cannot find name 'Foo'.
1+
tests/cases/conformance/externalModules/a.ts(6,9): error TS2686: Identifier 'Foo' must be imported from a module
22

33

44
==== tests/cases/conformance/externalModules/a.ts (1 errors) ====
@@ -9,7 +9,7 @@ tests/cases/conformance/externalModules/a.ts(6,9): error TS2304: Cannot find nam
99
// should error
1010
let z = Foo;
1111
~~~
12-
!!! error TS2304: Cannot find name 'Foo'.
12+
!!! error TS2686: Identifier 'Foo' must be imported from a module
1313

1414
==== tests/cases/conformance/externalModules/foo.d.ts (0 errors) ====
1515

tests/baselines/reference/umd5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ Bar.fn();
2323
var x;
2424
var y = x.n;
2525
// should error
26-
var z = Foo;
26+
var z = exports.Foo;

0 commit comments

Comments
 (0)