Skip to content

Commit 9013665

Browse files
author
Andy
authored
Replace FileMap with Map where there is no keyMapper (#16724)
* Replace FileMap with Map where there is no keyMapper * Remove `toKey` and use `keyMapper` directly
1 parent 9260a39 commit 9013665

File tree

13 files changed

+65
-71
lines changed

13 files changed

+65
-71
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23266,10 +23266,10 @@ namespace ts {
2326623266
// this variable and functions that use it are deliberately moved here from the outer scope
2326723267
// to avoid scope pollution
2326823268
const resolvedTypeReferenceDirectives = host.getResolvedTypeReferenceDirectives();
23269-
let fileToDirective: FileMap<string>;
23269+
let fileToDirective: Map<string>;
2327023270
if (resolvedTypeReferenceDirectives) {
2327123271
// populate reverse mapping: file path -> type reference directive that was resolved to this file
23272-
fileToDirective = createFileMap<string>();
23272+
fileToDirective = createMap<string>();
2327323273
resolvedTypeReferenceDirectives.forEach((resolvedDirective, key) => {
2327423274
if (!resolvedDirective) {
2327523275
return;
@@ -23397,7 +23397,7 @@ namespace ts {
2339723397
// check that at least one declaration of top level symbol originates from type declaration file
2339823398
for (const decl of symbol.declarations) {
2339923399
const file = getSourceFileOfNode(decl);
23400-
if (fileToDirective.contains(file.path)) {
23400+
if (fileToDirective.has(file.path)) {
2340123401
return true;
2340223402
}
2340323403
}

src/compiler/core.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ namespace ts {
145145
};
146146
}
147147

148-
export function createFileMap<T>(keyMapper?: (key: string) => string): FileMap<T> {
148+
export function createFileMap<T>(keyMapper: (key: string) => string): FileMap<T> {
149149
const files = createMap<T>();
150150
return {
151151
get,
@@ -169,28 +169,24 @@ namespace ts {
169169

170170
// path should already be well-formed so it does not need to be normalized
171171
function get(path: Path): T {
172-
return files.get(toKey(path));
172+
return files.get(keyMapper(path));
173173
}
174174

175175
function set(path: Path, value: T) {
176-
files.set(toKey(path), value);
176+
files.set(keyMapper(path), value);
177177
}
178178

179179
function contains(path: Path) {
180-
return files.has(toKey(path));
180+
return files.has(keyMapper(path));
181181
}
182182

183183
function remove(path: Path) {
184-
files.delete(toKey(path));
184+
files.delete(keyMapper(path));
185185
}
186186

187187
function clear() {
188188
files.clear();
189189
}
190-
191-
function toKey(path: Path): string {
192-
return keyMapper ? keyMapper(path) : path;
193-
}
194190
}
195191

196192
export function toPath(fileName: string, basePath: string, getCanonicalFileName: (path: string) => string): Path {

src/compiler/moduleNameResolver.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ namespace ts {
306306
}
307307

308308
export function createModuleResolutionCache(currentDirectory: string, getCanonicalFileName: (s: string) => string): ModuleResolutionCache {
309-
const directoryToModuleNameMap = createFileMap<Map<ResolvedModuleWithFailedLookupLocations>>();
309+
const directoryToModuleNameMap = createMap<Map<ResolvedModuleWithFailedLookupLocations>>();
310310
const moduleNameToDirectoryMap = createMap<PerModuleNameCache>();
311311

312312
return { getOrCreateCacheForDirectory, getOrCreateCacheForModuleName };
@@ -334,7 +334,7 @@ namespace ts {
334334
}
335335

336336
function createPerModuleNameCache(): PerModuleNameCache {
337-
const directoryPathMap = createFileMap<ResolvedModuleWithFailedLookupLocations>();
337+
const directoryPathMap = createMap<ResolvedModuleWithFailedLookupLocations>();
338338

339339
return { get, set };
340340

@@ -356,7 +356,7 @@ namespace ts {
356356
function set(directory: string, result: ResolvedModuleWithFailedLookupLocations): void {
357357
const path = toPath(directory, currentDirectory, getCanonicalFileName);
358358
// if entry is already in cache do nothing
359-
if (directoryPathMap.contains(path)) {
359+
if (directoryPathMap.has(path)) {
360360
return;
361361
}
362362
directoryPathMap.set(path, result);
@@ -370,7 +370,7 @@ namespace ts {
370370
let current = path;
371371
while (true) {
372372
const parent = getDirectoryPath(current);
373-
if (parent === current || directoryPathMap.contains(parent)) {
373+
if (parent === current || directoryPathMap.has(parent)) {
374374
break;
375375
}
376376
directoryPathMap.set(parent, result);

src/compiler/program.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ namespace ts {
382382
}
383383

384384
interface DiagnosticCache {
385-
perFile?: FileMap<Diagnostic[]>;
385+
perFile?: Map<Diagnostic[]>;
386386
allDiagnostics?: Diagnostic[];
387387
}
388388

@@ -472,7 +472,7 @@ namespace ts {
472472
resolveTypeReferenceDirectiveNamesWorker = (typeReferenceDirectiveNames, containingFile) => loadWithLocalCache(typeReferenceDirectiveNames, containingFile, loader);
473473
}
474474

475-
const filesByName = createFileMap<SourceFile>();
475+
const filesByName = createMap<SourceFile>();
476476
// stores 'filename -> file association' ignoring case
477477
// used to track cases when two file names differ only in casing
478478
const filesByNameIgnoreCase = host.useCaseSensitiveFileNames() ? createFileMap<SourceFile>(fileName => fileName.toLowerCase()) : undefined;
@@ -1314,7 +1314,7 @@ namespace ts {
13141314
const result = getDiagnostics(sourceFile, cancellationToken) || emptyArray;
13151315
if (sourceFile) {
13161316
if (!cache.perFile) {
1317-
cache.perFile = createFileMap<Diagnostic[]>();
1317+
cache.perFile = createMap<Diagnostic[]>();
13181318
}
13191319
cache.perFile.set(sourceFile.path, result);
13201320
}
@@ -1531,7 +1531,7 @@ namespace ts {
15311531

15321532
// Get source file from normalized fileName
15331533
function findSourceFile(fileName: string, path: Path, isDefaultLib: boolean, refFile?: SourceFile, refPos?: number, refEnd?: number): SourceFile {
1534-
if (filesByName.contains(path)) {
1534+
if (filesByName.has(path)) {
15351535
const file = filesByName.get(path);
15361536
// try to check if we've already seen this file but with a different casing in path
15371537
// NOTE: this only makes sense for case-insensitive file systems
@@ -1951,7 +1951,7 @@ namespace ts {
19511951
// If the emit is enabled make sure that every output file is unique and not overwriting any of the input files
19521952
if (!options.noEmit && !options.suppressOutputPathCheck) {
19531953
const emitHost = getEmitHost();
1954-
const emitFilesSeen = createFileMap<boolean>(!host.useCaseSensitiveFileNames() ? key => key.toLocaleLowerCase() : undefined);
1954+
const emitFilesSeen = createFileMap<boolean>(!host.useCaseSensitiveFileNames() ? key => key.toLocaleLowerCase() : key => key);
19551955
forEachEmittedFile(emitHost, (emitFileNames) => {
19561956
verifyEmitFilePath(emitFileNames.jsFilePath, emitFilesSeen);
19571957
verifyEmitFilePath(emitFileNames.declarationFilePath, emitFilesSeen);
@@ -1963,7 +1963,7 @@ namespace ts {
19631963
if (emitFileName) {
19641964
const emitFilePath = toPath(emitFileName, currentDirectory, getCanonicalFileName);
19651965
// Report error if the output overwrites input file
1966-
if (filesByName.contains(emitFilePath)) {
1966+
if (filesByName.has(emitFilePath)) {
19671967
let chain: DiagnosticMessageChain;
19681968
if (!options.configFilePath) {
19691969
// The program is from either an inferred project or an external project

src/harness/harness.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -893,13 +893,13 @@ namespace Harness {
893893
const getCanonicalFileName = ts.createGetCanonicalFileName(useCaseSensitiveFileNames);
894894

895895
/** Maps a symlink name to a realpath. Used only for exposing `realpath`. */
896-
const realPathMap = ts.createFileMap<string>();
896+
const realPathMap = ts.createMap<string>();
897897
/**
898898
* Maps a file name to a source file.
899899
* This will have a different SourceFile for every symlink pointing to that file;
900900
* if the program resolves realpaths then symlink entries will be ignored.
901901
*/
902-
const fileMap = ts.createFileMap<ts.SourceFile>();
902+
const fileMap = ts.createMap<ts.SourceFile>();
903903
for (const file of inputFiles) {
904904
if (file.content !== undefined) {
905905
const fileName = ts.normalizePath(file.unitName);
@@ -975,7 +975,7 @@ namespace Harness {
975975
getCanonicalFileName,
976976
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
977977
getNewLine: () => newLine,
978-
fileExists: fileName => fileMap.contains(toPath(fileName)),
978+
fileExists: fileName => fileMap.has(toPath(fileName)),
979979
readFile: (fileName: string): string => {
980980
const file = fileMap.get(toPath(fileName));
981981
if (ts.endsWith(fileName, "json")) {
@@ -999,7 +999,7 @@ namespace Harness {
999999
getDirectories: d => {
10001000
const path = ts.toPath(d, currentDirectory, getCanonicalFileName);
10011001
const result: string[] = [];
1002-
fileMap.forEachValue(key => {
1002+
ts.forEachKey(fileMap, key => {
10031003
if (key.indexOf(path) === 0 && key.lastIndexOf("/") > path.length) {
10041004
let dirName = key.substr(path.length, key.indexOf("/", path.length + 1) - path.length);
10051005
if (dirName[0] === "/") {
@@ -1015,12 +1015,12 @@ namespace Harness {
10151015
};
10161016
}
10171017

1018-
function mapHasFileInDirectory(directoryPath: ts.Path, map: ts.FileMap<any>): boolean {
1018+
function mapHasFileInDirectory(directoryPath: ts.Path, map: ts.Map<{}>): boolean {
10191019
if (!map) {
10201020
return false;
10211021
}
10221022
let exists = false;
1023-
map.forEachValue(fileName => {
1023+
ts.forEachKey(map, fileName => {
10241024
if (!exists && ts.startsWith(fileName, directoryPath) && fileName[directoryPath.length] === "/") {
10251025
exists = true;
10261026
}

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ namespace ts.projectSystem {
261261
return typeof (<File>s).content === "string";
262262
}
263263

264-
export function addFolder(fullPath: string, toPath: (s: string) => Path, fs: FileMap<FSEntry>): Folder {
264+
export function addFolder(fullPath: string, toPath: (s: string) => Path, fs: Map<FSEntry>): Folder {
265265
const path = toPath(fullPath);
266-
if (fs.contains(path)) {
266+
if (fs.has(path)) {
267267
Debug.assert(isFolder(fs.get(path)));
268268
return (<Folder>fs.get(path));
269269
}
@@ -370,7 +370,7 @@ namespace ts.projectSystem {
370370

371371
private readonly output: string[] = [];
372372

373-
private fs: ts.FileMap<FSEntry>;
373+
private fs: Map<FSEntry>;
374374
private getCanonicalFileName: (s: string) => string;
375375
private toPath: (f: string) => Path;
376376
private timeoutCallbacks = new Callbacks();
@@ -390,7 +390,7 @@ namespace ts.projectSystem {
390390

391391
reloadFS(filesOrFolders: FileOrFolder[]) {
392392
this.filesOrFolders = filesOrFolders;
393-
this.fs = createFileMap<FSEntry>();
393+
this.fs = createMap<FSEntry>();
394394
// always inject safelist file in the list of files
395395
for (const fileOrFolder of filesOrFolders.concat(safeList)) {
396396
const path = this.toPath(fileOrFolder.path);
@@ -408,12 +408,12 @@ namespace ts.projectSystem {
408408

409409
fileExists(s: string) {
410410
const path = this.toPath(s);
411-
return this.fs.contains(path) && isFile(this.fs.get(path));
411+
return this.fs.has(path) && isFile(this.fs.get(path));
412412
}
413413

414414
getFileSize(s: string) {
415415
const path = this.toPath(s);
416-
if (this.fs.contains(path)) {
416+
if (this.fs.has(path)) {
417417
const entry = this.fs.get(path);
418418
if (isFile(entry)) {
419419
return entry.fileSize ? entry.fileSize : entry.content.length;
@@ -424,12 +424,12 @@ namespace ts.projectSystem {
424424

425425
directoryExists(s: string) {
426426
const path = this.toPath(s);
427-
return this.fs.contains(path) && isFolder(this.fs.get(path));
427+
return this.fs.has(path) && isFolder(this.fs.get(path));
428428
}
429429

430430
getDirectories(s: string) {
431431
const path = this.toPath(s);
432-
if (!this.fs.contains(path)) {
432+
if (!this.fs.has(path)) {
433433
return [];
434434
}
435435
else {

src/server/builder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ namespace ts.server {
8484
* NOTE: this field is created on demand and should not be accessed directly.
8585
* Use 'getFileInfos' instead.
8686
*/
87-
private fileInfos_doNotAccessDirectly: FileMap<T>;
87+
private fileInfos_doNotAccessDirectly: Map<T>;
8888

8989
constructor(public readonly project: Project, private ctor: { new (scriptInfo: ScriptInfo, project: Project): T }) {
9090
}
9191

9292
private getFileInfos() {
93-
return this.fileInfos_doNotAccessDirectly || (this.fileInfos_doNotAccessDirectly = createFileMap<T>());
93+
return this.fileInfos_doNotAccessDirectly || (this.fileInfos_doNotAccessDirectly = createMap<T>());
9494
}
9595

9696
protected hasFileInfos() {
@@ -117,19 +117,19 @@ namespace ts.server {
117117
}
118118

119119
protected getFileInfoPaths(): Path[] {
120-
return this.getFileInfos().getKeys();
120+
return arrayFrom(this.getFileInfos().keys() as Iterator<Path>);
121121
}
122122

123123
protected setFileInfo(path: Path, info: T) {
124124
this.getFileInfos().set(path, info);
125125
}
126126

127127
protected removeFileInfo(path: Path) {
128-
this.getFileInfos().remove(path);
128+
this.getFileInfos().delete(path);
129129
}
130130

131131
protected forEachFileInfo(action: (fileInfo: T) => any) {
132-
this.getFileInfos().forEachValue((_path, value) => action(value));
132+
this.getFileInfos().forEach(action);
133133
}
134134

135135
abstract getFilesAffectedBy(scriptInfo: ScriptInfo): string[];

src/server/editorServices.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ namespace ts.server {
343343
/**
344344
* Container of all known scripts
345345
*/
346-
private readonly filenameToScriptInfo = createFileMap<ScriptInfo>();
346+
private readonly filenameToScriptInfo = createMap<ScriptInfo>();
347347
/**
348348
* maps external project file name to list of config files that were the part of this project
349349
*/
@@ -568,7 +568,7 @@ namespace ts.server {
568568
if (info.containingProjects.length === 0) {
569569
// Orphan script info, remove it as we can always reload it on next open
570570
info.stopWatcher();
571-
this.filenameToScriptInfo.remove(info.path);
571+
this.filenameToScriptInfo.delete(info.path);
572572
}
573573
else {
574574
// file has been changed which might affect the set of referenced files in projects that include
@@ -588,7 +588,7 @@ namespace ts.server {
588588
// TODO: handle isOpen = true case
589589

590590
if (!info.isScriptOpen()) {
591-
this.filenameToScriptInfo.remove(info.path);
591+
this.filenameToScriptInfo.delete(info.path);
592592
this.lastDeletedFile = info;
593593

594594
// capture list of projects since detachAllProjects will wipe out original list
@@ -852,14 +852,13 @@ namespace ts.server {
852852
}
853853

854854
private deleteOrphanScriptInfoNotInAnyProject() {
855-
for (const path of this.filenameToScriptInfo.getKeys()) {
856-
const info = this.filenameToScriptInfo.get(path);
855+
this.filenameToScriptInfo.forEach(info => {
857856
if (!info.isScriptOpen() && info.containingProjects.length === 0) {
858857
// if there are not projects that include this script info - delete it
859858
info.stopWatcher();
860-
this.filenameToScriptInfo.remove(info.path);
859+
this.filenameToScriptInfo.delete(info.path);
861860
}
862-
}
861+
});
863862
}
864863

865864
/**

src/server/lsHost.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace ts.server {
66
export class LSHost implements ts.LanguageServiceHost, ModuleResolutionHost {
77
private compilationSettings: ts.CompilerOptions;
8-
private readonly resolvedModuleNames = createFileMap<Map<ResolvedModuleWithFailedLookupLocations>>();
9-
private readonly resolvedTypeReferenceDirectives = createFileMap<Map<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>>();
8+
private readonly resolvedModuleNames = createMap<Map<ResolvedModuleWithFailedLookupLocations>>();
9+
private readonly resolvedTypeReferenceDirectives = createMap<Map<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>>();
1010
private readonly getCanonicalFileName: (fileName: string) => string;
1111

1212
private filesWithChangedSetOfUnresolvedImports: Path[];
@@ -65,7 +65,7 @@ namespace ts.server {
6565
private resolveNamesWithLocalCache<T extends { failedLookupLocations: string[] }, R>(
6666
names: string[],
6767
containingFile: string,
68-
cache: ts.FileMap<Map<T>>,
68+
cache: Map<Map<T>>,
6969
loader: (name: string, containingFile: string, options: CompilerOptions, host: ModuleResolutionHost) => T,
7070
getResult: (s: T) => R,
7171
getResultFileName: (result: R) => string | undefined,
@@ -231,8 +231,8 @@ namespace ts.server {
231231
}
232232

233233
notifyFileRemoved(info: ScriptInfo) {
234-
this.resolvedModuleNames.remove(info.path);
235-
this.resolvedTypeReferenceDirectives.remove(info.path);
234+
this.resolvedModuleNames.delete(info.path);
235+
this.resolvedTypeReferenceDirectives.delete(info.path);
236236
}
237237

238238
setCompilationSettings(opt: ts.CompilerOptions) {

0 commit comments

Comments
 (0)