Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,94 @@ describe('CodeActionsProvider', () => {
]);
});

it('organizes imports with module script and store', async () => {
const { provider, document } = setup('organize-imports-module-store.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: [
{
newText:
"import { _,_d } from 'svelte-i18n';\nimport { _e } from 'svelte-i18n1';\n",
range: {
end: {
character: 0,
line: 2
},
start: {
character: 2,
line: 1
}
}
},
{
newText: '',
range: {
end: {
character: 2,
line: 6
},
start: {
character: 2,
line: 5
}
}
},
{
newText: '',
range: {
end: {
character: 2,
line: 7
},
start: {
character: 2,
line: 6
}
}
},
{
newText: '',
range: {
end: {
character: 37,
line: 7
},
start: {
character: 2,
line: 7
}
}
}
],
textDocument: {
uri: getUri('organize-imports-module-store.svelte'),
version: 0
}
}
]
},
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,13 @@
<script context="module">
import { _ } from 'svelte-i18n';
</script>

<script>
import { _ } from 'svelte-i18n';
import { _d } from 'svelte-i18n';
import { _e } from 'svelte-i18n1';
</script>

<p>{$_('test')}</p>
<p>{$_d('test')}</p>
<p>{$_e('test')}</p>
7 changes: 5 additions & 2 deletions packages/svelte2tsx/src/svelte2tsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,10 @@ export function svelte2tsx(
}
}

const implicitStoreValues = new ImplicitStoreValues(resolvedStores);
const renderFunctionStart = scriptTag
? str.original.lastIndexOf('>', scriptTag.content.start) + 1
: instanceScriptTarget;
const implicitStoreValues = new ImplicitStoreValues(resolvedStores, renderFunctionStart);
//move the instance script and process the content
let exportedNames = new ExportedNames();
let getters = new Set<string>();
Expand Down Expand Up @@ -492,7 +495,7 @@ export function svelte2tsx(
processModuleScriptTag(
str,
moduleScriptTag,
new ImplicitStoreValues(implicitStoreValues.getAccessedStores())
new ImplicitStoreValues(implicitStoreValues.getAccessedStores(), renderFunctionStart)
);
}

Expand Down
28 changes: 14 additions & 14 deletions packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitStoreValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ImplicitStoreValues {
public addReactiveDeclaration = this.reactiveDeclarations.push.bind(this.reactiveDeclarations);
public addImportStatement = this.importStatements.push.bind(this.importStatements);

constructor(storesResolvedInTemplate: string[] = []) {
constructor(storesResolvedInTemplate: string[] = [], private renderFunctionStart: number) {
storesResolvedInTemplate.forEach(this.addStoreAcess);
}

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

this.importStatements
.filter(({ name }) => name && this.accessedStores.has(name.getText()))
.forEach((node) => this.attachStoreValueDeclarationToImport(node, astOffset, str));
this.attachStoreValueDeclarationOfImportsToRenderFn(str);
}

public getAccessedStores(): string[] {
Expand Down Expand Up @@ -89,17 +87,19 @@ export class ImplicitStoreValues {
str.appendRight(endPos, storeDeclarations);
}

private attachStoreValueDeclarationToImport(
node: ts.ImportClause | ts.ImportSpecifier,
astOffset: number,
str: MagicString
) {
const storeName = node.name.getText();
const storeDeclaration = surroundWithIgnoreComments(this.createStoreDeclaration(storeName));
const importStatement = ts.isImportClause(node) ? node.parent : node.parent.parent.parent;
const endPos = importStatement.getEnd() + astOffset;
private attachStoreValueDeclarationOfImportsToRenderFn(str: MagicString) {
const storeNames = this.importStatements
.filter(({ name }) => name && this.accessedStores.has(name.getText()))
.map(({ name }) => name.getText());
if (!storeNames.length) {
return;
}

const storeDeclarations = surroundWithIgnoreComments(
this.createStoreDeclarations(storeNames)
);

str.appendRight(endPos, storeDeclaration);
str.appendRight(this.renderFunctionStart, storeDeclarations);
}

private createStoreDeclarations(storeNames: string[]): string {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
///<reference types="svelte" />
<></>;
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Ω*/
import {store1, store2} from './store';
const store3 = writable('')/*Ωignore_startΩ*/;let $store3 = __sveltets_store_get(store3);/*Ωignore_endΩ*/;
const store4 = writable('')/*Ωignore_startΩ*/;let $store4 = __sveltets_store_get(store4);/*Ωignore_endΩ*/;
;<></>;function render() {

/*Ωignore_startΩ*/;let $store1 = __sveltets_store_get(store1);;let $store2 = __sveltets_store_get(store2);/*Ωignore_endΩ*/
;(__sveltets_store_get(store1), $store1);
;(__sveltets_store_get(store3), $store3);
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import storeA from './store';
import { storeB } from './store';
import { storeB as storeC } from './store';
function render() {

/*Ωignore_startΩ*/;let $storeA = __sveltets_store_get(storeA);/*Ωignore_endΩ*/
/*Ωignore_startΩ*/;let $storeB = __sveltets_store_get(storeB);/*Ωignore_endΩ*/
/*Ωignore_startΩ*/;let $storeC = __sveltets_store_get(storeC);/*Ωignore_endΩ*/
/*Ωignore_startΩ*/;let $storeA = __sveltets_store_get(storeA);;let $storeB = __sveltets_store_get(storeB);;let $storeC = __sveltets_store_get(storeC);/*Ωignore_endΩ*/



;
() => (<>

Expand Down