diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3ed4c383ef2..b8e51c7f390 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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; diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 7bf50f675aa..3f89d3e3f9d 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -753,7 +753,7 @@ namespace ts { function getNameOfCompilerOptionValue(value: CompilerOptionsValue, customTypeMap: Map): 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; } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 9f8f1ba3f55..effe24a253e 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -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(map: Map, callback: (value: T, key: string) => U | undefined): U | undefined { + export function forEachEntry(map: Map, 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(map: Map<{}>, callback: (key: string) => T | undefined): T | undefined { + export function forEachKey(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(map: Map, 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(source: Map, target: Map): void { + export function copyEntries(source: Map, target: Map): void { source.forEach((value, key) => { target.set(key, value); }); @@ -987,7 +964,7 @@ namespace ts { export function cloneMap(map: Map) { const clone = createMap(); - copyMapEntries(map, clone); + copyEntries(map, clone); return clone; } diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index e010519d641..a006ab4fced 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -156,7 +156,7 @@ namespace ts { }); if (usedTypeDirectiveReferences) { - forEachKeyInMap(usedTypeDirectiveReferences, directive => { + forEachKey(usedTypeDirectiveReferences, directive => { referencesOutput += `/// ${newLine}`; }); } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index b6138e92a7b..324a21f229e 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -464,7 +464,7 @@ namespace ts { classifiableNames = createMap(); for (const sourceFile of files) { - copyMapEntries(sourceFile.classifiableNames, classifiableNames); + copyEntries(sourceFile.classifiableNames, classifiableNames); } } diff --git a/src/harness/unittests/cachingInServerLSHost.ts b/src/harness/unittests/cachingInServerLSHost.ts index b6a90f53259..8389186c486 100644 --- a/src/harness/unittests/cachingInServerLSHost.ts +++ b/src/harness/unittests/cachingInServerLSHost.ts @@ -1,4 +1,4 @@ -/// +/// namespace ts { interface File { @@ -8,7 +8,7 @@ namespace ts { function createDefaultServerHost(fileMap: Map): server.ServerHost { const existingDirectories = createMap(); - forEachKeyInMap(fileMap, name => { + forEachKey(fileMap, name => { let dir = getDirectoryPath(name); let previous: string; do { diff --git a/src/harness/unittests/reuseProgramStructure.ts b/src/harness/unittests/reuseProgramStructure.ts index eb4395154e4..8a89b8d27c6 100644 --- a/src/harness/unittests/reuseProgramStructure.ts +++ b/src/harness/unittests/reuseProgramStructure.ts @@ -1,4 +1,4 @@ -/// +/// /// namespace ts { @@ -197,13 +197,13 @@ namespace ts { function mapsAreEqual(left: Map, right: Map, 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; } diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 239cd385f57..8865d821d29 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -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) }); diff --git a/src/server/project.ts b/src/server/project.ts index 054b484392e..94a08a485f9 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -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); } diff --git a/src/services/completions.ts b/src/services/completions.ts index 4715222db08..04e3952b88b 100644 --- a/src/services/completions.ts +++ b/src/services/completions.ts @@ -1,4 +1,4 @@ -/// +/// /* @internal */ namespace ts.Completions { @@ -378,7 +378,7 @@ namespace ts.Completions { } } - forEachKeyInMap(foundFiles, foundFile => { + forEachKey(foundFiles, foundFile => { result.push(createCompletionEntryForModule(foundFile, ScriptElementKind.scriptElement, span)); }); } diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index 9e53537b69b..a7bf76e0656 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -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 = (decl).body; diff --git a/src/services/navigateTo.ts b/src/services/navigateTo.ts index 40f22941268..aa72da1ba5c 100644 --- a/src/services/navigateTo.ts +++ b/src/services/navigateTo.ts @@ -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. diff --git a/src/services/transpile.ts b/src/services/transpile.ts index d989a528497..b72399384b7 100644 --- a/src/services/transpile.ts +++ b/src/services/transpile.ts @@ -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 || 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)); }