Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) don't remove next char in remove import code action #1451

Merged
merged 1 commit into from Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -162,14 +162,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 @@ -337,6 +337,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>