Skip to content

Commit b50eae1

Browse files
authored
(fix) make organize imports work with module script (#454)
#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> ```
1 parent fc7c064 commit b50eae1

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
7474
// Handle svelte2tsx wrong import mapping:
7575
// The character after the last import maps to the start of the script
7676
// TODO find a way to fix this in svelte2tsx and then remove this
77-
if (range.end.line === 0 && range.end.character === 1) {
77+
if (
78+
(range.end.line === 0 && range.end.character === 1) ||
79+
range.end.line < range.start.line
80+
) {
7881
edit.span.length -= 1;
7982
range = mapRangeToOriginal(fragment, convertRange(fragment, edit.span));
8083
range.end.character += 1;

packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,81 @@ describe('CodeActionsProvider', () => {
165165
]);
166166
});
167167

168+
it('organizes imports with module script', async () => {
169+
const { provider, document } = setup('organize-imports-with-module.svelte');
170+
171+
const codeActions = await provider.getCodeActions(
172+
document,
173+
Range.create(Position.create(1, 4), Position.create(1, 5)), // irrelevant
174+
{
175+
diagnostics: [],
176+
only: [CodeActionKind.SourceOrganizeImports],
177+
},
178+
);
179+
(<TextDocumentEdit>codeActions[0]?.edit?.documentChanges?.[0])?.edits.forEach(
180+
(edit) => (edit.newText = harmonizeNewLines(edit.newText)),
181+
);
182+
183+
assert.deepStrictEqual(codeActions, [
184+
{
185+
edit: {
186+
documentChanges: [
187+
{
188+
edits: [
189+
{
190+
// eslint-disable-next-line max-len
191+
newText: "import A from './A';\nimport { c } from './c';\n",
192+
range: {
193+
start: {
194+
line: 1,
195+
character: 2,
196+
},
197+
end: {
198+
line: 2,
199+
character: 0,
200+
},
201+
},
202+
},
203+
{
204+
newText: '',
205+
range: {
206+
start: {
207+
line: 6,
208+
character: 2,
209+
},
210+
end: {
211+
line: 7,
212+
character: 2,
213+
},
214+
},
215+
},
216+
{
217+
newText: '',
218+
range: {
219+
start: {
220+
line: 7,
221+
character: 2,
222+
},
223+
end: {
224+
line: 7,
225+
character: 22,
226+
},
227+
},
228+
},
229+
],
230+
textDocument: {
231+
uri: getUri('organize-imports-with-module.svelte'),
232+
version: null,
233+
},
234+
},
235+
],
236+
},
237+
kind: CodeActionKind.SourceOrganizeImports,
238+
title: 'Organize Imports',
239+
},
240+
]);
241+
});
242+
168243
it('should do extract into const refactor', async () => {
169244
const { provider, document } = setup('codeactions.svelte');
170245

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script context="module">
2+
import {c} from './c';
3+
// do whatever
4+
</script>
5+
6+
<script lang="ts">
7+
import B from './B';
8+
import A from './A';
9+
</script>
10+
11+
<A>{c}</A>

0 commit comments

Comments
 (0)