Skip to content

Commit

Permalink
feat: allow as expressions for bindable props (#2372)
Browse files Browse the repository at this point in the history
* feat: allow `as` expressions for bindable props

* chore: add test to ts-runes-bindable

* chore: copy paste expected without formatting

* chore: test tabs vs spaces

* chore: fix expected for svelte 5
  • Loading branch information
paoloricciuti authored May 17, 2024
1 parent 80622df commit 3147c81
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"range": { "start": { "line": 4, "character": 0 }, "end": { "line": 4, "character": 0 } },
"severity": 1,
"source": "ts",
"message": "`<svelte:element>` must have a 'this' attribute",
"message": "`<svelte:element>` must have a 'this' attribute with a value",
"code": -1
}
]
7 changes: 6 additions & 1 deletion packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ export class ExportedNames {
: element.name.text;

if (element.initializer) {
const call = element.initializer;
let call = element.initializer;
// if it's an as expression we need to check wether the as
// expression expression is a call
if (ts.isAsExpression(call)) {
call = call.expression;
}
if (ts.isCallExpression(call) && ts.isIdentifier(call.expression)) {
if (call.expression.text === '$bindable') {
this.$props.bindings.push(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
///<reference types="svelte" />
;function render() {
/*Ωignore_startΩ*/;type $$ComponentProps = { a: unknown, b?: unknown };/*Ωignore_endΩ*/
let { a, b = $bindable() }: $$ComponentProps = $props();
/*Ωignore_startΩ*/;type $$ComponentProps = { a: unknown, b?: unknown, c?: number };/*Ωignore_endΩ*/
let { a, b = $bindable(), c = $bindable(0) as number }: $$ComponentProps = $props();
;
async () => {};
return { props: {} as any as $$ComponentProps, slots: {}, events: {} }}

export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_with_any_event(render())) {
constructor(options = __sveltets_2_runes_constructor(__sveltets_2_with_any_event(render()))) { super(options); }
$$bindings = __sveltets_$$bindings('b');
$$bindings = __sveltets_$$bindings('b', 'c');
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<script lang="ts">
let { a, b = $bindable() } = $props();
let { a, b = $bindable(), c = $bindable(0) as number } = $props();
</script>

0 comments on commit 3147c81

Please sign in to comment.