Skip to content

Change export = structurally to allow other exported elements in the same file #62103

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
301 changes: 174 additions & 127 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5454,13 +5454,6 @@ export interface TypeChecker {
getAccessibleSymbolChain(symbol: Symbol, enclosingDeclaration: Node | undefined, meaning: SymbolFlags, useOnlyExternalAliasing: boolean): Symbol[] | undefined;
getTypePredicateOfSignature(signature: Signature): TypePredicate | undefined;
/** @internal */ resolveExternalModuleName(moduleSpecifier: Expression): Symbol | undefined;
/**
* An external module with an 'export =' declaration resolves to the target of the 'export =' declaration,
* and an external module with no 'export =' declaration resolves to the module itself.
*
* @internal
*/
resolveExternalModuleSymbol(symbol: Symbol): Symbol;
/**
* @param node A location where we might consider accessing `this`. Not necessarily a ThisExpression.
*
Expand Down
5 changes: 2 additions & 3 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1289,10 +1289,9 @@ function getNewImportFixes(
//
// import fs = require("fs"); // or const in JS
// fs.writeFile
const exportEquals = checker.resolveExternalModuleSymbol(exportInfo.moduleSymbol);
let namespacePrefix;
if (exportEquals !== exportInfo.moduleSymbol) {
namespacePrefix = forEachNameOfDefaultExport(exportEquals, checker, getEmitScriptTarget(compilerOptions), identity)!;
if (exportInfo.moduleSymbol.exports?.has(InternalSymbolName.ExportEquals)) {
namespacePrefix = forEachNameOfDefaultExport(exportInfo.moduleSymbol.exports?.get(InternalSymbolName.ExportEquals)!, checker, getEmitScriptTarget(compilerOptions), identity)!;
}
namespacePrefix ||= moduleSymbolToValidIdentifier(
exportInfo.moduleSymbol,
Expand Down
2 changes: 1 addition & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5386,7 +5386,7 @@ function getAutoImportSymbolFromCompletionEntryData(name: string, data: Completi

if (!moduleSymbol) return undefined;
let symbol = data.exportName === InternalSymbolName.ExportEquals
? checker.resolveExternalModuleSymbol(moduleSymbol)
? moduleSymbol.exports?.get(InternalSymbolName.ExportEquals)
: checker.tryGetMemberInModuleExportsAndProperties(data.exportName, moduleSymbol);
if (!symbol) return undefined;
const isDefaultExport = data.exportName === InternalSymbolName.Default;
Expand Down
11 changes: 4 additions & 7 deletions src/services/exportInfoMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export function createCacheableExportInfoMap(host: CacheableExportInfoMapHost):
);
const symbol = info.symbol || cachedSymbol || Debug.checkDefined(
exportKind === ExportKind.ExportEquals
? checker.resolveExternalModuleSymbol(moduleSymbol)
? moduleSymbol
: checker.tryGetMemberInModuleExportsAndProperties(unescapeLeadingUnderscores(info.symbolTableKey), moduleSymbol),
`Could not find symbol '${info.symbolName}' by key '${info.symbolTableKey}' in module ${moduleSymbol.name}`,
);
Expand Down Expand Up @@ -604,14 +604,11 @@ export function getDefaultLikeExportInfo(moduleSymbol: Symbol, checker: TypeChec
symbol: Symbol;
exportKind: ExportKind;
} | undefined {
const exportEquals = checker.resolveExternalModuleSymbol(moduleSymbol);
if (exportEquals !== moduleSymbol) {
const defaultExport = checker.tryGetMemberInModuleExports(InternalSymbolName.Default, exportEquals);
if (defaultExport) return { symbol: defaultExport, exportKind: ExportKind.Default };
return { symbol: exportEquals, exportKind: ExportKind.ExportEquals };
}
const defaultExport = checker.tryGetMemberInModuleExports(InternalSymbolName.Default, moduleSymbol);
if (defaultExport) return { symbol: defaultExport, exportKind: ExportKind.Default };
if (moduleSymbol.exports?.has(InternalSymbolName.ExportEquals)) {
return { symbol: moduleSymbol.exports.get(InternalSymbolName.ExportEquals)!, exportKind: ExportKind.ExportEquals };
}
}

function isImportableSymbol(symbol: Symbol, checker: TypeChecker) {
Expand Down
4 changes: 2 additions & 2 deletions src/services/refactors/convertImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ImportDeclaration,
ImportKind,
ImportSpecifier,
InternalSymbolName,
isExportSpecifier,
isImportDeclaration,
isJSDocImportTag,
Expand Down Expand Up @@ -281,8 +282,7 @@ export function doChangeNamedToNamespaceOrDefault(sourceFile: SourceFile, progra
function isExportEqualsModule(moduleSpecifier: Expression, checker: TypeChecker) {
const externalModule = checker.resolveExternalModuleName(moduleSpecifier);
if (!externalModule) return false;
const exportEquals = checker.resolveExternalModuleSymbol(externalModule);
return externalModule !== exportEquals;
return !!externalModule.exports?.has(InternalSymbolName.ExportEquals);
}

function createImport(node: ImportDeclaration, defaultImportName: Identifier | undefined, elements: readonly ImportSpecifier[] | undefined): ImportDeclaration {
Expand Down
Loading