Skip to content

Commit 05c515e

Browse files
authored
fix: prevent unused variable error for bindable (#2710)
#2268
1 parent c3ffb3a commit 05c515e

File tree

7 files changed

+35
-4
lines changed

7 files changed

+35
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"range": { "start": { "line": 3, "character": 8 }, "end": { "line": 3, "character": 12 } },
4+
"severity": 4,
5+
"source": "ts",
6+
"message": "'foo2' is declared but its value is never read.",
7+
"code": 6133,
8+
"tags": [1]
9+
}
10+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script lang="ts">
2+
let {
3+
foo = $bindable(),
4+
foo2
5+
} = $props();
6+
7+
function onClick() {
8+
foo = 42;
9+
}
10+
</script>
11+
12+
<button onclick={onClick}>Click me</button>

packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ export class ExportedNames {
177177
}
178178
}
179179

180+
if (this.$props.bindings.length > 0) {
181+
this.str.appendLeft(
182+
node.end + this.astOffset,
183+
surroundWithIgnoreComments(
184+
';' + this.$props.bindings.map((prop) => prop + ';').join('')
185+
)
186+
);
187+
}
188+
180189
// Easy mode: User uses TypeScript and typed the $props() rune
181190
if (node.initializer.typeArguments?.length > 0 || node.type) {
182191
this.hoistableInterfaces.analyze$propsRune(node);

packages/svelte2tsx/test/svelte2tsx/samples/runes-best-effort-types.v5/expectedv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
///<reference types="svelte" />
22
;function $$render() {
33

4-
let/** @typedef {{ a: any, b?: boolean, c?: number, d?: string, e?: any, f?: Record<string, any>, g?: typeof foo, h?: any[], i?: any, j?: any, k?: number, l?: Function }} $$ComponentProps *//** @type {$$ComponentProps} */ { a, b = true, c = 1, d = '', e = null, f = {}, g = foo, h = [], i = undefined, j = $bindable(), k = $bindable(1), l = () => {} } = $props();
4+
let/** @typedef {{ a: any, b?: boolean, c?: number, d?: string, e?: any, f?: Record<string, any>, g?: typeof foo, h?: any[], i?: any, j?: any, k?: number, l?: Function }} $$ComponentProps *//** @type {$$ComponentProps} */ { a, b = true, c = 1, d = '', e = null, f = {}, g = foo, h = [], i = undefined, j = $bindable(), k = $bindable(1), l = () => {} } = $props()/*Ωignore_startΩ*/;j;k;/*Ωignore_endΩ*/;
55
;
66
async () => {};
77
return { props: /** @type {$$ComponentProps} */({}), exports: {}, bindings: __sveltets_$$bindings('j', 'k'), slots: {}, events: {} }}

packages/svelte2tsx/test/svelte2tsx/samples/runes-bindable.v5/expectedv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
///<reference types="svelte" />
22
;function $$render() {
33

4-
let/** @typedef {{ a: any, b?: any }} $$ComponentProps *//** @type {$$ComponentProps} */ { a, b = $bindable() } = $props();
4+
let/** @typedef {{ a: any, b?: any }} $$ComponentProps *//** @type {$$ComponentProps} */ { a, b = $bindable() } = $props()/*Ωignore_startΩ*/;b;/*Ωignore_endΩ*/;
55
;
66
async () => {};
77
return { props: /** @type {$$ComponentProps} */({}), exports: {}, bindings: __sveltets_$$bindings('b'), slots: {}, events: {} }}

packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-best-effort-types.v5/expectedv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
///<reference types="svelte" />
22
;function $$render() {
33
/*Ωignore_startΩ*/;type $$ComponentProps = { a: any, b?: boolean, c?: number, d?: string, e?: any, f?: Record<string, any>, g?: typeof foo, h?: Bar, i?: Baz, j?: any[], k?: any, l?: any, m?: number, n?: Function };/*Ωignore_endΩ*/
4-
let { a, b = true, c = 1, d = '', e = null, f = {}, g = foo, h = null as Bar, i = null as any as Baz, j = [], k = undefined, l = $bindable(), m = $bindable(1), n = () => {} }: $$ComponentProps = $props();
4+
let { a, b = true, c = 1, d = '', e = null, f = {}, g = foo, h = null as Bar, i = null as any as Baz, j = [], k = undefined, l = $bindable(), m = $bindable(1), n = () => {} }: $$ComponentProps = $props()/*Ωignore_startΩ*/;l;m;/*Ωignore_endΩ*/;
55
;
66
async () => {};
77
return { props: {} as any as $$ComponentProps, exports: {}, bindings: __sveltets_$$bindings('l', 'm'), slots: {}, events: {} }}

packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-bindable.v5/expectedv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
///<reference types="svelte" />
22
;function $$render() {
33
/*Ωignore_startΩ*/;type $$ComponentProps = { a: any, b?: any, c?: number };/*Ωignore_endΩ*/
4-
let { a, b = $bindable(), c = $bindable(0) as number }: $$ComponentProps = $props();
4+
let { a, b = $bindable(), c = $bindable(0) as number }: $$ComponentProps = $props()/*Ωignore_startΩ*/;b;c;/*Ωignore_endΩ*/;
55
;
66
async () => {};
77
return { props: {} as any as $$ComponentProps, exports: {}, bindings: __sveltets_$$bindings('b', 'c'), slots: {}, events: {} }}

0 commit comments

Comments
 (0)