Skip to content

Commit 3acb74f

Browse files
committed
simplify
1 parent 6f1d6be commit 3acb74f

File tree

1 file changed

+43
-61
lines changed
  • packages/svelte/src/compiler/phases/3-transform/client/visitors/shared

1 file changed

+43
-61
lines changed

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

Lines changed: 43 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,6 @@ import is_reference from 'is-reference';
1111
import { locator } from '../../../../../state.js';
1212
import { escape_html } from '../../../../../../escaping.js';
1313

14-
/**
15-
* @param {Array<AST.Text | AST.ExpressionTag>} values
16-
*/
17-
export function get_states_and_calls(values) {
18-
let states = 0;
19-
let calls = 0;
20-
for (let i = 0; i < values.length; i++) {
21-
const node = values[i];
22-
23-
if (node.type === 'ExpressionTag') {
24-
if (node.metadata.expression.has_call) {
25-
calls++;
26-
}
27-
if (node.metadata.expression.has_state) {
28-
states++;
29-
}
30-
}
31-
}
32-
33-
return { states, calls };
34-
}
35-
3614
/**
3715
* @param {Expression} node
3816
* @param {boolean} [is_attr]
@@ -70,55 +48,59 @@ export function build_template_chunk(values, visit, state) {
7048
let quasi = b.quasi('');
7149
const quasis = [quasi];
7250

73-
const { states, calls } = get_states_and_calls(values);
74-
75-
let has_call = calls > 0;
76-
let has_state = states > 0;
77-
let contains_multiple_call_expression = calls > 1;
51+
let has_call = false;
52+
let has_state = false;
7853
let can_inline = true;
54+
let contains_multiple_call_expression = false;
7955

80-
for (let i = 0; i < values.length; i++) {
81-
const node = values[i];
56+
for (const node of values) {
57+
if (node.type === 'ExpressionTag') {
58+
if (node.metadata.expression.has_call) {
59+
if (has_call) contains_multiple_call_expression = true;
60+
has_call = true;
61+
}
8262

83-
if (node.type === 'Text') {
84-
quasi.value.cooked += node.data;
85-
} else if (node.type === 'ExpressionTag' && node.expression.type === 'Literal') {
86-
if (node.expression.value != null) {
87-
quasi.value.cooked += node.expression.value + '';
63+
if (node.metadata.expression.has_state) {
64+
has_state = true;
8865
}
89-
} else {
66+
9067
if (!node.metadata.expression.can_inline) {
9168
can_inline = false;
9269
}
70+
}
71+
}
9372

94-
if (contains_multiple_call_expression) {
95-
const id = b.id(state.scope.generate('stringified_text'));
96-
state.init.push(
97-
b.const(
98-
id,
99-
create_derived(
100-
state,
101-
b.thunk(
102-
b.logical(
103-
'??',
104-
/** @type {Expression} */ (visit(node.expression, state)),
105-
b.literal('')
106-
)
107-
)
108-
)
109-
)
110-
);
111-
expressions.push(b.call('$.get', id));
112-
} else if (values.length === 1) {
113-
// If we have a single expression, then pass that in directly to possibly avoid doing
114-
// extra work in the template_effect (instead we do the work in set_text).
115-
return { value: visit(node.expression, state), has_state, has_call, can_inline };
73+
for (let i = 0; i < values.length; i++) {
74+
const node = values[i];
75+
76+
if (node.type === 'Text') {
77+
quasi.value.cooked += node.data;
78+
} else {
79+
if (node.expression.type === 'Literal') {
80+
if (node.expression.value != null) {
81+
quasi.value.cooked += node.expression.value + '';
82+
}
11683
} else {
117-
expressions.push(b.logical('??', visit(node.expression, state), b.literal('')));
118-
}
84+
if (contains_multiple_call_expression) {
85+
const id = b.id(state.scope.generate('stringified_text'));
86+
const expression = /** @type {Expression} */ (visit(node.expression, state));
87+
88+
state.init.push(
89+
b.const(id, create_derived(state, b.thunk(b.logical('??', expression, b.literal('')))))
90+
);
91+
92+
expressions.push(b.call('$.get', id));
93+
} else if (values.length === 1) {
94+
// If we have a single expression, then pass that in directly to possibly avoid doing
95+
// extra work in the template_effect (instead we do the work in set_text).
96+
return { value: visit(node.expression, state), has_state, has_call, can_inline };
97+
} else {
98+
expressions.push(b.logical('??', visit(node.expression, state), b.literal('')));
99+
}
119100

120-
quasi = b.quasi('', i + 1 === values.length);
121-
quasis.push(quasi);
101+
quasi = b.quasi('', i + 1 === values.length);
102+
quasis.push(quasi);
103+
}
122104
}
123105
}
124106

0 commit comments

Comments
 (0)