Skip to content

Commit e7bb0eb

Browse files
authored
refactor: let exportMapKey accepts bad symbol name (#54678)
1 parent 3ade502 commit e7bb0eb

File tree

30 files changed

+4713
-4701
lines changed

30 files changed

+4713
-4701
lines changed

src/harness/fourslashImpl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2427,7 +2427,7 @@ export class TestState {
24272427
baselineFile,
24282428
annotations + "\n\n" + stringify(result, (key, value) => {
24292429
return key === "exportMapKey"
2430-
? value.replace(/\|[0-9]+/g, "|*")
2430+
? value.replace(/ \d+ /g, " * ")
24312431
: value;
24322432
}),
24332433
);

src/services/codefixes/importFixes.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
emptyArray,
2626
every,
2727
ExportKind,
28+
ExportMapInfoKey,
2829
factory,
2930
first,
3031
firstDefined,
@@ -514,7 +515,7 @@ interface FixAddToExistingImportInfo {
514515
export function getImportCompletionAction(
515516
targetSymbol: Symbol,
516517
moduleSymbol: Symbol,
517-
exportMapKey: string | undefined,
518+
exportMapKey: ExportMapInfoKey | undefined,
518519
sourceFile: SourceFile,
519520
symbolName: string,
520521
isJsxTagName: boolean,

src/services/completions.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import {
5858
escapeSnippetText,
5959
every,
6060
ExportKind,
61+
ExportMapInfoKey,
6162
Expression,
6263
ExpressionWithTypeArguments,
6364
factory,
@@ -485,14 +486,14 @@ interface SymbolOriginInfoExport extends SymbolOriginInfo {
485486
moduleSymbol: Symbol;
486487
isDefaultExport: boolean;
487488
exportName: string;
488-
exportMapKey: string;
489+
exportMapKey: ExportMapInfoKey;
489490
}
490491

491492
interface SymbolOriginInfoResolvedExport extends SymbolOriginInfo {
492493
symbolName: string;
493494
moduleSymbol: Symbol;
494495
exportName: string;
495-
exportMapKey?: string;
496+
exportMapKey?: ExportMapInfoKey;
496497
moduleSpecifier: string;
497498
}
498499

src/services/exportInfoMap.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ export interface ExportInfoMap {
112112
isUsableByFile(importingFile: Path): boolean;
113113
clear(): void;
114114
add(importingFile: Path, symbol: Symbol, key: __String, moduleSymbol: Symbol, moduleFile: SourceFile | undefined, exportKind: ExportKind, isFromPackageJson: boolean, checker: TypeChecker): void;
115-
get(importingFile: Path, key: string): readonly SymbolExportInfo[] | undefined;
116-
search<T>(importingFile: Path, preferCapitalized: boolean, matches: (name: string, targetFlags: SymbolFlags) => boolean, action: (info: readonly SymbolExportInfo[], symbolName: string, isFromAmbientModule: boolean, key: string) => T | undefined): T | undefined;
115+
get(importingFile: Path, key: ExportMapInfoKey): readonly SymbolExportInfo[] | undefined;
116+
search<T>(importingFile: Path, preferCapitalized: boolean, matches: (name: string, targetFlags: SymbolFlags) => boolean, action: (info: readonly SymbolExportInfo[], symbolName: string, isFromAmbientModule: boolean, key: ExportMapInfoKey) => T | undefined): T | undefined;
117117
releaseSymbols(): void;
118118
isEmpty(): boolean;
119119
/** @returns Whether the change resulted in the cache being cleared */
@@ -127,10 +127,11 @@ export interface CacheableExportInfoMapHost {
127127
getGlobalTypingsCacheLocation(): string | undefined;
128128
}
129129

130+
export type ExportMapInfoKey = string & { __exportInfoKey: void; };
130131
/** @internal */
131132
export function createCacheableExportInfoMap(host: CacheableExportInfoMapHost): ExportInfoMap {
132133
let exportInfoId = 1;
133-
const exportInfo = createMultiMap<string, CachedSymbolExportInfo>();
134+
const exportInfo = createMultiMap<ExportMapInfoKey, CachedSymbolExportInfo>();
134135
const symbols = new Map<number, [symbol: Symbol, moduleSymbol: Symbol]>();
135136
/**
136137
* Key: node_modules package name (no @types).
@@ -266,7 +267,7 @@ export function createCacheableExportInfoMap(host: CacheableExportInfoMapHost):
266267
},
267268
};
268269
if (Debug.isDebugging) {
269-
Object.defineProperty(cache, "__cache", { get: () => exportInfo });
270+
Object.defineProperty(cache, "__cache", { value: exportInfo });
270271
}
271272
return cache;
272273

@@ -309,14 +310,19 @@ export function createCacheableExportInfoMap(host: CacheableExportInfoMapHost):
309310
};
310311
}
311312

312-
function key(importedName: string, symbol: Symbol, ambientModuleName: string | undefined, checker: TypeChecker): string {
313+
function key(importedName: string, symbol: Symbol, ambientModuleName: string | undefined, checker: TypeChecker) {
313314
const moduleKey = ambientModuleName || "";
314-
return `${importedName}|${getSymbolId(skipAlias(symbol, checker))}|${moduleKey}`;
315+
return `${importedName.length} ${getSymbolId(skipAlias(symbol, checker))} ${importedName} ${moduleKey}` as ExportMapInfoKey;
315316
}
316317

317-
function parseKey(key: string) {
318-
const symbolName = key.substring(0, key.indexOf("|"));
319-
const moduleKey = key.substring(key.lastIndexOf("|") + 1);
318+
function parseKey(key: ExportMapInfoKey) {
319+
const firstSpace = key.indexOf(" ");
320+
const secondSpace = key.indexOf(" ", firstSpace + 1);
321+
const symbolNameLength = parseInt(key.substring(0, firstSpace), 10);
322+
323+
const data = key.substring(secondSpace + 1);
324+
const symbolName = data.substring(0, symbolNameLength);
325+
const moduleKey = data.substring(symbolNameLength + 1);
320326
const ambientModuleName = moduleKey === "" ? undefined : moduleKey;
321327
return { symbolName, ambientModuleName };
322328
}

src/services/types.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
DocumentPositionMapper,
1010
EmitOutput,
1111
ExportInfoMap,
12+
ExportMapInfoKey,
1213
FileReference,
1314
GetEffectiveTypeRootsHost,
1415
HasChangedAutomaticTypeDirectiveNames,
@@ -1390,7 +1391,7 @@ export interface CompletionEntryDataAutoImport {
13901391
* in the case of InternalSymbolName.ExportEquals and InternalSymbolName.Default.
13911392
*/
13921393
exportName: string;
1393-
exportMapKey?: string;
1394+
exportMapKey?: ExportMapInfoKey;
13941395
moduleSpecifier?: string;
13951396
/** The file name declaring the export's module symbol, if it was an external module */
13961397
fileName?: string;
@@ -1401,7 +1402,7 @@ export interface CompletionEntryDataAutoImport {
14011402
}
14021403

14031404
export interface CompletionEntryDataUnresolved extends CompletionEntryDataAutoImport {
1404-
exportMapKey: string;
1405+
exportMapKey: ExportMapInfoKey;
14051406
}
14061407

14071408
export interface CompletionEntryDataResolved extends CompletionEntryDataAutoImport {

src/testRunner/unittests/helpers/tsserver.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function sanitizeLog(s: string): string {
160160
s = s.replace(/collectAutoImports: \d+(?:\.\d+)?/g, `collectAutoImports: *`);
161161
s = s.replace(/continuePreviousIncompleteResponse: \d+(?:\.\d+)?/g, `continuePreviousIncompleteResponse: *`);
162162
s = s.replace(/dependencies in \d+(?:\.\d+)?/g, `dependencies in *`);
163-
s = s.replace(/"exportMapKey":\s*"[_$a-zA-Z][_$_$a-zA-Z0-9]*\|\d+\|/g, match => match.replace(/\|\d+\|/, `|*|`));
163+
s = s.replace(/"exportMapKey":\s*"\d+ \d+ /g, match => match.replace(/ \d+ /, ` * `));
164164
return s;
165165
}
166166

tests/baselines/reference/api/typescript.d.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -11040,7 +11040,7 @@ declare namespace ts {
1104011040
* in the case of InternalSymbolName.ExportEquals and InternalSymbolName.Default.
1104111041
*/
1104211042
exportName: string;
11043-
exportMapKey?: string;
11043+
exportMapKey?: ExportMapInfoKey;
1104411044
moduleSpecifier?: string;
1104511045
/** The file name declaring the export's module symbol, if it was an external module */
1104611046
fileName?: string;
@@ -11050,7 +11050,7 @@ declare namespace ts {
1105011050
isPackageJsonImport?: true;
1105111051
}
1105211052
interface CompletionEntryDataUnresolved extends CompletionEntryDataAutoImport {
11053-
exportMapKey: string;
11053+
exportMapKey: ExportMapInfoKey;
1105411054
}
1105511055
interface CompletionEntryDataResolved extends CompletionEntryDataAutoImport {
1105611056
moduleSpecifier: string;
@@ -11365,6 +11365,9 @@ declare namespace ts {
1136511365
span: TextSpan;
1136611366
preferences: UserPreferences;
1136711367
}
11368+
type ExportMapInfoKey = string & {
11369+
__exportInfoKey: void;
11370+
};
1136811371
/** The classifier is used for syntactic highlighting in editors via the TSServer */
1136911372
function createClassifier(): Classifier;
1137011373
interface DocumentHighlights {

tests/baselines/reference/importStatementCompletions3.baseline

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"isImportStatementCompletion": true,
5050
"data": {
5151
"exportName": "foo",
52-
"exportMapKey": "foo|*|",
52+
"exportMapKey": "3 * foo ",
5353
"moduleSpecifier": "./$foo",
5454
"fileName": "/tests/cases/fourslash/$foo.ts"
5555
},

tests/baselines/reference/tsserver/autoImportProvider/Shared-source-files-between-AutoImportProvider-and-main-program.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ Info seq [hh:mm:ss:mss] response:
558558
"source": "/node_modules/@types/node/index",
559559
"data": {
560560
"exportName": "Stats",
561-
"exportMapKey": "Stats|*|",
561+
"exportMapKey": "5 * Stats ",
562562
"fileName": "/node_modules/@types/node/index.d.ts"
563563
}
564564
},
@@ -572,7 +572,7 @@ Info seq [hh:mm:ss:mss] response:
572572
"isPackageJsonImport": true,
573573
"data": {
574574
"exportName": "Volume",
575-
"exportMapKey": "Volume|*|",
575+
"exportMapKey": "6 * Volume ",
576576
"fileName": "/node_modules/memfs/lib/index.d.ts",
577577
"isPackageJsonImport": true
578578
}

tests/baselines/reference/tsserver/completions/works.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Info seq [hh:mm:ss:mss] response:
175175
"source": "/a",
176176
"data": {
177177
"exportName": "foo",
178-
"exportMapKey": "foo|*|",
178+
"exportMapKey": "3 * foo ",
179179
"fileName": "/a.ts"
180180
}
181181
}
@@ -201,7 +201,7 @@ Info seq [hh:mm:ss:mss] request:
201201
"data": {
202202
"exportName": "foo",
203203
"fileName": "/a.ts",
204-
"exportMapKey": "foo|*|"
204+
"exportMapKey": "3 * foo "
205205
}
206206
}
207207
]
@@ -302,7 +302,7 @@ Info seq [hh:mm:ss:mss] request:
302302
"data": {
303303
"exportName": "foo",
304304
"fileName": "/a.ts",
305-
"exportMapKey": "foo|*|"
305+
"exportMapKey": "3 * foo "
306306
}
307307
}
308308
]

0 commit comments

Comments
 (0)