Rename and consolidate map iteration helpers

This commit is contained in:
Andy Hanson 2016-12-28 09:16:38 -08:00
parent 39c19a74ea
commit 932eaa3f90
13 changed files with 32 additions and 55 deletions

View File

@ -1815,7 +1815,7 @@ namespace ts {
}
// Check if symbol is any of the alias
return forEachInMap(symbols, symbolFromSymbolTable => {
return forEachEntry(symbols, symbolFromSymbolTable => {
if (symbolFromSymbolTable.flags & SymbolFlags.Alias
&& symbolFromSymbolTable.name !== "export="
&& !getDeclarationOfKind(symbolFromSymbolTable, SyntaxKind.ExportSpecifier)) {
@ -7758,7 +7758,7 @@ namespace ts {
const maybeCache = maybeStack[depth];
// If result is definitely true, copy assumptions to global cache, else copy to next level up
const destinationCache = (result === Ternary.True || depth === 0) ? relation : maybeStack[depth - 1];
copyMapEntries(maybeCache, destinationCache);
copyEntries(maybeCache, destinationCache);
}
else {
// A false result goes straight into global cache (when something is false under assumptions it
@ -18001,7 +18001,7 @@ namespace ts {
else {
const blockLocals = catchClause.block.locals;
if (blockLocals) {
forEachKeyInMap(catchClause.locals, caughtName => {
forEachKey(catchClause.locals, caughtName => {
const blockLocal = blockLocals.get(caughtName);
if (blockLocal && (blockLocal.flags & SymbolFlags.BlockScopedVariable) !== 0) {
grammarErrorOnNode(blockLocal.valueDeclaration, Diagnostics.Cannot_redeclare_identifier_0_in_catch_clause, caughtName);
@ -19187,7 +19187,7 @@ namespace ts {
}
function hasExportedMembers(moduleSymbol: Symbol) {
return someInMap(moduleSymbol.exports, (_, id) => id !== "export=");
return forEachEntry(moduleSymbol.exports, (_, id) => id !== "export=");
}
function checkExternalModuleExports(node: SourceFile | ModuleDeclaration) {
@ -20097,7 +20097,7 @@ namespace ts {
// otherwise - check if at least one export is value
symbolLinks.exportsSomeValue = hasExportAssignment
? !!(moduleSymbol.flags & SymbolFlags.Value)
: someInMap(getExportsOfModule(moduleSymbol), isValue);
: forEachEntry(getExportsOfModule(moduleSymbol), isValue);
}
return symbolLinks.exportsSomeValue;

View File

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

View File

@ -877,15 +877,15 @@ namespace ts {
}
/**
* Calls `callback` for each entry in the map, returning the first defined result.
* Calls `callback` for each entry in the map, returning the first truthy result.
* Use `map.forEach` instead for normal iteration.
*/
export function forEachInMap<T, U>(map: Map<T>, callback: (value: T, key: string) => U | undefined): U | undefined {
export function forEachEntry<T, U>(map: Map<T>, callback: (value: T, key: string) => U | undefined): U | undefined {
const iterator = map.entries();
for (let { value: pair, done } = iterator.next(); !done; { value: pair, done } = iterator.next()) {
const [key, value] = pair;
const result = callback(value, key);
if (result !== undefined) {
if (result) {
return result;
}
}
@ -893,42 +893,19 @@ namespace ts {
}
/** `forEachInMap` for just keys. */
export function forEachKeyInMap<T>(map: Map<{}>, callback: (key: string) => T | undefined): T | undefined {
export function forEachKey<T>(map: Map<{}>, callback: (key: string) => T | undefined): T | undefined {
const iterator = map.keys();
for (let { value: key, done } = iterator.next(); !done; { value: key, done } = iterator.next()) {
const result = callback(key);
if (result !== undefined) {
if (result) {
return result;
}
}
return undefined;
}
/** Whether `predicate` is true for some entry in the map. */
export function someInMap<T>(map: Map<T>, predicate: (value: T, key: string) => boolean): boolean {
const iterator = map.entries();
for (let { value: pair, done } = iterator.next(); !done; { value: pair, done } = iterator.next()) {
const [key, value] = pair;
if (predicate(value, key)) {
return true;
}
}
return false;
}
/** `someInMap` for just keys. */
export function someKeyInMap(map: Map<{}>, predicate: (key: string) => boolean): boolean {
const iterator = map.keys();
for (let { value: key, done } = iterator.next(); !done; { value: key, done } = iterator.next()) {
if (predicate(key)) {
return true;
}
}
return false;
}
/** Copy entries from `source` to `target`. */
export function copyMapEntries<T>(source: Map<T>, target: Map<T>): void {
export function copyEntries<T>(source: Map<T>, target: Map<T>): void {
source.forEach((value, key) => {
target.set(key, value);
});
@ -987,7 +964,7 @@ namespace ts {
export function cloneMap<T>(map: Map<T>) {
const clone = createMap<T>();
copyMapEntries(map, clone);
copyEntries(map, clone);
return clone;
}

View File

@ -156,7 +156,7 @@ namespace ts {
});
if (usedTypeDirectiveReferences) {
forEachKeyInMap(usedTypeDirectiveReferences, directive => {
forEachKey(usedTypeDirectiveReferences, directive => {
referencesOutput += `/// <reference types="${directive}" />${newLine}`;
});
}

View File

@ -464,7 +464,7 @@ namespace ts {
classifiableNames = createMap<string>();
for (const sourceFile of files) {
copyMapEntries(sourceFile.classifiableNames, classifiableNames);
copyEntries(sourceFile.classifiableNames, classifiableNames);
}
}

View File

@ -1,4 +1,4 @@
/// <reference path="..\harness.ts" />
/// <reference path="..\harness.ts" />
namespace ts {
interface File {
@ -8,7 +8,7 @@ namespace ts {
function createDefaultServerHost(fileMap: Map<File>): server.ServerHost {
const existingDirectories = createMap<boolean>();
forEachKeyInMap(fileMap, name => {
forEachKey(fileMap, name => {
let dir = getDirectoryPath(name);
let previous: string;
do {

View File

@ -1,4 +1,4 @@
/// <reference path="..\harness.ts" />
/// <reference path="..\harness.ts" />
/// <reference path="..\..\harness\harnessLanguageService.ts" />
namespace ts {
@ -197,13 +197,13 @@ namespace ts {
function mapsAreEqual<T>(left: Map<T>, right: Map<T>, valuesAreEqual?: (left: T, right: T) => boolean): boolean {
if (left === right) return true;
if (!left || !right) return false;
const someInLeftHasNoMatch = someInMap(left, (leftValue, leftKey) => {
const someInLeftHasNoMatch = forEachEntry(left, (leftValue, leftKey) => {
if (!right.has(leftKey)) return true;
const rightValue = right.get(leftKey);
return !(valuesAreEqual ? valuesAreEqual(leftValue, rightValue) : leftValue === rightValue);
});
if (someInLeftHasNoMatch) return false;
const someInRightHasNoMatch = someKeyInMap(right, rightKey => !left.has(rightKey));
const someInRightHasNoMatch = forEachKey(right, rightKey => !left.has(rightKey));
return !someInRightHasNoMatch;
}

View File

@ -1373,7 +1373,7 @@ namespace ts.server {
}
// close projects that were missing in the input list
forEachKeyInMap(projectsToClose, externalProjectName => {
forEachKey(projectsToClose, externalProjectName => {
this.closeExternalProject(externalProjectName, /*suppressRefresh*/ true)
});

View File

@ -619,12 +619,12 @@ namespace ts.server {
const removed: string[] = [];
const updated: string[] = arrayFrom(updatedFileNames.keys());
forEachKeyInMap(currentFiles, id => {
forEachKey(currentFiles, id => {
if (!lastReportedFileNames.has(id)) {
added.push(id);
}
});
forEachKeyInMap(lastReportedFileNames, id => {
forEachKey(lastReportedFileNames, id => {
if (!currentFiles.has(id)) {
removed.push(id);
}

View File

@ -1,4 +1,4 @@
/// <reference path='../compiler/utilities.ts' />
/// <reference path='../compiler/utilities.ts' />
/* @internal */
namespace ts.Completions {
@ -378,7 +378,7 @@ namespace ts.Completions {
}
}
forEachKeyInMap(foundFiles, foundFile => {
forEachKey(foundFiles, foundFile => {
result.push(createCompletionEntryForModule(foundFile, ScriptElementKind.scriptElement, span));
});
}

View File

@ -1,4 +1,4 @@
/* @internal */
/* @internal */
namespace ts.FindAllReferences {
export function findReferencedSymbols(typeChecker: TypeChecker, cancellationToken: CancellationToken, sourceFiles: SourceFile[], sourceFile: SourceFile, position: number, findInStrings: boolean, findInComments: boolean): ReferencedSymbol[] {
const node = getTouchingPropertyName(sourceFile, position, /*includeJsDocComment*/ true);
@ -508,7 +508,7 @@ namespace ts.FindAllReferences {
result.push(ctrKeyword);
}
forEachInMap(classSymbol.exports, member => {
classSymbol.exports.forEach(member => {
const decl = member.valueDeclaration;
if (decl && decl.kind === SyntaxKind.MethodDeclaration) {
const body = (<MethodDeclaration>decl).body;

View File

@ -1,4 +1,4 @@
/* @internal */
/* @internal */
namespace ts.NavigateTo {
type RawNavigateToItem = { name: string; fileName: string; matchKind: PatternMatchKind; isCaseSensitive: boolean; declaration: Declaration };
@ -14,7 +14,7 @@ namespace ts.NavigateTo {
continue;
}
forEachInMap(sourceFile.getNamedDeclarations(), (declarations, name) => {
forEachEntry(sourceFile.getNamedDeclarations(), (declarations, name) => {
if (declarations) {
// First do a quick check to see if the name of the declaration matches the
// last portion of the (possibly) dotted name they're searching for.

View File

@ -1,4 +1,4 @@
namespace ts {
namespace ts {
export interface TranspileOptions {
compilerOptions?: CompilerOptions;
fileName?: string;
@ -126,7 +126,7 @@ namespace ts {
function fixupCompilerOptions(options: CompilerOptions, diagnostics: Diagnostic[]): CompilerOptions {
// Lazily create this value to fix module loading errors.
commandLineOptionsStringToEnum = commandLineOptionsStringToEnum || <CommandLineOptionOfCustomType[]>filter(optionDeclarations, o =>
typeof o.type === "object" && !forEachInMap(o.type, v => typeof v !== "number"));
typeof o.type === "object" && !forEachEntry(o.type, v => typeof v !== "number"));
options = clone(options);
@ -142,7 +142,7 @@ namespace ts {
options[opt.name] = parseCustomTypeOption(opt, value, diagnostics);
}
else {
if (!someInMap(opt.type, v => v === value)) {
if (!forEachEntry(opt.type, v => v === value)) {
// Supplied value isn't a valid enum value.
diagnostics.push(createCompilerDiagnosticForInvalidCustomType(opt));
}