Skip to content

Commit cdc3840

Browse files
authored
Fix crash on declaration emit for globalThis (#37992)
1 parent a16c441 commit cdc3840

14 files changed

+201
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3845,7 +3845,7 @@ namespace ts {
38453845
// If we're trying to reference some object literal in, eg `var a = { x: 1 }`, the symbol for the literal, `__object`, is distinct
38463846
// from the symbol of the declaration it is being assigned to. Since we can use the declaration to refer to the literal, however,
38473847
// we'd like to make that connection here - potentially causing us to paint the declaration's visibility, and therefore the literal.
3848-
const firstDecl: Node = first(symbol.declarations);
3848+
const firstDecl: Node | false = !!length(symbol.declarations) && first(symbol.declarations);
38493849
if (!length(containers) && meaning & SymbolFlags.Value && firstDecl && isObjectLiteralExpression(firstDecl)) {
38503850
if (firstDecl.parent && isVariableDeclaration(firstDecl.parent) && firstDecl === firstDecl.parent.initializer) {
38513851
containers = [getSymbolOfNode(firstDecl.parent)];
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/index.ts(2,14): error TS4025: Exported variable 'globalThis' has or is using private name 'globalThis'.
2+
3+
4+
==== tests/cases/compiler/index.ts (1 errors) ====
5+
import { variable } from "./variable";
6+
export const globalThis = variable;
7+
~~~~~~~~~~
8+
!!! error TS4025: Exported variable 'globalThis' has or is using private name 'globalThis'.
9+
10+
==== tests/cases/compiler/variable.ts (0 errors) ====
11+
export const variable = globalThis;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/globalThisDeclarationEmit.ts] ////
2+
3+
//// [index.ts]
4+
import { variable } from "./variable";
5+
export const globalThis = variable;
6+
7+
//// [variable.ts]
8+
export const variable = globalThis;
9+
10+
//// [variable.js]
11+
"use strict";
12+
exports.__esModule = true;
13+
exports.variable = void 0;
14+
exports.variable = globalThis;
15+
//// [index.js]
16+
"use strict";
17+
exports.__esModule = true;
18+
exports.globalThis = void 0;
19+
var variable_1 = require("./variable");
20+
exports.globalThis = variable_1.variable;
21+
22+
23+
//// [variable.d.ts]
24+
export declare const variable: typeof globalThis;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import { variable } from "./variable";
3+
>variable : Symbol(variable, Decl(index.ts, 0, 8))
4+
5+
export const globalThis = variable;
6+
>globalThis : Symbol(globalThis, Decl(index.ts, 1, 12))
7+
>variable : Symbol(variable, Decl(index.ts, 0, 8))
8+
9+
=== tests/cases/compiler/variable.ts ===
10+
export const variable = globalThis;
11+
>variable : Symbol(variable, Decl(variable.ts, 0, 12))
12+
>globalThis : Symbol(globalThis)
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import { variable } from "./variable";
3+
>variable : typeof globalThis
4+
5+
export const globalThis = variable;
6+
>globalThis : typeof globalThis
7+
>variable : typeof globalThis
8+
9+
=== tests/cases/compiler/variable.ts ===
10+
export const variable = globalThis;
11+
>variable : typeof globalThis
12+
>globalThis : typeof globalThis
13+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/globalThisDeclarationEmit2.ts] ////
2+
3+
//// [index.ts]
4+
import { variable } from "./variable";
5+
export { variable as globalThis };
6+
7+
//// [variable.ts]
8+
export const variable = globalThis;
9+
10+
//// [variable.js]
11+
"use strict";
12+
exports.__esModule = true;
13+
exports.variable = void 0;
14+
exports.variable = globalThis;
15+
//// [index.js]
16+
"use strict";
17+
exports.__esModule = true;
18+
exports.globalThis = void 0;
19+
var variable_1 = require("./variable");
20+
exports.globalThis = variable_1.variable;
21+
22+
23+
//// [variable.d.ts]
24+
export declare const variable: typeof globalThis;
25+
//// [index.d.ts]
26+
import { variable } from "./variable";
27+
export { variable as globalThis };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import { variable } from "./variable";
3+
>variable : Symbol(variable, Decl(index.ts, 0, 8))
4+
5+
export { variable as globalThis };
6+
>variable : Symbol(variable, Decl(index.ts, 0, 8))
7+
>globalThis : Symbol(globalThis, Decl(index.ts, 1, 8))
8+
9+
=== tests/cases/compiler/variable.ts ===
10+
export const variable = globalThis;
11+
>variable : Symbol(variable, Decl(variable.ts, 0, 12))
12+
>globalThis : Symbol(globalThis)
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import { variable } from "./variable";
3+
>variable : typeof globalThis
4+
5+
export { variable as globalThis };
6+
>variable : typeof globalThis
7+
>globalThis : typeof globalThis
8+
9+
=== tests/cases/compiler/variable.ts ===
10+
export const variable = globalThis;
11+
>variable : typeof globalThis
12+
>globalThis : typeof globalThis
13+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [tests/cases/compiler/globalThisDeclarationEmit3.ts] ////
2+
3+
//// [index.ts]
4+
import { variable } from "./variable";
5+
export { variable as globalThis };
6+
7+
//// [variable.ts]
8+
import mod = globalThis;
9+
export { mod as variable };
10+
11+
//// [variable.js]
12+
"use strict";
13+
exports.__esModule = true;
14+
exports.variable = void 0;
15+
var mod = globalThis;
16+
exports.variable = mod;
17+
//// [index.js]
18+
"use strict";
19+
exports.__esModule = true;
20+
exports.globalThis = void 0;
21+
var variable_1 = require("./variable");
22+
exports.globalThis = variable_1.variable;
23+
24+
25+
//// [variable.d.ts]
26+
import mod = globalThis;
27+
export { mod as variable };
28+
//// [index.d.ts]
29+
import { variable } from "./variable";
30+
export { variable as globalThis };
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import { variable } from "./variable";
3+
>variable : Symbol(variable, Decl(index.ts, 0, 8))
4+
5+
export { variable as globalThis };
6+
>variable : Symbol(variable, Decl(index.ts, 0, 8))
7+
>globalThis : Symbol(globalThis, Decl(index.ts, 1, 8))
8+
9+
=== tests/cases/compiler/variable.ts ===
10+
import mod = globalThis;
11+
>mod : Symbol(mod, Decl(variable.ts, 0, 0))
12+
>globalThis : Symbol(mod)
13+
14+
export { mod as variable };
15+
>mod : Symbol(mod, Decl(variable.ts, 0, 0))
16+
>variable : Symbol(variable, Decl(variable.ts, 1, 8))
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import { variable } from "./variable";
3+
>variable : typeof variable
4+
5+
export { variable as globalThis };
6+
>variable : typeof variable
7+
>globalThis : typeof variable
8+
9+
=== tests/cases/compiler/variable.ts ===
10+
import mod = globalThis;
11+
>mod : typeof mod
12+
>globalThis : typeof mod
13+
14+
export { mod as variable };
15+
>mod : typeof mod
16+
>variable : typeof mod
17+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @declaration: true
2+
// @filename: index.ts
3+
import { variable } from "./variable";
4+
export const globalThis = variable;
5+
6+
// @filename: variable.ts
7+
export const variable = globalThis;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @declaration: true
2+
// @filename: index.ts
3+
import { variable } from "./variable";
4+
export { variable as globalThis };
5+
6+
// @filename: variable.ts
7+
export const variable = globalThis;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @declaration: true
2+
// @filename: index.ts
3+
import { variable } from "./variable";
4+
export { variable as globalThis };
5+
6+
// @filename: variable.ts
7+
import mod = globalThis;
8+
export { mod as variable };

0 commit comments

Comments
 (0)