diff --git a/extensions/typescript-language-features/src/features/quickFix.ts b/extensions/typescript-language-features/src/features/quickFix.ts index 5c9ff0ab802..4de63dc3e13 100644 --- a/extensions/typescript-language-features/src/features/quickFix.ts +++ b/extensions/typescript-language-features/src/features/quickFix.ts @@ -132,6 +132,7 @@ class VsCodeCodeAction extends vscode.CodeAction { public readonly tsAction: Proto.CodeFixAction, title: string, kind: vscode.CodeActionKind, + public readonly isFixAll: boolean, ) { super(title, kind); } @@ -232,9 +233,8 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider { } const allActions = Array.from(results.values); - const allTsActions = allActions.map(x => x.tsAction); for (const action of allActions) { - action.isPreferred = isPreferredFix(action.tsAction, allTsActions); + action.isPreferred = isPreferredFix(action, allActions); } return allActions; } @@ -277,7 +277,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider { diagnostic: vscode.Diagnostic, tsAction: Proto.CodeFixAction ): VsCodeCodeAction { - const codeAction = new VsCodeCodeAction(tsAction, tsAction.description, vscode.CodeActionKind.QuickFix); + const codeAction = new VsCodeCodeAction(tsAction, tsAction.description, vscode.CodeActionKind.QuickFix, false); codeAction.edit = getEditForCodeAction(this.client, tsAction); codeAction.diagnostics = [diagnostic]; codeAction.command = { @@ -313,7 +313,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider { const action = new VsCodeCodeAction( tsAction, tsAction.fixAllDescription || localize('fixAllInFileLabel', '{0} (Fix all in file)', tsAction.description), - vscode.CodeActionKind.QuickFix); + vscode.CodeActionKind.QuickFix, true); action.diagnostics = [diagnostic]; action.command = { command: ApplyFixAllCodeAction.ID, @@ -347,18 +347,23 @@ const preferredFixes = new Map([ ]); function isPreferredFix( - tsAction: Proto.CodeFixAction, - allActions: readonly Proto.CodeFixAction[] + action: VsCodeCodeAction, + allActions: readonly VsCodeCodeAction[] ): boolean { - const priority = preferredFixes.get(tsAction.fixName); + if (action.isFixAll) { + return false; + } + + const priority = preferredFixes.get(action.tsAction.fixName); if (typeof priority === 'undefined') { return false; } + return allActions.every(otherAction => { - if (otherAction === tsAction) { + if (otherAction.tsAction === action.tsAction) { return true; } - const otherPriority = preferredFixes.get(otherAction.fixName); + const otherPriority = preferredFixes.get(otherAction.tsAction.fixName); if (typeof otherPriority === 'undefined') { return true; } diff --git a/extensions/typescript-language-features/src/test/quickFix.test.ts b/extensions/typescript-language-features/src/test/quickFix.test.ts new file mode 100644 index 00000000000..c1e591bf2ba --- /dev/null +++ b/extensions/typescript-language-features/src/test/quickFix.test.ts @@ -0,0 +1,43 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as assert from 'assert'; +import 'mocha'; +import * as vscode from 'vscode'; +import { disposeAll } from '../utils/dispose'; +import { createTestEditor, joinLines, wait } from './testUtils'; + +const testDocumentUri = vscode.Uri.parse('untitled:test.ts'); + + +suite('TypeScript Quick Fix', () => { + + const _disposables: vscode.Disposable[] = []; + + teardown(async () => { + disposeAll(_disposables); + + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); + }); + + test('Fix all should not be marked as preferred #97866', async () => { + const editor = await createTestEditor(testDocumentUri, + `export const _ = 1;`, + `const a$0 = 1;`, + `const b = 2;`, + ); + + await wait(2000); + + await vscode.commands.executeCommand('editor.action.autoFix'); + + await wait(500); + + assert.strictEqual(editor.document.getText(), joinLines( + `export const _ = 1;`, + `const b = 2;`, + )); + }); +});