Skip to content

Commit

Permalink
refactor(language-service): reuse code fixes map for has fix check (#…
Browse files Browse the repository at this point in the history
…58759)

The error codes that have fixes are present in a Map instance and
can be directly leveraged to determine if an error code can be fixed.
This avoids multiple levels of iteration that would otherwise be needed.

PR Close #58759
  • Loading branch information
clydin authored and AndrewKushnir committed Nov 20, 2024
1 parent d4f5c85 commit d87f483
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/language-service/src/codefixes/code_fixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {TemplateInfo} from '../utils';
import {CodeActionMeta, FixIdForCodeFixesAll, isFixAllAvailable} from './utils';

export class CodeFixes {
private errorCodeToFixes: Map<number, CodeActionMeta[]> = new Map();
private errorCodeToFixes = new Map<number, CodeActionMeta[]>();
private fixIdToRegistration = new Map<FixIdForCodeFixesAll, CodeActionMeta>();

constructor(
Expand All @@ -40,6 +40,10 @@ export class CodeFixes {
}
}

hasFixForCode(code: number): boolean {
return this.errorCodeToFixes.has(code);
}

/**
* When the user moves the cursor or hovers on a diagnostics, this function will be invoked by LS,
* and collect all the responses from the `codeActionMetas` which could handle the `errorCodes`.
Expand Down
2 changes: 1 addition & 1 deletion packages/language-service/src/language_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ export class LanguageService {
* Related context: https://github.com/angular/vscode-ng-language-service/pull/2050#discussion_r1673079263
*/
hasCodeFixesForErrorCode(errorCode: number): boolean {
return this.codeFixes.codeActionMetas.some((m) => m.errorCodes.includes(errorCode));
return this.codeFixes.hasFixForCode(errorCode);
}

getCodeFixesAtPosition(
Expand Down

0 comments on commit d87f483

Please sign in to comment.