Skip to content

Commit 7056411

Browse files
author
Andy
authored
Make use of array helper functions (#16226)
* Make use of array helper functions * Remove unnecessary 'ts.'
1 parent 6f42f9a commit 7056411

File tree

9 files changed

+30
-80
lines changed

9 files changed

+30
-80
lines changed

src/compiler/core.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ namespace ts {
473473
* @param array The array to map.
474474
* @param mapfn The callback used to map the result into one or more values.
475475
*/
476-
export function flatMap<T, U>(array: T[], mapfn: (x: T, i: number) => U | U[]): U[] {
476+
export function flatMap<T, U>(array: T[] | undefined, mapfn: (x: T, i: number) => U | U[] | undefined): U[] | undefined {
477477
let result: U[];
478478
if (array) {
479479
result = [];

src/compiler/factory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2503,8 +2503,8 @@ namespace ts {
25032503
helpers
25042504
} = sourceEmitNode;
25052505
if (!destEmitNode) destEmitNode = {};
2506-
if (leadingComments) destEmitNode.leadingComments = addRange(leadingComments.slice(), destEmitNode.leadingComments);
2507-
if (trailingComments) destEmitNode.trailingComments = addRange(trailingComments.slice(), destEmitNode.trailingComments);
2506+
if (leadingComments) destEmitNode.leadingComments = concatenate(leadingComments, destEmitNode.leadingComments);
2507+
if (trailingComments) destEmitNode.trailingComments = concatenate(trailingComments, destEmitNode.trailingComments);
25082508
if (flags) destEmitNode.flags = flags;
25092509
if (commentRange) destEmitNode.commentRange = commentRange;
25102510
if (sourceMapRange) destEmitNode.sourceMapRange = sourceMapRange;

src/compiler/program.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -984,16 +984,12 @@ namespace ts {
984984
if (sourceFile) {
985985
return getDiagnostics(sourceFile, cancellationToken);
986986
}
987-
988-
const allDiagnostics: Diagnostic[] = [];
989-
forEach(program.getSourceFiles(), sourceFile => {
987+
return sortAndDeduplicateDiagnostics(flatMap(program.getSourceFiles(), sourceFile => {
990988
if (cancellationToken) {
991989
cancellationToken.throwIfCancellationRequested();
992990
}
993-
addRange(allDiagnostics, getDiagnostics(sourceFile, cancellationToken));
994-
});
995-
996-
return sortAndDeduplicateDiagnostics(allDiagnostics);
991+
return getDiagnostics(sourceFile, cancellationToken);
992+
}));
997993
}
998994

999995
function getSyntacticDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
@@ -1330,16 +1326,13 @@ namespace ts {
13301326
}
13311327

13321328
function getOptionsDiagnostics(): Diagnostic[] {
1333-
const allDiagnostics: Diagnostic[] = [];
1334-
addRange(allDiagnostics, fileProcessingDiagnostics.getGlobalDiagnostics());
1335-
addRange(allDiagnostics, programDiagnostics.getGlobalDiagnostics());
1336-
return sortAndDeduplicateDiagnostics(allDiagnostics);
1329+
return sortAndDeduplicateDiagnostics(concatenate(
1330+
fileProcessingDiagnostics.getGlobalDiagnostics(),
1331+
programDiagnostics.getGlobalDiagnostics()));
13371332
}
13381333

13391334
function getGlobalDiagnostics(): Diagnostic[] {
1340-
const allDiagnostics: Diagnostic[] = [];
1341-
addRange(allDiagnostics, getDiagnosticsProducingTypeChecker().getGlobalDiagnostics());
1342-
return sortAndDeduplicateDiagnostics(allDiagnostics);
1335+
return sortAndDeduplicateDiagnostics(getDiagnosticsProducingTypeChecker().getGlobalDiagnostics().slice());
13431336
}
13441337

13451338
function processRootFile(fileName: string, isDefaultLib: boolean) {

src/compiler/transformers/es2015.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2095,9 +2095,9 @@ namespace ts {
20952095
enableSubstitutionsForBlockScopedBindings();
20962096
}
20972097

2098-
const declarations = flatten(map(node.declarations, node.flags & NodeFlags.Let
2098+
const declarations = flatMap(node.declarations, node.flags & NodeFlags.Let
20992099
? visitVariableDeclarationInLetDeclarationList
2100-
: visitVariableDeclaration));
2100+
: visitVariableDeclaration);
21012101

21022102
const declarationList = createVariableDeclarationList(declarations);
21032103
setOriginalNode(declarationList, node);

src/compiler/utilities.ts

+4-18
Original file line numberDiff line numberDiff line change
@@ -1434,24 +1434,10 @@ namespace ts {
14341434
}
14351435

14361436
function getJSDocTags(node: Node, kind: SyntaxKind): JSDocTag[] {
1437-
const docs = getJSDocs(node);
1438-
if (docs) {
1439-
const result: JSDocTag[] = [];
1440-
for (const doc of docs) {
1441-
if (doc.kind === SyntaxKind.JSDocParameterTag) {
1442-
if (doc.kind === kind) {
1443-
result.push(doc as JSDocTag);
1444-
}
1445-
}
1446-
else {
1447-
const tags = (doc as JSDoc).tags;
1448-
if (tags) {
1449-
result.push(...filter(tags, tag => tag.kind === kind));
1450-
}
1451-
}
1452-
}
1453-
return result;
1454-
}
1437+
return flatMap(getJSDocs(node), doc =>
1438+
doc.kind === SyntaxKind.JSDocComment
1439+
? filter((doc as JSDoc).tags, tag => tag.kind === kind)
1440+
: doc.kind === kind && doc);
14551441
}
14561442

14571443
function getFirstJSDocTag(node: Node, kind: SyntaxKind): JSDocTag {

src/harness/fourslash.ts

+2-15
Original file line numberDiff line numberDiff line change
@@ -479,24 +479,11 @@ namespace FourSlash {
479479
}
480480

481481
private getDiagnostics(fileName: string): ts.Diagnostic[] {
482-
const syntacticErrors = this.languageService.getSyntacticDiagnostics(fileName);
483-
const semanticErrors = this.languageService.getSemanticDiagnostics(fileName);
484-
485-
const diagnostics: ts.Diagnostic[] = [];
486-
diagnostics.push.apply(diagnostics, syntacticErrors);
487-
diagnostics.push.apply(diagnostics, semanticErrors);
488-
489-
return diagnostics;
482+
return this.languageService.getSyntacticDiagnostics(fileName).concat(this.languageService.getSemanticDiagnostics(fileName));
490483
}
491484

492485
private getAllDiagnostics(): ts.Diagnostic[] {
493-
const diagnostics: ts.Diagnostic[] = [];
494-
495-
for (const fileName of this.languageServiceAdapterHost.getFilenames()) {
496-
diagnostics.push.apply(this.getDiagnostics(fileName));
497-
}
498-
499-
return diagnostics;
486+
return ts.flatMap(this.languageServiceAdapterHost.getFilenames(), fileName => this.getDiagnostics(fileName));
500487
}
501488

502489
public verifyErrorExistsAfterMarker(markerName: string, negative: boolean, after: boolean) {

src/harness/loggedIO.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,11 @@ namespace Playback {
232232
// different entry).
233233
// TODO (yuisu): We can certainly remove these once we recapture the RWC using new API
234234
const normalizedPath = ts.normalizePath(path).toLowerCase();
235-
const result: string[] = [];
236-
for (const directory of replayLog.directoriesRead) {
235+
return ts.flatMap(replayLog.directoriesRead, directory => {
237236
if (ts.normalizeSlashes(directory.path).toLowerCase() === normalizedPath) {
238-
result.push(...directory.result);
237+
return directory.result;
239238
}
240-
}
241-
242-
return result;
239+
});
243240
});
244241

245242
wrapper.writeFile = recordReplay(wrapper.writeFile, underlying)(

src/server/project.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -976,18 +976,15 @@ namespace ts.server {
976976
}
977977

978978
getExternalFiles(): SortedReadonlyArray<string> {
979-
const items: string[] = [];
980-
for (const plugin of this.plugins) {
981-
if (typeof plugin.getExternalFiles === "function") {
982-
try {
983-
items.push(...plugin.getExternalFiles(this));
984-
}
985-
catch (e) {
986-
this.projectService.logger.info(`A plugin threw an exception in getExternalFiles: ${e}`);
987-
}
979+
return toSortedReadonlyArray(flatMap(this.plugins, plugin => {
980+
if (typeof plugin.getExternalFiles !== "function") return;
981+
try {
982+
return plugin.getExternalFiles(this);
988983
}
989-
}
990-
return toSortedReadonlyArray(items);
984+
catch (e) {
985+
this.projectService.logger.info(`A plugin threw an exception in getExternalFiles: ${e}`);
986+
}
987+
}));
991988
}
992989

993990
watchConfigFile(callback: (project: ConfiguredProject) => void) {

src/services/refactorProvider.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,8 @@ namespace ts {
5252
}
5353

5454
export function getRefactorCodeActions(context: RefactorContext, refactorName: string): CodeAction[] | undefined {
55-
56-
let result: CodeAction[];
5755
const refactor = refactors.get(refactorName);
58-
if (!refactor) {
59-
return undefined;
60-
}
61-
62-
const codeActions = refactor.getCodeActions(context);
63-
if (codeActions) {
64-
addRange((result || (result = [])), codeActions);
65-
}
66-
return result;
56+
return refactor && refactor.getCodeActions(context);
6757
}
6858
}
6959
}

0 commit comments

Comments
 (0)