Repository: sveltejs/language-tools
Pull request #1173 (2021-09-10 15:19:13)

(fix) handle $store imported in module script
When module script is the only script
#1172


Patch:
diff --git a/packages/svelte2tsx/src/svelte2tsx/index.ts b/packages/svelte2tsx/src/svelte2tsx/index.ts
index da6a31d6..d74b5fce 100644
--- a/packages/svelte2tsx/src/svelte2tsx/index.ts
+++ b/packages/svelte2tsx/src/svelte2tsx/index.ts
@@ -372,7 +372,11 @@ export function svelte2tsx(
         processModuleScriptTag(
             str,
             moduleScriptTag,
-            new ImplicitStoreValues(implicitStoreValues.getAccessedStores(), renderFunctionStart)
+            new ImplicitStoreValues(
+                implicitStoreValues.getAccessedStores(),
+                renderFunctionStart,
+                scriptTag ? undefined : (input) => `</>;${input}<>`
+            )
         );
     }
 
diff --git a/packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitStoreValues.ts b/packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitStoreValues.ts
index 1474f5c4..443230c9 100644
--- a/packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitStoreValues.ts
+++ b/packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitStoreValues.ts
@@ -21,7 +21,11 @@ export class ImplicitStoreValues {
     public addReactiveDeclaration = this.reactiveDeclarations.push.bind(this.reactiveDeclarations);
     public addImportStatement = this.importStatements.push.bind(this.importStatements);
 
-    constructor(storesResolvedInTemplate: string[] = [], private renderFunctionStart: number) {
+    constructor(
+        storesResolvedInTemplate: string[] = [],
+        private renderFunctionStart: number,
+        private storeFromImportsWrapper = (input: string) => input
+    ) {
         storesResolvedInTemplate.forEach(this.addStoreAcess);
     }
 
@@ -103,8 +107,8 @@ export class ImplicitStoreValues {
             return;
         }
 
-        const storeDeclarations = surroundWithIgnoreComments(
-            this.createStoreDeclarations(storeNames)
+        const storeDeclarations = this.storeFromImportsWrapper(
+            surroundWithIgnoreComments(this.createStoreDeclarations(storeNames))
         );
 
         str.appendRight(this.renderFunctionStart, storeDeclarations);


Test patch:
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/$store-no-instance-only-module-script/expected.tsx b/packages/svelte2tsx/test/svelte2tsx/samples/$store-no-instance-only-module-script/expected.tsx
new file mode 100644
index 00000000..6fec99a3
--- /dev/null
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/$store-no-instance-only-module-script/expected.tsx
@@ -0,0 +1,13 @@
+///<reference types="svelte" />
+<></>;
+    import { store1 } from './somewhere';
+    const store2 = ''/*Ωignore_startΩ*/;let $store2 = __sveltets_1_store_get(store2);/*Ωignore_endΩ*/;
+;<></>;function render() {
+<></>;/*Ωignore_startΩ*/;let $store1 = __sveltets_1_store_get(store1);/*Ωignore_endΩ*/<>
+
+{(__sveltets_1_store_get(store1), $store1)}
+{(__sveltets_1_store_get(store2), $store2)}</>
+return { props: {}, slots: {}, getters: {}, events: {} }}
+
+export default class Input__SvelteComponent_ extends __sveltets_1_createSvelte2TsxComponent(__sveltets_1_partial(__sveltets_1_with_any_event(render()))) {
+}
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/$store-no-instance-only-module-script/input.svelte b/packages/svelte2tsx/test/svelte2tsx/samples/$store-no-instance-only-module-script/input.svelte
new file mode 100644
index 00000000..50bc7392
--- /dev/null
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/$store-no-instance-only-module-script/input.svelte
@@ -0,0 +1,7 @@
+<script context="module">
+    import { store1 } from './somewhere';
+    const store2 = '';
+</script>
+
+{$store1}
+{$store2}