Skip to content

Commit 29f9493

Browse files
authored
Fix crash when exporting+aliasing globalThis inside declare global (#34408)
* global module:Fix crash when exporting+aliasing globalThis * Fix another globalThis crash find-all-refs assumed that an export inside a `declare x` was always an ambient module, but it is not -- `declare global` does not allow `export`, so find-all-refs shouldn't return any refs for this error case.
1 parent a685ac4 commit 29f9493

8 files changed

+60
-3
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32454,7 +32454,7 @@ namespace ts {
3245432454
// find immediate value referenced by exported name (SymbolFlags.Alias is set so we don't chase down aliases)
3245532455
const symbol = resolveName(exportedName, exportedName.escapedText, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace | SymbolFlags.Alias,
3245632456
/*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ true);
32457-
if (symbol && (symbol === undefinedSymbol || isGlobalSourceFile(getDeclarationContainer(symbol.declarations[0])))) {
32457+
if (symbol && (symbol === undefinedSymbol || symbol === globalThisSymbol || isGlobalSourceFile(getDeclarationContainer(symbol.declarations[0])))) {
3245832458
error(exportedName, Diagnostics.Cannot_export_0_Only_local_declarations_can_be_exported_from_a_module, idText(exportedName));
3245932459
}
3246032460
else {

src/services/findAllReferences.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1395,8 +1395,10 @@ namespace ts.FindAllReferences.Core {
13951395
|| exportSpecifier.name.originalKeywordKind === SyntaxKind.DefaultKeyword;
13961396
const exportKind = isDefaultExport ? ExportKind.Default : ExportKind.Named;
13971397
const exportSymbol = Debug.assertDefined(exportSpecifier.symbol);
1398-
const exportInfo = Debug.assertDefined(getExportInfo(exportSymbol, exportKind, state.checker));
1399-
searchForImportsOfExport(referenceLocation, exportSymbol, exportInfo, state);
1398+
const exportInfo = getExportInfo(exportSymbol, exportKind, state.checker);
1399+
if (exportInfo) {
1400+
searchForImportsOfExport(referenceLocation, exportSymbol, exportInfo, state);
1401+
}
14001402
}
14011403

14021404
// At `export { x } from "foo"`, also search for the imported symbol `"foo".x`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/conformance/es2019/globalThisGlobalExportAsGlobal.ts(2,9): error TS2669: Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.
2+
tests/cases/conformance/es2019/globalThisGlobalExportAsGlobal.ts(3,14): error TS2661: Cannot export 'globalThis'. Only local declarations can be exported from a module.
3+
4+
5+
==== tests/cases/conformance/es2019/globalThisGlobalExportAsGlobal.ts (2 errors) ====
6+
// https://github.com/microsoft/TypeScript/issues/33754
7+
declare global {
8+
~~~~~~
9+
!!! error TS2669: Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.
10+
export { globalThis as global }
11+
~~~~~~~~~~
12+
!!! error TS2661: Cannot export 'globalThis'. Only local declarations can be exported from a module.
13+
}
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [globalThisGlobalExportAsGlobal.ts]
2+
// https://github.com/microsoft/TypeScript/issues/33754
3+
declare global {
4+
export { globalThis as global }
5+
}
6+
7+
8+
//// [globalThisGlobalExportAsGlobal.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/conformance/es2019/globalThisGlobalExportAsGlobal.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/33754
3+
declare global {
4+
>global : Symbol(global, Decl(globalThisGlobalExportAsGlobal.ts, 0, 0))
5+
6+
export { globalThis as global }
7+
>globalThis : Symbol(globalThis)
8+
>global : Symbol(global, Decl(globalThisGlobalExportAsGlobal.ts, 2, 12))
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/conformance/es2019/globalThisGlobalExportAsGlobal.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/33754
3+
declare global {
4+
>global : typeof global
5+
6+
export { globalThis as global }
7+
>globalThis : typeof globalThis
8+
>global : typeof globalThis
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// https://github.com/microsoft/TypeScript/issues/33754
2+
declare global {
3+
export { globalThis as global }
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////declare global {
4+
//// export { globalThis as [|global|] }
5+
////}
6+
7+
for (const r of test.ranges()) {
8+
verify.documentHighlightsOf(r, [r]);
9+
}

0 commit comments

Comments
 (0)