refactor: let exportMapKey accepts bad symbol name (#54678)

This commit is contained in:
Jack Works
2023-09-14 00:39:05 +08:00
committed by GitHub
parent 3ade5022d7
commit e7bb0ebfac
30 changed files with 4713 additions and 4701 deletions
+1 -1
View File
@@ -2427,7 +2427,7 @@ export class TestState {
baselineFile,
annotations + "\n\n" + stringify(result, (key, value) => {
return key === "exportMapKey"
? value.replace(/\|[0-9]+/g, "|*")
? value.replace(/ \d+ /g, " * ")
: value;
}),
);
+2 -1
View File
@@ -25,6 +25,7 @@ import {
emptyArray,
every,
ExportKind,
ExportMapInfoKey,
factory,
first,
firstDefined,
@@ -514,7 +515,7 @@ interface FixAddToExistingImportInfo {
export function getImportCompletionAction(
targetSymbol: Symbol,
moduleSymbol: Symbol,
exportMapKey: string | undefined,
exportMapKey: ExportMapInfoKey | undefined,
sourceFile: SourceFile,
symbolName: string,
isJsxTagName: boolean,
+3 -2
View File
@@ -58,6 +58,7 @@ import {
escapeSnippetText,
every,
ExportKind,
ExportMapInfoKey,
Expression,
ExpressionWithTypeArguments,
factory,
@@ -485,14 +486,14 @@ interface SymbolOriginInfoExport extends SymbolOriginInfo {
moduleSymbol: Symbol;
isDefaultExport: boolean;
exportName: string;
exportMapKey: string;
exportMapKey: ExportMapInfoKey;
}
interface SymbolOriginInfoResolvedExport extends SymbolOriginInfo {
symbolName: string;
moduleSymbol: Symbol;
exportName: string;
exportMapKey?: string;
exportMapKey?: ExportMapInfoKey;
moduleSpecifier: string;
}
+15 -9
View File
@@ -112,8 +112,8 @@ export interface ExportInfoMap {
isUsableByFile(importingFile: Path): boolean;
clear(): void;
add(importingFile: Path, symbol: Symbol, key: __String, moduleSymbol: Symbol, moduleFile: SourceFile | undefined, exportKind: ExportKind, isFromPackageJson: boolean, checker: TypeChecker): void;
get(importingFile: Path, key: string): readonly SymbolExportInfo[] | undefined;
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;
get(importingFile: Path, key: ExportMapInfoKey): readonly SymbolExportInfo[] | undefined;
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;
releaseSymbols(): void;
isEmpty(): boolean;
/** @returns Whether the change resulted in the cache being cleared */
@@ -127,10 +127,11 @@ export interface CacheableExportInfoMapHost {
getGlobalTypingsCacheLocation(): string | undefined;
}
export type ExportMapInfoKey = string & { __exportInfoKey: void; };
/** @internal */
export function createCacheableExportInfoMap(host: CacheableExportInfoMapHost): ExportInfoMap {
let exportInfoId = 1;
const exportInfo = createMultiMap<string, CachedSymbolExportInfo>();
const exportInfo = createMultiMap<ExportMapInfoKey, CachedSymbolExportInfo>();
const symbols = new Map<number, [symbol: Symbol, moduleSymbol: Symbol]>();
/**
* Key: node_modules package name (no @types).
@@ -266,7 +267,7 @@ export function createCacheableExportInfoMap(host: CacheableExportInfoMapHost):
},
};
if (Debug.isDebugging) {
Object.defineProperty(cache, "__cache", { get: () => exportInfo });
Object.defineProperty(cache, "__cache", { value: exportInfo });
}
return cache;
@@ -309,14 +310,19 @@ export function createCacheableExportInfoMap(host: CacheableExportInfoMapHost):
};
}
function key(importedName: string, symbol: Symbol, ambientModuleName: string | undefined, checker: TypeChecker): string {
function key(importedName: string, symbol: Symbol, ambientModuleName: string | undefined, checker: TypeChecker) {
const moduleKey = ambientModuleName || "";
return `${importedName}|${getSymbolId(skipAlias(symbol, checker))}|${moduleKey}`;
return `${importedName.length} ${getSymbolId(skipAlias(symbol, checker))} ${importedName} ${moduleKey}` as ExportMapInfoKey;
}
function parseKey(key: string) {
const symbolName = key.substring(0, key.indexOf("|"));
const moduleKey = key.substring(key.lastIndexOf("|") + 1);
function parseKey(key: ExportMapInfoKey) {
const firstSpace = key.indexOf(" ");
const secondSpace = key.indexOf(" ", firstSpace + 1);
const symbolNameLength = parseInt(key.substring(0, firstSpace), 10);
const data = key.substring(secondSpace + 1);
const symbolName = data.substring(0, symbolNameLength);
const moduleKey = data.substring(symbolNameLength + 1);
const ambientModuleName = moduleKey === "" ? undefined : moduleKey;
return { symbolName, ambientModuleName };
}
+3 -2
View File
@@ -9,6 +9,7 @@ import {
DocumentPositionMapper,
EmitOutput,
ExportInfoMap,
ExportMapInfoKey,
FileReference,
GetEffectiveTypeRootsHost,
HasChangedAutomaticTypeDirectiveNames,
@@ -1390,7 +1391,7 @@ export interface CompletionEntryDataAutoImport {
* in the case of InternalSymbolName.ExportEquals and InternalSymbolName.Default.
*/
exportName: string;
exportMapKey?: string;
exportMapKey?: ExportMapInfoKey;
moduleSpecifier?: string;
/** The file name declaring the export's module symbol, if it was an external module */
fileName?: string;
@@ -1401,7 +1402,7 @@ export interface CompletionEntryDataAutoImport {
}
export interface CompletionEntryDataUnresolved extends CompletionEntryDataAutoImport {
exportMapKey: string;
exportMapKey: ExportMapInfoKey;
}
export interface CompletionEntryDataResolved extends CompletionEntryDataAutoImport {
+1 -1
View File
@@ -160,7 +160,7 @@ function sanitizeLog(s: string): string {
s = s.replace(/collectAutoImports: \d+(?:\.\d+)?/g, `collectAutoImports: *`);
s = s.replace(/continuePreviousIncompleteResponse: \d+(?:\.\d+)?/g, `continuePreviousIncompleteResponse: *`);
s = s.replace(/dependencies in \d+(?:\.\d+)?/g, `dependencies in *`);
s = s.replace(/"exportMapKey":\s*"[_$a-zA-Z][_$_$a-zA-Z0-9]*\|\d+\|/g, match => match.replace(/\|\d+\|/, `|*|`));
s = s.replace(/"exportMapKey":\s*"\d+ \d+ /g, match => match.replace(/ \d+ /, ` * `));
return s;
}