Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chatty-windows-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure snippet hoisting works in the correct scope
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ function can_hoist_snippet(scope, scopes, visited = new Set()) {
if (binding.initial?.type === 'SnippetBlock') {
if (visited.has(binding)) continue;
visited.add(binding);
const snippet_scope = /** @type {Scope} */ (scopes.get(binding.initial));

if (can_hoist_snippet(binding.scope, scopes, visited)) {
if (can_hoist_snippet(snippet_scope, scopes, visited)) {
continue;
}
}
Expand Down
6 changes: 1 addition & 5 deletions packages/svelte/src/compiler/phases/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,12 +628,8 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {

SnippetBlock(node, context) {
const state = context.state;
// Special-case for root-level snippets: they become part of the instance scope
const is_top_level = !context.path.at(-2);
let scope = state.scope;
if (is_top_level) {
scope = /** @type {Scope} */ (parent);
}

scope.declare(node.expression, 'normal', 'function', node);

const child_scope = state.scope.child();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
"end": 20,
"type": "Action",
"name": "autofocus",
"modifiers": [],
"expression": null
"expression": null,
"modifiers": []
},
{
"start": 21,
"end": 34,
"type": "Action",
"name": "autofocus",
"modifiers": [],
"expression": null
"expression": null,
"modifiers": []
}
],
"children": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"end": 39,
"type": "Action",
"name": "tooltip",
"modifiers": [],
"expression": {
"type": "CallExpression",
"start": 21,
Expand Down Expand Up @@ -66,7 +65,8 @@
}
],
"optional": false
}
},
"modifiers": []
}
],
"children": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"end": 28,
"type": "Action",
"name": "tooltip",
"modifiers": [],
"expression": {
"type": "Identifier",
"start": 20,
Expand All @@ -31,7 +30,8 @@
}
},
"name": "message"
}
},
"modifiers": []
}
],
"children": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"end": 36,
"type": "Action",
"name": "tooltip",
"modifiers": [],
"expression": {
"type": "Literal",
"start": 21,
Expand All @@ -32,7 +31,8 @@
},
"value": "tooltip msg",
"raw": "'tooltip msg'"
}
},
"modifiers": []
}
],
"children": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"end": 20,
"type": "Action",
"name": "autofocus",
"modifiers": [],
"expression": null
"expression": null,
"modifiers": []
}
],
"children": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"end": 50,
"type": "Animation",
"name": "flip",
"modifiers": [],
"expression": null
"expression": null,
"modifiers": []
}
],
"children": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"end": 22,
"type": "Class",
"name": "foo",
"modifiers": [],
"expression": {
"type": "Identifier",
"start": 16,
Expand All @@ -31,7 +30,8 @@
}
},
"name": "isFoo"
}
},
"modifiers": []
}
],
"children": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"end": 23,
"type": "EventHandler",
"name": "click",
"modifiers": [],
"expression": {
"type": "Identifier",
"start": 19,
Expand All @@ -31,7 +30,8 @@
}
},
"name": "foo"
}
},
"modifiers": []
}
],
"children": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
"end": 46,
"type": "Binding",
"name": "foo",
"modifiers": [],
"expression": {
"start": 43,
"end": 46,
"type": "Identifier",
"name": "foo"
}
},
"modifiers": []
}
],
"children": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"end": 55,
"type": "Binding",
"name": "value",
"modifiers": [],
"expression": {
"type": "Identifier",
"start": 50,
Expand All @@ -38,7 +37,8 @@
}
},
"name": "name"
}
},
"modifiers": []
}
],
"children": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"end": 45,
"type": "EventHandler",
"name": "click",
"modifiers": [],
"expression": {
"type": "ArrowFunctionExpression",
"start": 19,
Expand Down Expand Up @@ -100,7 +99,8 @@
}
}
}
}
},
"modifiers": []
}
],
"children": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"end": 692,
"type": "EventHandler",
"name": "click",
"modifiers": [],
"expression": {
"type": "ArrowFunctionExpression",
"start": 596,
Expand Down Expand Up @@ -137,7 +136,8 @@
"end": 594
}
]
}
},
"modifiers": []
}
],
"children": [
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/tests/parser-legacy/samples/refs/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"end": 53,
"type": "Binding",
"name": "this",
"modifiers": [],
"expression": {
"type": "Identifier",
"start": 49,
Expand All @@ -38,7 +37,8 @@
}
},
"name": "foo"
}
},
"modifiers": []
}
],
"children": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"end": 12,
"type": "Transition",
"name": "fade",
"modifiers": [],
"expression": null,
"modifiers": [],
"intro": true,
"outro": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"end": 30,
"type": "Transition",
"name": "style",
"modifiers": [],
"expression": {
"type": "ObjectExpression",
"start": 16,
Expand Down Expand Up @@ -85,6 +84,7 @@
}
]
},
"modifiers": [],
"intro": true,
"outro": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"end": 147,
"type": "OnDirective",
"name": "click",
"modifiers": [],
"expression": {
"type": "ArrowFunctionExpression",
"start": 73,
Expand Down Expand Up @@ -316,7 +315,8 @@
}
]
}
}
},
"modifiers": []
}
],
"fragment": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: 'a'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
let abc = 'a'
</script>

{@render b()}

{#snippet a()}
{abc}
{/snippet}

{#snippet b()}
{@render a()}
{/snippet}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: '<h1>Hello world!</h1>'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script module>
export { foo }
</script>

<script>
let name = 'world';
</script>

<h1>Hello {name}!</h1>

{#snippet foo()}
oo
{/snippet}
Loading