diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a0d21dbc49d81..141fb83b1b7c0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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)]; diff --git a/tests/baselines/reference/globalThisDeclarationEmit.errors.txt b/tests/baselines/reference/globalThisDeclarationEmit.errors.txt new file mode 100644 index 0000000000000..8d536b0fd1c7f --- /dev/null +++ b/tests/baselines/reference/globalThisDeclarationEmit.errors.txt @@ -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; \ No newline at end of file diff --git a/tests/baselines/reference/globalThisDeclarationEmit.js b/tests/baselines/reference/globalThisDeclarationEmit.js new file mode 100644 index 0000000000000..52793067733a9 --- /dev/null +++ b/tests/baselines/reference/globalThisDeclarationEmit.js @@ -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; diff --git a/tests/baselines/reference/globalThisDeclarationEmit.symbols b/tests/baselines/reference/globalThisDeclarationEmit.symbols new file mode 100644 index 0000000000000..782d3315e48a3 --- /dev/null +++ b/tests/baselines/reference/globalThisDeclarationEmit.symbols @@ -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) + diff --git a/tests/baselines/reference/globalThisDeclarationEmit.types b/tests/baselines/reference/globalThisDeclarationEmit.types new file mode 100644 index 0000000000000..ed60137fb0805 --- /dev/null +++ b/tests/baselines/reference/globalThisDeclarationEmit.types @@ -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 + diff --git a/tests/baselines/reference/globalThisDeclarationEmit2.js b/tests/baselines/reference/globalThisDeclarationEmit2.js new file mode 100644 index 0000000000000..ac6f9ecd03691 --- /dev/null +++ b/tests/baselines/reference/globalThisDeclarationEmit2.js @@ -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 }; diff --git a/tests/baselines/reference/globalThisDeclarationEmit2.symbols b/tests/baselines/reference/globalThisDeclarationEmit2.symbols new file mode 100644 index 0000000000000..1b17abe4e9411 --- /dev/null +++ b/tests/baselines/reference/globalThisDeclarationEmit2.symbols @@ -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) + diff --git a/tests/baselines/reference/globalThisDeclarationEmit2.types b/tests/baselines/reference/globalThisDeclarationEmit2.types new file mode 100644 index 0000000000000..cd399a150c2ef --- /dev/null +++ b/tests/baselines/reference/globalThisDeclarationEmit2.types @@ -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 + diff --git a/tests/baselines/reference/globalThisDeclarationEmit3.js b/tests/baselines/reference/globalThisDeclarationEmit3.js new file mode 100644 index 0000000000000..060c2330ac2db --- /dev/null +++ b/tests/baselines/reference/globalThisDeclarationEmit3.js @@ -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 }; diff --git a/tests/baselines/reference/globalThisDeclarationEmit3.symbols b/tests/baselines/reference/globalThisDeclarationEmit3.symbols new file mode 100644 index 0000000000000..566b7c7fc6f34 --- /dev/null +++ b/tests/baselines/reference/globalThisDeclarationEmit3.symbols @@ -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)) + diff --git a/tests/baselines/reference/globalThisDeclarationEmit3.types b/tests/baselines/reference/globalThisDeclarationEmit3.types new file mode 100644 index 0000000000000..13f5996306229 --- /dev/null +++ b/tests/baselines/reference/globalThisDeclarationEmit3.types @@ -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 + diff --git a/tests/cases/compiler/globalThisDeclarationEmit.ts b/tests/cases/compiler/globalThisDeclarationEmit.ts new file mode 100644 index 0000000000000..4bc08520f8f43 --- /dev/null +++ b/tests/cases/compiler/globalThisDeclarationEmit.ts @@ -0,0 +1,7 @@ +// @declaration: true +// @filename: index.ts +import { variable } from "./variable"; +export const globalThis = variable; + +// @filename: variable.ts +export const variable = globalThis; \ No newline at end of file diff --git a/tests/cases/compiler/globalThisDeclarationEmit2.ts b/tests/cases/compiler/globalThisDeclarationEmit2.ts new file mode 100644 index 0000000000000..e190ed08521bd --- /dev/null +++ b/tests/cases/compiler/globalThisDeclarationEmit2.ts @@ -0,0 +1,7 @@ +// @declaration: true +// @filename: index.ts +import { variable } from "./variable"; +export { variable as globalThis }; + +// @filename: variable.ts +export const variable = globalThis; \ No newline at end of file diff --git a/tests/cases/compiler/globalThisDeclarationEmit3.ts b/tests/cases/compiler/globalThisDeclarationEmit3.ts new file mode 100644 index 0000000000000..40a3712391eca --- /dev/null +++ b/tests/cases/compiler/globalThisDeclarationEmit3.ts @@ -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 }; \ No newline at end of file