From 37845224204272e73ef3bc8288757cc69a58ba4d Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 14 Jun 2024 16:39:36 -0700 Subject: [PATCH] Merge ade482cabeee8cb49c7a47b654db19650dec5868 into b1c52c53cc6c8cf35b19accfc3e29916489c821e --- src/compiler/checker.ts | 3 +- src/compiler/utilities.ts | 9 ++++ src/services/utilities.ts | 9 ---- ...ortTypeInGlobalThisTypeArgument.errors.txt | 37 +++++++++++++++ ...ationImportTypeInGlobalThisTypeArgument.js | 46 +++++++++++++++++++ ...ationImportTypeInGlobalThisTypeArgument.ts | 34 ++++++++++++++ 6 files changed, 128 insertions(+), 10 deletions(-) create mode 100644 tests/baselines/reference/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.errors.txt create mode 100644 tests/baselines/reference/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.js create mode 100644 tests/cases/compiler/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5a0e11d8d8d70..6b54c8a9d370e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -541,6 +541,7 @@ import { isExternalModuleIndicator, isExternalModuleNameRelative, isExternalModuleReference, + isExternalModuleSymbol, isExternalOrCommonJsModule, isForInOrOfStatement, isForInStatement, @@ -8960,7 +8961,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { const parentSymbol = nodeSymbol && isSymbolAccessible(nodeSymbol, context.enclosingDeclaration, meaning, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === SymbolAccessibility.Accessible && lookupSymbolChain(nodeSymbol, context, meaning, /*yieldModuleSymbol*/ true)[0]; - if (parentSymbol && parentSymbol.flags & SymbolFlags.Module) { + if (parentSymbol && isExternalModuleSymbol(parentSymbol)) { name = getSpecifierForModuleSymbol(parentSymbol, context); } else { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 16a092532bd45..81cfddfc0578a 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -632,6 +632,15 @@ export function isTransientSymbol(symbol: Symbol): symbol is TransientSymbol { return (symbol.flags & SymbolFlags.Transient) !== 0; } +/** + * True if the symbol is for an external module, as opposed to a namespace. + * + * @internal + */ +export function isExternalModuleSymbol(moduleSymbol: Symbol): boolean { + return !!(moduleSymbol.flags & SymbolFlags.Module) && (moduleSymbol.escapedName as string).charCodeAt(0) === CharacterCodes.doubleQuote; +} + const stringWriter = createSingleLineStringWriter(); function createSingleLineStringWriter(): EmitTextWriter { diff --git a/src/services/utilities.ts b/src/services/utilities.ts index abfe9f908fcba..b5e726b912e38 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -2408,15 +2408,6 @@ export function isTypeKeywordTokenOrIdentifier(node: Node) { return isTypeKeywordToken(node) || isIdentifier(node) && node.text === "type"; } -/** - * True if the symbol is for an external module, as opposed to a namespace. - * - * @internal - */ -export function isExternalModuleSymbol(moduleSymbol: Symbol): boolean { - return !!(moduleSymbol.flags & SymbolFlags.Module) && moduleSymbol.name.charCodeAt(0) === CharacterCodes.doubleQuote; -} - /** * Returns `true` the first time it encounters a node and `false` afterwards. * diff --git a/tests/baselines/reference/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.errors.txt b/tests/baselines/reference/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.errors.txt new file mode 100644 index 0000000000000..5e1769bc3646f --- /dev/null +++ b/tests/baselines/reference/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.errors.txt @@ -0,0 +1,37 @@ +/types.js(3,21): error TS2304: Cannot find name 'Keyword'. +/types.js(3,30): error TS2304: Cannot find name 'ParamValueTyped'. + + +==== /contractHelper.d.ts (0 errors) ==== + export function handleParamGovernance(zcf: any): { + publicMixin: { + getGovernedParams: () => globalThis.ERef; + }; + }; + +==== /exported.d.ts (0 errors) ==== + type _ERef = T | Promise; + import { ParamStateRecord as _ParamStateRecord } from './types.js'; + declare global { + // @ts-ignore TS2666 + export { + _ERef as ERef, + _ParamStateRecord as ParamStateRecord, + }; + } + +==== /types.js (2 errors) ==== + export {}; + /** + * @typedef {Record} ParamStateRecord a Record containing + ~~~~~~~ +!!! error TS2304: Cannot find name 'Keyword'. + ~~~~~~~~~~~~~~~ +!!! error TS2304: Cannot find name 'ParamValueTyped'. + * keyword pairs with descriptions of parameters under governance. + */ + +==== /index.js (0 errors) ==== + import { handleParamGovernance } from './contractHelper.js'; + export const blah = handleParamGovernance({}); + \ No newline at end of file diff --git a/tests/baselines/reference/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.js b/tests/baselines/reference/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.js new file mode 100644 index 0000000000000..45784a12d5086 --- /dev/null +++ b/tests/baselines/reference/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.js @@ -0,0 +1,46 @@ +//// [tests/cases/compiler/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.ts] //// + +//// [contractHelper.d.ts] +export function handleParamGovernance(zcf: any): { + publicMixin: { + getGovernedParams: () => globalThis.ERef; + }; +}; + +//// [exported.d.ts] +type _ERef = T | Promise; +import { ParamStateRecord as _ParamStateRecord } from './types.js'; +declare global { + // @ts-ignore TS2666 + export { + _ERef as ERef, + _ParamStateRecord as ParamStateRecord, + }; +} + +//// [types.js] +export {}; +/** + * @typedef {Record} ParamStateRecord a Record containing + * keyword pairs with descriptions of parameters under governance. + */ + +//// [index.js] +import { handleParamGovernance } from './contractHelper.js'; +export const blah = handleParamGovernance({}); + + + + +//// [types.d.ts] +/** + * a Record containing + * keyword pairs with descriptions of parameters under governance. + */ +export type ParamStateRecord = Record; +//// [index.d.ts] +export const blah: { + publicMixin: { + getGovernedParams: () => globalThis.ERef; + }; +}; diff --git a/tests/cases/compiler/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.ts b/tests/cases/compiler/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.ts new file mode 100644 index 0000000000000..faccc6f97164b --- /dev/null +++ b/tests/cases/compiler/reuseTypeAnnotationImportTypeInGlobalThisTypeArgument.ts @@ -0,0 +1,34 @@ +// @checkJs: true +// @declaration: true +// @module: preserve +// @emitDeclarationOnly: true +// @noTypesAndSymbols: true + +// @Filename: /contractHelper.d.ts +export function handleParamGovernance(zcf: any): { + publicMixin: { + getGovernedParams: () => globalThis.ERef; + }; +}; + +// @Filename: /exported.d.ts +type _ERef = T | Promise; +import { ParamStateRecord as _ParamStateRecord } from './types.js'; +declare global { + // @ts-ignore TS2666 + export { + _ERef as ERef, + _ParamStateRecord as ParamStateRecord, + }; +} + +// @Filename: /types.js +export {}; +/** + * @typedef {Record} ParamStateRecord a Record containing + * keyword pairs with descriptions of parameters under governance. + */ + +// @Filename: /index.js +import { handleParamGovernance } from './contractHelper.js'; +export const blah = handleParamGovernance({});