pull/15961/head
Rich Harris 4 months ago
parent 673151adce
commit 6bb5c27944

@ -290,7 +290,8 @@ export function RegularElement(node, context) {
const { value, has_state } = build_attribute_value( const { value, has_state } = build_attribute_value(
attribute.value, attribute.value,
context, context,
(value, metadata) => (metadata.has_call ? get_expression_id(context.state, value) : value) (value, metadata) =>
metadata.has_call ? get_expression_id(context.state.expressions, value) : value
); );
const update = build_element_attribute_update(node, node_id, name, value, attributes); const update = build_element_attribute_update(node, node_id, name, value, attributes);
@ -482,10 +483,11 @@ function setup_select_synchronization(value_binding, context) {
/** /**
* @param {AST.ClassDirective[]} class_directives * @param {AST.ClassDirective[]} class_directives
* @param {Expression[]} expressions
* @param {ComponentContext} context * @param {ComponentContext} context
* @return {ObjectExpression | Identifier} * @return {ObjectExpression | Identifier}
*/ */
export function build_class_directives_object(class_directives, context) { export function build_class_directives_object(class_directives, expressions, context) {
let properties = []; let properties = [];
let has_call_or_state = false; let has_call_or_state = false;
@ -497,15 +499,16 @@ export function build_class_directives_object(class_directives, context) {
const directives = b.object(properties); const directives = b.object(properties);
return has_call_or_state ? get_expression_id(context.state, directives) : directives; return has_call_or_state ? get_expression_id(expressions, directives) : directives;
} }
/** /**
* @param {AST.StyleDirective[]} style_directives * @param {AST.StyleDirective[]} style_directives
* @param {Expression[]} expressions
* @param {ComponentContext} context * @param {ComponentContext} context
* @return {ObjectExpression | ArrayExpression}} * @return {ObjectExpression | ArrayExpression}}
*/ */
export function build_style_directives_object(style_directives, context) { export function build_style_directives_object(style_directives, expressions, context) {
let normal_properties = []; let normal_properties = [];
let important_properties = []; let important_properties = [];
@ -514,7 +517,7 @@ export function build_style_directives_object(style_directives, context) {
directive.value === true directive.value === true
? build_getter({ name: directive.name, type: 'Identifier' }, context.state) ? build_getter({ name: directive.name, type: 'Identifier' }, context.state)
: build_attribute_value(directive.value, context, (value, metadata) => : build_attribute_value(directive.value, context, (value, metadata) =>
metadata.has_call ? get_expression_id(context.state, value) : value metadata.has_call ? get_expression_id(expressions, value) : value
).value; ).value;
const property = b.init(directive.name, expression); const property = b.init(directive.name, expression);
@ -653,7 +656,7 @@ function build_element_special_value_attribute(element, node_id, attribute, cont
? // if is a select with value we will also invoke `init_select` which need a reference before the template effect so we memoize separately ? // if is a select with value we will also invoke `init_select` which need a reference before the template effect so we memoize separately
is_select_with_value is_select_with_value
? memoize_expression(state, value) ? memoize_expression(state, value)
: get_expression_id(state, value) : get_expression_id(state.expressions, value) // TODO i think this will break in spread, needs to be `expressions`
: value : value
); );

@ -71,7 +71,7 @@ export function build_set_attributes(
b.prop( b.prop(
'init', 'init',
b.array([b.id('$.CLASS')]), b.array([b.id('$.CLASS')]),
build_class_directives_object(class_directives, context) build_class_directives_object(class_directives, expressions, context)
) )
); );
} }
@ -81,7 +81,7 @@ export function build_set_attributes(
b.prop( b.prop(
'init', 'init',
b.array([b.id('$.STYLE')]), b.array([b.id('$.STYLE')]),
build_style_directives_object(style_directives, context) build_style_directives_object(style_directives, expressions, context)
) )
); );
} }
@ -160,7 +160,7 @@ export function build_set_class(element, node_id, attribute, class_directives, c
value = b.call('$.clsx', value); value = b.call('$.clsx', value);
} }
return metadata.has_call ? get_expression_id(context.state, value) : value; return metadata.has_call ? get_expression_id(context.state.expressions, value) : value;
}); });
/** @type {Identifier | undefined} */ /** @type {Identifier | undefined} */
@ -173,7 +173,7 @@ export function build_set_class(element, node_id, attribute, class_directives, c
let next; let next;
if (class_directives.length) { if (class_directives.length) {
next = build_class_directives_object(class_directives, context); next = build_class_directives_object(class_directives, context.state.expressions, context);
has_state ||= class_directives.some((d) => d.metadata.expression.has_state); has_state ||= class_directives.some((d) => d.metadata.expression.has_state);
if (has_state) { if (has_state) {
@ -228,7 +228,7 @@ export function build_set_class(element, node_id, attribute, class_directives, c
*/ */
export function build_set_style(node_id, attribute, style_directives, context) { export function build_set_style(node_id, attribute, style_directives, context) {
let { value, has_state } = build_attribute_value(attribute.value, context, (value, metadata) => let { value, has_state } = build_attribute_value(attribute.value, context, (value, metadata) =>
metadata.has_call ? get_expression_id(context.state, value) : value metadata.has_call ? get_expression_id(context.state.expressions, value) : value
); );
/** @type {Identifier | undefined} */ /** @type {Identifier | undefined} */
@ -241,7 +241,7 @@ export function build_set_style(node_id, attribute, style_directives, context) {
let next; let next;
if (style_directives.length) { if (style_directives.length) {
next = build_style_directives_object(style_directives, context); next = build_style_directives_object(style_directives, context.state.expressions, context);
has_state ||= style_directives.some((d) => d.metadata.expression.has_state); has_state ||= style_directives.some((d) => d.metadata.expression.has_state);
if (has_state) { if (has_state) {

@ -22,11 +22,11 @@ export function memoize_expression(state, value) {
/** /**
* *
* @param {ComponentClientTransformState} state * @param {Expression[]} expressions
* @param {Expression} value * @param {Expression} value
*/ */
export function get_expression_id(state, value) { export function get_expression_id(expressions, value) {
return b.id(`$${state.expressions.push(value) - 1}`); return b.id(`$${expressions.push(value) - 1}`);
} }
/** /**
@ -40,7 +40,8 @@ export function build_template_chunk(
values, values,
visit, visit,
state, state,
memoize = (value, metadata) => (metadata.has_call ? get_expression_id(state, value) : value) memoize = (value, metadata) =>
metadata.has_call ? get_expression_id(state.expressions, value) : value
) { ) {
/** @type {Expression[]} */ /** @type {Expression[]} */
const expressions = []; const expressions = [];

Loading…
Cancel
Save