Skip to content

Commit 5770063

Browse files
committed
chore: refactor is_inlinable_expression to accept the attribute
1 parent 509ca70 commit 5770063

File tree

5 files changed

+10
-16
lines changed

5 files changed

+10
-16
lines changed

packages/svelte/src/compiler/phases/2-analyze/visitors/ExpressionTag.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function ExpressionTag(node, context) {
2222
* the subtree as dynamic. This is because if it's inlinable it will be inlined in the template
2323
* directly making the whole thing actually static.
2424
*/
25-
if (!attribute_parent || !is_inlinable_expression(node, context.state.scope)) {
25+
if (!attribute_parent || !is_inlinable_expression(attribute_parent, context.state.scope)) {
2626
// TODO ideally we wouldn't do this here, we'd just do it on encountering
2727
// an `Identifier` within the tag. But we currently need to handle `{42}` etc
2828
mark_subtree_dynamic(context.path);

packages/svelte/src/compiler/phases/2-analyze/visitors/Identifier.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,14 @@ export function Identifier(node, context) {
2121
return;
2222
}
2323

24-
const expression_tag_parent = context.path.find((parent) => parent.type === 'ExpressionTag');
2524
const attribute_parent = context.path.find((parent) => parent.type === 'Attribute');
2625

2726
/**
2827
* if the identifier is part of an expression tag of an attribute we want to check if it's inlinable
2928
* before marking the subtree as dynamic. This is because if it's inlinable it will be inlined in the template
3029
* directly making the whole thing actually static.
3130
*/
32-
if (
33-
!attribute_parent ||
34-
!expression_tag_parent ||
35-
!is_inlinable_expression(expression_tag_parent, context.state.scope)
36-
) {
31+
if (!attribute_parent || !is_inlinable_expression(attribute_parent, context.state.scope)) {
3732
mark_subtree_dynamic(context.path);
3833
}
3934

packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,7 @@ function build_element_attribute_update_assignment(element, node_id, attribute,
578578
);
579579
}
580580

581-
const inlinable_expression =
582-
attribute.value === true
583-
? false // not an expression
584-
: is_inlinable_expression(attribute.value, context.state.scope);
581+
const inlinable_expression = is_inlinable_expression(attribute, context.state.scope);
585582
if (attribute.metadata.expression.has_state) {
586583
if (has_call) {
587584
state.init.push(build_update(update));

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ function is_static_element(node, state) {
159159
!is_text_attribute(attribute) &&
160160
// If the attribute is not a text attribute but is inlinable we will directly inline it in the
161161
// the template so before returning false we need to check that the attribute is not inlinable
162-
!is_inlinable_expression(attribute.value, state.scope)
162+
!is_inlinable_expression(attribute, state.scope)
163163
) {
164164
return false;
165165
}

packages/svelte/src/compiler/phases/utils.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** @import { AST, Binding } from '#compiler' */
2+
/** @import { Scope } from './scope' */
23

34
/**
45
* Whether a variable can be referenced directly from template string.
@@ -16,11 +17,12 @@ function can_inline_variable(binding) {
1617
}
1718

1819
/**
19-
* @param {(AST.Text | AST.ExpressionTag) | (AST.Text | AST.ExpressionTag)[]} node_or_nodes
20-
* @param {import('./scope.js').Scope} scope
20+
* @param {AST.Attribute} attribute
21+
* @param {Scope} scope
2122
*/
22-
export function is_inlinable_expression(node_or_nodes, scope) {
23-
let nodes = Array.isArray(node_or_nodes) ? node_or_nodes : [node_or_nodes];
23+
export function is_inlinable_expression(attribute, scope) {
24+
if (attribute.value === true) return false; // not an expression
25+
let nodes = Array.isArray(attribute.value) ? attribute.value : [attribute.value];
2426
let has_expression_tag = false;
2527
for (let value of nodes) {
2628
if (value.type === 'ExpressionTag') {

0 commit comments

Comments
 (0)