Skip to content

Commit 07d746c

Browse files
committed
refactor: let exportMapKey accepts bad symbol name
1 parent e1a9290 commit 07d746c

File tree

31 files changed

+4718
-4703
lines changed

31 files changed

+4718
-4703
lines changed

src/harness/fourslashImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@ export class TestState {
23742374
}
23752375
Harness.Baseline.runBaseline(baselineFile, annotations + "\n\n" + stringify(result, (key, value) => {
23762376
return key === "exportMapKey"
2377-
? value.replace(/\|[0-9]+/g, "|*")
2377+
? value.replace(/ \d+ /g, " * ")
23782378
: value;
23792379
}));
23802380
}

src/services/codefixes/importFixes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
emptyArray,
2525
every,
2626
ExportKind,
27+
ExportMapInfoKey,
2728
factory,
2829
first,
2930
firstDefined,
@@ -492,7 +493,7 @@ interface FixAddToExistingImportInfo {
492493
export function getImportCompletionAction(
493494
targetSymbol: Symbol,
494495
moduleSymbol: Symbol,
495-
exportMapKey: string | undefined,
496+
exportMapKey: ExportMapInfoKey | undefined,
496497
sourceFile: SourceFile,
497498
symbolName: string,
498499
isJsxTagName: boolean,

src/services/completions.ts

Lines changed: 3 additions & 2 deletions
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,
@@ -475,14 +476,14 @@ interface SymbolOriginInfoExport extends SymbolOriginInfo {
475476
moduleSymbol: Symbol;
476477
isDefaultExport: boolean;
477478
exportName: string;
478-
exportMapKey: string;
479+
exportMapKey: ExportMapInfoKey;
479480
}
480481

481482
interface SymbolOriginInfoResolvedExport extends SymbolOriginInfo {
482483
symbolName: string;
483484
moduleSymbol: Symbol;
484485
exportName: string;
485-
exportMapKey?: string;
486+
exportMapKey?: ExportMapInfoKey;
486487
moduleSpecifier: string;
487488
}
488489

src/services/exportInfoMap.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ export interface ExportInfoMap {
113113
isUsableByFile(importingFile: Path): boolean;
114114
clear(): void;
115115
add(importingFile: Path, symbol: Symbol, key: __String, moduleSymbol: Symbol, moduleFile: SourceFile | undefined, exportKind: ExportKind, isFromPackageJson: boolean, checker: TypeChecker): void;
116-
get(importingFile: Path, key: string): readonly SymbolExportInfo[] | undefined;
117-
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;
116+
get(importingFile: Path, key: ExportMapInfoKey): readonly SymbolExportInfo[] | undefined;
117+
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;
118118
releaseSymbols(): void;
119119
isEmpty(): boolean;
120120
/** @returns Whether the change resulted in the cache being cleared */
@@ -128,10 +128,11 @@ export interface CacheableExportInfoMapHost {
128128
getGlobalTypingsCacheLocation(): string | undefined;
129129
}
130130

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

@@ -306,14 +307,19 @@ export function createCacheableExportInfoMap(host: CacheableExportInfoMapHost):
306307
};
307308
}
308309

309-
function key(importedName: string, symbol: Symbol, ambientModuleName: string | undefined, checker: TypeChecker): string {
310+
function key(importedName: string, symbol: Symbol, ambientModuleName: string | undefined, checker: TypeChecker) {
310311
const moduleKey = ambientModuleName || "";
311-
return `${importedName}|${getSymbolId(skipAlias(symbol, checker))}|${moduleKey}`;
312+
return `${importedName.length} ${getSymbolId(skipAlias(symbol, checker))} ${importedName} ${moduleKey}` as ExportMapInfoKey;
312313
}
313314

314-
function parseKey(key: string) {
315-
const symbolName = key.substring(0, key.indexOf("|"));
316-
const moduleKey = key.substring(key.lastIndexOf("|") + 1);
315+
function parseKey(key: ExportMapInfoKey) {
316+
const firstSpace = key.indexOf(" ");
317+
const secondSpace = key.indexOf(" ", firstSpace + 1);
318+
const symbolNameLength = parseInt(key.substring(0, firstSpace), 10);
319+
320+
const data = key.substring(secondSpace + 1);
321+
const symbolName = data.substring(0, symbolNameLength);
322+
const moduleKey = data.substring(symbolNameLength + 1);
317323
const ambientModuleName = moduleKey === "" ? undefined : moduleKey;
318324
return { symbolName, ambientModuleName };
319325
}

src/services/types.ts

Lines changed: 3 additions & 2 deletions
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,
@@ -1380,7 +1381,7 @@ export interface CompletionEntryDataAutoImport {
13801381
* in the case of InternalSymbolName.ExportEquals and InternalSymbolName.Default.
13811382
*/
13821383
exportName: string;
1383-
exportMapKey?: string;
1384+
exportMapKey?: ExportMapInfoKey;
13841385
moduleSpecifier?: string;
13851386
/** The file name declaring the export's module symbol, if it was an external module */
13861387
fileName?: string;
@@ -1391,7 +1392,7 @@ export interface CompletionEntryDataAutoImport {
13911392
}
13921393

13931394
export interface CompletionEntryDataUnresolved extends CompletionEntryDataAutoImport {
1394-
exportMapKey: string;
1395+
exportMapKey: ExportMapInfoKey;
13951396
}
13961397

13971398
export interface CompletionEntryDataResolved extends CompletionEntryDataAutoImport {

src/testRunner/unittests/helpers/tsserver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function sanitizeLog(s: string): string {
155155
s = s.replace(/collectAutoImports: \d+(?:\.\d+)?/g, `collectAutoImports: *`);
156156
s = s.replace(/continuePreviousIncompleteResponse: \d+(?:\.\d+)?/g, `continuePreviousIncompleteResponse: *`);
157157
s = s.replace(/dependencies in \d+(?:\.\d+)?/g, `dependencies in *`);
158-
s = s.replace(/\"exportMapKey\"\:\s*\"[_$a-zA-Z][_$_$a-zA-Z0-9]*\|\d+\|/g, match => match.replace(/\|\d+\|/, `|*|`));
158+
s = s.replace(/\"exportMapKey\"\:\s*\"\d+ \d+ /g, match => match.replace(/ \d+ /, ` * `));
159159
return s;
160160
}
161161

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10786,7 +10786,7 @@ declare namespace ts {
1078610786
* in the case of InternalSymbolName.ExportEquals and InternalSymbolName.Default.
1078710787
*/
1078810788
exportName: string;
10789-
exportMapKey?: string;
10789+
exportMapKey?: ExportMapInfoKey;
1079010790
moduleSpecifier?: string;
1079110791
/** The file name declaring the export's module symbol, if it was an external module */
1079210792
fileName?: string;
@@ -10796,7 +10796,7 @@ declare namespace ts {
1079610796
isPackageJsonImport?: true;
1079710797
}
1079810798
interface CompletionEntryDataUnresolved extends CompletionEntryDataAutoImport {
10799-
exportMapKey: string;
10799+
exportMapKey: ExportMapInfoKey;
1080010800
}
1080110801
interface CompletionEntryDataResolved extends CompletionEntryDataAutoImport {
1080210802
moduleSpecifier: string;
@@ -11107,6 +11107,9 @@ declare namespace ts {
1110711107
span: TextSpan;
1110811108
preferences: UserPreferences;
1110911109
}
11110+
type ExportMapInfoKey = string & {
11111+
__exportInfoKey: void;
11112+
};
1111011113
/** The classifier is used for syntactic highlighting in editors via the TSServer */
1111111114
function createClassifier(): Classifier;
1111211115
interface DocumentHighlights {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6817,7 +6817,7 @@ declare namespace ts {
68176817
* in the case of InternalSymbolName.ExportEquals and InternalSymbolName.Default.
68186818
*/
68196819
exportName: string;
6820-
exportMapKey?: string;
6820+
exportMapKey?: ExportMapInfoKey;
68216821
moduleSpecifier?: string;
68226822
/** The file name declaring the export's module symbol, if it was an external module */
68236823
fileName?: string;
@@ -6827,7 +6827,7 @@ declare namespace ts {
68276827
isPackageJsonImport?: true;
68286828
}
68296829
interface CompletionEntryDataUnresolved extends CompletionEntryDataAutoImport {
6830-
exportMapKey: string;
6830+
exportMapKey: ExportMapInfoKey;
68316831
}
68326832
interface CompletionEntryDataResolved extends CompletionEntryDataAutoImport {
68336833
moduleSpecifier: string;
@@ -7138,6 +7138,9 @@ declare namespace ts {
71387138
span: TextSpan;
71397139
preferences: UserPreferences;
71407140
}
7141+
type ExportMapInfoKey = string & {
7142+
__exportInfoKey: void;
7143+
};
71417144
/** The classifier is used for syntactic highlighting in editors via the TSServer */
71427145
function createClassifier(): Classifier;
71437146
interface DocumentHighlights {

tests/baselines/reference/importStatementCompletions3.baseline

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)