Skip to content

Commit 1002594

Browse files
committed
refactor: let exportMapKey accepts bad symbol name
1 parent 08e9f17 commit 1002594

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
@@ -2383,7 +2383,7 @@ export class TestState {
23832383
}
23842384
Harness.Baseline.runBaseline(baselineFile, annotations + "\n\n" + stringify(result, (key, value) => {
23852385
return key === "exportMapKey"
2386-
? value.replace(/\|[0-9]+/g, "|*")
2386+
? value.replace(/ \d+ /g, " * ")
23872387
: value;
23882388
}));
23892389
}

src/services/codefixes/importFixes.ts

Lines changed: 2 additions & 1 deletion
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,
@@ -503,7 +504,7 @@ interface FixAddToExistingImportInfo {
503504
export function getImportCompletionAction(
504505
targetSymbol: Symbol,
505506
moduleSymbol: Symbol,
506-
exportMapKey: string | undefined,
507+
exportMapKey: ExportMapInfoKey | undefined,
507508
sourceFile: SourceFile,
508509
symbolName: string,
509510
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,
@@ -482,14 +483,14 @@ interface SymbolOriginInfoExport extends SymbolOriginInfo {
482483
moduleSymbol: Symbol;
483484
isDefaultExport: boolean;
484485
exportName: string;
485-
exportMapKey: string;
486+
exportMapKey: ExportMapInfoKey;
486487
}
487488

488489
interface SymbolOriginInfoResolvedExport extends SymbolOriginInfo {
489490
symbolName: string;
490491
moduleSymbol: Symbol;
491492
exportName: string;
492-
exportMapKey?: string;
493+
exportMapKey?: ExportMapInfoKey;
493494
moduleSpecifier: string;
494495
}
495496

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,
@@ -1388,7 +1389,7 @@ export interface CompletionEntryDataAutoImport {
13881389
* in the case of InternalSymbolName.ExportEquals and InternalSymbolName.Default.
13891390
*/
13901391
exportName: string;
1391-
exportMapKey?: string;
1392+
exportMapKey?: ExportMapInfoKey;
13921393
moduleSpecifier?: string;
13931394
/** The file name declaring the export's module symbol, if it was an external module */
13941395
fileName?: string;
@@ -1399,7 +1400,7 @@ export interface CompletionEntryDataAutoImport {
13991400
}
14001401

14011402
export interface CompletionEntryDataUnresolved extends CompletionEntryDataAutoImport {
1402-
exportMapKey: string;
1403+
exportMapKey: ExportMapInfoKey;
14031404
}
14041405

14051406
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
@@ -10813,7 +10813,7 @@ declare namespace ts {
1081310813
* in the case of InternalSymbolName.ExportEquals and InternalSymbolName.Default.
1081410814
*/
1081510815
exportName: string;
10816-
exportMapKey?: string;
10816+
exportMapKey?: ExportMapInfoKey;
1081710817
moduleSpecifier?: string;
1081810818
/** The file name declaring the export's module symbol, if it was an external module */
1081910819
fileName?: string;
@@ -10823,7 +10823,7 @@ declare namespace ts {
1082310823
isPackageJsonImport?: true;
1082410824
}
1082510825
interface CompletionEntryDataUnresolved extends CompletionEntryDataAutoImport {
10826-
exportMapKey: string;
10826+
exportMapKey: ExportMapInfoKey;
1082710827
}
1082810828
interface CompletionEntryDataResolved extends CompletionEntryDataAutoImport {
1082910829
moduleSpecifier: string;
@@ -11138,6 +11138,9 @@ declare namespace ts {
1113811138
span: TextSpan;
1113911139
preferences: UserPreferences;
1114011140
}
11141+
type ExportMapInfoKey = string & {
11142+
__exportInfoKey: void;
11143+
};
1114111144
/** The classifier is used for syntactic highlighting in editors via the TSServer */
1114211145
function createClassifier(): Classifier;
1114311146
interface DocumentHighlights {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6837,7 +6837,7 @@ declare namespace ts {
68376837
* in the case of InternalSymbolName.ExportEquals and InternalSymbolName.Default.
68386838
*/
68396839
exportName: string;
6840-
exportMapKey?: string;
6840+
exportMapKey?: ExportMapInfoKey;
68416841
moduleSpecifier?: string;
68426842
/** The file name declaring the export's module symbol, if it was an external module */
68436843
fileName?: string;
@@ -6847,7 +6847,7 @@ declare namespace ts {
68476847
isPackageJsonImport?: true;
68486848
}
68496849
interface CompletionEntryDataUnresolved extends CompletionEntryDataAutoImport {
6850-
exportMapKey: string;
6850+
exportMapKey: ExportMapInfoKey;
68516851
}
68526852
interface CompletionEntryDataResolved extends CompletionEntryDataAutoImport {
68536853
moduleSpecifier: string;
@@ -7162,6 +7162,9 @@ declare namespace ts {
71627162
span: TextSpan;
71637163
preferences: UserPreferences;
71647164
}
7165+
type ExportMapInfoKey = string & {
7166+
__exportInfoKey: void;
7167+
};
71657168
/** The classifier is used for syntactic highlighting in editors via the TSServer */
71667169
function createClassifier(): Classifier;
71677170
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
}

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

Lines changed: 3 additions & 3 deletions
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)