Merge pull request #8104 from Microsoft/newEntitiesInGlobalAugmentations

permit global augmentations to introduce new names
This commit is contained in:
Vladimir Matveev 2016-04-15 15:09:22 -07:00
commit 79a3e77dd8
11 changed files with 275 additions and 62 deletions

View File

@ -15572,7 +15572,9 @@ namespace ts {
break;
case SyntaxKind.ImportEqualsDeclaration:
if ((<ImportEqualsDeclaration>node).moduleReference.kind !== SyntaxKind.StringLiteral) {
error((<ImportEqualsDeclaration>node).name, Diagnostics.Module_augmentation_cannot_introduce_new_names_in_the_top_level_scope);
if (!isGlobalAugmentation) {
error((<ImportEqualsDeclaration>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);

View File

@ -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";

View File

@ -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.

View File

@ -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.

View File

@ -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) ====
/// <reference path="f1.d.ts"/>
/// <reference path="f2.d.ts"/>
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.
}
}

View File

@ -0,0 +1,28 @@
=== tests/cases/compiler/f3.ts ===
/// <reference path="f1.d.ts"/>
No type information for this code./// <reference path="f2.d.ts"/>
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))
}
}

View File

@ -0,0 +1,28 @@
=== tests/cases/compiler/f3.ts ===
/// <reference path="f1.d.ts"/>
No type information for this code./// <reference path="f2.d.ts"/>
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
}
}

View File

@ -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;

View File

@ -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))

View File

@ -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

View File

@ -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;