Skip to content

Fix crash on declaration emit for globalThis #37992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3845,7 +3845,7 @@ namespace ts {
// If we're trying to reference some object literal in, eg `var a = { x: 1 }`, the symbol for the literal, `__object`, is distinct
// from the symbol of the declaration it is being assigned to. Since we can use the declaration to refer to the literal, however,
// we'd like to make that connection here - potentially causing us to paint the declaration's visibility, and therefore the literal.
const firstDecl: Node = first(symbol.declarations);
const firstDecl: Node | false = !!length(symbol.declarations) && first(symbol.declarations);
if (!length(containers) && meaning & SymbolFlags.Value && firstDecl && isObjectLiteralExpression(firstDecl)) {
if (firstDecl.parent && isVariableDeclaration(firstDecl.parent) && firstDecl === firstDecl.parent.initializer) {
containers = [getSymbolOfNode(firstDecl.parent)];
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/globalThisDeclarationEmit.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
tests/cases/compiler/index.ts(2,14): error TS4025: Exported variable 'globalThis' has or is using private name 'globalThis'.


==== tests/cases/compiler/index.ts (1 errors) ====
import { variable } from "./variable";
export const globalThis = variable;
~~~~~~~~~~
!!! error TS4025: Exported variable 'globalThis' has or is using private name 'globalThis'.

==== tests/cases/compiler/variable.ts (0 errors) ====
export const variable = globalThis;
24 changes: 24 additions & 0 deletions tests/baselines/reference/globalThisDeclarationEmit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [tests/cases/compiler/globalThisDeclarationEmit.ts] ////

//// [index.ts]
import { variable } from "./variable";
export const globalThis = variable;

//// [variable.ts]
export const variable = globalThis;

//// [variable.js]
"use strict";
exports.__esModule = true;
exports.variable = void 0;
exports.variable = globalThis;
//// [index.js]
"use strict";
exports.__esModule = true;
exports.globalThis = void 0;
var variable_1 = require("./variable");
exports.globalThis = variable_1.variable;


//// [variable.d.ts]
export declare const variable: typeof globalThis;
13 changes: 13 additions & 0 deletions tests/baselines/reference/globalThisDeclarationEmit.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/index.ts ===
import { variable } from "./variable";
>variable : Symbol(variable, Decl(index.ts, 0, 8))

export const globalThis = variable;
>globalThis : Symbol(globalThis, Decl(index.ts, 1, 12))
>variable : Symbol(variable, Decl(index.ts, 0, 8))

=== tests/cases/compiler/variable.ts ===
export const variable = globalThis;
>variable : Symbol(variable, Decl(variable.ts, 0, 12))
>globalThis : Symbol(globalThis)

13 changes: 13 additions & 0 deletions tests/baselines/reference/globalThisDeclarationEmit.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/index.ts ===
import { variable } from "./variable";
>variable : typeof globalThis

export const globalThis = variable;
>globalThis : typeof globalThis
>variable : typeof globalThis

=== tests/cases/compiler/variable.ts ===
export const variable = globalThis;
>variable : typeof globalThis
>globalThis : typeof globalThis

27 changes: 27 additions & 0 deletions tests/baselines/reference/globalThisDeclarationEmit2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [tests/cases/compiler/globalThisDeclarationEmit2.ts] ////

//// [index.ts]
import { variable } from "./variable";
export { variable as globalThis };

//// [variable.ts]
export const variable = globalThis;

//// [variable.js]
"use strict";
exports.__esModule = true;
exports.variable = void 0;
exports.variable = globalThis;
//// [index.js]
"use strict";
exports.__esModule = true;
exports.globalThis = void 0;
var variable_1 = require("./variable");
exports.globalThis = variable_1.variable;


//// [variable.d.ts]
export declare const variable: typeof globalThis;
//// [index.d.ts]
import { variable } from "./variable";
export { variable as globalThis };
13 changes: 13 additions & 0 deletions tests/baselines/reference/globalThisDeclarationEmit2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/index.ts ===
import { variable } from "./variable";
>variable : Symbol(variable, Decl(index.ts, 0, 8))

export { variable as globalThis };
>variable : Symbol(variable, Decl(index.ts, 0, 8))
>globalThis : Symbol(globalThis, Decl(index.ts, 1, 8))

=== tests/cases/compiler/variable.ts ===
export const variable = globalThis;
>variable : Symbol(variable, Decl(variable.ts, 0, 12))
>globalThis : Symbol(globalThis)

13 changes: 13 additions & 0 deletions tests/baselines/reference/globalThisDeclarationEmit2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/index.ts ===
import { variable } from "./variable";
>variable : typeof globalThis

export { variable as globalThis };
>variable : typeof globalThis
>globalThis : typeof globalThis

=== tests/cases/compiler/variable.ts ===
export const variable = globalThis;
>variable : typeof globalThis
>globalThis : typeof globalThis

30 changes: 30 additions & 0 deletions tests/baselines/reference/globalThisDeclarationEmit3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [tests/cases/compiler/globalThisDeclarationEmit3.ts] ////

//// [index.ts]
import { variable } from "./variable";
export { variable as globalThis };

//// [variable.ts]
import mod = globalThis;
export { mod as variable };

//// [variable.js]
"use strict";
exports.__esModule = true;
exports.variable = void 0;
var mod = globalThis;
exports.variable = mod;
//// [index.js]
"use strict";
exports.__esModule = true;
exports.globalThis = void 0;
var variable_1 = require("./variable");
exports.globalThis = variable_1.variable;


//// [variable.d.ts]
import mod = globalThis;
export { mod as variable };
//// [index.d.ts]
import { variable } from "./variable";
export { variable as globalThis };
17 changes: 17 additions & 0 deletions tests/baselines/reference/globalThisDeclarationEmit3.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/index.ts ===
import { variable } from "./variable";
>variable : Symbol(variable, Decl(index.ts, 0, 8))

export { variable as globalThis };
>variable : Symbol(variable, Decl(index.ts, 0, 8))
>globalThis : Symbol(globalThis, Decl(index.ts, 1, 8))

=== tests/cases/compiler/variable.ts ===
import mod = globalThis;
>mod : Symbol(mod, Decl(variable.ts, 0, 0))
>globalThis : Symbol(mod)

export { mod as variable };
>mod : Symbol(mod, Decl(variable.ts, 0, 0))
>variable : Symbol(variable, Decl(variable.ts, 1, 8))

17 changes: 17 additions & 0 deletions tests/baselines/reference/globalThisDeclarationEmit3.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/index.ts ===
import { variable } from "./variable";
>variable : typeof variable

export { variable as globalThis };
>variable : typeof variable
>globalThis : typeof variable

=== tests/cases/compiler/variable.ts ===
import mod = globalThis;
>mod : typeof mod
>globalThis : typeof mod

export { mod as variable };
>mod : typeof mod
>variable : typeof mod

7 changes: 7 additions & 0 deletions tests/cases/compiler/globalThisDeclarationEmit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @declaration: true
// @filename: index.ts
import { variable } from "./variable";
export const globalThis = variable;

// @filename: variable.ts
export const variable = globalThis;
7 changes: 7 additions & 0 deletions tests/cases/compiler/globalThisDeclarationEmit2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @declaration: true
// @filename: index.ts
import { variable } from "./variable";
export { variable as globalThis };

// @filename: variable.ts
export const variable = globalThis;
8 changes: 8 additions & 0 deletions tests/cases/compiler/globalThisDeclarationEmit3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @declaration: true
// @filename: index.ts
import { variable } from "./variable";
export { variable as globalThis };

// @filename: variable.ts
import mod = globalThis;
export { mod as variable };