Skip to content

Commit a19bf0c

Browse files
committed
Revert "[DO NOT MERGE UNTIL 5.6] Fix re-exported defaults in ExportInfoMap (microsoft#58837)"
This reverts commit 1948e92.
1 parent b258429 commit a19bf0c

File tree

5 files changed

+30
-96
lines changed

5 files changed

+30
-96
lines changed

src/services/codefixes/importFixes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ function getNewImportFixes(
11871187
const exportEquals = checker.resolveExternalModuleSymbol(exportInfo.moduleSymbol);
11881188
let namespacePrefix;
11891189
if (exportEquals !== exportInfo.moduleSymbol) {
1190-
namespacePrefix = forEachNameOfDefaultExport(exportEquals, checker, getEmitScriptTarget(compilerOptions), identity)!;
1190+
namespacePrefix = forEachNameOfDefaultExport(exportEquals, checker, compilerOptions, /*preferCapitalizedNames*/ false, identity)!;
11911191
}
11921192
namespacePrefix ||= moduleSymbolToValidIdentifier(
11931193
exportInfo.moduleSymbol,
@@ -1544,7 +1544,7 @@ function getExportInfos(
15441544
if (
15451545
defaultInfo
15461546
&& symbolFlagsHaveMeaning(checker.getSymbolFlags(defaultInfo.symbol), currentTokenMeaning)
1547-
&& forEachNameOfDefaultExport(defaultInfo.symbol, checker, getEmitScriptTarget(compilerOptions), (name, capitalizedName) => (isJsxTagName ? capitalizedName ?? name : name) === symbolName)
1547+
&& forEachNameOfDefaultExport(defaultInfo.symbol, checker, compilerOptions, isJsxTagName, name => name === symbolName)
15481548
) {
15491549
addSymbol(moduleSymbol, sourceFile, defaultInfo.symbol, defaultInfo.exportKind, program, isFromPackageJson);
15501550
}

src/services/exportInfoMap.ts

+6-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
append,
55
arrayIsEqualTo,
66
CancellationToken,
7+
CompilerOptions,
78
consumesNodeCoreModules,
89
createMultiMap,
910
Debug,
@@ -17,7 +18,9 @@ import {
1718
GetCanonicalFileName,
1819
getDefaultLikeExportNameFromDeclaration,
1920
getDirectoryPath,
21+
getEmitScriptTarget,
2022
getLocalSymbolForExportDefault,
23+
getNamesForExportedSymbol,
2124
getNodeModulePathParts,
2225
getPackageNameFromTypesPackageName,
2326
getRegexFromPattern,
@@ -43,7 +46,6 @@ import {
4346
Path,
4447
pathContainsNodeModules,
4548
Program,
46-
ScriptTarget,
4749
skipAlias,
4850
SourceFile,
4951
startsWith,
@@ -196,7 +198,7 @@ export function createCacheableExportInfoMap(host: CacheableExportInfoMapHost):
196198
// get a better name.
197199
const names = exportKind === ExportKind.Named || isExternalModuleSymbol(namedSymbol)
198200
? unescapeLeadingUnderscores(symbolTableKey)
199-
: getNamesForExportedSymbol(namedSymbol, checker, /*scriptTarget*/ undefined);
201+
: getNamesForExportedSymbol(namedSymbol, /*scriptTarget*/ undefined);
200202

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

559-
function getNamesForExportedSymbol(defaultExport: Symbol, checker: TypeChecker, scriptTarget: ScriptTarget | undefined) {
560-
let names: string | string[] | undefined;
561-
forEachNameOfDefaultExport(defaultExport, checker, scriptTarget, (name, capitalizedName) => {
562-
names = capitalizedName ? [name, capitalizedName] : name;
563-
return true;
564-
});
565-
return Debug.checkDefined(names);
566-
}
567-
568561
/**
569562
* @internal
570563
* May call `cb` multiple times with the same name.
571564
* Terminates when `cb` returns a truthy value.
572565
*/
573-
export function forEachNameOfDefaultExport<T>(defaultExport: Symbol, checker: TypeChecker, scriptTarget: ScriptTarget | undefined, cb: (name: string, capitalizedName?: string) => T | undefined): T | undefined {
566+
export function forEachNameOfDefaultExport<T>(defaultExport: Symbol, checker: TypeChecker, compilerOptions: CompilerOptions, preferCapitalizedNames: boolean, cb: (name: string) => T | undefined): T | undefined {
574567
let chain: Symbol[] | undefined;
575568
let current: Symbol | undefined = defaultExport;
576569

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

596589
for (const symbol of chain ?? emptyArray) {
597590
if (symbol.parent && isExternalModuleSymbol(symbol.parent)) {
598-
const final = cb(
599-
moduleSymbolToValidIdentifier(symbol.parent, scriptTarget, /*forceCapitalize*/ false),
600-
moduleSymbolToValidIdentifier(symbol.parent, scriptTarget, /*forceCapitalize*/ true),
601-
);
591+
const final = cb(moduleSymbolToValidIdentifier(symbol.parent, getEmitScriptTarget(compilerOptions), preferCapitalizedNames));
602592
if (final) return final;
603593
}
604594
}

src/services/utilities.ts

+20-11
Original file line numberDiff line numberDiff line change
@@ -4024,13 +4024,22 @@ export function firstOrOnly<T>(valueOrArray: T | readonly T[]): T {
40244024
return isArray(valueOrArray) ? first(valueOrArray) : valueOrArray;
40254025
}
40264026

4027-
/**
4028-
* If a type checker and multiple files are available, consider using `forEachNameOfDefaultExport`
4029-
* instead, which searches for names of re-exported defaults/namespaces in target files.
4030-
* @internal
4031-
*/
4027+
/** @internal */
4028+
export function getNamesForExportedSymbol(symbol: Symbol, scriptTarget: ScriptTarget | undefined): string | [lowercase: string, capitalized: string] {
4029+
if (needsNameFromDeclaration(symbol)) {
4030+
const fromDeclaration = getDefaultLikeExportNameFromDeclaration(symbol);
4031+
if (fromDeclaration) return fromDeclaration;
4032+
const fileNameCase = moduleSymbolToValidIdentifier(getSymbolParentOrFail(symbol), scriptTarget, /*forceCapitalize*/ false);
4033+
const capitalized = moduleSymbolToValidIdentifier(getSymbolParentOrFail(symbol), scriptTarget, /*forceCapitalize*/ true);
4034+
if (fileNameCase === capitalized) return fileNameCase;
4035+
return [fileNameCase, capitalized];
4036+
}
4037+
return symbol.name;
4038+
}
4039+
4040+
/** @internal */
40324041
export function getNameForExportedSymbol(symbol: Symbol, scriptTarget: ScriptTarget | undefined, preferCapitalized?: boolean) {
4033-
if (symbol.escapedName === InternalSymbolName.ExportEquals || symbol.escapedName === InternalSymbolName.Default) {
4042+
if (needsNameFromDeclaration(symbol)) {
40344043
// Names for default exports:
40354044
// - export default foo => foo
40364045
// - export { foo as default } => foo
@@ -4041,11 +4050,11 @@ export function getNameForExportedSymbol(symbol: Symbol, scriptTarget: ScriptTar
40414050
return symbol.name;
40424051
}
40434052

4044-
/**
4045-
* If a type checker and multiple files are available, consider using `forEachNameOfDefaultExport`
4046-
* instead, which searches for names of re-exported defaults/namespaces in target files.
4047-
* @internal
4048-
*/
4053+
function needsNameFromDeclaration(symbol: Symbol) {
4054+
return !(symbol.flags & SymbolFlags.Transient) && (symbol.escapedName === InternalSymbolName.ExportEquals || symbol.escapedName === InternalSymbolName.Default);
4055+
}
4056+
4057+
/** @internal */
40494058
export function getDefaultLikeExportNameFromDeclaration(symbol: Symbol): string | undefined {
40504059
return firstDefined(symbol.declarations, d => {
40514060
// "export default" in this case. See `ExportAssignment`for more details.

tests/baselines/reference/tsserver/fourslashServer/completionsImport_jsModuleExportsAssignment.js

+2-28
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ Info seq [hh:mm:ss:mss] getCompletionData: Is inside comment: *
375375
Info seq [hh:mm:ss:mss] getCompletionData: Get previous token: *
376376
Info seq [hh:mm:ss:mss] getExportInfoMap: cache miss or empty; calculating new results
377377
Info seq [hh:mm:ss:mss] getExportInfoMap: done in * ms
378-
Info seq [hh:mm:ss:mss] collectAutoImports: resolved 0 module specifiers, plus 0 ambient and 4 from cache
378+
Info seq [hh:mm:ss:mss] collectAutoImports: resolved 0 module specifiers, plus 0 ambient and 3 from cache
379379
Info seq [hh:mm:ss:mss] collectAutoImports: response is incomplete
380380
Info seq [hh:mm:ss:mss] collectAutoImports: *
381381
Info seq [hh:mm:ss:mss] getCompletionData: Semantic work: *
@@ -1048,19 +1048,6 @@ Info seq [hh:mm:ss:mss] response:
10481048
"fileName": "/third_party/marked/src/defaults.js"
10491049
}
10501050
},
1051-
{
1052-
"name": "defaults",
1053-
"kind": "property",
1054-
"kindModifiers": "",
1055-
"sortText": "16",
1056-
"hasAction": true,
1057-
"source": "/third_party/marked/src/defaults",
1058-
"data": {
1059-
"exportName": "export=",
1060-
"exportMapKey": "8 * defaults ",
1061-
"fileName": "/third_party/marked/src/defaults.js"
1062-
}
1063-
},
10641051
{
10651052
"name": "defaults",
10661053
"kind": "alias",
@@ -1265,7 +1252,7 @@ Info seq [hh:mm:ss:mss] getCompletionData: Get current token: *
12651252
Info seq [hh:mm:ss:mss] getCompletionData: Is inside comment: *
12661253
Info seq [hh:mm:ss:mss] getCompletionData: Get previous token: *
12671254
Info seq [hh:mm:ss:mss] getExportInfoMap: cache hit
1268-
Info seq [hh:mm:ss:mss] collectAutoImports: resolved 0 module specifiers, plus 0 ambient and 4 from cache
1255+
Info seq [hh:mm:ss:mss] collectAutoImports: resolved 0 module specifiers, plus 0 ambient and 3 from cache
12691256
Info seq [hh:mm:ss:mss] collectAutoImports: response is incomplete
12701257
Info seq [hh:mm:ss:mss] collectAutoImports: *
12711258
Info seq [hh:mm:ss:mss] getCompletionData: Semantic work: *
@@ -1951,19 +1938,6 @@ Info seq [hh:mm:ss:mss] response:
19511938
"fileName": "/third_party/marked/src/defaults.js"
19521939
}
19531940
},
1954-
{
1955-
"name": "defaults",
1956-
"kind": "property",
1957-
"kindModifiers": "",
1958-
"sortText": "16",
1959-
"hasAction": true,
1960-
"source": "/third_party/marked/src/defaults",
1961-
"data": {
1962-
"exportName": "export=",
1963-
"exportMapKey": "8 * defaults ",
1964-
"fileName": "/third_party/marked/src/defaults.js"
1965-
}
1966-
},
19671941
{
19681942
"name": "defaults",
19691943
"kind": "alias",

tests/cases/fourslash/completionsImport_reExportDefault2.ts

-39
This file was deleted.

0 commit comments

Comments
 (0)