Skip to content

Commit

Permalink
(fix) don't organize imports in error state (#1500)
Browse files Browse the repository at this point in the history
If there's a parser error, we fall back to only the script contents, so organize imports likely throws out a lot of seemingly unused imports because they are only used in the template. Therefore do nothing in this case.
#1476
  • Loading branch information
dummdidumm committed May 28, 2022
1 parent 16b34c4 commit 34a5fad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Expand Up @@ -103,7 +103,10 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
const { lang, tsDoc, userPreferences } = await this.getLSAndTSDoc(document);
const fragment = tsDoc.getFragment();

if (cancellationToken?.isCancellationRequested) {
if (cancellationToken?.isCancellationRequested || tsDoc.parserError) {
// If there's a parser error, we fall back to only the script contents,
// so organize imports likely throws out a lot of seemingly unused imports
// because they are only used in the template. Therefore do nothing in this case.
return [];
}

Expand Down
Expand Up @@ -905,6 +905,21 @@ function test(useNewTransformation: boolean) {
]);
});

it('organize imports should do nothing if there is a parser error', async () => {
const { provider, document } = setup('organize-imports-error.svelte');

const codeActions = await provider.getCodeActions(
document,
Range.create(Position.create(1, 4), Position.create(1, 5)),
{
diagnostics: [],
only: [CodeActionKind.SourceOrganizeImports]
}
);

assert.deepStrictEqual(codeActions, []);
});

it('should do extract into const refactor', async () => {
const { provider, document } = setup('codeactions.svelte');

Expand Down

0 comments on commit 34a5fad

Please sign in to comment.