Skip to content

Commit 932eaa3

Browse files
author
Andy Hanson
committed
Rename and consolidate map iteration helpers
1 parent 39c19a7 commit 932eaa3

13 files changed

Lines changed: 32 additions & 55 deletions

src/compiler/checker.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,7 +1815,7 @@ namespace ts {
18151815
}
18161816

18171817
// Check if symbol is any of the alias
1818-
return forEachInMap(symbols, symbolFromSymbolTable => {
1818+
return forEachEntry(symbols, symbolFromSymbolTable => {
18191819
if (symbolFromSymbolTable.flags & SymbolFlags.Alias
18201820
&& symbolFromSymbolTable.name !== "export="
18211821
&& !getDeclarationOfKind(symbolFromSymbolTable, SyntaxKind.ExportSpecifier)) {
@@ -7758,7 +7758,7 @@ namespace ts {
77587758
const maybeCache = maybeStack[depth];
77597759
// If result is definitely true, copy assumptions to global cache, else copy to next level up
77607760
const destinationCache = (result === Ternary.True || depth === 0) ? relation : maybeStack[depth - 1];
7761-
copyMapEntries(maybeCache, destinationCache);
7761+
copyEntries(maybeCache, destinationCache);
77627762
}
77637763
else {
77647764
// A false result goes straight into global cache (when something is false under assumptions it
@@ -18001,7 +18001,7 @@ namespace ts {
1800118001
else {
1800218002
const blockLocals = catchClause.block.locals;
1800318003
if (blockLocals) {
18004-
forEachKeyInMap(catchClause.locals, caughtName => {
18004+
forEachKey(catchClause.locals, caughtName => {
1800518005
const blockLocal = blockLocals.get(caughtName);
1800618006
if (blockLocal && (blockLocal.flags & SymbolFlags.BlockScopedVariable) !== 0) {
1800718007
grammarErrorOnNode(blockLocal.valueDeclaration, Diagnostics.Cannot_redeclare_identifier_0_in_catch_clause, caughtName);
@@ -19187,7 +19187,7 @@ namespace ts {
1918719187
}
1918819188

1918919189
function hasExportedMembers(moduleSymbol: Symbol) {
19190-
return someInMap(moduleSymbol.exports, (_, id) => id !== "export=");
19190+
return forEachEntry(moduleSymbol.exports, (_, id) => id !== "export=");
1919119191
}
1919219192

1919319193
function checkExternalModuleExports(node: SourceFile | ModuleDeclaration) {
@@ -20097,7 +20097,7 @@ namespace ts {
2009720097
// otherwise - check if at least one export is value
2009820098
symbolLinks.exportsSomeValue = hasExportAssignment
2009920099
? !!(moduleSymbol.flags & SymbolFlags.Value)
20100-
: someInMap(getExportsOfModule(moduleSymbol), isValue);
20100+
: forEachEntry(getExportsOfModule(moduleSymbol), isValue);
2010120101
}
2010220102

2010320103
return symbolLinks.exportsSomeValue;

src/compiler/commandLineParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ namespace ts {
753753

754754
function getNameOfCompilerOptionValue(value: CompilerOptionsValue, customTypeMap: Map<string | number>): string | undefined {
755755
// There is a typeMap associated with this command-line option so use it to map value back to its name
756-
return forEachInMap(customTypeMap, (mapValue, key) => {
756+
return forEachEntry(customTypeMap, (mapValue, key) => {
757757
if (mapValue === value) {
758758
return key;
759759
}

src/compiler/core.ts

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -877,58 +877,35 @@ namespace ts {
877877
}
878878

879879
/**
880-
* Calls `callback` for each entry in the map, returning the first defined result.
880+
* Calls `callback` for each entry in the map, returning the first truthy result.
881881
* Use `map.forEach` instead for normal iteration.
882882
*/
883-
export function forEachInMap<T, U>(map: Map<T>, callback: (value: T, key: string) => U | undefined): U | undefined {
883+
export function forEachEntry<T, U>(map: Map<T>, callback: (value: T, key: string) => U | undefined): U | undefined {
884884
const iterator = map.entries();
885885
for (let { value: pair, done } = iterator.next(); !done; { value: pair, done } = iterator.next()) {
886886
const [key, value] = pair;
887887
const result = callback(value, key);
888-
if (result !== undefined) {
888+
if (result) {
889889
return result;
890890
}
891891
}
892892
return undefined;
893893
}
894894

895895
/** `forEachInMap` for just keys. */
896-
export function forEachKeyInMap<T>(map: Map<{}>, callback: (key: string) => T | undefined): T | undefined {
896+
export function forEachKey<T>(map: Map<{}>, callback: (key: string) => T | undefined): T | undefined {
897897
const iterator = map.keys();
898898
for (let { value: key, done } = iterator.next(); !done; { value: key, done } = iterator.next()) {
899899
const result = callback(key);
900-
if (result !== undefined) {
900+
if (result) {
901901
return result;
902902
}
903903
}
904904
return undefined;
905905
}
906906

907-
/** Whether `predicate` is true for some entry in the map. */
908-
export function someInMap<T>(map: Map<T>, predicate: (value: T, key: string) => boolean): boolean {
909-
const iterator = map.entries();
910-
for (let { value: pair, done } = iterator.next(); !done; { value: pair, done } = iterator.next()) {
911-
const [key, value] = pair;
912-
if (predicate(value, key)) {
913-
return true;
914-
}
915-
}
916-
return false;
917-
}
918-
919-
/** `someInMap` for just keys. */
920-
export function someKeyInMap(map: Map<{}>, predicate: (key: string) => boolean): boolean {
921-
const iterator = map.keys();
922-
for (let { value: key, done } = iterator.next(); !done; { value: key, done } = iterator.next()) {
923-
if (predicate(key)) {
924-
return true;
925-
}
926-
}
927-
return false;
928-
}
929-
930907
/** Copy entries from `source` to `target`. */
931-
export function copyMapEntries<T>(source: Map<T>, target: Map<T>): void {
908+
export function copyEntries<T>(source: Map<T>, target: Map<T>): void {
932909
source.forEach((value, key) => {
933910
target.set(key, value);
934911
});
@@ -987,7 +964,7 @@ namespace ts {
987964

988965
export function cloneMap<T>(map: Map<T>) {
989966
const clone = createMap<T>();
990-
copyMapEntries(map, clone);
967+
copyEntries(map, clone);
991968
return clone;
992969
}
993970

src/compiler/declarationEmitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ namespace ts {
156156
});
157157

158158
if (usedTypeDirectiveReferences) {
159-
forEachKeyInMap(usedTypeDirectiveReferences, directive => {
159+
forEachKey(usedTypeDirectiveReferences, directive => {
160160
referencesOutput += `/// <reference types="${directive}" />${newLine}`;
161161
});
162162
}

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ namespace ts {
464464
classifiableNames = createMap<string>();
465465

466466
for (const sourceFile of files) {
467-
copyMapEntries(sourceFile.classifiableNames, classifiableNames);
467+
copyEntries(sourceFile.classifiableNames, classifiableNames);
468468
}
469469
}
470470

src/harness/unittests/cachingInServerLSHost.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path="..\harness.ts" />
1+
/// <reference path="..\harness.ts" />
22

33
namespace ts {
44
interface File {
@@ -8,7 +8,7 @@ namespace ts {
88

99
function createDefaultServerHost(fileMap: Map<File>): server.ServerHost {
1010
const existingDirectories = createMap<boolean>();
11-
forEachKeyInMap(fileMap, name => {
11+
forEachKey(fileMap, name => {
1212
let dir = getDirectoryPath(name);
1313
let previous: string;
1414
do {

src/harness/unittests/reuseProgramStructure.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path="..\harness.ts" />
1+
/// <reference path="..\harness.ts" />
22
/// <reference path="..\..\harness\harnessLanguageService.ts" />
33

44
namespace ts {
@@ -197,13 +197,13 @@ namespace ts {
197197
function mapsAreEqual<T>(left: Map<T>, right: Map<T>, valuesAreEqual?: (left: T, right: T) => boolean): boolean {
198198
if (left === right) return true;
199199
if (!left || !right) return false;
200-
const someInLeftHasNoMatch = someInMap(left, (leftValue, leftKey) => {
200+
const someInLeftHasNoMatch = forEachEntry(left, (leftValue, leftKey) => {
201201
if (!right.has(leftKey)) return true;
202202
const rightValue = right.get(leftKey);
203203
return !(valuesAreEqual ? valuesAreEqual(leftValue, rightValue) : leftValue === rightValue);
204204
});
205205
if (someInLeftHasNoMatch) return false;
206-
const someInRightHasNoMatch = someKeyInMap(right, rightKey => !left.has(rightKey));
206+
const someInRightHasNoMatch = forEachKey(right, rightKey => !left.has(rightKey));
207207
return !someInRightHasNoMatch;
208208
}
209209

src/server/editorServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ namespace ts.server {
13731373
}
13741374

13751375
// close projects that were missing in the input list
1376-
forEachKeyInMap(projectsToClose, externalProjectName => {
1376+
forEachKey(projectsToClose, externalProjectName => {
13771377
this.closeExternalProject(externalProjectName, /*suppressRefresh*/ true)
13781378
});
13791379

src/server/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,12 +619,12 @@ namespace ts.server {
619619
const removed: string[] = [];
620620
const updated: string[] = arrayFrom(updatedFileNames.keys());
621621

622-
forEachKeyInMap(currentFiles, id => {
622+
forEachKey(currentFiles, id => {
623623
if (!lastReportedFileNames.has(id)) {
624624
added.push(id);
625625
}
626626
});
627-
forEachKeyInMap(lastReportedFileNames, id => {
627+
forEachKey(lastReportedFileNames, id => {
628628
if (!currentFiles.has(id)) {
629629
removed.push(id);
630630
}

src/services/completions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path='../compiler/utilities.ts' />
1+
/// <reference path='../compiler/utilities.ts' />
22

33
/* @internal */
44
namespace ts.Completions {
@@ -378,7 +378,7 @@ namespace ts.Completions {
378378
}
379379
}
380380

381-
forEachKeyInMap(foundFiles, foundFile => {
381+
forEachKey(foundFiles, foundFile => {
382382
result.push(createCompletionEntryForModule(foundFile, ScriptElementKind.scriptElement, span));
383383
});
384384
}

0 commit comments

Comments
 (0)