Skip to content

Commit 79a3e77

Browse files
committed
Merge pull request #8104 from Microsoft/newEntitiesInGlobalAugmentations
permit global augmentations to introduce new names
2 parents 0cfe24e + 6f3f690 commit 79a3e77

11 files changed

+275
-62
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15572,7 +15572,9 @@ namespace ts {
1557215572
break;
1557315573
case SyntaxKind.ImportEqualsDeclaration:
1557415574
if ((<ImportEqualsDeclaration>node).moduleReference.kind !== SyntaxKind.StringLiteral) {
15575-
error((<ImportEqualsDeclaration>node).name, Diagnostics.Module_augmentation_cannot_introduce_new_names_in_the_top_level_scope);
15575+
if (!isGlobalAugmentation) {
15576+
error((<ImportEqualsDeclaration>node).name, Diagnostics.Module_augmentation_cannot_introduce_new_names_in_the_top_level_scope);
15577+
}
1557615578
break;
1557715579
}
1557815580
// fallthrough
@@ -15596,6 +15598,9 @@ namespace ts {
1559615598
case SyntaxKind.InterfaceDeclaration:
1559715599
case SyntaxKind.ModuleDeclaration:
1559815600
case SyntaxKind.TypeAliasDeclaration:
15601+
if (isGlobalAugmentation) {
15602+
return;
15603+
}
1559915604
const symbol = getSymbolOfNode(node);
1560015605
if (symbol) {
1560115606
// module augmentations cannot introduce new names on the top level scope of the module
@@ -15604,14 +15609,8 @@ namespace ts {
1560415609
// 2. main check - report error if value declaration of the parent symbol is module augmentation)
1560515610
let reportError = !(symbol.flags & SymbolFlags.Merged);
1560615611
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-
}
15612+
// symbol should not originate in augmentation
15613+
reportError = isExternalModuleAugmentation(symbol.parent.declarations[0]);
1561515614
}
1561615615
if (reportError) {
1561715616
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [tests/cases/compiler/newNamesInGlobalAugmentations1.ts] ////
2+
3+
//// [f1.d.ts]
4+
5+
export {};
6+
7+
declare module M.M1 {
8+
export let x: number;
9+
}
10+
declare global {
11+
interface SymbolConstructor {
12+
observable: symbol;
13+
}
14+
class Cls {x}
15+
let [a, b]: number[];
16+
export import X = M.M1.x;
17+
}
18+
19+
//// [main.ts]
20+
21+
Symbol.observable;
22+
new Cls().x
23+
let c = a + b + X;
24+
25+
//// [main.js]
26+
Symbol.observable;
27+
new Cls().x;
28+
let c = a + b + X;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
=== tests/cases/compiler/f1.d.ts ===
2+
3+
export {};
4+
5+
declare module M.M1 {
6+
>M : Symbol(M, Decl(f1.d.ts, 1, 10))
7+
>M1 : Symbol(M1, Decl(f1.d.ts, 3, 17))
8+
9+
export let x: number;
10+
>x : Symbol(x, Decl(f1.d.ts, 4, 14))
11+
}
12+
declare global {
13+
>global : Symbol(, Decl(f1.d.ts, 5, 1))
14+
15+
interface SymbolConstructor {
16+
>SymbolConstructor : Symbol(SymbolConstructor, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(f1.d.ts, 6, 16))
17+
18+
observable: symbol;
19+
>observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 7, 33))
20+
}
21+
class Cls {x}
22+
>Cls : Symbol(Cls, Decl(f1.d.ts, 9, 5))
23+
>x : Symbol(Cls.x, Decl(f1.d.ts, 10, 15))
24+
25+
let [a, b]: number[];
26+
>a : Symbol(a, Decl(f1.d.ts, 11, 9))
27+
>b : Symbol(b, Decl(f1.d.ts, 11, 11))
28+
29+
export import X = M.M1.x;
30+
>X : Symbol(X, Decl(f1.d.ts, 11, 25))
31+
>M : Symbol(M, Decl(f1.d.ts, 1, 10))
32+
>M1 : Symbol(M.M1, Decl(f1.d.ts, 3, 17))
33+
>x : Symbol(X, Decl(f1.d.ts, 4, 14))
34+
}
35+
36+
=== tests/cases/compiler/main.ts ===
37+
38+
Symbol.observable;
39+
>Symbol.observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 7, 33))
40+
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
41+
>observable : Symbol(SymbolConstructor.observable, Decl(f1.d.ts, 7, 33))
42+
43+
new Cls().x
44+
>new Cls().x : Symbol(Cls.x, Decl(f1.d.ts, 10, 15))
45+
>Cls : Symbol(Cls, Decl(f1.d.ts, 9, 5))
46+
>x : Symbol(Cls.x, Decl(f1.d.ts, 10, 15))
47+
48+
let c = a + b + X;
49+
>c : Symbol(c, Decl(main.ts, 3, 3))
50+
>a : Symbol(a, Decl(f1.d.ts, 11, 9))
51+
>b : Symbol(b, Decl(f1.d.ts, 11, 11))
52+
>X : Symbol(X, Decl(f1.d.ts, 11, 25))
53+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
=== tests/cases/compiler/f1.d.ts ===
2+
3+
export {};
4+
5+
declare module M.M1 {
6+
>M : typeof M
7+
>M1 : typeof M1
8+
9+
export let x: number;
10+
>x : number
11+
}
12+
declare global {
13+
>global : any
14+
15+
interface SymbolConstructor {
16+
>SymbolConstructor : SymbolConstructor
17+
18+
observable: symbol;
19+
>observable : symbol
20+
}
21+
class Cls {x}
22+
>Cls : Cls
23+
>x : any
24+
25+
let [a, b]: number[];
26+
>a : number
27+
>b : number
28+
29+
export import X = M.M1.x;
30+
>X : number
31+
>M : typeof M
32+
>M1 : typeof M.M1
33+
>x : number
34+
}
35+
36+
=== tests/cases/compiler/main.ts ===
37+
38+
Symbol.observable;
39+
>Symbol.observable : symbol
40+
>Symbol : SymbolConstructor
41+
>observable : symbol
42+
43+
new Cls().x
44+
>new Cls().x : any
45+
>new Cls() : Cls
46+
>Cls : typeof Cls
47+
>x : any
48+
49+
let c = a + b + X;
50+
>c : number
51+
>a + b + X : number
52+
>a + b : number
53+
>a : number
54+
>b : number
55+
>X : number
56+

0 commit comments

Comments
 (0)