permit global augmentations to introduce new names

This commit is contained in:
Vladimir Matveev
2016-04-14 17:16:53 -07:00
parent 19a9f7f82d
commit 78177cfc2c
11 changed files with 221 additions and 61 deletions
+5 -8
View File
@@ -15596,6 +15596,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 +15607,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);
@@ -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";
@@ -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.
@@ -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.
@@ -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.
}
}
@@ -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))
}
}
@@ -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
}
}
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/newNamesInGlobalAugmentations1.ts] ////
//// [f1.d.ts]
export {};
declare global {
interface SymbolConstructor {
observable: symbol;
}
class Cls {x}
let [a, b]: number[];
}
//// [main.ts]
Symbol.observable;
new Cls().x
//// [main.js]
Symbol.observable;
new Cls().x;
@@ -0,0 +1,34 @@
=== tests/cases/compiler/f1.d.ts ===
export {};
declare global {
>global : Symbol(, Decl(f1.d.ts, 1, 10))
interface SymbolConstructor {
>SymbolConstructor : Symbol(SymbolConstructor, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(f1.d.ts, 3, 16))
observable: symbol;
>observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 4, 33))
}
class Cls {x}
>Cls : Symbol(Cls, Decl(f1.d.ts, 6, 5))
>x : Symbol(Cls.x, Decl(f1.d.ts, 7, 15))
let [a, b]: number[];
>a : Symbol(a, Decl(f1.d.ts, 8, 9))
>b : Symbol(b, Decl(f1.d.ts, 8, 11))
}
=== tests/cases/compiler/main.ts ===
Symbol.observable;
>Symbol.observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 4, 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))
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))
@@ -0,0 +1,35 @@
=== tests/cases/compiler/f1.d.ts ===
export {};
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
}
=== 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
@@ -0,0 +1,17 @@
// @target: es6
// @filename: f1.d.ts
export {};
declare global {
interface SymbolConstructor {
observable: symbol;
}
class Cls {x}
let [a, b]: number[];
}
// @filename: main.ts
Symbol.observable;
new Cls().x