Skip to content

Commit

Permalink
(fix) make organize imports work with module script (#454)
Browse files Browse the repository at this point in the history
#221
```
<script context="module">
  import {c} from './c';
</script>

<script lang="ts">
  import B from './B';
  import A from './A';
</script>

<A>{c}</A>
```

---->

```
<script context="module">
  import A from './A';
  import { c } from './c';
</script>

<script lang="ts">

</script>

<A>{c}</A>
```
  • Loading branch information
dummdidumm authored Aug 14, 2020
1 parent fc7c064 commit b50eae1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
// Handle svelte2tsx wrong import mapping:
// The character after the last import maps to the start of the script
// TODO find a way to fix this in svelte2tsx and then remove this
if (range.end.line === 0 && range.end.character === 1) {
if (
(range.end.line === 0 && range.end.character === 1) ||
range.end.line < range.start.line
) {
edit.span.length -= 1;
range = mapRangeToOriginal(fragment, convertRange(fragment, edit.span));
range.end.character += 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,81 @@ describe('CodeActionsProvider', () => {
]);
});

it('organizes imports with module script', async () => {
const { provider, document } = setup('organize-imports-with-module.svelte');

const codeActions = await provider.getCodeActions(
document,
Range.create(Position.create(1, 4), Position.create(1, 5)), // irrelevant
{
diagnostics: [],
only: [CodeActionKind.SourceOrganizeImports],
},
);
(<TextDocumentEdit>codeActions[0]?.edit?.documentChanges?.[0])?.edits.forEach(
(edit) => (edit.newText = harmonizeNewLines(edit.newText)),
);

assert.deepStrictEqual(codeActions, [
{
edit: {
documentChanges: [
{
edits: [
{
// eslint-disable-next-line max-len
newText: "import A from './A';\nimport { c } from './c';\n",
range: {
start: {
line: 1,
character: 2,
},
end: {
line: 2,
character: 0,
},
},
},
{
newText: '',
range: {
start: {
line: 6,
character: 2,
},
end: {
line: 7,
character: 2,
},
},
},
{
newText: '',
range: {
start: {
line: 7,
character: 2,
},
end: {
line: 7,
character: 22,
},
},
},
],
textDocument: {
uri: getUri('organize-imports-with-module.svelte'),
version: null,
},
},
],
},
kind: CodeActionKind.SourceOrganizeImports,
title: 'Organize Imports',
},
]);
});

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script context="module">
import {c} from './c';
// do whatever
</script>

<script lang="ts">
import B from './B';
import A from './A';
</script>

<A>{c}</A>

0 comments on commit b50eae1

Please sign in to comment.