Skip to content

Commit 7b5e1e9

Browse files
author
Andy
authored
Use array helpers instead of 'reduce' (#17172)
1 parent c6a6467 commit 7b5e1e9

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

src/server/editorServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ namespace ts.server {
201201
* This helper function processes a list of projects and return the concatenated, sortd and deduplicated output of processing each project.
202202
*/
203203
export function combineProjectOutput<T>(projects: Project[], action: (project: Project) => T[], comparer?: (a: T, b: T) => number, areEqual?: (a: T, b: T) => boolean) {
204-
const result = projects.reduce<T[]>((previous, current) => concatenate(previous, action(current)), []).sort(comparer);
204+
const result = ts.flatMap(projects, action).sort(comparer);
205205
return projects.length > 1 ? deduplicate(result, areEqual) : result;
206206
}
207207

src/server/session.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -846,21 +846,22 @@ namespace ts.server {
846846
compareRenameLocation,
847847
(a, b) => a.file === b.file && a.start.line === b.start.line && a.start.offset === b.start.offset
848848
);
849-
const locs = fileSpans.reduce<protocol.SpanGroup[]>((accum, cur) => {
849+
850+
const locs: protocol.SpanGroup[] = [];
851+
for (const cur of fileSpans) {
850852
let curFileAccum: protocol.SpanGroup;
851-
if (accum.length > 0) {
852-
curFileAccum = accum[accum.length - 1];
853+
if (locs.length > 0) {
854+
curFileAccum = locs[locs.length - 1];
853855
if (curFileAccum.file !== cur.file) {
854856
curFileAccum = undefined;
855857
}
856858
}
857859
if (!curFileAccum) {
858860
curFileAccum = { file: cur.file, locs: [] };
859-
accum.push(curFileAccum);
861+
locs.push(curFileAccum);
860862
}
861863
curFileAccum.locs.push({ start: cur.start, end: cur.end });
862-
return accum;
863-
}, []);
864+
}
864865

865866
return { info: renameInfo, locs };
866867
}
@@ -1183,15 +1184,13 @@ namespace ts.server {
11831184
return undefined;
11841185
}
11851186
if (simplifiedResult) {
1186-
return completions.entries.reduce((result: protocol.CompletionEntry[], entry: ts.CompletionEntry) => {
1187+
return mapDefined(completions.entries, entry => {
11871188
if (completions.isMemberCompletion || (entry.name.toLowerCase().indexOf(prefix.toLowerCase()) === 0)) {
11881189
const { name, kind, kindModifiers, sortText, replacementSpan } = entry;
1189-
const convertedSpan: protocol.TextSpan =
1190-
replacementSpan ? this.decorateSpan(replacementSpan, scriptInfo) : undefined;
1191-
result.push({ name, kind, kindModifiers, sortText, replacementSpan: convertedSpan });
1190+
const convertedSpan = replacementSpan ? this.decorateSpan(replacementSpan, scriptInfo) : undefined;
1191+
return { name, kind, kindModifiers, sortText, replacementSpan: convertedSpan };
11921192
}
1193-
return result;
1194-
}, []).sort((a, b) => ts.compareStrings(a.name, b.name));
1193+
}).sort((a, b) => ts.compareStrings(a.name, b.name));
11951194
}
11961195
else {
11971196
return completions;
@@ -1203,13 +1202,8 @@ namespace ts.server {
12031202
const scriptInfo = project.getScriptInfoForNormalizedPath(file);
12041203
const position = this.getPosition(args, scriptInfo);
12051204

1206-
return args.entryNames.reduce((accum: protocol.CompletionEntryDetails[], entryName: string) => {
1207-
const details = project.getLanguageService().getCompletionEntryDetails(file, position, entryName);
1208-
if (details) {
1209-
accum.push(details);
1210-
}
1211-
return accum;
1212-
}, []);
1205+
return mapDefined(args.entryNames, entryName =>
1206+
project.getLanguageService().getCompletionEntryDetails(file, position, entryName));
12131207
}
12141208

12151209
private getCompileOnSaveAffectedFileList(args: protocol.FileRequestArgs): protocol.CompileOnSaveAffectedFileListSingleProject[] {
@@ -1274,14 +1268,11 @@ namespace ts.server {
12741268
}
12751269

12761270
private getDiagnostics(next: NextStep, delay: number, fileNames: string[]): void {
1277-
const checkList = fileNames.reduce((accum: PendingErrorCheck[], uncheckedFileName: string) => {
1271+
const checkList = mapDefined(fileNames, uncheckedFileName => {
12781272
const fileName = toNormalizedPath(uncheckedFileName);
12791273
const project = this.projectService.getDefaultProjectForFile(fileName, /*refreshInferredProjects*/ true);
1280-
if (project) {
1281-
accum.push({ fileName, project });
1282-
}
1283-
return accum;
1284-
}, []);
1274+
return project && { fileName, project };
1275+
});
12851276

12861277
if (checkList.length > 0) {
12871278
this.updateErrorCheck(next, checkList, this.changeSeq, (n) => n === this.changeSeq, delay);

0 commit comments

Comments
 (0)