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.
This commit is contained in:
Nathan Shively-Sanders
2019-10-15 14:05:39 -07:00
committed by GitHub
parent a685ac426c
commit 29f9493d87
8 changed files with 60 additions and 3 deletions

View File

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

View File

@@ -1395,8 +1395,10 @@ namespace ts.FindAllReferences.Core {
|| exportSpecifier.name.originalKeywordKind === SyntaxKind.DefaultKeyword;
const exportKind = isDefaultExport ? ExportKind.Default : ExportKind.Named;
const exportSymbol = Debug.assertDefined(exportSpecifier.symbol);
const exportInfo = Debug.assertDefined(getExportInfo(exportSymbol, exportKind, state.checker));
searchForImportsOfExport(referenceLocation, exportSymbol, exportInfo, state);
const exportInfo = getExportInfo(exportSymbol, exportKind, state.checker);
if (exportInfo) {
searchForImportsOfExport(referenceLocation, exportSymbol, exportInfo, state);
}
}
// At `export { x } from "foo"`, also search for the imported symbol `"foo".x`.

View File

@@ -0,0 +1,14 @@
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.
tests/cases/conformance/es2019/globalThisGlobalExportAsGlobal.ts(3,14): error TS2661: Cannot export 'globalThis'. Only local declarations can be exported from a module.
==== tests/cases/conformance/es2019/globalThisGlobalExportAsGlobal.ts (2 errors) ====
// https://github.com/microsoft/TypeScript/issues/33754
declare global {
~~~~~~
!!! error TS2669: Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.
export { globalThis as global }
~~~~~~~~~~
!!! error TS2661: Cannot export 'globalThis'. Only local declarations can be exported from a module.
}

View File

@@ -0,0 +1,8 @@
//// [globalThisGlobalExportAsGlobal.ts]
// https://github.com/microsoft/TypeScript/issues/33754
declare global {
export { globalThis as global }
}
//// [globalThisGlobalExportAsGlobal.js]

View File

@@ -0,0 +1,10 @@
=== tests/cases/conformance/es2019/globalThisGlobalExportAsGlobal.ts ===
// https://github.com/microsoft/TypeScript/issues/33754
declare global {
>global : Symbol(global, Decl(globalThisGlobalExportAsGlobal.ts, 0, 0))
export { globalThis as global }
>globalThis : Symbol(globalThis)
>global : Symbol(global, Decl(globalThisGlobalExportAsGlobal.ts, 2, 12))
}

View File

@@ -0,0 +1,10 @@
=== tests/cases/conformance/es2019/globalThisGlobalExportAsGlobal.ts ===
// https://github.com/microsoft/TypeScript/issues/33754
declare global {
>global : typeof global
export { globalThis as global }
>globalThis : typeof globalThis
>global : typeof globalThis
}

View File

@@ -0,0 +1,4 @@
// https://github.com/microsoft/TypeScript/issues/33754
declare global {
export { globalThis as global }
}

View File

@@ -0,0 +1,9 @@
/// <reference path="fourslash.ts" />
////declare global {
//// export { globalThis as [|global|] }
////}
for (const r of test.ranges()) {
verify.documentHighlightsOf(r, [r]);
}