Skip to content

Commit 8595e05

Browse files
committed
replace util methods with methods directly on template
1 parent 1bfd839 commit 8595e05

File tree

15 files changed

+52
-74
lines changed

15 files changed

+52
-74
lines changed

packages/svelte/src/compiler/phases/3-transform/client/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ export interface ComponentClientTransformState extends ClientTransformState {
5555
readonly after_update: Statement[];
5656
/** The HTML template string */
5757
readonly template: {
58-
quasi: string[];
59-
expressions: Expression[];
58+
pushQuasi: (q: string) => void;
59+
pushExpression: (e: Expression) => void;
6060
};
6161
readonly locations: SourceLocation[];
6262
readonly metadata: {

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -312,32 +312,6 @@ export function create_derived(state, arg) {
312312
return b.call(state.analysis.runes ? '$.derived' : '$.derived_safe_equal', arg);
313313
}
314314

315-
/**
316-
* @param {import('./types.js').ComponentClientTransformState} state
317-
* @param {string} quasi_to_add
318-
*/
319-
export function push_template_quasi(state, quasi_to_add) {
320-
const { quasi } = state.template;
321-
if (quasi.length === 0) {
322-
quasi.push(quasi_to_add);
323-
return;
324-
}
325-
quasi[quasi.length - 1] = quasi[quasi.length - 1].concat(quasi_to_add);
326-
}
327-
328-
/**
329-
* @param {import('./types.js').ComponentClientTransformState} state
330-
* @param {import('estree').Expression} expression_to_add
331-
*/
332-
export function push_template_expression(state, expression_to_add) {
333-
const { expressions, quasi } = state.template;
334-
if (quasi.length === 0) {
335-
quasi.push('');
336-
}
337-
expressions.push(expression_to_add);
338-
quasi.push('');
339-
}
340-
341315
/**
342316
* Whether a variable can be referenced directly from template string.
343317
* @param {import('#compiler').Binding | undefined} binding

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext } from '../types' */
44
import * as b from '../../../../utils/builders.js';
5-
import { create_derived_block_argument, push_template_quasi } from '../utils.js';
5+
import { create_derived_block_argument } from '../utils.js';
66

77
/**
88
* @param {AST.AwaitBlock} node
99
* @param {ComponentContext} context
1010
*/
1111
export function AwaitBlock(node, context) {
12-
push_template_quasi(context.state, '<!>');
12+
context.state.template.pushQuasi('<!>');
1313

1414
// Visit {#await <expression>} first to ensure that scopes are in the correct order
1515
const expression = b.thunk(/** @type {Expression} */ (context.visit(node.expression)));
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
/** @import { AST } from '#compiler' */
22
/** @import { ComponentContext } from '../types' */
33

4-
import { push_template_quasi } from '../utils.js';
5-
64
/**
75
* @param {AST.Comment} node
86
* @param {ComponentContext} context
97
*/
108
export function Comment(node, context) {
119
// We'll only get here if comments are not filtered out, which they are unless preserveComments is true
12-
push_template_quasi(context.state, `<!--${node.data}-->`);
10+
context.state.template.pushQuasi(`<!--${node.data}-->`);
1311
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { dev } from '../../../../state.js';
1313
import { extract_paths, object } from '../../../../utils/ast.js';
1414
import * as b from '../../../../utils/builders.js';
15-
import { build_getter, push_template_quasi } from '../utils.js';
15+
import { build_getter } from '../utils.js';
1616
import { get_value } from './shared/declarations.js';
1717

1818
/**
@@ -32,7 +32,7 @@ export function EachBlock(node, context) {
3232
);
3333

3434
if (!each_node_meta.is_controlled) {
35-
push_template_quasi(context.state, '<!>');
35+
context.state.template.pushQuasi('<!>');
3636
}
3737

3838
if (each_node_meta.array_name !== null) {

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ export function Fragment(node, context) {
5757
/** @type {Statement | undefined} */
5858
let close = undefined;
5959

60+
/** @type {string[]} */
61+
const quasi = [];
62+
/** @type {Expression[]} */
63+
const expressions = [];
64+
6065
/** @type {ComponentClientTransformState} */
6166
const state = {
6267
...context.state,
@@ -65,8 +70,20 @@ export function Fragment(node, context) {
6570
update: [],
6671
after_update: [],
6772
template: {
68-
quasi: [],
69-
expressions: []
73+
pushQuasi: (/** @type {string} */ quasi_to_add) => {
74+
if (quasi.length === 0) {
75+
quasi.push(quasi_to_add);
76+
return;
77+
}
78+
quasi[quasi.length - 1] = quasi[quasi.length - 1].concat(quasi_to_add);
79+
},
80+
pushExpression: (/** @type {Expression} */ expression_to_add) => {
81+
if (quasi.length === 0) {
82+
quasi.push('');
83+
}
84+
expressions.push(expression_to_add);
85+
quasi.push('');
86+
}
7087
},
7188
locations: [],
7289
transform: { ...context.state.transform },
@@ -120,8 +137,8 @@ export function Fragment(node, context) {
120137
/** @type {Expression[]} */
121138
const args = [
122139
b.template(
123-
state.template.quasi.map((q) => b.quasi(q, true)),
124-
state.template.expressions
140+
quasi.map((q) => b.quasi(q, true)),
141+
expressions
125142
)
126143
];
127144

@@ -178,14 +195,14 @@ export function Fragment(node, context) {
178195
flags |= TEMPLATE_USE_IMPORT_NODE;
179196
}
180197

181-
if (state.template.quasi.length === 1 && state.template.quasi[0] === '<!>') {
198+
if (quasi.length === 1 && quasi[0] === '<!>') {
182199
// special case — we can use `$.comment` instead of creating a unique template
183200
body.push(b.var(id, b.call('$.comment')));
184201
} else {
185202
add_template(template_name, [
186203
b.template(
187-
state.template.quasi.map((q) => b.quasi(q, true)),
188-
state.template.expressions
204+
quasi.map((q) => b.quasi(q, true)),
205+
expressions
189206
),
190207
b.literal(flags)
191208
]);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
/** @import { ComponentContext } from '../types' */
44
import { is_ignored } from '../../../../state.js';
55
import * as b from '../../../../utils/builders.js';
6-
import { push_template_quasi } from '../utils.js';
76

87
/**
98
* @param {AST.HtmlTag} node
109
* @param {ComponentContext} context
1110
*/
1211
export function HtmlTag(node, context) {
13-
push_template_quasi(context.state, '<!>');
12+
context.state.template.pushQuasi('<!>');
1413

1514
// push into init, so that bindings run afterwards, which might trigger another run and override hydration
1615
context.state.init.push(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext } from '../types' */
44
import * as b from '../../../../utils/builders.js';
5-
import { push_template_quasi } from '../utils.js';
65

76
/**
87
* @param {AST.IfBlock} node
98
* @param {ComponentContext} context
109
*/
1110
export function IfBlock(node, context) {
12-
push_template_quasi(context.state, '<!>');
11+
context.state.template.pushQuasi('<!>');
1312

1413
const consequent = /** @type {BlockStatement} */ (context.visit(node.consequent));
1514

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext } from '../types' */
44
import * as b from '../../../../utils/builders.js';
5-
import { push_template_quasi } from '../utils.js';
65

76
/**
87
* @param {AST.KeyBlock} node
98
* @param {ComponentContext} context
109
*/
1110
export function KeyBlock(node, context) {
12-
push_template_quasi(context.state, '<!>');
11+
context.state.template.pushQuasi('<!>');
1312

1413
const key = /** @type {Expression} */ (context.visit(node.expression));
1514
const body = /** @type {Expression} */ (context.visit(node.fragment));

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ import { is_custom_element_node } from '../../../nodes.js';
2121
import { clean_nodes, determine_namespace_for_children } from '../../utils.js';
2222
import {
2323
build_getter,
24-
can_inline_variable,
25-
push_template_expression,
26-
push_template_quasi
24+
can_inline_variable
2725
} from '../utils.js';
2826
import {
2927
get_attribute_name,
@@ -58,7 +56,7 @@ export function RegularElement(node, context) {
5856
}
5957

6058
if (node.name === 'noscript') {
61-
push_template_quasi(context.state, '<noscript></noscript>');
59+
context.state.template.pushQuasi('<noscript></noscript>');
6260
return;
6361
}
6462

@@ -72,7 +70,7 @@ export function RegularElement(node, context) {
7270
namespace: determine_namespace_for_children(node, context.state.metadata.namespace)
7371
};
7472

75-
push_template_quasi(context.state, `<${node.name}`);
73+
context.state.template.pushQuasi(`<${node.name}`);
7674

7775
/** @type {Array<AST.Attribute | AST.SpreadAttribute>} */
7876
const attributes = [];
@@ -246,8 +244,7 @@ export function RegularElement(node, context) {
246244
const value = is_text_attribute(attribute) ? attribute.value[0].data : true;
247245

248246
if (name !== 'class' || value) {
249-
push_template_quasi(
250-
context.state,
247+
context.state.template.pushQuasi(
251248
` ${attribute.name}${
252249
is_boolean_attribute(name) && value === true
253250
? ''
@@ -284,7 +281,7 @@ export function RegularElement(node, context) {
284281
context.state.after_update.push(b.stmt(b.call('$.replay_events', node_id)));
285282
}
286283

287-
push_template_quasi(context.state, '>');
284+
context.state.template.pushQuasi('>');
288285

289286
/** @type {SourceLocation[]} */
290287
const child_locations = [];
@@ -383,7 +380,7 @@ export function RegularElement(node, context) {
383380
}
384381

385382
if (!is_void(node.name)) {
386-
push_template_quasi(context.state, `</${node.name}>`);
383+
context.state.template.pushQuasi(`</${node.name}>`);
387384
}
388385
}
389386

@@ -471,7 +468,7 @@ function build_element_spread_attributes(
471468
value.type === 'Literal' &&
472469
context.state.metadata.namespace === 'html'
473470
) {
474-
push_template_quasi(context.state, ` is="${escape_html(value.value, true)}"`);
471+
context.state.template.pushQuasi(` is="${escape_html(value.value, true)}"`);
475472
continue;
476473
}
477474

@@ -629,9 +626,9 @@ function build_element_attribute_update_assignment(element, node_id, attribute,
629626
return true;
630627
} else {
631628
if (inlinable_expression) {
632-
push_template_quasi(context.state, ` ${name}="`);
633-
push_template_expression(context.state, value);
634-
push_template_quasi(context.state, '"');
629+
context.state.template.pushQuasi(` ${name}="`);
630+
context.state.template.pushExpression(value);
631+
context.state.template.pushQuasi('"');
635632
} else {
636633
state.init.push(update);
637634
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
/** @import { ComponentContext } from '../types' */
44
import { unwrap_optional } from '../../../../utils/ast.js';
55
import * as b from '../../../../utils/builders.js';
6-
import { push_template_quasi } from '../utils.js';
76

87
/**
98
* @param {AST.RenderTag} node
109
* @param {ComponentContext} context
1110
*/
1211
export function RenderTag(node, context) {
13-
push_template_quasi(context.state, '<!>');
12+
context.state.template.pushQuasi('<!>');
1413
const callee = unwrap_optional(node.expression).callee;
1514
const raw_args = unwrap_optional(node.expression).arguments;
1615

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext } from '../types' */
44
import * as b from '../../../../utils/builders.js';
5-
import { push_template_quasi } from '../utils.js';
65
import { build_attribute_value } from './shared/element.js';
76

87
/**
@@ -11,7 +10,7 @@ import { build_attribute_value } from './shared/element.js';
1110
*/
1211
export function SlotElement(node, context) {
1312
// <slot {a}>fallback</slot> --> $.slot($$slots.default, { get a() { .. } }, () => ...fallback);
14-
push_template_quasi(context.state, '<!>');
13+
context.state.template.pushQuasi('<!>');
1514

1615
/** @type {Property[]} */
1716
const props = [];

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
} from '../../../../utils/ast.js';
1010
import * as b from '../../../../utils/builders.js';
1111
import { determine_namespace_for_children } from '../../utils.js';
12-
import { push_template_quasi } from '../utils.js';
1312
import {
1413
build_attribute_value,
1514
build_class_directives,
@@ -22,7 +21,7 @@ import { build_render_statement, build_update } from './shared/utils.js';
2221
* @param {ComponentContext} context
2322
*/
2423
export function SvelteElement(node, context) {
25-
push_template_quasi(context.state, `<!>`);
24+
context.state.template.pushQuasi(`<!>`);
2625

2726
/** @type {Array<AST.Attribute | AST.SpreadAttribute>} */
2827
const attributes = [];

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { dev, is_ignored } from '../../../../../state.js';
55
import { get_attribute_chunks } from '../../../../../utils/ast.js';
66
import * as b from '../../../../../utils/builders.js';
7-
import { create_derived, push_template_quasi } from '../../utils.js';
7+
import { create_derived } from '../../utils.js';
88
import { build_bind_this, validate_binding } from '../shared/utils.js';
99
import { build_attribute_value } from '../shared/element.js';
1010
import { build_event_handler } from './events.js';
@@ -357,8 +357,7 @@ export function build_component(node, component_name, context, anchor = context.
357357
}
358358

359359
if (Object.keys(custom_css_props).length > 0) {
360-
push_template_quasi(
361-
context.state,
360+
context.state.template.pushQuasi(
362361
context.state.metadata.namespace === 'svg'
363362
? '<g><!></g>'
364363
: '<div style="display: contents"><!></div>'
@@ -370,7 +369,7 @@ export function build_component(node, component_name, context, anchor = context.
370369
b.stmt(b.call('$.reset', anchor))
371370
);
372371
} else {
373-
push_template_quasi(context.state, '<!>');
372+
context.state.template.pushQuasi('<!>');
374373
statements.push(b.stmt(fn(anchor)));
375374
}
376375

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/** @import { ComponentContext } from '../../types' */
44
import { is_event_attribute, is_text_attribute } from '../../../../../utils/ast.js';
55
import * as b from '../../../../../utils/builders.js';
6-
import { push_template_quasi } from '../../utils.js';
76
import { build_template_literal, build_update } from './utils.js';
87

98
/**
@@ -63,11 +62,11 @@ export function process_children(nodes, initial, is_element, { visit, state }) {
6362
function flush_sequence(sequence) {
6463
if (sequence.every((node) => node.type === 'Text')) {
6564
skipped += 1;
66-
push_template_quasi(state, sequence.map((node) => node.raw).join(''));
65+
state.template.pushQuasi(sequence.map((node) => node.raw).join(''));
6766
return;
6867
}
6968

70-
push_template_quasi(state, ' ');
69+
state.template.pushQuasi(' ');
7170

7271
const { has_state, has_call, value } = build_template_literal(sequence, visit, state);
7372

0 commit comments

Comments
 (0)