Skip to content

Commit

Permalink
fix: treat spread elements the same as call expressions (#14488)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris authored Dec 1, 2024
1 parent 9fcfd7f commit fe15ad4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-readers-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: treat spread elements the same as call expressions
2 changes: 2 additions & 0 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { RenderTag } from './visitors/RenderTag.js';
import { SlotElement } from './visitors/SlotElement.js';
import { SnippetBlock } from './visitors/SnippetBlock.js';
import { SpreadAttribute } from './visitors/SpreadAttribute.js';
import { SpreadElement } from './visitors/SpreadElement.js';
import { StyleDirective } from './visitors/StyleDirective.js';
import { SvelteBody } from './visitors/SvelteBody.js';
import { SvelteComponent } from './visitors/SvelteComponent.js';
Expand Down Expand Up @@ -163,6 +164,7 @@ const visitors = {
SlotElement,
SnippetBlock,
SpreadAttribute,
SpreadElement,
StyleDirective,
SvelteBody,
SvelteComponent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @import { SpreadElement } from 'estree' */
/** @import { Context } from '../types' */

/**
* @param {SpreadElement} node
* @param {Context} context
*/
export function SpreadElement(node, context) {
if (context.state.expression) {
// treat e.g. `[...x]` the same as `[...x.values()]`
context.state.expression.has_call = true;
context.state.expression.has_state = true;
}

context.next();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let { numbers } = $props();
</script>

{numbers.join(', ')}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
html: '<button>+1</button> 0, 1, 2',

test({ target, assert }) {
const btn = target.querySelector('button');

flushSync(() => btn?.click());
assert.htmlEqual(target.innerHTML, '<button>+1</button> 0, 1, 2, 3');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import { SvelteSet } from 'svelte/reactivity';
import Child from './Child.svelte';
const numbers = new SvelteSet([0, 1, 2]);
</script>

<button onclick={() => numbers.add(numbers.size)}>+1</button>

<Child numbers={[...numbers]} />

0 comments on commit fe15ad4

Please sign in to comment.