pull/12744/head
Rich Harris 2 years ago
parent 5518381a99
commit f72ecb84cc

@ -159,12 +159,7 @@ export function EachBlock(node, context) {
const index =
each_node_meta.contains_group_binding || !node.index ? each_node_meta.index : b.id(node.index);
const item = each_node_meta.item;
const binding = /** @type {Binding} */ (context.state.scope.get(item.name));
const getter = (/** @type {Identifier} */ id) => {
const item_with_loc = with_loc(item, id);
return (flags & EACH_ITEM_REACTIVE) === 0 ? item_with_loc : b.call('$.unwrap', item_with_loc);
};
child_state.getters[item.name] = getter;
const unwrapped = (flags & EACH_ITEM_REACTIVE) === 0 ? item : b.call('$.get', item);
if (node.index) {
child_state.getters[node.index] = (id) => {
@ -178,6 +173,8 @@ export function EachBlock(node, context) {
/** @type {Statement[]} */
const declarations = [];
child_state.getters[item.name] = unwrapped;
if (node.context.type === 'Identifier') {
child_state.setters[node.context.name] = create_mutation(
b.member(
@ -189,15 +186,14 @@ export function EachBlock(node, context) {
key_state.getters[node.context.name] = node.context;
} else {
const unwrapped = getter(binding.node);
const paths = extract_paths(node.context);
for (const path of paths) {
const name = /** @type {Identifier} */ (path.node).name;
const binding = /** @type {Binding} */ (context.state.scope.get(name));
const needs_derived = path.has_default_value; // to ensure that default value is only called once
const fn = b.thunk(
/** @type {Expression} */ (context.visit(path.expression?.(unwrapped), child_state))
/** @type {Expression} */ (context.visit(path.expression?.(item), child_state))
);
declarations.push(b.let(path.node, needs_derived ? b.call('$.derived_safe_equal', fn) : fn));
@ -205,7 +201,7 @@ export function EachBlock(node, context) {
const getter = needs_derived ? b.call('$.get', b.id(name)) : b.call(name);
child_state.getters[name] = getter;
child_state.setters[name] = create_mutation(
/** @type {Pattern} */ (path.update_expression(unwrapped))
/** @type {Pattern} */ (context.visit(path.update_expression(item), child_state))
);
// we need to eagerly evaluate the expression in order to hit any

@ -9,15 +9,23 @@ import { build_hoisted_params } from '../../utils.js';
export const visit_function = (node, context) => {
const metadata = node.metadata;
let state = context.state;
let state = { ...context.state, getters: { ...context.state.getters }, in_constructor: false };
// TODO do this in the `_` visitor?
for (const [name, binding] of state.scope.declarations) {
if (binding.declaration_kind === 'param') {
// TODO this should be unnecessary, EachBlock should declare its own scope
state.getters[name] = binding.node;
}
}
if (node.type === 'FunctionExpression') {
const parent = /** @type {Node} */ (context.path.at(-1));
const in_constructor = parent.type === 'MethodDefinition' && parent.kind === 'constructor';
state.in_constructor = parent.type === 'MethodDefinition' && parent.kind === 'constructor';
state = { ...context.state, in_constructor };
} else {
state = { ...context.state, in_constructor: false };
if (node.id) {
state.getters[node.id.name] = node.id;
}
}
if (metadata?.hoisted === true) {

Loading…
Cancel
Save