diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4e7d10e264d..5e001347e06 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 @@ -15596,6 +15598,9 @@ namespace ts { case SyntaxKind.InterfaceDeclaration: case SyntaxKind.ModuleDeclaration: case SyntaxKind.TypeAliasDeclaration: + if (isGlobalAugmentation) { + return; + } const symbol = getSymbolOfNode(node); if (symbol) { // module augmentations cannot introduce new names on the top level scope of the module @@ -15604,14 +15609,8 @@ namespace ts { // 2. main check - report error if value declaration of the parent symbol is module augmentation) let reportError = !(symbol.flags & SymbolFlags.Merged); if (!reportError) { - if (isGlobalAugmentation) { - // global symbol should not have parent since it is not explicitly exported - reportError = symbol.parent !== undefined; - } - else { - // symbol should not originate in augmentation - reportError = isExternalModuleAugmentation(symbol.parent.declarations[0]); - } + // symbol should not originate in augmentation + reportError = isExternalModuleAugmentation(symbol.parent.declarations[0]); } if (reportError) { error(node, Diagnostics.Module_augmentation_cannot_introduce_new_names_in_the_top_level_scope); diff --git a/tests/baselines/reference/moduleAugmentationGlobal4.errors.txt b/tests/baselines/reference/moduleAugmentationGlobal4.errors.txt deleted file mode 100644 index fd3444f56b1..00000000000 --- a/tests/baselines/reference/moduleAugmentationGlobal4.errors.txt +++ /dev/null @@ -1,25 +0,0 @@ -tests/cases/compiler/f1.ts(3,15): error TS2665: Module augmentation cannot introduce new names in the top level scope. -tests/cases/compiler/f2.ts(3,15): error TS2665: Module augmentation cannot introduce new names in the top level scope. - - -==== tests/cases/compiler/f1.ts (1 errors) ==== - - declare global { - interface Something {x} - ~~~~~~~~~ -!!! error TS2665: Module augmentation cannot introduce new names in the top level scope. - } - export {}; -==== tests/cases/compiler/f2.ts (1 errors) ==== - - declare global { - interface Something {y} - ~~~~~~~~~ -!!! error TS2665: Module augmentation cannot introduce new names in the top level scope. - } - export {}; -==== tests/cases/compiler/f3.ts (0 errors) ==== - import "./f1"; - import "./f2"; - - \ No newline at end of file diff --git a/tests/baselines/reference/moduleAugmentationGlobal4.symbols b/tests/baselines/reference/moduleAugmentationGlobal4.symbols new file mode 100644 index 00000000000..f9cdac3fc2e --- /dev/null +++ b/tests/baselines/reference/moduleAugmentationGlobal4.symbols @@ -0,0 +1,26 @@ +=== tests/cases/compiler/f1.ts === + +declare global { +>global : Symbol(, Decl(f1.ts, 0, 0)) + + interface Something {x} +>Something : Symbol(Something, Decl(f1.ts, 1, 16), Decl(f2.ts, 1, 16)) +>x : Symbol(Something.x, Decl(f1.ts, 2, 25)) +} +export {}; +=== tests/cases/compiler/f2.ts === + +declare global { +>global : Symbol(, Decl(f2.ts, 0, 0)) + + interface Something {y} +>Something : Symbol(Something, Decl(f1.ts, 1, 16), Decl(f2.ts, 1, 16)) +>y : Symbol(Something.y, Decl(f2.ts, 2, 25)) +} +export {}; +=== tests/cases/compiler/f3.ts === +import "./f1"; +No type information for this code.import "./f2"; +No type information for this code. +No type information for this code. +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/moduleAugmentationGlobal4.types b/tests/baselines/reference/moduleAugmentationGlobal4.types new file mode 100644 index 00000000000..e3e780d69ff --- /dev/null +++ b/tests/baselines/reference/moduleAugmentationGlobal4.types @@ -0,0 +1,26 @@ +=== tests/cases/compiler/f1.ts === + +declare global { +>global : any + + interface Something {x} +>Something : Something +>x : any +} +export {}; +=== tests/cases/compiler/f2.ts === + +declare global { +>global : any + + interface Something {y} +>Something : Something +>y : any +} +export {}; +=== tests/cases/compiler/f3.ts === +import "./f1"; +No type information for this code.import "./f2"; +No type information for this code. +No type information for this code. +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/moduleAugmentationGlobal5.errors.txt b/tests/baselines/reference/moduleAugmentationGlobal5.errors.txt deleted file mode 100644 index 4f1f9133760..00000000000 --- a/tests/baselines/reference/moduleAugmentationGlobal5.errors.txt +++ /dev/null @@ -1,28 +0,0 @@ -tests/cases/compiler/f1.d.ts(4,19): error TS2665: Module augmentation cannot introduce new names in the top level scope. -tests/cases/compiler/f2.d.ts(3,19): error TS2665: Module augmentation cannot introduce new names in the top level scope. - - -==== tests/cases/compiler/f3.ts (0 errors) ==== - /// - /// - import "A"; - import "B"; - - -==== tests/cases/compiler/f1.d.ts (1 errors) ==== - - declare module "A" { - global { - interface Something {x} - ~~~~~~~~~ -!!! error TS2665: Module augmentation cannot introduce new names in the top level scope. - } - } -==== tests/cases/compiler/f2.d.ts (1 errors) ==== - declare module "B" { - global { - interface Something {y} - ~~~~~~~~~ -!!! error TS2665: Module augmentation cannot introduce new names in the top level scope. - } - } \ No newline at end of file diff --git a/tests/baselines/reference/moduleAugmentationGlobal5.symbols b/tests/baselines/reference/moduleAugmentationGlobal5.symbols new file mode 100644 index 00000000000..27548b05ef2 --- /dev/null +++ b/tests/baselines/reference/moduleAugmentationGlobal5.symbols @@ -0,0 +1,28 @@ +=== tests/cases/compiler/f3.ts === +/// +No type information for this code./// +No type information for this code.import "A"; +No type information for this code.import "B"; +No type information for this code. +No type information for this code. +No type information for this code.=== tests/cases/compiler/f1.d.ts === + +declare module "A" { + global { +>global : Symbol(, Decl(f1.d.ts, 1, 20)) + + interface Something {x} +>Something : Symbol(Something, Decl(f1.d.ts, 2, 12), Decl(f2.d.ts, 1, 12)) +>x : Symbol(Something.x, Decl(f1.d.ts, 3, 29)) + } +} +=== tests/cases/compiler/f2.d.ts === +declare module "B" { + global { +>global : Symbol(, Decl(f2.d.ts, 0, 20)) + + interface Something {y} +>Something : Symbol(Something, Decl(f1.d.ts, 2, 12), Decl(f2.d.ts, 1, 12)) +>y : Symbol(Something.y, Decl(f2.d.ts, 2, 29)) + } +} diff --git a/tests/baselines/reference/moduleAugmentationGlobal5.types b/tests/baselines/reference/moduleAugmentationGlobal5.types new file mode 100644 index 00000000000..b2e6139eb26 --- /dev/null +++ b/tests/baselines/reference/moduleAugmentationGlobal5.types @@ -0,0 +1,28 @@ +=== tests/cases/compiler/f3.ts === +/// +No type information for this code./// +No type information for this code.import "A"; +No type information for this code.import "B"; +No type information for this code. +No type information for this code. +No type information for this code.=== tests/cases/compiler/f1.d.ts === + +declare module "A" { + global { +>global : any + + interface Something {x} +>Something : Something +>x : any + } +} +=== tests/cases/compiler/f2.d.ts === +declare module "B" { + global { +>global : any + + interface Something {y} +>Something : Something +>y : any + } +} diff --git a/tests/baselines/reference/newNamesInGlobalAugmentations1.js b/tests/baselines/reference/newNamesInGlobalAugmentations1.js new file mode 100644 index 00000000000..0400f4c3751 --- /dev/null +++ b/tests/baselines/reference/newNamesInGlobalAugmentations1.js @@ -0,0 +1,28 @@ +//// [tests/cases/compiler/newNamesInGlobalAugmentations1.ts] //// + +//// [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[]; + export import X = M.M1.x; +} + +//// [main.ts] + +Symbol.observable; +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 new file mode 100644 index 00000000000..6215132dd00 --- /dev/null +++ b/tests/baselines/reference/newNamesInGlobalAugmentations1.symbols @@ -0,0 +1,53 @@ +=== tests/cases/compiler/f1.d.ts === + +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, 5, 1)) + + interface SymbolConstructor { +>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, 7, 33)) + } + class Cls {x} +>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, 11, 9)) +>b : Symbol(b, Decl(f1.d.ts, 11, 11)) + + export 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, 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, 7, 33)) + +new Cls().x +>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 new file mode 100644 index 00000000000..a9b879fb4c5 --- /dev/null +++ b/tests/baselines/reference/newNamesInGlobalAugmentations1.types @@ -0,0 +1,56 @@ +=== tests/cases/compiler/f1.d.ts === + +export {}; + +declare module M.M1 { +>M : typeof M +>M1 : typeof M1 + + export let x: number; +>x : number +} +declare global { +>global : any + + interface SymbolConstructor { +>SymbolConstructor : SymbolConstructor + + observable: symbol; +>observable : symbol + } + class Cls {x} +>Cls : Cls +>x : any + + let [a, b]: number[]; +>a : number +>b : number + + export import X = M.M1.x; +>X : number +>M : typeof M +>M1 : typeof M.M1 +>x : number +} + +=== tests/cases/compiler/main.ts === + +Symbol.observable; +>Symbol.observable : symbol +>Symbol : SymbolConstructor +>observable : symbol + +new Cls().x +>new Cls().x : any +>new Cls() : Cls +>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 new file mode 100644 index 00000000000..73ed4e5e2e8 --- /dev/null +++ b/tests/cases/compiler/newNamesInGlobalAugmentations1.ts @@ -0,0 +1,22 @@ +// @target: es6 + +// @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[]; + export import X = M.M1.x; +} + +// @filename: main.ts + +Symbol.observable; +new Cls().x +let c = a + b + X; \ No newline at end of file