Skip to content

Commit 2f48428

Browse files
committed
Update most usages of Map<K, true> to Set
1 parent c6fc934 commit 2f48428

26 files changed

+201
-200
lines changed

src/compiler/builder.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace ts {
2828
/**
2929
* The map has key by source file's path that has been changed
3030
*/
31-
changedFilesSet?: ReadonlyMap<string, true>;
31+
changedFilesSet?: ReadonlySet<string>;
3232
/**
3333
* Set of affected files being iterated
3434
*/
@@ -49,7 +49,7 @@ namespace ts {
4949
/**
5050
* True if the semantic diagnostics were copied from the old state
5151
*/
52-
semanticDiagnosticsFromOldState?: Map<string, true>;
52+
semanticDiagnosticsFromOldState?: Set<string>;
5353
/**
5454
* program corresponding to this state
5555
*/
@@ -93,7 +93,7 @@ namespace ts {
9393
/**
9494
* The map has key by source file's path that has been changed
9595
*/
96-
changedFilesSet: Map<string, true>;
96+
changedFilesSet: Set<string>;
9797
/**
9898
* Set of affected files being iterated
9999
*/
@@ -118,15 +118,15 @@ namespace ts {
118118
/**
119119
* Already seen affected files
120120
*/
121-
seenAffectedFiles: Map<string, true> | undefined;
121+
seenAffectedFiles: Set<string> | undefined;
122122
/**
123123
* whether this program has cleaned semantic diagnostics cache for lib files
124124
*/
125125
cleanedDiagnosticsOfLibFiles?: boolean;
126126
/**
127127
* True if the semantic diagnostics were copied from the old state
128128
*/
129-
semanticDiagnosticsFromOldState?: Map<string, true>;
129+
semanticDiagnosticsFromOldState?: Set<string>;
130130
/**
131131
* program corresponding to this state
132132
*/
@@ -161,9 +161,9 @@ namespace ts {
161161
programEmitComplete?: true;
162162
}
163163

164-
function hasSameKeys<T, U>(map1: ReadonlyMap<string, T> | undefined, map2: ReadonlyMap<string, U> | undefined): boolean {
164+
function hasSameKeys(map1: ReadonlyCollection<string> | undefined, map2: ReadonlyCollection<string> | undefined): boolean {
165165
// Has same size and every key is present in both maps
166-
return map1 as ReadonlyMap<string, T | U> === map2 || map1 !== undefined && map2 !== undefined && map1.size === map2.size && !forEachKey(map1, key => !map2.has(key));
166+
return map1 === map2 || map1 !== undefined && map2 !== undefined && map1.size === map2.size && !forEachKey(map1, key => !map2.has(key));
167167
}
168168

169169
/**
@@ -178,7 +178,7 @@ namespace ts {
178178
if (!outFile(compilerOptions)) {
179179
state.semanticDiagnosticsPerFile = createMap<readonly Diagnostic[]>();
180180
}
181-
state.changedFilesSet = createMap<true>();
181+
state.changedFilesSet = new Set();
182182

183183
const useOldState = BuilderState.canReuseOldState(state.referencedMap, oldState);
184184
const oldCompilerOptions = useOldState ? oldState!.compilerOptions : undefined;
@@ -196,14 +196,12 @@ namespace ts {
196196
}
197197

198198
// Copy old state's changed files set
199-
if (changedFilesSet) {
200-
copyEntries(changedFilesSet, state.changedFilesSet);
201-
}
199+
changedFilesSet?.forEach(value => state.changedFilesSet.add(value));
202200
if (!outFile(compilerOptions) && oldState!.affectedFilesPendingEmit) {
203201
state.affectedFilesPendingEmit = oldState!.affectedFilesPendingEmit.slice();
204202
state.affectedFilesPendingEmitKind = cloneMapOrUndefined(oldState!.affectedFilesPendingEmitKind);
205203
state.affectedFilesPendingEmitIndex = oldState!.affectedFilesPendingEmitIndex;
206-
state.seenAffectedFiles = createMap();
204+
state.seenAffectedFiles = new Set();
207205
}
208206
}
209207

@@ -227,7 +225,7 @@ namespace ts {
227225
// Referenced file was deleted in the new program
228226
newReferences && forEachKey(newReferences, path => !state.fileInfos.has(path) && oldState!.fileInfos.has(path))) {
229227
// Register file as changed file and do not copy semantic diagnostics, since all changed files need to be re-evaluated
230-
state.changedFilesSet.set(sourceFilePath, true);
228+
state.changedFilesSet.add(sourceFilePath);
231229
}
232230
else if (canCopySemanticDiagnostics) {
233231
const sourceFile = newProgram.getSourceFileByPath(sourceFilePath as Path)!;
@@ -240,23 +238,23 @@ namespace ts {
240238
if (diagnostics) {
241239
state.semanticDiagnosticsPerFile!.set(sourceFilePath, oldState!.hasReusableDiagnostic ? convertToDiagnostics(diagnostics as readonly ReusableDiagnostic[], newProgram, getCanonicalFileName) : diagnostics as readonly Diagnostic[]);
242240
if (!state.semanticDiagnosticsFromOldState) {
243-
state.semanticDiagnosticsFromOldState = createMap<true>();
241+
state.semanticDiagnosticsFromOldState = new Set();
244242
}
245-
state.semanticDiagnosticsFromOldState.set(sourceFilePath, true);
243+
state.semanticDiagnosticsFromOldState.add(sourceFilePath);
246244
}
247245
}
248246
});
249247

250248
// If the global file is removed, add all files as changed
251249
if (useOldState && forEachEntry(oldState!.fileInfos, (info, sourceFilePath) => info.affectsGlobalScope && !state.fileInfos.has(sourceFilePath))) {
252250
BuilderState.getAllFilesExcludingDefaultLibraryFile(state, newProgram, /*firstSourceFile*/ undefined)
253-
.forEach(file => state.changedFilesSet.set(file.resolvedPath, true));
251+
.forEach(file => state.changedFilesSet.add(file.resolvedPath));
254252
}
255253
else if (oldCompilerOptions && !outFile(compilerOptions) && compilerOptionsAffectEmit(compilerOptions, oldCompilerOptions)) {
256254
// Add all files to affectedFilesPendingEmit since emit changed
257255
newProgram.getSourceFiles().forEach(f => addToAffectedFilesPendingEmit(state, f.resolvedPath, BuilderFileEmit.Full));
258256
Debug.assert(!state.seenAffectedFiles || !state.seenAffectedFiles.size);
259-
state.seenAffectedFiles = state.seenAffectedFiles || createMap<true>();
257+
state.seenAffectedFiles = state.seenAffectedFiles || new Set();
260258
}
261259

262260
state.buildInfoEmitPending = !!state.changedFilesSet.size;
@@ -308,15 +306,15 @@ namespace ts {
308306
function cloneBuilderProgramState(state: Readonly<BuilderProgramState>): BuilderProgramState {
309307
const newState = BuilderState.clone(state) as BuilderProgramState;
310308
newState.semanticDiagnosticsPerFile = cloneMapOrUndefined(state.semanticDiagnosticsPerFile);
311-
newState.changedFilesSet = cloneMap(state.changedFilesSet);
309+
newState.changedFilesSet = new Set(state.changedFilesSet);
312310
newState.affectedFiles = state.affectedFiles;
313311
newState.affectedFilesIndex = state.affectedFilesIndex;
314312
newState.currentChangedFilePath = state.currentChangedFilePath;
315313
newState.currentAffectedFilesSignatures = cloneMapOrUndefined(state.currentAffectedFilesSignatures);
316314
newState.currentAffectedFilesExportedModulesMap = cloneMapOrUndefined(state.currentAffectedFilesExportedModulesMap);
317-
newState.seenAffectedFiles = cloneMapOrUndefined(state.seenAffectedFiles);
315+
newState.seenAffectedFiles = state.seenAffectedFiles && new Set(state.seenAffectedFiles);
318316
newState.cleanedDiagnosticsOfLibFiles = state.cleanedDiagnosticsOfLibFiles;
319-
newState.semanticDiagnosticsFromOldState = cloneMapOrUndefined(state.semanticDiagnosticsFromOldState);
317+
newState.semanticDiagnosticsFromOldState = state.semanticDiagnosticsFromOldState && new Set(state.semanticDiagnosticsFromOldState);
320318
newState.program = state.program;
321319
newState.compilerOptions = state.compilerOptions;
322320
newState.affectedFilesPendingEmit = state.affectedFilesPendingEmit && state.affectedFilesPendingEmit.slice();
@@ -391,7 +389,7 @@ namespace ts {
391389
state.affectedFiles = BuilderState.getFilesAffectedBy(state, program, nextKey.value as Path, cancellationToken, computeHash, state.currentAffectedFilesSignatures, state.currentAffectedFilesExportedModulesMap);
392390
state.currentChangedFilePath = nextKey.value as Path;
393391
state.affectedFilesIndex = 0;
394-
state.seenAffectedFiles = state.seenAffectedFiles || createMap<true>();
392+
state.seenAffectedFiles = state.seenAffectedFiles || new Set();
395393
}
396394
}
397395

@@ -533,7 +531,7 @@ namespace ts {
533531
}
534532

535533
Debug.assert(!!state.currentAffectedFilesExportedModulesMap);
536-
const seenFileAndExportsOfFile = createMap<true>();
534+
const seenFileAndExportsOfFile = new Set<string>();
537535
// Go through exported modules from cache first
538536
// If exported modules has path, all files referencing file exported from are affected
539537
if (forEachEntry(state.currentAffectedFilesExportedModulesMap, (exportedModules, exportedFromPath) =>
@@ -555,7 +553,7 @@ namespace ts {
555553
/**
556554
* Iterate on files referencing referencedPath
557555
*/
558-
function forEachFilesReferencingPath(state: BuilderProgramState, referencedPath: Path, seenFileAndExportsOfFile: Map<string, true>, fn: (state: BuilderProgramState, filePath: Path) => boolean) {
556+
function forEachFilesReferencingPath(state: BuilderProgramState, referencedPath: Path, seenFileAndExportsOfFile: Set<string>, fn: (state: BuilderProgramState, filePath: Path) => boolean) {
559557
return forEachEntry(state.referencedMap!, (referencesInFile, filePath) =>
560558
referencesInFile.has(referencedPath) && forEachFileAndExportsOfFile(state, filePath as Path, seenFileAndExportsOfFile, fn)
561559
);
@@ -564,8 +562,8 @@ namespace ts {
564562
/**
565563
* fn on file and iterate on anything that exports this file
566564
*/
567-
function forEachFileAndExportsOfFile(state: BuilderProgramState, filePath: Path, seenFileAndExportsOfFile: Map<string, true>, fn: (state: BuilderProgramState, filePath: Path) => boolean): boolean {
568-
if (!addToSeen(seenFileAndExportsOfFile, filePath)) {
565+
function forEachFileAndExportsOfFile(state: BuilderProgramState, filePath: Path, seenFileAndExportsOfFile: Set<string>, fn: (state: BuilderProgramState, filePath: Path) => boolean): boolean {
566+
if (!tryAddToSet(seenFileAndExportsOfFile, filePath)) {
569567
return false;
570568
}
571569

@@ -622,7 +620,7 @@ namespace ts {
622620
state.programEmitComplete = true;
623621
}
624622
else {
625-
state.seenAffectedFiles!.set((affected as SourceFile).resolvedPath, true);
623+
state.seenAffectedFiles!.add((affected as SourceFile).resolvedPath);
626624
if (emitKind !== undefined) {
627625
(state.seenEmittedFiles || (state.seenEmittedFiles = createMap())).set((affected as SourceFile).resolvedPath, emitKind);
628626
}
@@ -1149,7 +1147,7 @@ namespace ts {
11491147
// template is undefined, and instead will just exit the loop.
11501148
for (const key in mapLike) {
11511149
if (hasProperty(mapLike, key)) {
1152-
map.set(toPath(key), arrayToSet(mapLike[key], toPath));
1150+
map.set(toPath(key), new Set(mapLike[key].map(toPath)));
11531151
}
11541152
}
11551153
return map;

src/compiler/builderState.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace ts {
5050
* That means hence forth these files are assumed to have
5151
* no change in their signature for this version of the program
5252
*/
53-
hasCalledUpdateShapeSignature: Map<string, true>;
53+
hasCalledUpdateShapeSignature: Set<string>;
5454
/**
5555
* Cache of all files excluding default library file for the current program
5656
*/
@@ -73,7 +73,7 @@ namespace ts {
7373
/**
7474
* Referenced files with values for the keys as referenced file's path to be true
7575
*/
76-
export type ReferencedSet = ReadonlyMap<string, true>;
76+
export type ReferencedSet = ReadonlySet<string>;
7777
/**
7878
* Compute the hash to store the shape of the file
7979
*/
@@ -113,8 +113,8 @@ namespace ts {
113113
/**
114114
* Gets the referenced files for a file from the program with values for the keys as referenced file's path to be true
115115
*/
116-
function getReferencedFiles(program: Program, sourceFile: SourceFile, getCanonicalFileName: GetCanonicalFileName): Map<string, true> | undefined {
117-
let referencedFiles: Map<string, true> | undefined;
116+
function getReferencedFiles(program: Program, sourceFile: SourceFile, getCanonicalFileName: GetCanonicalFileName): Set<string> | undefined {
117+
let referencedFiles: Set<string> | undefined;
118118

119119
// We need to use a set here since the code can contain the same import twice,
120120
// but that will only be one dependency.
@@ -186,9 +186,9 @@ namespace ts {
186186

187187
function addReferencedFile(referencedPath: Path) {
188188
if (!referencedFiles) {
189-
referencedFiles = createMap<true>();
189+
referencedFiles = new Set<string>();
190190
}
191-
referencedFiles.set(referencedPath, true);
191+
referencedFiles.add(referencedPath);
192192
}
193193
}
194194

@@ -206,7 +206,7 @@ namespace ts {
206206
const fileInfos = createMap<FileInfo>();
207207
const referencedMap = newProgram.getCompilerOptions().module !== ModuleKind.None ? createMap<ReferencedSet>() : undefined;
208208
const exportedModulesMap = referencedMap ? createMap<ReferencedSet>() : undefined;
209-
const hasCalledUpdateShapeSignature = createMap<true>();
209+
const hasCalledUpdateShapeSignature = new Set<string>();
210210
const useOldState = canReuseOldState(referencedMap, oldState);
211211

212212
// Create the reference map, and set the file infos
@@ -258,7 +258,7 @@ namespace ts {
258258
fileInfos,
259259
referencedMap: cloneMapOrUndefined(state.referencedMap),
260260
exportedModulesMap: cloneMapOrUndefined(state.exportedModulesMap),
261-
hasCalledUpdateShapeSignature: cloneMap(state.hasCalledUpdateShapeSignature),
261+
hasCalledUpdateShapeSignature: new Set(state.hasCalledUpdateShapeSignature),
262262
};
263263
}
264264

@@ -298,7 +298,7 @@ namespace ts {
298298

299299
export function updateSignatureOfFile(state: BuilderState, signature: string | undefined, path: Path) {
300300
state.fileInfos.get(path)!.signature = signature;
301-
state.hasCalledUpdateShapeSignature.set(path, true);
301+
state.hasCalledUpdateShapeSignature.add(path);
302302
}
303303

304304
/**
@@ -365,16 +365,16 @@ namespace ts {
365365
return;
366366
}
367367

368-
let exportedModules: Map<string, true> | undefined;
368+
let exportedModules: Set<string> | undefined;
369369
exportedModulesFromDeclarationEmit.forEach(symbol => addExportedModule(getReferencedFileFromImportedModuleSymbol(symbol)));
370370
exportedModulesMapCache.set(sourceFile.resolvedPath, exportedModules || false);
371371

372372
function addExportedModule(exportedModulePath: Path | undefined) {
373373
if (exportedModulePath) {
374374
if (!exportedModules) {
375-
exportedModules = createMap<true>();
375+
exportedModules = new Set();
376376
}
377-
exportedModules.set(exportedModulePath, true);
377+
exportedModules.add(exportedModulePath);
378378
}
379379
}
380380
}

0 commit comments

Comments
 (0)