diff --git a/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts b/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts index de00639f2..933670b85 100644 --- a/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts @@ -276,7 +276,8 @@ export class CodeActionsProviderImpl implements CodeActionsProvider { if ( diagnostic.range.start.line < 0 || diagnostic.range.end.line < 0 || - diagnostic.code !== DiagnosticCode.CANNOT_FIND_NAME + (diagnostic.code !== DiagnosticCode.CANNOT_FIND_NAME && + diagnostic.code !== DiagnosticCode.CANNOT_FIND_NAME_X_DID_YOU_MEAN_Y) ) { continue; } @@ -314,7 +315,7 @@ export class CodeActionsProviderImpl implements CodeActionsProvider { virtualDoc.openedByClient = true; // let typescript know about the virtual document // getLSAndTSDoc instead of getSnapshot so that project dirty state is correctly tracked by us - // otherwise, sometime the applied code fix might not be picked up by the language service + // otherwise, sometime the applied code fix might not be picked up by the language service // because we think the project is still dirty and doesn't update the project version await this.lsAndTsDocResolver.getLSAndTSDoc(virtualDoc); @@ -858,7 +859,11 @@ export class CodeActionsProviderImpl implements CodeActionsProvider { if (codeFix.fixName === FIX_IMPORT_FIX_NAME) { const allCannotFindNameDiagnostics = lang .getSemanticDiagnostics(fileName) - .filter((diagnostic) => diagnostic.code === DiagnosticCode.CANNOT_FIND_NAME); + .filter( + (diagnostic) => + diagnostic.code === DiagnosticCode.CANNOT_FIND_NAME || + diagnostic.code === DiagnosticCode.CANNOT_FIND_NAME_X_DID_YOU_MEAN_Y + ); if (allCannotFindNameDiagnostics.length < 2) { checkedFixIds.add(codeFix.fixId); diff --git a/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts b/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts index 578562942..781dfa795 100644 --- a/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts +++ b/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts @@ -938,7 +938,7 @@ describe('CodeActionsProvider', function () { }); }); - it.only('provide quick fix to fix all missing import component', async () => { + it('provide quick fix to fix all missing import component', async () => { const { provider, document, docManager, lsAndTsDocResolver } = setup( 'codeaction-custom-fix-all-component.svelte' ); @@ -1007,6 +1007,58 @@ describe('CodeActionsProvider', function () { assert.strictEqual(cannotFindNameDiagnostics.length, 0); }); + it('provide quick fix to fix all missing import component with typo diagnostics', async () => { + const { provider, document } = setup('codeaction-custom-fix-all-component4.svelte'); + + const range = Range.create(Position.create(4, 1), Position.create(4, 15)); + const codeActions = await provider.getCodeActions(document, range, { + diagnostics: [ + { + code: DiagnosticCode.CANNOT_FIND_NAME_X_DID_YOU_MEAN_Y, + message: "Cannot find name 'FixAllImported'. Did you mean 'FixAllImported3'?", + range: range, + source: 'ts' + } + ], + only: [CodeActionKind.QuickFix] + }); + + const fixAll = codeActions.find((action) => action.data); + const resolvedFixAll = await provider.resolveCodeAction(document, fixAll!); + + (resolvedFixAll?.edit?.documentChanges?.[0])?.edits.forEach( + (edit) => (edit.newText = harmonizeNewLines(edit.newText)) + ); + + assert.deepStrictEqual(resolvedFixAll.edit, { + documentChanges: [ + { + edits: [ + { + newText: + `\n${indent}import FixAllImported from \"./importing/FixAllImported.svelte\";\n` + + `${indent}import FixAllImported2 from \"./importing/FixAllImported2.svelte\";\n`, + range: { + start: { + character: 18, + line: 0 + }, + end: { + character: 18, + line: 0 + } + } + } + ], + textDocument: { + uri: getUri('codeaction-custom-fix-all-component4.svelte'), + version: null + } + } + ] + }); + }); + it('provide quick fix to fix all missing import component without duplicate (script)', async () => { const { provider, document } = setup('codeaction-custom-fix-all-component2.svelte'); diff --git a/packages/language-server/test/plugins/typescript/testfiles/code-actions/codeaction-custom-fix-all-component4.svelte b/packages/language-server/test/plugins/typescript/testfiles/code-actions/codeaction-custom-fix-all-component4.svelte new file mode 100644 index 000000000..0e75af233 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/testfiles/code-actions/codeaction-custom-fix-all-component4.svelte @@ -0,0 +1,6 @@ + + + +