From 96b4d233a7db078394b4581c6e2e9ac1313c0047 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 10 Jul 2017 11:11:41 -0700 Subject: [PATCH] Use array helpers in more places --- src/harness/fourslash.ts | 34 ++++++++++++--------------------- src/services/pathCompletions.ts | 9 ++------- src/services/services.ts | 13 +++---------- 3 files changed, 17 insertions(+), 39 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 434a3981461a6..4c97bd90b25e5 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -2320,29 +2320,19 @@ namespace FourSlash { * @param fileName Path to file where error should be retrieved from. */ private getCodeFixActions(fileName: string, errorCode?: number): ts.CodeAction[] { - const diagnosticsForCodeFix = this.getDiagnostics(fileName).map(diagnostic => { - return { - start: diagnostic.start, - length: diagnostic.length, - code: diagnostic.code - }; - }); - const dedupedDiagnositcs = ts.deduplicate(diagnosticsForCodeFix, ts.equalOwnProperties); - - let actions: ts.CodeAction[] = undefined; - - for (const diagnostic of dedupedDiagnositcs) { + const diagnosticsForCodeFix = this.getDiagnostics(fileName).map(diagnostic => ({ + start: diagnostic.start, + length: diagnostic.length, + code: diagnostic.code + })); + return ts.flatMap(ts.deduplicate(diagnosticsForCodeFix, ts.equalOwnProperties), diagnostic => { if (errorCode && errorCode !== diagnostic.code) { - continue; + return; } - const newActions = this.languageService.getCodeFixesAtPosition(fileName, diagnostic.start, diagnostic.length, [diagnostic.code], this.formatCodeSettings); - if (newActions && newActions.length) { - actions = actions ? actions.concat(newActions) : newActions; - } - } - return actions; + return this.languageService.getCodeFixesAtPosition(fileName, diagnostic.start, diagnostic.length, [diagnostic.code], this.formatCodeSettings); + }); } private applyCodeActions(actions: ts.CodeAction[], index?: number): void { @@ -2373,7 +2363,7 @@ namespace FourSlash { const codeFixes = this.getCodeFixActions(this.activeFile.fileName, errorCode); - if (!codeFixes || codeFixes.length === 0) { + if (codeFixes.length === 0) { if (expectedTextArray.length !== 0) { this.raiseError("No codefixes returned."); } @@ -2702,11 +2692,11 @@ namespace FourSlash { public verifyCodeFixAvailable(negative: boolean) { const codeFix = this.getCodeFixActions(this.activeFile.fileName); - if (negative && codeFix) { + if (negative && codeFix.length) { this.raiseError(`verifyCodeFixAvailable failed - expected no fixes but found one.`); } - if (!(negative || codeFix)) { + if (!(negative || codeFix.length)) { this.raiseError(`verifyCodeFixAvailable failed - expected code fixes but none found.`); } } diff --git a/src/services/pathCompletions.ts b/src/services/pathCompletions.ts index bc94fb7dbc5ed..8addcf494bf3f 100644 --- a/src/services/pathCompletions.ts +++ b/src/services/pathCompletions.ts @@ -40,13 +40,8 @@ namespace ts.Completions.PathCompletions { rootDirs = map(rootDirs, rootDirectory => normalizePath(isRootedDiskPath(rootDirectory) ? rootDirectory : combinePaths(basePath, rootDirectory))); // Determine the path to the directory containing the script relative to the root directory it is contained within - let relativeDirectory: string; - for (const rootDirectory of rootDirs) { - if (containsPath(rootDirectory, scriptPath, basePath, ignoreCase)) { - relativeDirectory = scriptPath.substr(rootDirectory.length); - break; - } - } + const relativeDirectory = forEach(rootDirs, rootDirectory => + containsPath(rootDirectory, scriptPath, basePath, ignoreCase) ? scriptPath.substr(rootDirectory.length) : undefined); // Now find a path for each potential directory that is to be merged with the one containing the script return deduplicate(map(rootDirs, rootDirectory => combinePaths(rootDirectory, relativeDirectory))); diff --git a/src/services/services.ts b/src/services/services.ts index 759de900b848a..69e1954a507f0 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1770,19 +1770,12 @@ namespace ts { const sourceFile = getValidSourceFile(fileName); const span = { start, length: end - start }; const newLineCharacter = getNewLineOrDefaultFromHost(host); + const rulesProvider = getRuleProvider(formatOptions); - let allFixes: CodeAction[] = []; - - forEach(deduplicate(errorCodes), errorCode => { + return flatMap(deduplicate(errorCodes), errorCode => { cancellationToken.throwIfCancellationRequested(); - const rulesProvider = getRuleProvider(formatOptions); - const fixes = codefix.getFixes({ errorCode, sourceFile, span, program, newLineCharacter, host, cancellationToken, rulesProvider }); - if (fixes) { - allFixes = allFixes.concat(fixes); - } + return codefix.getFixes({ errorCode, sourceFile, span, program, newLineCharacter, host, cancellationToken, rulesProvider }); }); - - return allFixes; } function getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion {