Skip to content

Commit 7f0ca11

Browse files
committed
simplify
1 parent ce63d39 commit 7f0ca11

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { dev, is_ignored, locator } from '../../../../state.js';
1414
import { is_event_attribute, is_text_attribute } from '../../../../utils/ast.js';
1515
import * as b from '../../../../utils/builders.js';
1616
import { is_custom_element_node } from '../../../nodes.js';
17-
import { is_inlinable_attribute, is_inlinable_sequence } from '../../../utils.js';
17+
import { is_inlinable_attribute } from '../../../utils.js';
1818
import { clean_nodes, determine_namespace_for_children } from '../../utils.js';
1919
import { build_getter, create_derived } from '../utils.js';
2020
import {
@@ -361,9 +361,10 @@ export function RegularElement(node, context) {
361361
get_states_and_calls(trimmed);
362362

363363
if (states_and_calls && states_and_calls.states === 0) {
364-
let { value } = build_template_literal(trimmed, context.visit, child_state);
364+
let { value, can_inline } = build_template_literal(trimmed, context.visit, child_state);
365+
365366
// if the expression is inlinable we just push it to the template
366-
if (is_inlinable_sequence(trimmed)) {
367+
if (can_inline) {
367368
state.template.push(escape_inline_expression(value));
368369
} else {
369370
// else we programmatically set the value

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/** @import { Expression } from 'estree' */
22
/** @import { AST, SvelteNode } from '#compiler' */
33
/** @import { ComponentContext } from '../../types' */
4-
import { is_event_attribute, is_text_attribute } from '../../../../../utils/ast.js';
4+
import { is_event_attribute } from '../../../../../utils/ast.js';
55
import * as b from '../../../../../utils/builders.js';
6-
import { is_inlinable_attribute, is_inlinable_sequence } from '../../../../utils.js';
6+
import { is_inlinable_attribute } from '../../../../utils.js';
77
import { build_template_literal, build_update, escape_inline_expression } from './utils.js';
88

99
/**
@@ -61,17 +61,21 @@ export function process_children(nodes, initial, is_element, { visit, state }) {
6161
* @param {Sequence} sequence
6262
*/
6363
function flush_sequence(sequence) {
64-
// TODO this should be folded into the `is_inlinable_sequence` block below,
64+
// TODO this should be folded into the `if (can_inline)` block below,
6565
// but it currently causes the script-style-non-top-level test to fail
6666
if (sequence.every((node) => node.type === 'Text')) {
6767
skipped += 1;
6868
state.template.push(sequence.map((node) => node.raw).join(''));
6969
return;
7070
}
7171

72-
const { has_state, has_call, value } = build_template_literal(sequence, visit, state);
72+
const { has_state, has_call, value, can_inline } = build_template_literal(
73+
sequence,
74+
visit,
75+
state
76+
);
7377

74-
if (is_inlinable_sequence(sequence)) {
78+
if (can_inline) {
7579
skipped += 1;
7680
state.template.push(escape_inline_expression(value));
7781
return;

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function escape_inline_expression(node, is_attr) {
6161
* @param {Array<AST.Text | AST.ExpressionTag>} values
6262
* @param {(node: SvelteNode, state: any) => any} visit
6363
* @param {ComponentClientTransformState} state
64-
* @returns {{ value: Expression, has_state: boolean, has_call: boolean }}
64+
* @returns {{ value: Expression, has_state: boolean, has_call: boolean, can_inline: boolean }}
6565
*/
6666
export function build_template_literal(values, visit, state) {
6767
/** @type {Expression[]} */
@@ -75,6 +75,7 @@ export function build_template_literal(values, visit, state) {
7575
let has_call = calls > 0;
7676
let has_state = states > 0;
7777
let contains_multiple_call_expression = calls > 1;
78+
let can_inline = true;
7879

7980
for (let i = 0; i < values.length; i++) {
8081
const node = values[i];
@@ -86,6 +87,10 @@ export function build_template_literal(values, visit, state) {
8687
quasi.value.cooked += node.expression.value + '';
8788
}
8889
} else {
90+
if (!node.metadata.expression.can_inline) {
91+
can_inline = false;
92+
}
93+
8994
if (contains_multiple_call_expression) {
9095
const id = b.id(state.scope.generate('stringified_text'));
9196
state.init.push(
@@ -107,7 +112,7 @@ export function build_template_literal(values, visit, state) {
107112
} else if (values.length === 1) {
108113
// If we have a single expression, then pass that in directly to possibly avoid doing
109114
// extra work in the template_effect (instead we do the work in set_text).
110-
return { value: visit(node.expression, state), has_state, has_call };
115+
return { value: visit(node.expression, state), has_state, has_call, can_inline };
111116
} else {
112117
expressions.push(b.logical('??', visit(node.expression, state), b.literal('')));
113118
}
@@ -123,7 +128,7 @@ export function build_template_literal(values, visit, state) {
123128

124129
const value = b.template(quasis, expressions);
125130

126-
return { value, has_state, has_call };
131+
return { value, has_state, has_call, can_inline };
127132
}
128133

129134
/**

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,8 @@ export function is_inlinable_attribute(attribute) {
99
if (attribute.value === true) return false;
1010
if (is_boolean_attribute(attribute.name.toLowerCase())) return false;
1111

12-
if (Array.isArray(attribute.value)) {
13-
return is_inlinable_sequence(attribute.value);
14-
}
15-
16-
return attribute.value.metadata.expression.can_inline;
17-
}
18-
19-
/**
20-
* @param {Array<AST.Text | AST.ExpressionTag>} nodes
21-
*/
22-
export function is_inlinable_sequence(nodes) {
23-
for (let value of nodes) {
24-
if (value.type === 'ExpressionTag' && !value.metadata.expression.can_inline) {
12+
for (const node of Array.isArray(attribute.value) ? attribute.value : [attribute.value]) {
13+
if (node.type === 'ExpressionTag' && !node.metadata.expression.can_inline) {
2514
return false;
2615
}
2716
}

0 commit comments

Comments
 (0)