Skip to content

Commit 2531658

Browse files
authored
fix: ensure use directives execute in the correct sequence (#13384)
Fixes #13382. This PR ensures that action directives are now executed in the same sequence as Svelte 4, so child element before parent element.
1 parent d423004 commit 2531658

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

.changeset/afraid-trainers-occur.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure use directives execute in the correct sequence

packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,18 @@ export function RegularElement(node, context) {
157157
}
158158
}
159159

160+
/** @type {typeof state} */
161+
const element_after_update_state = { ...context.state, after_update: [] };
162+
160163
for (const attribute of other_directives) {
161164
if (attribute.type === 'OnDirective') {
162165
const handler = /** @type {Expression} */ (context.visit(attribute));
163166

164-
context.state.after_update.push(
167+
element_after_update_state.after_update.push(
165168
b.stmt(has_use ? b.call('$.effect', b.thunk(handler)) : handler)
166169
);
167170
} else {
168-
context.visit(attribute);
171+
context.visit(attribute, element_after_update_state);
169172
}
170173
}
171174

@@ -401,13 +404,19 @@ export function RegularElement(node, context) {
401404
b.block([
402405
...child_state.init,
403406
child_state.update.length > 0 ? build_render_statement(child_state.update) : b.empty,
404-
...child_state.after_update
407+
...child_state.after_update,
408+
...element_after_update_state.after_update
405409
])
406410
);
407411
} else if (node.fragment.metadata.dynamic) {
408412
context.state.init.push(...child_state.init);
409413
context.state.update.push(...child_state.update);
410-
context.state.after_update.push(...child_state.after_update);
414+
context.state.after_update.push(
415+
...child_state.after_update,
416+
...element_after_update_state.after_update
417+
);
418+
} else {
419+
context.state.after_update.push(...element_after_update_state.after_update);
411420
}
412421

413422
if (lookup.has('dir')) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
async test({ assert, logs }) {
5+
assert.deepEqual(logs, ['1', '2', '3', '4', '5', '6']);
6+
}
7+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
const action = (element) => {
3+
console.log(element.id);
4+
};
5+
</script>
6+
7+
<div use:action id="5">
8+
<div use:action id="3">
9+
<div use:action id="1">
10+
</div>
11+
<div use:action id="2">
12+
</div>
13+
</div>
14+
<div use:action id="4">
15+
</div>
16+
</div>
17+
<div use:action id="6">
18+
</div>

0 commit comments

Comments
 (0)