Codefix: Don’t return a fixId if there’s definitely nothing else that can be fixed (#35765)

* Start fixing fixId

* Fix tests

* Add comment

* Fix unit tests, remove fixAllDescription when unavailable

* Add codeFixAllAvailable to fourslash harness
This commit is contained in:
Andrew Branch
2020-01-16 10:07:37 -08:00
committed by GitHub
parent eeff036519
commit 797c5362a2
23 changed files with 97 additions and 58 deletions
+24 -4
View File
@@ -10,7 +10,7 @@ namespace ts.codefix {
: getLocaleSpecificMessage(diag);
}
export function createCodeFixActionNoFixId(fixName: string, changes: FileTextChanges[], description: DiagnosticAndArguments) {
export function createCodeFixActionWithoutFixAll(fixName: string, changes: FileTextChanges[], description: DiagnosticAndArguments) {
return createCodeFixActionWorker(fixName, diagnosticToString(description), changes, /*fixId*/ undefined, /*fixAllDescription*/ undefined);
}
@@ -38,8 +38,24 @@ namespace ts.codefix {
return arrayFrom(errorCodeToFixes.keys());
}
function removeFixIdIfFixAllUnavailable(registration: CodeFixRegistration, diagnostics: Diagnostic[]) {
const { errorCodes } = registration;
let maybeFixableDiagnostics = 0;
for (const diag of diagnostics) {
if (contains(errorCodes, diag.code)) maybeFixableDiagnostics++;
if (maybeFixableDiagnostics > 1) break;
}
const fixAllUnavailable = maybeFixableDiagnostics < 2;
return ({ fixId, fixAllDescription, ...action }: CodeFixAction): CodeFixAction => {
return fixAllUnavailable ? action : { ...action, fixId, fixAllDescription };
};
}
export function getFixes(context: CodeFixContext): readonly CodeFixAction[] {
return flatMap(errorCodeToFixes.get(String(context.errorCode)) || emptyArray, f => f.getCodeActions(context));
const diagnostics = getDiagnostics(context);
const registrations = errorCodeToFixes.get(String(context.errorCode));
return flatMap(registrations, f => map(f.getCodeActions(context), removeFixIdIfFixAllUnavailable(f, diagnostics)));
}
export function getAllFixes(context: CodeFixAllContext): CombinedCodeActions {
@@ -65,11 +81,15 @@ namespace ts.codefix {
return createCombinedCodeActions(changes, commands.length === 0 ? undefined : commands);
}
export function eachDiagnostic({ program, sourceFile, cancellationToken }: CodeFixAllContext, errorCodes: readonly number[], cb: (diag: DiagnosticWithLocation) => void): void {
for (const diag of program.getSemanticDiagnostics(sourceFile, cancellationToken).concat(computeSuggestionDiagnostics(sourceFile, program, cancellationToken))) {
export function eachDiagnostic(context: CodeFixAllContext, errorCodes: readonly number[], cb: (diag: DiagnosticWithLocation) => void): void {
for (const diag of getDiagnostics(context)) {
if (contains(errorCodes, diag.code)) {
cb(diag as DiagnosticWithLocation);
}
}
}
function getDiagnostics({ program, sourceFile, cancellationToken }: CodeFixContextBase) {
return program.getSemanticDiagnostics(sourceFile, cancellationToken).concat(computeSuggestionDiagnostics(sourceFile, program, cancellationToken));
}
}