chore: refactor `is_inlinable_expression` to accept the attribute

pull/14269/head
paoloricciuti 2 years ago
parent 509ca700c1
commit 57700638a3

@ -22,7 +22,7 @@ export function ExpressionTag(node, context) {
* the subtree as dynamic. This is because if it's inlinable it will be inlined in the template * the subtree as dynamic. This is because if it's inlinable it will be inlined in the template
* directly making the whole thing actually static. * directly making the whole thing actually static.
*/ */
if (!attribute_parent || !is_inlinable_expression(node, context.state.scope)) { if (!attribute_parent || !is_inlinable_expression(attribute_parent, context.state.scope)) {
// TODO ideally we wouldn't do this here, we'd just do it on encountering // TODO ideally we wouldn't do this here, we'd just do it on encountering
// an `Identifier` within the tag. But we currently need to handle `{42}` etc // an `Identifier` within the tag. But we currently need to handle `{42}` etc
mark_subtree_dynamic(context.path); mark_subtree_dynamic(context.path);

@ -21,7 +21,6 @@ export function Identifier(node, context) {
return; return;
} }
const expression_tag_parent = context.path.find((parent) => parent.type === 'ExpressionTag');
const attribute_parent = context.path.find((parent) => parent.type === 'Attribute'); const attribute_parent = context.path.find((parent) => parent.type === 'Attribute');
/** /**
@ -29,11 +28,7 @@ export function Identifier(node, context) {
* before marking the subtree as dynamic. This is because if it's inlinable it will be inlined in the template * before marking the subtree as dynamic. This is because if it's inlinable it will be inlined in the template
* directly making the whole thing actually static. * directly making the whole thing actually static.
*/ */
if ( if (!attribute_parent || !is_inlinable_expression(attribute_parent, context.state.scope)) {
!attribute_parent ||
!expression_tag_parent ||
!is_inlinable_expression(expression_tag_parent, context.state.scope)
) {
mark_subtree_dynamic(context.path); mark_subtree_dynamic(context.path);
} }

@ -578,10 +578,7 @@ function build_element_attribute_update_assignment(element, node_id, attribute,
); );
} }
const inlinable_expression = const inlinable_expression = is_inlinable_expression(attribute, context.state.scope);
attribute.value === true
? false // not an expression
: is_inlinable_expression(attribute.value, context.state.scope);
if (attribute.metadata.expression.has_state) { if (attribute.metadata.expression.has_state) {
if (has_call) { if (has_call) {
state.init.push(build_update(update)); state.init.push(build_update(update));

@ -159,7 +159,7 @@ function is_static_element(node, state) {
!is_text_attribute(attribute) && !is_text_attribute(attribute) &&
// If the attribute is not a text attribute but is inlinable we will directly inline it in the // If the attribute is not a text attribute but is inlinable we will directly inline it in the
// the template so before returning false we need to check that the attribute is not inlinable // the template so before returning false we need to check that the attribute is not inlinable
!is_inlinable_expression(attribute.value, state.scope) !is_inlinable_expression(attribute, state.scope)
) { ) {
return false; return false;
} }

@ -1,4 +1,5 @@
/** @import { AST, Binding } from '#compiler' */ /** @import { AST, Binding } from '#compiler' */
/** @import { Scope } from './scope' */
/** /**
* Whether a variable can be referenced directly from template string. * Whether a variable can be referenced directly from template string.
@ -16,11 +17,12 @@ function can_inline_variable(binding) {
} }
/** /**
* @param {(AST.Text | AST.ExpressionTag) | (AST.Text | AST.ExpressionTag)[]} node_or_nodes * @param {AST.Attribute} attribute
* @param {import('./scope.js').Scope} scope * @param {Scope} scope
*/ */
export function is_inlinable_expression(node_or_nodes, scope) { export function is_inlinable_expression(attribute, scope) {
let nodes = Array.isArray(node_or_nodes) ? node_or_nodes : [node_or_nodes]; if (attribute.value === true) return false; // not an expression
let nodes = Array.isArray(attribute.value) ? attribute.value : [attribute.value];
let has_expression_tag = false; let has_expression_tag = false;
for (let value of nodes) { for (let value of nodes) {
if (value.type === 'ExpressionTag') { if (value.type === 'ExpressionTag') {

Loading…
Cancel
Save