Skip to content

Commit 663a42b

Browse files
authored
Allow references to globalThis to be made in declaration emit (microsoft#35279)
1 parent cfa367d commit 663a42b

28 files changed

+205
-46
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3312,8 +3312,8 @@ namespace ts {
33123312
return [symbol!];
33133313
}
33143314

3315-
// Check if symbol is any of the alias
3316-
return forEachEntry(symbols, symbolFromSymbolTable => {
3315+
// Check if symbol is any of the aliases in scope
3316+
const result = forEachEntry(symbols, symbolFromSymbolTable => {
33173317
if (symbolFromSymbolTable.flags & SymbolFlags.Alias
33183318
&& symbolFromSymbolTable.escapedName !== InternalSymbolName.ExportEquals
33193319
&& symbolFromSymbolTable.escapedName !== InternalSymbolName.Default
@@ -3326,16 +3326,9 @@ namespace ts {
33263326
) {
33273327

33283328
const resolvedImportedSymbol = resolveAlias(symbolFromSymbolTable);
3329-
if (isAccessible(symbolFromSymbolTable, resolvedImportedSymbol, ignoreQualification)) {
3330-
return [symbolFromSymbolTable];
3331-
}
3332-
3333-
// Look in the exported members, if we can find accessibleSymbolChain, symbol is accessible using this chain
3334-
// but only if the symbolFromSymbolTable can be qualified
3335-
const candidateTable = getExportsOfSymbol(resolvedImportedSymbol);
3336-
const accessibleSymbolsFromExports = candidateTable && getAccessibleSymbolChainFromSymbolTable(candidateTable, /*ignoreQualification*/ true);
3337-
if (accessibleSymbolsFromExports && canQualifySymbol(symbolFromSymbolTable, getQualifiedLeftMeaning(meaning))) {
3338-
return [symbolFromSymbolTable].concat(accessibleSymbolsFromExports);
3329+
const candidate = getCandidateListForSymbol(symbolFromSymbolTable, resolvedImportedSymbol, ignoreQualification);
3330+
if (candidate) {
3331+
return candidate;
33393332
}
33403333
}
33413334
if (symbolFromSymbolTable.escapedName === symbol!.escapedName && symbolFromSymbolTable.exportSymbol) {
@@ -3344,6 +3337,23 @@ namespace ts {
33443337
}
33453338
}
33463339
});
3340+
3341+
// If there's no result and we're looking at the global symbol table, treat `globalThis` like an alias and try to lookup thru that
3342+
return result || (symbols === globals ? getCandidateListForSymbol(globalThisSymbol, globalThisSymbol, ignoreQualification) : undefined);
3343+
}
3344+
3345+
function getCandidateListForSymbol(symbolFromSymbolTable: Symbol, resolvedImportedSymbol: Symbol, ignoreQualification: boolean | undefined) {
3346+
if (isAccessible(symbolFromSymbolTable, resolvedImportedSymbol, ignoreQualification)) {
3347+
return [symbolFromSymbolTable];
3348+
}
3349+
3350+
// Look in the exported members, if we can find accessibleSymbolChain, symbol is accessible using this chain
3351+
// but only if the symbolFromSymbolTable can be qualified
3352+
const candidateTable = getExportsOfSymbol(resolvedImportedSymbol);
3353+
const accessibleSymbolsFromExports = candidateTable && getAccessibleSymbolChainFromSymbolTable(candidateTable, /*ignoreQualification*/ true);
3354+
if (accessibleSymbolsFromExports && canQualifySymbol(symbolFromSymbolTable, getQualifiedLeftMeaning(meaning))) {
3355+
return [symbolFromSymbolTable].concat(accessibleSymbolsFromExports);
3356+
}
33473357
}
33483358
}
33493359

tests/baselines/reference/ModuleWithExportedAndNonExportedClasses.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/conformance/internalModules/exportDeclarations/ModuleWithExportedAndNonExportedClasses.ts ===
22
module A {
3-
>A : typeof A
3+
>A : typeof globalThis.A
44

55
export class A {
66
>A : A

tests/baselines/reference/collisionCodeGenModuleWithMemberClassConflict.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/compiler/collisionCodeGenModuleWithMemberClassConflict.ts ===
22
module m1 {
3-
>m1 : typeof m1
3+
>m1 : typeof globalThis.m1
44

55
export class m1 {
66
>m1 : m1
@@ -14,7 +14,7 @@ var foo = new m1.m1();
1414
>m1 : typeof m1.m1
1515

1616
module m2 {
17-
>m2 : typeof m2
17+
>m2 : typeof globalThis.m2
1818

1919
export class m2 {
2020
>m2 : m2

tests/baselines/reference/collisionCodeGenModuleWithMemberVariable.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/compiler/collisionCodeGenModuleWithMemberVariable.ts ===
22
module m1 {
3-
>m1 : typeof m1
3+
>m1 : typeof globalThis.m1
44

55
export var m1 = 10;
66
>m1 : number

tests/baselines/reference/collisionCodeGenModuleWithModuleReopening.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/compiler/collisionCodeGenModuleWithModuleReopening.ts ===
22
module m1 {
3-
>m1 : typeof m1
3+
>m1 : typeof globalThis.m1
44

55
export class m1 {
66
>m1 : m1
@@ -14,7 +14,7 @@ var foo = new m1.m1();
1414
>m1 : typeof m1.m1
1515

1616
module m1 {
17-
>m1 : typeof m1
17+
>m1 : typeof globalThis.m1
1818

1919
export class c1 {
2020
>c1 : c1
@@ -37,7 +37,7 @@ var foo2 = new m1.c1();
3737
>c1 : typeof m1.c1
3838

3939
module m2 {
40-
>m2 : typeof m2
40+
>m2 : typeof globalThis.m2
4141

4242
export class c1 {
4343
>c1 : c1
@@ -59,7 +59,7 @@ var foo3 = new m2.c1();
5959
>c1 : typeof m2.c1
6060

6161
module m2 {
62-
>m2 : typeof m2
62+
>m2 : typeof globalThis.m2
6363

6464
export class m2 {
6565
>m2 : m2

tests/baselines/reference/collisionCodeGenModuleWithPrivateMember.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/compiler/collisionCodeGenModuleWithPrivateMember.ts ===
22
module m1 {
3-
>m1 : typeof m1
3+
>m1 : typeof globalThis.m1
44

55
class m1 {
66
>m1 : m1

tests/baselines/reference/collisionCodeGenModuleWithUnicodeNames.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/compiler/collisionCodeGenModuleWithUnicodeNames.ts ===
22
module 才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüß才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüßAbcd123 {
3-
>才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüß才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüßAbcd123 : typeof 才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüß才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüßAbcd123
3+
>才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüß才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüßAbcd123 : typeof globalThis.才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüß才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüßAbcd123
44

55
export class 才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüß才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüßAbcd123 {
66
>才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüß才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüßAbcd123 : 才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüß才能ソЫⅨ蒤郳र्क्ड्राüışğİliيونيكودöÄüßAbcd123

tests/baselines/reference/declFileTypeAnnotationVisibilityErrorTypeAlias.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module M1 {
3434
>Window : Window
3535

3636
export var p: W; // No error
37-
>p : string | Window
37+
>p : string | globalThis.Window
3838
}
3939
}
4040

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//// [tests/cases/compiler/declarationsForFileShadowingGlobalNoError.ts] ////
2+
3+
//// [dom.ts]
4+
export type DOMNode = Node;
5+
//// [custom.ts]
6+
export type Node = {};
7+
//// [index.ts]
8+
import { Node } from './custom'
9+
import { DOMNode } from './dom'
10+
11+
type Constructor = new (...args: any[]) => any
12+
13+
export const mixin = (Base: Constructor) => {
14+
return class extends Base {
15+
get(domNode: DOMNode) {}
16+
}
17+
}
18+
19+
//// [dom.js]
20+
"use strict";
21+
exports.__esModule = true;
22+
//// [custom.js]
23+
"use strict";
24+
exports.__esModule = true;
25+
//// [index.js]
26+
"use strict";
27+
var __extends = (this && this.__extends) || (function () {
28+
var extendStatics = function (d, b) {
29+
extendStatics = Object.setPrototypeOf ||
30+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
31+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
32+
return extendStatics(d, b);
33+
};
34+
return function (d, b) {
35+
extendStatics(d, b);
36+
function __() { this.constructor = d; }
37+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
38+
};
39+
})();
40+
exports.__esModule = true;
41+
exports.mixin = function (Base) {
42+
return /** @class */ (function (_super) {
43+
__extends(class_1, _super);
44+
function class_1() {
45+
return _super !== null && _super.apply(this, arguments) || this;
46+
}
47+
class_1.prototype.get = function (domNode) { };
48+
return class_1;
49+
}(Base));
50+
};
51+
52+
53+
//// [dom.d.ts]
54+
export declare type DOMNode = Node;
55+
//// [custom.d.ts]
56+
export declare type Node = {};
57+
//// [index.d.ts]
58+
declare type Constructor = new (...args: any[]) => any;
59+
export declare const mixin: (Base: Constructor) => {
60+
new (...args: any[]): {
61+
[x: string]: any;
62+
get(domNode: globalThis.Node): void;
63+
};
64+
};
65+
export {};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
=== tests/cases/compiler/dom.ts ===
2+
export type DOMNode = Node;
3+
>DOMNode : Symbol(DOMNode, Decl(dom.ts, 0, 0))
4+
>Node : Symbol(Node, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
5+
6+
=== tests/cases/compiler/custom.ts ===
7+
export type Node = {};
8+
>Node : Symbol(Node, Decl(custom.ts, 0, 0))
9+
10+
=== tests/cases/compiler/index.ts ===
11+
import { Node } from './custom'
12+
>Node : Symbol(Node, Decl(index.ts, 0, 8))
13+
14+
import { DOMNode } from './dom'
15+
>DOMNode : Symbol(DOMNode, Decl(index.ts, 1, 8))
16+
17+
type Constructor = new (...args: any[]) => any
18+
>Constructor : Symbol(Constructor, Decl(index.ts, 1, 31))
19+
>args : Symbol(args, Decl(index.ts, 3, 24))
20+
21+
export const mixin = (Base: Constructor) => {
22+
>mixin : Symbol(mixin, Decl(index.ts, 5, 12))
23+
>Base : Symbol(Base, Decl(index.ts, 5, 22))
24+
>Constructor : Symbol(Constructor, Decl(index.ts, 1, 31))
25+
26+
return class extends Base {
27+
>Base : Symbol(Base, Decl(index.ts, 5, 22))
28+
29+
get(domNode: DOMNode) {}
30+
>get : Symbol((Anonymous class).get, Decl(index.ts, 6, 29))
31+
>domNode : Symbol(domNode, Decl(index.ts, 7, 8))
32+
>DOMNode : Symbol(DOMNode, Decl(index.ts, 1, 8))
33+
}
34+
}

0 commit comments

Comments
 (0)