fix: improve each block index handling (#9644)

* fix: improve each block index handling

* format
pull/9642/head
Dominic Gannaway 1 year ago committed by GitHub
parent 02f3f42981
commit dee5bed829
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: improve each block index handling

@ -2491,7 +2491,7 @@ export const template_visitors = {
next(); next();
}, },
BindDirective(node, context) { BindDirective(node, context) {
const { state, path } = context; const { state, path, visit } = context;
/** @type {import('estree').Expression[]} */ /** @type {import('estree').Expression[]} */
const properties = []; const properties = [];
@ -2621,10 +2621,18 @@ export const template_visitors = {
break; break;
} }
case 'this': case 'this': {
call_expr = b.call(`$.bind_this`, state.node, setter, node.expression); const expression = node.expression;
call_expr = b.call(
`$.bind_this`,
state.node,
setter,
expression.type === 'Identifier'
? expression
: /** @type {import('estree').Expression} */ (visit(expression))
);
break; break;
}
case 'textContent': case 'textContent':
case 'innerHTML': case 'innerHTML':
case 'innerText': case 'innerText':

@ -495,12 +495,10 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
} }
if (node.index) { if (node.index) {
scope.declare( const is_keyed =
b.id(node.index), node.key &&
// TODO see logic in EachBlock in dom.ts (node.key.type !== 'Identifier' || !node.index || node.key.name !== node.index);
node.key ? 'derived' : 'normal', scope.declare(b.id(node.index), is_keyed ? 'derived' : 'normal', 'const');
'const'
);
} }
if (node.key) visit(node.key, { scope }); if (node.key) visit(node.key, { scope });

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
html: '<div>0</div><div>1</div>'
});

@ -0,0 +1,3 @@
{#each ["a", "b"] as result, i (i)}
<div>{i}</div>
{/each}
Loading…
Cancel
Save