diff --git a/extensions/typescript/src/features/quickFixProvider.ts b/extensions/typescript/src/features/quickFixProvider.ts index 6960233c56d..49aaa8b6e04 100644 --- a/extensions/typescript/src/features/quickFixProvider.ts +++ b/extensions/typescript/src/features/quickFixProvider.ts @@ -75,6 +75,32 @@ class ApplyFixAllCodeAction implements Command { } } +/** + * Unique set of diagnostics keyed on diagnostic range and error code. + */ +class DiagnosticsSet { + public static from(diagnostics: vscode.Diagnostic[]) { + const values = new Map(); + for (const diagnostic of diagnostics) { + values.set(DiagnosticsSet.key(diagnostic), diagnostic); + } + return new DiagnosticsSet(values); + } + + private static key(diagnostic: vscode.Diagnostic) { + const { start, end } = diagnostic.range; + return `${diagnostic.code}-${start.line},${start.character}-${end.line},${end.character}`; + } + + private constructor( + private readonly _values: Map + ) { } + + public get values(): Iterable { + return this._values.values(); + } +} + class SupportedCodeActionProvider { private _supportedCodeActions?: Thenable>; @@ -84,7 +110,8 @@ class SupportedCodeActionProvider { public async getFixableDiagnosticsForContext(context: vscode.CodeActionContext): Promise { const supportedActions = await this.supportedCodeActions; - return context.diagnostics.filter(diagnostic => supportedActions.has(+diagnostic.code)); + const fixableDiagnostics = DiagnosticsSet.from(context.diagnostics.filter(diagnostic => supportedActions.has(+diagnostic.code))); + return Array.from(fixableDiagnostics.values); } private get supportedCodeActions(): Thenable> {