Skip to content

Commit 3abba67

Browse files
authored
(fix) move store decl from import into render fn (#889)
Don't append the store declaration of imports that are stores right after the import. Instead, append it to the start of the render function. This way, imports are grouped without these declarations at the top, which makes "organize imports" behave correctly again and not put ignore-comments into the edits. #879
1 parent 6734686 commit 3abba67

File tree

6 files changed

+126
-22
lines changed

6 files changed

+126
-22
lines changed

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,94 @@ describe('CodeActionsProvider', () => {
316316
]);
317317
});
318318

319+
it('organizes imports with module script and store', async () => {
320+
const { provider, document } = setup('organize-imports-module-store.svelte');
321+
322+
const codeActions = await provider.getCodeActions(
323+
document,
324+
Range.create(Position.create(1, 4), Position.create(1, 5)), // irrelevant
325+
{
326+
diagnostics: [],
327+
only: [CodeActionKind.SourceOrganizeImports]
328+
}
329+
);
330+
(<TextDocumentEdit>codeActions[0]?.edit?.documentChanges?.[0])?.edits.forEach(
331+
(edit) => (edit.newText = harmonizeNewLines(edit.newText))
332+
);
333+
334+
assert.deepStrictEqual(codeActions, [
335+
{
336+
edit: {
337+
documentChanges: [
338+
{
339+
edits: [
340+
{
341+
newText:
342+
"import { _,_d } from 'svelte-i18n';\nimport { _e } from 'svelte-i18n1';\n",
343+
range: {
344+
end: {
345+
character: 0,
346+
line: 2
347+
},
348+
start: {
349+
character: 2,
350+
line: 1
351+
}
352+
}
353+
},
354+
{
355+
newText: '',
356+
range: {
357+
end: {
358+
character: 2,
359+
line: 6
360+
},
361+
start: {
362+
character: 2,
363+
line: 5
364+
}
365+
}
366+
},
367+
{
368+
newText: '',
369+
range: {
370+
end: {
371+
character: 2,
372+
line: 7
373+
},
374+
start: {
375+
character: 2,
376+
line: 6
377+
}
378+
}
379+
},
380+
{
381+
newText: '',
382+
range: {
383+
end: {
384+
character: 37,
385+
line: 7
386+
},
387+
start: {
388+
character: 2,
389+
line: 7
390+
}
391+
}
392+
}
393+
],
394+
textDocument: {
395+
uri: getUri('organize-imports-module-store.svelte'),
396+
version: 0
397+
}
398+
}
399+
]
400+
},
401+
kind: CodeActionKind.SourceOrganizeImports,
402+
title: 'Organize Imports'
403+
}
404+
]);
405+
});
406+
319407
it('should do extract into const refactor', async () => {
320408
const { provider, document } = setup('codeactions.svelte');
321409

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script context="module">
2+
import { _ } from 'svelte-i18n';
3+
</script>
4+
5+
<script>
6+
import { _ } from 'svelte-i18n';
7+
import { _d } from 'svelte-i18n';
8+
import { _e } from 'svelte-i18n1';
9+
</script>
10+
11+
<p>{$_('test')}</p>
12+
<p>{$_d('test')}</p>
13+
<p>{$_e('test')}</p>

packages/svelte2tsx/src/svelte2tsx/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,10 @@ export function svelte2tsx(
460460
}
461461
}
462462

463-
const implicitStoreValues = new ImplicitStoreValues(resolvedStores);
463+
const renderFunctionStart = scriptTag
464+
? str.original.lastIndexOf('>', scriptTag.content.start) + 1
465+
: instanceScriptTarget;
466+
const implicitStoreValues = new ImplicitStoreValues(resolvedStores, renderFunctionStart);
464467
//move the instance script and process the content
465468
let exportedNames = new ExportedNames();
466469
let getters = new Set<string>();
@@ -497,7 +500,7 @@ export function svelte2tsx(
497500
processModuleScriptTag(
498501
str,
499502
moduleScriptTag,
500-
new ImplicitStoreValues(implicitStoreValues.getAccessedStores())
503+
new ImplicitStoreValues(implicitStoreValues.getAccessedStores(), renderFunctionStart)
501504
);
502505
}
503506

packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitStoreValues.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class ImplicitStoreValues {
2020
public addReactiveDeclaration = this.reactiveDeclarations.push.bind(this.reactiveDeclarations);
2121
public addImportStatement = this.importStatements.push.bind(this.importStatements);
2222

23-
constructor(storesResolvedInTemplate: string[] = []) {
23+
constructor(storesResolvedInTemplate: string[] = [], private renderFunctionStart: number) {
2424
storesResolvedInTemplate.forEach(this.addStoreAcess);
2525
}
2626

@@ -37,9 +37,7 @@ export class ImplicitStoreValues {
3737
this.attachStoreValueDeclarationToReactiveAssignment(node, astOffset, str)
3838
);
3939

40-
this.importStatements
41-
.filter(({ name }) => name && this.accessedStores.has(name.getText()))
42-
.forEach((node) => this.attachStoreValueDeclarationToImport(node, astOffset, str));
40+
this.attachStoreValueDeclarationOfImportsToRenderFn(str);
4341
}
4442

4543
public getAccessedStores(): string[] {
@@ -89,17 +87,19 @@ export class ImplicitStoreValues {
8987
str.appendRight(endPos, storeDeclarations);
9088
}
9189

92-
private attachStoreValueDeclarationToImport(
93-
node: ts.ImportClause | ts.ImportSpecifier,
94-
astOffset: number,
95-
str: MagicString
96-
) {
97-
const storeName = node.name.getText();
98-
const storeDeclaration = surroundWithIgnoreComments(this.createStoreDeclaration(storeName));
99-
const importStatement = ts.isImportClause(node) ? node.parent : node.parent.parent.parent;
100-
const endPos = importStatement.getEnd() + astOffset;
90+
private attachStoreValueDeclarationOfImportsToRenderFn(str: MagicString) {
91+
const storeNames = this.importStatements
92+
.filter(({ name }) => name && this.accessedStores.has(name.getText()))
93+
.map(({ name }) => name.getText());
94+
if (!storeNames.length) {
95+
return;
96+
}
97+
98+
const storeDeclarations = surroundWithIgnoreComments(
99+
this.createStoreDeclarations(storeNames)
100+
);
101101

102-
str.appendRight(endPos, storeDeclaration);
102+
str.appendRight(this.renderFunctionStart, storeDeclarations);
103103
}
104104

105105
private createStoreDeclarations(storeNames: string[]): string {

packages/svelte2tsx/test/svelte2tsx/samples/store-from-module/expected.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
///<reference types="svelte" />
22
<></>;
3-
import {store1, store2} from './store';/*Ωignore_startΩ*/;let $store1 = __sveltets_store_get(store1);/*Ωignore_endΩ*//*Ωignore_startΩ*/;let $store2 = __sveltets_store_get(store2);/*Ωignore_endΩ*/
3+
import {store1, store2} from './store';
44
const store3 = writable('')/*Ωignore_startΩ*/;let $store3 = __sveltets_store_get(store3);/*Ωignore_endΩ*/;
55
const store4 = writable('')/*Ωignore_startΩ*/;let $store4 = __sveltets_store_get(store4);/*Ωignore_endΩ*/;
66
;<></>;function render() {
7-
7+
/*Ωignore_startΩ*/;let $store1 = __sveltets_store_get(store1);;let $store2 = __sveltets_store_get(store2);/*Ωignore_endΩ*/
88
;(__sveltets_store_get(store1), $store1);
99
;(__sveltets_store_get(store3), $store3);
1010
;

packages/svelte2tsx/test/svelte2tsx/samples/store-import/expected.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import storeA from './store';
44
import { storeB } from './store';
55
import { storeB as storeC } from './store';
66
function render() {
7-
8-
/*Ωignore_startΩ*/;let $storeA = __sveltets_store_get(storeA);/*Ωignore_endΩ*/
9-
/*Ωignore_startΩ*/;let $storeB = __sveltets_store_get(storeB);/*Ωignore_endΩ*/
10-
/*Ωignore_startΩ*/;let $storeC = __sveltets_store_get(storeC);/*Ωignore_endΩ*/
7+
/*Ωignore_startΩ*/;let $storeA = __sveltets_store_get(storeA);;let $storeB = __sveltets_store_get(storeB);;let $storeC = __sveltets_store_get(storeC);/*Ωignore_endΩ*/
8+
9+
10+
1111
;
1212
() => (<>
1313

0 commit comments

Comments
 (0)