Skip to content

Commit 226b264

Browse files
committed
simplify
1 parent 3a8f4e0 commit 226b264

File tree

10 files changed

+30
-32
lines changed

10 files changed

+30
-32
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ export function Attribute(node, context) {
6161
) {
6262
continue;
6363
}
64-
65-
node.metadata.expression.has_state ||= chunk.metadata.expression.has_state;
66-
node.metadata.expression.has_call ||= chunk.metadata.expression.has_call;
6764
}
6865

6966
if (is_event_attribute(node)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export function BindDirective(node, context) {
222222

223223
if (value !== undefined) {
224224
group_getter = b.thunk(
225-
b.block([b.stmt(build_attribute_value(value, context)), b.return(expression)])
225+
b.block([b.stmt(build_attribute_value(value, context).value), b.return(expression)])
226226
);
227227
}
228228
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function RegularElement(node, context) {
106106
case 'Attribute':
107107
// `is` attributes need to be part of the template, otherwise they break
108108
if (attribute.name === 'is' && context.state.metadata.namespace === 'html') {
109-
const value = build_attribute_value(attribute.value, context);
109+
const { value } = build_attribute_value(attribute.value, context);
110110

111111
if (value.type === 'Literal' && typeof value.value === 'string') {
112112
context.state.template.push(` is="${escape_html(value.value, true)}"`);
@@ -537,7 +537,7 @@ function build_element_attribute_update_assignment(
537537
const is_svg = context.state.metadata.namespace === 'svg' || element.name === 'svg';
538538
const is_mathml = context.state.metadata.namespace === 'mathml';
539539

540-
let value = build_attribute_value(attribute.value, context, (value) =>
540+
let { value, has_state } = build_attribute_value(attribute.value, context, (value) =>
541541
get_expression_id(state, value)
542542
);
543543

@@ -611,7 +611,7 @@ function build_element_attribute_update_assignment(
611611
);
612612
}
613613

614-
if (attribute.metadata.expression.has_state) {
614+
if (has_state) {
615615
state.update.push(update);
616616
return true;
617617
} else {
@@ -630,7 +630,7 @@ function build_element_attribute_update_assignment(
630630
function build_custom_element_attribute_update_assignment(node_id, attribute, context) {
631631
const state = context.state;
632632
const name = attribute.name; // don't lowercase, as we set the element's property, which might be case sensitive
633-
let value = build_attribute_value(attribute.value, context);
633+
let { value, has_state } = build_attribute_value(attribute.value, context);
634634

635635
// We assume that noone's going to redefine the semantics of the class attribute on custom elements, i.e. it's still used for CSS classes
636636
if (name === 'class' && attribute.metadata.needs_clsx) {
@@ -642,7 +642,7 @@ function build_custom_element_attribute_update_assignment(node_id, attribute, co
642642

643643
const update = b.stmt(b.call('$.set_custom_element_data', node_id, b.literal(name), value));
644644

645-
if (attribute.metadata.expression.has_state) {
645+
if (has_state) {
646646
// this is different from other updates — it doesn't get grouped,
647647
// because set_custom_element_data may not be idempotent
648648
state.init.push(b.stmt(b.call('$.template_effect', b.thunk(update.expression))));
@@ -665,7 +665,7 @@ function build_custom_element_attribute_update_assignment(node_id, attribute, co
665665
*/
666666
function build_element_special_value_attribute(element, node_id, attribute, context) {
667667
const state = context.state;
668-
const value = build_attribute_value(attribute.value, context, (value) =>
668+
const { value, has_state } = build_attribute_value(attribute.value, context, (value) =>
669669
get_expression_id(state, value)
670670
);
671671

@@ -701,7 +701,7 @@ function build_element_special_value_attribute(element, node_id, attribute, cont
701701
state.init.push(b.stmt(b.call('$.init_select', node_id, b.thunk(value))));
702702
}
703703

704-
if (attribute.metadata.expression.has_state) {
704+
if (has_state) {
705705
const id = state.scope.generate(`${node_id.name}_value`);
706706
build_update_assignment(
707707
state,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ export function SlotElement(node, context) {
3030
if (attribute.type === 'SpreadAttribute') {
3131
spreads.push(b.thunk(/** @type {Expression} */ (context.visit(attribute))));
3232
} else if (attribute.type === 'Attribute') {
33-
const value = build_attribute_value(attribute.value, context, (value) =>
33+
const { value, has_state } = build_attribute_value(attribute.value, context, (value) =>
3434
memoize_expression(context.state, value)
3535
);
3636

3737
if (attribute.name === 'name') {
3838
name = /** @type {Literal} */ (value);
3939
is_default = false;
4040
} else if (attribute.name !== 'slot') {
41-
if (attribute.metadata.expression.has_state) {
41+
if (has_state) {
4242
props.push(b.get(attribute.name, [b.return(value)]));
4343
} else {
4444
props.push(b.init(attribute.name, value));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function SvelteBoundary(node, context) {
2323

2424
const expression = /** @type {Expression} */ (context.visit(chunk.expression, context.state));
2525

26-
if (attribute.metadata.expression.has_state) {
26+
if (chunk.metadata.expression.has_state) {
2727
props.properties.push(b.get(attribute.name, [b.return(expression)]));
2828
} else {
2929
props.properties.push(b.init(attribute.name, expression));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export function SvelteElement(node, context) {
144144
get_tag,
145145
node.metadata.svg || node.metadata.mathml ? b.true : b.false,
146146
inner.length > 0 && b.arrow([element_id, b.id('$$anchor')], b.block(inner)),
147-
dynamic_namespace && b.thunk(build_attribute_value(dynamic_namespace, context)),
147+
dynamic_namespace && b.thunk(build_attribute_value(dynamic_namespace, context).value),
148148
location && b.array([b.literal(location.line), b.literal(location.column)])
149149
)
150150
)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export function build_component(node, component_name, context, anchor = context.
137137
build_attribute_value(attribute.value, context, (value) =>
138138
// TODO put the derived in the local block
139139
memoize_expression(context.state, value)
140-
)
140+
).value
141141
)
142142
);
143143
continue;
@@ -151,11 +151,11 @@ export function build_component(node, component_name, context, anchor = context.
151151
has_children_prop = true;
152152
}
153153

154-
const value = build_attribute_value(attribute.value, context, (value) =>
154+
const { value, has_state } = build_attribute_value(attribute.value, context, (value) =>
155155
memoize_expression(context.state, value)
156156
);
157157

158-
if (attribute.metadata.expression.has_state) {
158+
if (has_state) {
159159
let arg = value;
160160

161161
// When we have a non-simple computation, anything other than an Identifier or Member expression,

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ export function build_set_attributes(
2828
is_custom_element,
2929
state
3030
) {
31-
let has_state = false;
31+
let is_dynamic = false;
3232

3333
/** @type {ObjectExpression['properties']} */
3434
const values = [];
3535

3636
for (const attribute of attributes) {
3737
if (attribute.type === 'Attribute') {
38-
const value = build_attribute_value(attribute.value, context, (value) =>
38+
const { value, has_state } = build_attribute_value(attribute.value, context, (value) =>
3939
get_expression_id(context.state, value)
4040
);
4141

@@ -51,10 +51,10 @@ export function build_set_attributes(
5151
values.push(b.init(attribute.name, value));
5252
}
5353

54-
has_state ||= attribute.metadata.expression.has_state;
54+
is_dynamic ||= has_state;
5555
} else {
5656
// objects could contain reactive getters -> play it safe and always assume spread attributes are reactive
57-
has_state = true;
57+
is_dynamic = true;
5858

5959
let value = /** @type {Expression} */ (context.visit(attribute));
6060

@@ -70,15 +70,15 @@ export function build_set_attributes(
7070
const call = b.call(
7171
'$.set_attributes',
7272
element_id,
73-
has_state ? attributes_id : b.literal(null),
73+
is_dynamic ? attributes_id : b.literal(null),
7474
b.object(values),
7575
context.state.analysis.css.hash !== '' && b.literal(context.state.analysis.css.hash),
7676
preserve_attribute_case,
7777
is_custom_element,
7878
is_ignored(element, 'hydration_attribute_changed') && b.true
7979
);
8080

81-
if (has_state) {
81+
if (is_dynamic) {
8282
context.state.init.push(b.let(attributes_id));
8383
const update = b.stmt(b.assignment('=', attributes_id, call));
8484
context.state.update.push(update);
@@ -113,7 +113,7 @@ export function build_style_directives(
113113
? build_getter({ name: directive.name, type: 'Identifier' }, context.state)
114114
: build_attribute_value(directive.value, context, (value) =>
115115
get_expression_id(context.state, value)
116-
);
116+
).value;
117117

118118
const update = b.stmt(
119119
b.call(
@@ -170,26 +170,29 @@ export function build_class_directives(
170170
* @param {AST.Attribute['value']} value
171171
* @param {ComponentContext} context
172172
* @param {(value: Expression) => Expression} memoize
173-
* @returns {Expression}
173+
* @returns {{ value: Expression, has_state: boolean }}
174174
*/
175175
export function build_attribute_value(value, context, memoize = (value) => value) {
176176
if (value === true) {
177-
return b.literal(true);
177+
return { value: b.literal(true), has_state: false };
178178
}
179179

180180
if (!Array.isArray(value) || value.length === 1) {
181181
const chunk = Array.isArray(value) ? value[0] : value;
182182

183183
if (chunk.type === 'Text') {
184-
return b.literal(chunk.data);
184+
return { value: b.literal(chunk.data), has_state: false };
185185
}
186186

187187
let expression = /** @type {Expression} */ (context.visit(chunk.expression));
188188

189-
return chunk.metadata.expression.has_call ? memoize(expression) : expression;
189+
return {
190+
value: chunk.metadata.expression.has_call ? memoize(expression) : expression,
191+
has_state: chunk.metadata.expression.has_state
192+
};
190193
}
191194

192-
return build_template_chunk(value, context.visit, context.state, memoize).value;
195+
return build_template_chunk(value, context.visit, context.state, memoize);
193196
}
194197

195198
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export function create_attribute(name, start, end, value) {
4444
name,
4545
value,
4646
metadata: {
47-
expression: create_expression_metadata(),
4847
delegated: null,
4948
needs_clsx: false
5049
}

packages/svelte/src/compiler/types/template.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,6 @@ export namespace AST {
479479
value: true | ExpressionTag | Array<Text | ExpressionTag>;
480480
/** @internal */
481481
metadata: {
482-
expression: ExpressionMetadata;
483482
/** May be set if this is an event attribute */
484483
delegated: null | DelegatedEvent;
485484
/** May be `true` if this is a `class` attribute that needs `clsx` */

0 commit comments

Comments
 (0)