add each.metadata.keyed

pull/12642/head
Rich Harris 2 years ago
parent 7af0e604d8
commit 9eefe34b2a

@ -1547,6 +1547,13 @@ const common_visitors = {
},
RenderTag(node, context) {
context.next({ ...context.state, render_tag: node });
},
EachBlock(node) {
if (node.key) {
// treat `{#each items as item, i (i)}` as a normal indexed block, everything else as keyed
node.metadata.keyed =
node.key.type !== 'Identifier' || !node.index || node.key.name !== node.index;
}
}
};

@ -2409,10 +2409,7 @@ export const template_visitors = {
let flags = 0;
if (
node.key &&
(node.key.type !== 'Identifier' || !node.index || node.key.name !== node.index)
) {
if (node.metadata.keyed) {
flags |= EACH_KEYED;
if (node.index) {
@ -2421,7 +2418,7 @@ export const template_visitors = {
// In runes mode, if key === item, we don't need to wrap the item in a source
const key_is_item =
node.key.type === 'Identifier' &&
/** @type {Expression} */ (node.key).type === 'Identifier' &&
node.context.type === 'Identifier' &&
node.context.name === node.key.name;
@ -2614,8 +2611,11 @@ export const template_visitors = {
/** @type {Expression} */
let key_function = b.id('$.index');
if (node.key) {
const expression = /** @type {Expression} */ (context.visit(node.key, key_state));
if (node.metadata.keyed) {
const expression = /** @type {Expression} */ (
context.visit(/** @type {Expression} */ (node.key), key_state)
);
key_function = b.arrow([node.context, index], expression);
}

@ -592,6 +592,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
}
node.metadata = {
keyed: false,
contains_group_binding: false,
array_name: needs_array_deduplication ? state.scope.root.unique('$$array') : null,
index: scope.root.unique('$$index'),

@ -393,6 +393,7 @@ export interface EachBlock extends BaseNode {
index?: string;
key?: Expression;
metadata: {
keyed: boolean;
contains_group_binding: boolean;
/** Set if something in the array expression is shadowed within the each block */
array_name: Identifier | null;

Loading…
Cancel
Save