Skip to content

Commit

Permalink
Revert "[DO NOT MERGE UNTIL 5.6] Fix re-exported defaults in ExportIn…
Browse files Browse the repository at this point in the history
…foMap (microsoft#58837)"

This reverts commit 1948e92.
  • Loading branch information
andrewbranch committed Jun 13, 2024
1 parent b258429 commit a19bf0c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 96 deletions.
4 changes: 2 additions & 2 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ function getNewImportFixes(
const exportEquals = checker.resolveExternalModuleSymbol(exportInfo.moduleSymbol);
let namespacePrefix;
if (exportEquals !== exportInfo.moduleSymbol) {
namespacePrefix = forEachNameOfDefaultExport(exportEquals, checker, getEmitScriptTarget(compilerOptions), identity)!;
namespacePrefix = forEachNameOfDefaultExport(exportEquals, checker, compilerOptions, /*preferCapitalizedNames*/ false, identity)!;
}
namespacePrefix ||= moduleSymbolToValidIdentifier(
exportInfo.moduleSymbol,
Expand Down Expand Up @@ -1544,7 +1544,7 @@ function getExportInfos(
if (
defaultInfo
&& symbolFlagsHaveMeaning(checker.getSymbolFlags(defaultInfo.symbol), currentTokenMeaning)
&& forEachNameOfDefaultExport(defaultInfo.symbol, checker, getEmitScriptTarget(compilerOptions), (name, capitalizedName) => (isJsxTagName ? capitalizedName ?? name : name) === symbolName)
&& forEachNameOfDefaultExport(defaultInfo.symbol, checker, compilerOptions, isJsxTagName, name => name === symbolName)
) {
addSymbol(moduleSymbol, sourceFile, defaultInfo.symbol, defaultInfo.exportKind, program, isFromPackageJson);
}
Expand Down
22 changes: 6 additions & 16 deletions src/services/exportInfoMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
append,
arrayIsEqualTo,
CancellationToken,
CompilerOptions,
consumesNodeCoreModules,
createMultiMap,
Debug,
Expand All @@ -17,7 +18,9 @@ import {
GetCanonicalFileName,
getDefaultLikeExportNameFromDeclaration,
getDirectoryPath,
getEmitScriptTarget,
getLocalSymbolForExportDefault,
getNamesForExportedSymbol,
getNodeModulePathParts,
getPackageNameFromTypesPackageName,
getRegexFromPattern,
Expand All @@ -43,7 +46,6 @@ import {
Path,
pathContainsNodeModules,
Program,
ScriptTarget,
skipAlias,
SourceFile,
startsWith,
Expand Down Expand Up @@ -196,7 +198,7 @@ export function createCacheableExportInfoMap(host: CacheableExportInfoMapHost):
// get a better name.
const names = exportKind === ExportKind.Named || isExternalModuleSymbol(namedSymbol)
? unescapeLeadingUnderscores(symbolTableKey)
: getNamesForExportedSymbol(namedSymbol, checker, /*scriptTarget*/ undefined);
: getNamesForExportedSymbol(namedSymbol, /*scriptTarget*/ undefined);

const symbolName = typeof names === "string" ? names : names[0];
const capitalizedSymbolName = typeof names === "string" ? undefined : names[1];
Expand Down Expand Up @@ -556,21 +558,12 @@ function isImportableSymbol(symbol: Symbol, checker: TypeChecker) {
return !checker.isUndefinedSymbol(symbol) && !checker.isUnknownSymbol(symbol) && !isKnownSymbol(symbol) && !isPrivateIdentifierSymbol(symbol);
}

function getNamesForExportedSymbol(defaultExport: Symbol, checker: TypeChecker, scriptTarget: ScriptTarget | undefined) {
let names: string | string[] | undefined;
forEachNameOfDefaultExport(defaultExport, checker, scriptTarget, (name, capitalizedName) => {
names = capitalizedName ? [name, capitalizedName] : name;
return true;
});
return Debug.checkDefined(names);
}

/**
* @internal
* May call `cb` multiple times with the same name.
* Terminates when `cb` returns a truthy value.
*/
export function forEachNameOfDefaultExport<T>(defaultExport: Symbol, checker: TypeChecker, scriptTarget: ScriptTarget | undefined, cb: (name: string, capitalizedName?: string) => T | undefined): T | undefined {
export function forEachNameOfDefaultExport<T>(defaultExport: Symbol, checker: TypeChecker, compilerOptions: CompilerOptions, preferCapitalizedNames: boolean, cb: (name: string) => T | undefined): T | undefined {
let chain: Symbol[] | undefined;
let current: Symbol | undefined = defaultExport;

Expand All @@ -595,10 +588,7 @@ export function forEachNameOfDefaultExport<T>(defaultExport: Symbol, checker: Ty

for (const symbol of chain ?? emptyArray) {
if (symbol.parent && isExternalModuleSymbol(symbol.parent)) {
const final = cb(
moduleSymbolToValidIdentifier(symbol.parent, scriptTarget, /*forceCapitalize*/ false),
moduleSymbolToValidIdentifier(symbol.parent, scriptTarget, /*forceCapitalize*/ true),
);
const final = cb(moduleSymbolToValidIdentifier(symbol.parent, getEmitScriptTarget(compilerOptions), preferCapitalizedNames));
if (final) return final;
}
}
Expand Down
31 changes: 20 additions & 11 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4024,13 +4024,22 @@ export function firstOrOnly<T>(valueOrArray: T | readonly T[]): T {
return isArray(valueOrArray) ? first(valueOrArray) : valueOrArray;
}

/**
* If a type checker and multiple files are available, consider using `forEachNameOfDefaultExport`
* instead, which searches for names of re-exported defaults/namespaces in target files.
* @internal
*/
/** @internal */
export function getNamesForExportedSymbol(symbol: Symbol, scriptTarget: ScriptTarget | undefined): string | [lowercase: string, capitalized: string] {
if (needsNameFromDeclaration(symbol)) {
const fromDeclaration = getDefaultLikeExportNameFromDeclaration(symbol);
if (fromDeclaration) return fromDeclaration;
const fileNameCase = moduleSymbolToValidIdentifier(getSymbolParentOrFail(symbol), scriptTarget, /*forceCapitalize*/ false);
const capitalized = moduleSymbolToValidIdentifier(getSymbolParentOrFail(symbol), scriptTarget, /*forceCapitalize*/ true);
if (fileNameCase === capitalized) return fileNameCase;
return [fileNameCase, capitalized];
}
return symbol.name;
}

/** @internal */
export function getNameForExportedSymbol(symbol: Symbol, scriptTarget: ScriptTarget | undefined, preferCapitalized?: boolean) {
if (symbol.escapedName === InternalSymbolName.ExportEquals || symbol.escapedName === InternalSymbolName.Default) {
if (needsNameFromDeclaration(symbol)) {
// Names for default exports:
// - export default foo => foo
// - export { foo as default } => foo
Expand All @@ -4041,11 +4050,11 @@ export function getNameForExportedSymbol(symbol: Symbol, scriptTarget: ScriptTar
return symbol.name;
}

/**
* If a type checker and multiple files are available, consider using `forEachNameOfDefaultExport`
* instead, which searches for names of re-exported defaults/namespaces in target files.
* @internal
*/
function needsNameFromDeclaration(symbol: Symbol) {
return !(symbol.flags & SymbolFlags.Transient) && (symbol.escapedName === InternalSymbolName.ExportEquals || symbol.escapedName === InternalSymbolName.Default);
}

/** @internal */
export function getDefaultLikeExportNameFromDeclaration(symbol: Symbol): string | undefined {
return firstDefined(symbol.declarations, d => {
// "export default" in this case. See `ExportAssignment`for more details.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ Info seq [hh:mm:ss:mss] getCompletionData: Is inside comment: *
Info seq [hh:mm:ss:mss] getCompletionData: Get previous token: *
Info seq [hh:mm:ss:mss] getExportInfoMap: cache miss or empty; calculating new results
Info seq [hh:mm:ss:mss] getExportInfoMap: done in * ms
Info seq [hh:mm:ss:mss] collectAutoImports: resolved 0 module specifiers, plus 0 ambient and 4 from cache
Info seq [hh:mm:ss:mss] collectAutoImports: resolved 0 module specifiers, plus 0 ambient and 3 from cache
Info seq [hh:mm:ss:mss] collectAutoImports: response is incomplete
Info seq [hh:mm:ss:mss] collectAutoImports: *
Info seq [hh:mm:ss:mss] getCompletionData: Semantic work: *
Expand Down Expand Up @@ -1048,19 +1048,6 @@ Info seq [hh:mm:ss:mss] response:
"fileName": "/third_party/marked/src/defaults.js"
}
},
{
"name": "defaults",
"kind": "property",
"kindModifiers": "",
"sortText": "16",
"hasAction": true,
"source": "/third_party/marked/src/defaults",
"data": {
"exportName": "export=",
"exportMapKey": "8 * defaults ",
"fileName": "/third_party/marked/src/defaults.js"
}
},
{
"name": "defaults",
"kind": "alias",
Expand Down Expand Up @@ -1265,7 +1252,7 @@ Info seq [hh:mm:ss:mss] getCompletionData: Get current token: *
Info seq [hh:mm:ss:mss] getCompletionData: Is inside comment: *
Info seq [hh:mm:ss:mss] getCompletionData: Get previous token: *
Info seq [hh:mm:ss:mss] getExportInfoMap: cache hit
Info seq [hh:mm:ss:mss] collectAutoImports: resolved 0 module specifiers, plus 0 ambient and 4 from cache
Info seq [hh:mm:ss:mss] collectAutoImports: resolved 0 module specifiers, plus 0 ambient and 3 from cache
Info seq [hh:mm:ss:mss] collectAutoImports: response is incomplete
Info seq [hh:mm:ss:mss] collectAutoImports: *
Info seq [hh:mm:ss:mss] getCompletionData: Semantic work: *
Expand Down Expand Up @@ -1951,19 +1938,6 @@ Info seq [hh:mm:ss:mss] response:
"fileName": "/third_party/marked/src/defaults.js"
}
},
{
"name": "defaults",
"kind": "property",
"kindModifiers": "",
"sortText": "16",
"hasAction": true,
"source": "/third_party/marked/src/defaults",
"data": {
"exportName": "export=",
"exportMapKey": "8 * defaults ",
"fileName": "/third_party/marked/src/defaults.js"
}
},
{
"name": "defaults",
"kind": "alias",
Expand Down
39 changes: 0 additions & 39 deletions tests/cases/fourslash/completionsImport_reExportDefault2.ts

This file was deleted.

0 comments on commit a19bf0c

Please sign in to comment.