Skip to content

Commit

Permalink
(fix) don't remove next char in remove import code action (#1451)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 committed Apr 29, 2022
1 parent 68ce356 commit 5a8eed4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
Expand Up @@ -192,14 +192,22 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
) {
edit.span.length -= 1;
range = mapRangeToOriginal(fragment, convertRange(fragment, edit.span));
range.end.character += 1;
if (
fragment instanceof SvelteSnapshotFragment &&
isAtEndOfLine(
getLineAtPosition(range.end, fragment.originalText),
range.end.character
)
) {

if (!(fragment instanceof SvelteSnapshotFragment)) {
range.end.character += 1;
return range;
}

const line = getLineAtPosition(range.end, fragment.originalText);
// remove-import code action will removes the
// line break generated by svelte2tsx,
// but when there's no line break in the source
// move back to next character would remove the next character
if ([';', '"', "'"].includes(line[range.end.character])) {
range.end.character += 1;
}

if (isAtEndOfLine(line, range.end.character)) {
range.end.line += 1;
range.end.character = 0;
}
Expand Down
Expand Up @@ -340,6 +340,52 @@ function test(useNewTransformation: boolean) {
]);
});

it('remove import inline with script tag', async () => {
const { provider, document } = setup('remove-imports-inline.svelte');

const codeActions = await provider.getCodeActions(
document,
Range.create(Position.create(0, 9), Position.create(0, 9)),
{
diagnostics: [
{
code: 6133,
message: "'CodeActions' is declared but its value is never read",
range: Range.create(Position.create(0, 8), Position.create(0, 54)),
source: 'js'
}
],
only: [CodeActionKind.QuickFix]
}
);

assert.deepStrictEqual(codeActions, <CodeAction[]>[
{
edit: {
documentChanges: [
{
edits: [
{
newText: '',
range: {
end: Position.create(0, 54),
start: Position.create(0, 8)
}
}
],
textDocument: {
uri: getUri('remove-imports-inline.svelte'),
version: null
}
}
]
},
kind: 'quickfix',
title: "Remove import from './codeactions.svelte'"
}
]);
});

it('organizes imports', async () => {
const { provider, document } = setup('codeactions.svelte');

Expand Down
@@ -0,0 +1 @@
<script>import CodeActions from './codeactions.svelte'</script>

0 comments on commit 5a8eed4

Please sign in to comment.