Skip to content

Commit

Permalink
fix-all as well
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 committed May 16, 2024
1 parent 58d5094 commit 159fa82
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
Expand Down Expand Up @@ -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!);

(<TextDocumentEdit>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');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script lang="ts">
class FixAllImported3 {}
</script>

<FixAllImported />
<FixAllImported2 />

0 comments on commit 159fa82

Please sign in to comment.