From 0f323ea74a99e4a07525ff55ce21c76163769ccf Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Thu, 14 Apr 2016 17:57:17 -0700 Subject: [PATCH] allow top level 'import x = identifier | qname' in module augmentations --- src/compiler/binder.ts | 2 +- src/compiler/checker.ts | 4 +- .../newNamesInGlobalAugmentations1.js | 8 +++- .../newNamesInGlobalAugmentations1.symbols | 43 +++++++++++++------ .../newNamesInGlobalAugmentations1.types | 21 +++++++++ .../newNamesInGlobalAugmentations1.ts | 7 ++- 6 files changed, 69 insertions(+), 16 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 9311aa7cc26..0d71abcf123 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -355,7 +355,7 @@ namespace ts { function declareModuleMember(node: Declaration, symbolFlags: SymbolFlags, symbolExcludes: SymbolFlags): Symbol { const hasExportModifier = getCombinedNodeFlags(node) & NodeFlags.Export; if (symbolFlags & SymbolFlags.Alias) { - if (node.kind === SyntaxKind.ExportSpecifier || (node.kind === SyntaxKind.ImportEqualsDeclaration && hasExportModifier)) { + if (node.kind === SyntaxKind.ExportSpecifier || (node.kind === SyntaxKind.ImportEqualsDeclaration && (hasExportModifier || container.flags & NodeFlags.ExportContext))) { return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); } else { diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 53c9b066caf..3690c24e19b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15572,7 +15572,9 @@ namespace ts { break; case SyntaxKind.ImportEqualsDeclaration: if ((node).moduleReference.kind !== SyntaxKind.StringLiteral) { - error((node).name, Diagnostics.Module_augmentation_cannot_introduce_new_names_in_the_top_level_scope); + if (!isGlobalAugmentation) { + error((node).name, Diagnostics.Module_augmentation_cannot_introduce_new_names_in_the_top_level_scope); + } break; } // fallthrough diff --git a/tests/baselines/reference/newNamesInGlobalAugmentations1.js b/tests/baselines/reference/newNamesInGlobalAugmentations1.js index 5dc04f2f4aa..053fe46fd42 100644 --- a/tests/baselines/reference/newNamesInGlobalAugmentations1.js +++ b/tests/baselines/reference/newNamesInGlobalAugmentations1.js @@ -4,19 +4,25 @@ export {}; +declare module M.M1 { + export let x: number; +} declare global { interface SymbolConstructor { observable: symbol; } class Cls {x} let [a, b]: number[]; + import X = M.M1.x; } //// [main.ts] Symbol.observable; -new Cls().x +new Cls().x +let c = a + b + X; //// [main.js] Symbol.observable; new Cls().x; +let c = a + b + X; diff --git a/tests/baselines/reference/newNamesInGlobalAugmentations1.symbols b/tests/baselines/reference/newNamesInGlobalAugmentations1.symbols index be8ec6aa14d..eab27b632eb 100644 --- a/tests/baselines/reference/newNamesInGlobalAugmentations1.symbols +++ b/tests/baselines/reference/newNamesInGlobalAugmentations1.symbols @@ -2,33 +2,52 @@ export {}; +declare module M.M1 { +>M : Symbol(M, Decl(f1.d.ts, 1, 10)) +>M1 : Symbol(M1, Decl(f1.d.ts, 3, 17)) + + export let x: number; +>x : Symbol(x, Decl(f1.d.ts, 4, 14)) +} declare global { ->global : Symbol(, Decl(f1.d.ts, 1, 10)) +>global : Symbol(, Decl(f1.d.ts, 5, 1)) interface SymbolConstructor { ->SymbolConstructor : Symbol(SymbolConstructor, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(f1.d.ts, 3, 16)) +>SymbolConstructor : Symbol(SymbolConstructor, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(f1.d.ts, 6, 16)) observable: symbol; ->observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 4, 33)) +>observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 7, 33)) } class Cls {x} ->Cls : Symbol(Cls, Decl(f1.d.ts, 6, 5)) ->x : Symbol(Cls.x, Decl(f1.d.ts, 7, 15)) +>Cls : Symbol(Cls, Decl(f1.d.ts, 9, 5)) +>x : Symbol(Cls.x, Decl(f1.d.ts, 10, 15)) let [a, b]: number[]; ->a : Symbol(a, Decl(f1.d.ts, 8, 9)) ->b : Symbol(b, Decl(f1.d.ts, 8, 11)) +>a : Symbol(a, Decl(f1.d.ts, 11, 9)) +>b : Symbol(b, Decl(f1.d.ts, 11, 11)) + + import X = M.M1.x; +>X : Symbol(X, Decl(f1.d.ts, 11, 25)) +>M : Symbol(M, Decl(f1.d.ts, 1, 10)) +>M1 : Symbol(M.M1, Decl(f1.d.ts, 3, 17)) +>x : Symbol(X, Decl(f1.d.ts, 4, 14)) } === tests/cases/compiler/main.ts === Symbol.observable; ->Symbol.observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 4, 33)) +>Symbol.observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 7, 33)) >Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --)) ->observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 4, 33)) +>observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 7, 33)) new Cls().x ->new Cls().x : Symbol(Cls.x, Decl(f1.d.ts, 7, 15)) ->Cls : Symbol(Cls, Decl(f1.d.ts, 6, 5)) ->x : Symbol(Cls.x, Decl(f1.d.ts, 7, 15)) +>new Cls().x : Symbol(Cls.x, Decl(f1.d.ts, 10, 15)) +>Cls : Symbol(Cls, Decl(f1.d.ts, 9, 5)) +>x : Symbol(Cls.x, Decl(f1.d.ts, 10, 15)) + +let c = a + b + X; +>c : Symbol(c, Decl(main.ts, 3, 3)) +>a : Symbol(a, Decl(f1.d.ts, 11, 9)) +>b : Symbol(b, Decl(f1.d.ts, 11, 11)) +>X : Symbol(X, Decl(f1.d.ts, 11, 25)) diff --git a/tests/baselines/reference/newNamesInGlobalAugmentations1.types b/tests/baselines/reference/newNamesInGlobalAugmentations1.types index 8d5baf19d08..e1331709d2f 100644 --- a/tests/baselines/reference/newNamesInGlobalAugmentations1.types +++ b/tests/baselines/reference/newNamesInGlobalAugmentations1.types @@ -2,6 +2,13 @@ export {}; +declare module M.M1 { +>M : typeof M +>M1 : typeof M1 + + export let x: number; +>x : number +} declare global { >global : any @@ -18,6 +25,12 @@ declare global { let [a, b]: number[]; >a : number >b : number + + import X = M.M1.x; +>X : number +>M : typeof M +>M1 : typeof M.M1 +>x : number } === tests/cases/compiler/main.ts === @@ -33,3 +46,11 @@ new Cls().x >Cls : typeof Cls >x : any +let c = a + b + X; +>c : number +>a + b + X : number +>a + b : number +>a : number +>b : number +>X : number + diff --git a/tests/cases/compiler/newNamesInGlobalAugmentations1.ts b/tests/cases/compiler/newNamesInGlobalAugmentations1.ts index 78cd1ec5c13..22102353250 100644 --- a/tests/cases/compiler/newNamesInGlobalAugmentations1.ts +++ b/tests/cases/compiler/newNamesInGlobalAugmentations1.ts @@ -3,15 +3,20 @@ // @filename: f1.d.ts export {}; +declare module M.M1 { + export let x: number; +} declare global { interface SymbolConstructor { observable: symbol; } class Cls {x} let [a, b]: number[]; + import X = M.M1.x; } // @filename: main.ts Symbol.observable; -new Cls().x \ No newline at end of file +new Cls().x +let c = a + b + X; \ No newline at end of file