Skip to content

Commit 23da0ab

Browse files
author
Andy
authored
Use array helpers in more places (microsoft#17055)
1 parent 2561ced commit 23da0ab

File tree

3 files changed

+17
-39
lines changed

3 files changed

+17
-39
lines changed

src/harness/fourslash.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,29 +2336,19 @@ namespace FourSlash {
23362336
* @param fileName Path to file where error should be retrieved from.
23372337
*/
23382338
private getCodeFixActions(fileName: string, errorCode?: number): ts.CodeAction[] {
2339-
const diagnosticsForCodeFix = this.getDiagnostics(fileName).map(diagnostic => {
2340-
return {
2341-
start: diagnostic.start,
2342-
length: diagnostic.length,
2343-
code: diagnostic.code
2344-
};
2345-
});
2346-
const dedupedDiagnositcs = ts.deduplicate(diagnosticsForCodeFix, ts.equalOwnProperties);
2347-
2348-
let actions: ts.CodeAction[] = undefined;
2349-
2350-
for (const diagnostic of dedupedDiagnositcs) {
2339+
const diagnosticsForCodeFix = this.getDiagnostics(fileName).map(diagnostic => ({
2340+
start: diagnostic.start,
2341+
length: diagnostic.length,
2342+
code: diagnostic.code
2343+
}));
23512344

2345+
return ts.flatMap(ts.deduplicate(diagnosticsForCodeFix, ts.equalOwnProperties), diagnostic => {
23522346
if (errorCode && errorCode !== diagnostic.code) {
2353-
continue;
2347+
return;
23542348
}
23552349

2356-
const newActions = this.languageService.getCodeFixesAtPosition(fileName, diagnostic.start, diagnostic.start + diagnostic.length, [diagnostic.code], this.formatCodeSettings);
2357-
if (newActions && newActions.length) {
2358-
actions = actions ? actions.concat(newActions) : newActions;
2359-
}
2360-
}
2361-
return actions;
2350+
return this.languageService.getCodeFixesAtPosition(fileName, diagnostic.start, diagnostic.start + diagnostic.length, [diagnostic.code], this.formatCodeSettings);
2351+
});
23622352
}
23632353

23642354
private applyCodeActions(actions: ts.CodeAction[], index?: number): void {
@@ -2389,7 +2379,7 @@ namespace FourSlash {
23892379

23902380
const codeFixes = this.getCodeFixActions(this.activeFile.fileName, errorCode);
23912381

2392-
if (!codeFixes || codeFixes.length === 0) {
2382+
if (codeFixes.length === 0) {
23932383
if (expectedTextArray.length !== 0) {
23942384
this.raiseError("No codefixes returned.");
23952385
}
@@ -2718,11 +2708,11 @@ namespace FourSlash {
27182708
public verifyCodeFixAvailable(negative: boolean) {
27192709
const codeFix = this.getCodeFixActions(this.activeFile.fileName);
27202710

2721-
if (negative && codeFix) {
2711+
if (negative && codeFix.length) {
27222712
this.raiseError(`verifyCodeFixAvailable failed - expected no fixes but found one.`);
27232713
}
27242714

2725-
if (!(negative || codeFix)) {
2715+
if (!(negative || codeFix.length)) {
27262716
this.raiseError(`verifyCodeFixAvailable failed - expected code fixes but none found.`);
27272717
}
27282718
}

src/services/pathCompletions.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,8 @@ namespace ts.Completions.PathCompletions {
4040
rootDirs = map(rootDirs, rootDirectory => normalizePath(isRootedDiskPath(rootDirectory) ? rootDirectory : combinePaths(basePath, rootDirectory)));
4141

4242
// Determine the path to the directory containing the script relative to the root directory it is contained within
43-
let relativeDirectory: string;
44-
for (const rootDirectory of rootDirs) {
45-
if (containsPath(rootDirectory, scriptPath, basePath, ignoreCase)) {
46-
relativeDirectory = scriptPath.substr(rootDirectory.length);
47-
break;
48-
}
49-
}
43+
const relativeDirectory = forEach(rootDirs, rootDirectory =>
44+
containsPath(rootDirectory, scriptPath, basePath, ignoreCase) ? scriptPath.substr(rootDirectory.length) : undefined);
5045

5146
// Now find a path for each potential directory that is to be merged with the one containing the script
5247
return deduplicate(map(rootDirs, rootDirectory => combinePaths(rootDirectory, relativeDirectory)));

src/services/services.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,19 +1770,12 @@ namespace ts {
17701770
const sourceFile = getValidSourceFile(fileName);
17711771
const span = createTextSpanFromBounds(start, end);
17721772
const newLineCharacter = getNewLineOrDefaultFromHost(host);
1773+
const rulesProvider = getRuleProvider(formatOptions);
17731774

1774-
let allFixes: CodeAction[] = [];
1775-
1776-
forEach(deduplicate(errorCodes), errorCode => {
1775+
return flatMap(deduplicate(errorCodes), errorCode => {
17771776
cancellationToken.throwIfCancellationRequested();
1778-
const rulesProvider = getRuleProvider(formatOptions);
1779-
const fixes = codefix.getFixes({ errorCode, sourceFile, span, program, newLineCharacter, host, cancellationToken, rulesProvider });
1780-
if (fixes) {
1781-
allFixes = allFixes.concat(fixes);
1782-
}
1777+
return codefix.getFixes({ errorCode, sourceFile, span, program, newLineCharacter, host, cancellationToken, rulesProvider });
17831778
});
1784-
1785-
return allFixes;
17861779
}
17871780

17881781
function getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion {

0 commit comments

Comments
 (0)