Skip to content

Commit

Permalink
fix: handle runes looking like stores
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Nov 10, 2023
1 parent 64d7b77 commit 221ee2e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/svelte2tsx/src/svelte2tsx/processInstanceScriptContent.ts
Expand Up @@ -119,8 +119,9 @@ export function processInstanceScriptContent(
}
}
} else {
const text = ident.text;
//track potential store usage to be resolved
if (ident.text.startsWith('$')) {
if (text.startsWith('$')) {
if (
(!ts.isPropertyAccessExpression(parent) || parent.expression == ident) &&
(!ts.isPropertyAssignment(parent) || parent.initializer == ident) &&
Expand All @@ -130,7 +131,15 @@ export function processInstanceScriptContent(
!ts.isTypeAliasDeclaration(parent) &&
!ts.isInterfaceDeclaration(parent)
) {
pendingStoreResolutions.push({ node: ident, parent, scope });
// Handle the const { ...props } = $props() case
const is_rune =
(text === '$props' || text === '$derived' || text === '$state') &&
ts.isCallExpression(parent) &&
ts.isVariableDeclaration(parent.parent) &&
parent.parent.name.getText().includes(text.slice(1));
if (!is_rune) {
pendingStoreResolutions.push({ node: ident, parent, scope });
}
}
}
}
Expand Down
@@ -0,0 +1,14 @@
///<reference types="svelte" />
;function render() {

let { props } = $props();
let state = $state(0);
let derived = $derived(state * 2);
;
async () => {

state; derived;};
return { props: /** @type {Record<string, never>} */ ({}), slots: {}, events: {} }}

export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(__sveltets_2_with_any_event(render()))) {
}
@@ -0,0 +1,7 @@
<script>
let { props } = $props();
let state = $state(0);
let derived = $derived(state * 2);
</script>

{state} {derived}
@@ -0,0 +1,17 @@
///<reference types="svelte" />
;function render() {

const props = null/*Ωignore_startΩ*/;let $props = __sveltets_2_store_get(props);/*Ωignore_endΩ*/;
$props;
const state = null/*Ωignore_startΩ*/;let $state = __sveltets_2_store_get(state);/*Ωignore_endΩ*/;
$state;
const derived = null/*Ωignore_startΩ*/;let $derived = __sveltets_2_store_get(derived);/*Ωignore_endΩ*/;
$derived;
;
async () => {

state; derived;};
return { props: /** @type {Record<string, never>} */ ({}), slots: {}, events: {} }}

export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(__sveltets_2_with_any_event(render()))) {
}
@@ -0,0 +1,10 @@
<script>
const props = null;
$props;
const state = null;
$state;
const derived = null;
$derived;
</script>

{state} {derived}

0 comments on commit 221ee2e

Please sign in to comment.