Skip to content

Commit 78177cf

Browse files
committed
permit global augmentations to introduce new names
1 parent 19a9f7f commit 78177cf

11 files changed

+221
-61
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15596,6 +15596,9 @@ namespace ts {
1559615596
case SyntaxKind.InterfaceDeclaration:
1559715597
case SyntaxKind.ModuleDeclaration:
1559815598
case SyntaxKind.TypeAliasDeclaration:
15599+
if (isGlobalAugmentation) {
15600+
return;
15601+
}
1559915602
const symbol = getSymbolOfNode(node);
1560015603
if (symbol) {
1560115604
// module augmentations cannot introduce new names on the top level scope of the module
@@ -15604,14 +15607,8 @@ namespace ts {
1560415607
// 2. main check - report error if value declaration of the parent symbol is module augmentation)
1560515608
let reportError = !(symbol.flags & SymbolFlags.Merged);
1560615609
if (!reportError) {
15607-
if (isGlobalAugmentation) {
15608-
// global symbol should not have parent since it is not explicitly exported
15609-
reportError = symbol.parent !== undefined;
15610-
}
15611-
else {
15612-
// symbol should not originate in augmentation
15613-
reportError = isExternalModuleAugmentation(symbol.parent.declarations[0]);
15614-
}
15610+
// symbol should not originate in augmentation
15611+
reportError = isExternalModuleAugmentation(symbol.parent.declarations[0]);
1561515612
}
1561615613
if (reportError) {
1561715614
error(node, Diagnostics.Module_augmentation_cannot_introduce_new_names_in_the_top_level_scope);

tests/baselines/reference/moduleAugmentationGlobal4.errors.txt

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/compiler/f1.ts ===
2+
3+
declare global {
4+
>global : Symbol(, Decl(f1.ts, 0, 0))
5+
6+
interface Something {x}
7+
>Something : Symbol(Something, Decl(f1.ts, 1, 16), Decl(f2.ts, 1, 16))
8+
>x : Symbol(Something.x, Decl(f1.ts, 2, 25))
9+
}
10+
export {};
11+
=== tests/cases/compiler/f2.ts ===
12+
13+
declare global {
14+
>global : Symbol(, Decl(f2.ts, 0, 0))
15+
16+
interface Something {y}
17+
>Something : Symbol(Something, Decl(f1.ts, 1, 16), Decl(f2.ts, 1, 16))
18+
>y : Symbol(Something.y, Decl(f2.ts, 2, 25))
19+
}
20+
export {};
21+
=== tests/cases/compiler/f3.ts ===
22+
import "./f1";
23+
No type information for this code.import "./f2";
24+
No type information for this code.
25+
No type information for this code.
26+
No type information for this code.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/compiler/f1.ts ===
2+
3+
declare global {
4+
>global : any
5+
6+
interface Something {x}
7+
>Something : Something
8+
>x : any
9+
}
10+
export {};
11+
=== tests/cases/compiler/f2.ts ===
12+
13+
declare global {
14+
>global : any
15+
16+
interface Something {y}
17+
>Something : Something
18+
>y : any
19+
}
20+
export {};
21+
=== tests/cases/compiler/f3.ts ===
22+
import "./f1";
23+
No type information for this code.import "./f2";
24+
No type information for this code.
25+
No type information for this code.
26+
No type information for this code.

tests/baselines/reference/moduleAugmentationGlobal5.errors.txt

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/f3.ts ===
2+
/// <reference path="f1.d.ts"/>
3+
No type information for this code./// <reference path="f2.d.ts"/>
4+
No type information for this code.import "A";
5+
No type information for this code.import "B";
6+
No type information for this code.
7+
No type information for this code.
8+
No type information for this code.=== tests/cases/compiler/f1.d.ts ===
9+
10+
declare module "A" {
11+
global {
12+
>global : Symbol(, Decl(f1.d.ts, 1, 20))
13+
14+
interface Something {x}
15+
>Something : Symbol(Something, Decl(f1.d.ts, 2, 12), Decl(f2.d.ts, 1, 12))
16+
>x : Symbol(Something.x, Decl(f1.d.ts, 3, 29))
17+
}
18+
}
19+
=== tests/cases/compiler/f2.d.ts ===
20+
declare module "B" {
21+
global {
22+
>global : Symbol(, Decl(f2.d.ts, 0, 20))
23+
24+
interface Something {y}
25+
>Something : Symbol(Something, Decl(f1.d.ts, 2, 12), Decl(f2.d.ts, 1, 12))
26+
>y : Symbol(Something.y, Decl(f2.d.ts, 2, 29))
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/f3.ts ===
2+
/// <reference path="f1.d.ts"/>
3+
No type information for this code./// <reference path="f2.d.ts"/>
4+
No type information for this code.import "A";
5+
No type information for this code.import "B";
6+
No type information for this code.
7+
No type information for this code.
8+
No type information for this code.=== tests/cases/compiler/f1.d.ts ===
9+
10+
declare module "A" {
11+
global {
12+
>global : any
13+
14+
interface Something {x}
15+
>Something : Something
16+
>x : any
17+
}
18+
}
19+
=== tests/cases/compiler/f2.d.ts ===
20+
declare module "B" {
21+
global {
22+
>global : any
23+
24+
interface Something {y}
25+
>Something : Something
26+
>y : any
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/newNamesInGlobalAugmentations1.ts] ////
2+
3+
//// [f1.d.ts]
4+
5+
export {};
6+
7+
declare global {
8+
interface SymbolConstructor {
9+
observable: symbol;
10+
}
11+
class Cls {x}
12+
let [a, b]: number[];
13+
}
14+
15+
//// [main.ts]
16+
17+
Symbol.observable;
18+
new Cls().x
19+
20+
//// [main.js]
21+
Symbol.observable;
22+
new Cls().x;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
=== tests/cases/compiler/f1.d.ts ===
2+
3+
export {};
4+
5+
declare global {
6+
>global : Symbol(, Decl(f1.d.ts, 1, 10))
7+
8+
interface SymbolConstructor {
9+
>SymbolConstructor : Symbol(SymbolConstructor, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(f1.d.ts, 3, 16))
10+
11+
observable: symbol;
12+
>observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 4, 33))
13+
}
14+
class Cls {x}
15+
>Cls : Symbol(Cls, Decl(f1.d.ts, 6, 5))
16+
>x : Symbol(Cls.x, Decl(f1.d.ts, 7, 15))
17+
18+
let [a, b]: number[];
19+
>a : Symbol(a, Decl(f1.d.ts, 8, 9))
20+
>b : Symbol(b, Decl(f1.d.ts, 8, 11))
21+
}
22+
23+
=== tests/cases/compiler/main.ts ===
24+
25+
Symbol.observable;
26+
>Symbol.observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 4, 33))
27+
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
28+
>observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 4, 33))
29+
30+
new Cls().x
31+
>new Cls().x : Symbol(Cls.x, Decl(f1.d.ts, 7, 15))
32+
>Cls : Symbol(Cls, Decl(f1.d.ts, 6, 5))
33+
>x : Symbol(Cls.x, Decl(f1.d.ts, 7, 15))
34+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/f1.d.ts ===
2+
3+
export {};
4+
5+
declare global {
6+
>global : any
7+
8+
interface SymbolConstructor {
9+
>SymbolConstructor : SymbolConstructor
10+
11+
observable: symbol;
12+
>observable : symbol
13+
}
14+
class Cls {x}
15+
>Cls : Cls
16+
>x : any
17+
18+
let [a, b]: number[];
19+
>a : number
20+
>b : number
21+
}
22+
23+
=== tests/cases/compiler/main.ts ===
24+
25+
Symbol.observable;
26+
>Symbol.observable : symbol
27+
>Symbol : SymbolConstructor
28+
>observable : symbol
29+
30+
new Cls().x
31+
>new Cls().x : any
32+
>new Cls() : Cls
33+
>Cls : typeof Cls
34+
>x : any
35+

0 commit comments

Comments
 (0)