Use array helpers in more places (#17055)

This commit is contained in:
Andy
2017-07-11 09:00:34 -07:00
committed by GitHub
parent 2561ced1e3
commit 23da0ab501
3 changed files with 17 additions and 39 deletions

View File

@@ -2336,29 +2336,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.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.start + diagnostic.length, [diagnostic.code], this.formatCodeSettings);
});
}
private applyCodeActions(actions: ts.CodeAction[], index?: number): void {
@@ -2389,7 +2379,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.");
}
@@ -2718,11 +2708,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.`);
}
}

View File

@@ -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)));

View File

@@ -1770,19 +1770,12 @@ namespace ts {
const sourceFile = getValidSourceFile(fileName);
const span = createTextSpanFromBounds(start, end);
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 {