Skip to content

Commit 47e8ad7

Browse files
authored
chore: convert member expression property strings to identifiers (#12890)
* chore: convert member expression property strings to identifiers * oops * tweak * while we're here
1 parent eaee7d3 commit 47e8ad7

File tree

15 files changed

+41
-61
lines changed

15 files changed

+41
-61
lines changed

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,8 @@ export function client_component(analysis, options) {
473473
const incoming = b.member(b.id('module.default'), HMR, true);
474474

475475
const accept_fn_body = [
476-
b.stmt(
477-
b.assignment('=', b.member(incoming, b.id('source')), b.member(existing, b.id('source')))
478-
),
479-
b.stmt(
480-
b.call('$.set', b.member(existing, b.id('source')), b.member(incoming, b.id('original')))
481-
)
476+
b.stmt(b.assignment('=', b.member(incoming, 'source'), b.member(existing, 'source'))),
477+
b.stmt(b.call('$.set', b.member(existing, 'source'), b.member(incoming, 'original')))
482478
];
483479

484480
if (analysis.css.hash) {
@@ -488,7 +484,7 @@ export function client_component(analysis, options) {
488484
b.call(
489485
b.member(
490486
b.call('document.querySelector', b.literal('#' + analysis.css.hash)),
491-
b.id('remove'),
487+
'remove',
492488
false,
493489
true
494490
)
@@ -498,9 +494,7 @@ export function client_component(analysis, options) {
498494
}
499495

500496
const hmr = b.block([
501-
b.stmt(
502-
b.assignment('=', id, b.call('$.hmr', id, b.thunk(b.member(existing, b.id('source')))))
503-
),
497+
b.stmt(b.assignment('=', id, b.call('$.hmr', id, b.thunk(b.member(existing, 'source'))))),
504498

505499
b.stmt(b.call('import.meta.hot.accept', b.arrow([b.id('module')], b.block(accept_fn_body))))
506500
]);
@@ -515,11 +509,7 @@ export function client_component(analysis, options) {
515509
// add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
516510
body.unshift(
517511
b.stmt(
518-
b.assignment(
519-
'=',
520-
b.member(b.id(analysis.name), b.id('$.FILENAME'), true),
521-
b.literal(filename)
522-
)
512+
b.assignment('=', b.member(b.id(analysis.name), '$.FILENAME', true), b.literal(filename))
523513
)
524514
);
525515
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export function Fragment(node, context) {
9696
call = b.call(
9797
'$.add_locations',
9898
call,
99-
b.member(b.id(context.state.analysis.name), b.id('$.FILENAME'), true),
99+
b.member(b.id(context.state.analysis.name), '$.FILENAME', true),
100100
build_locations(state.locations)
101101
);
102102
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function LetDirective(node, context) {
3333
b.object_pattern(node.expression.properties)
3434
: // @ts-expect-error types don't match, but it can't contain spread elements and the structure is otherwise fine
3535
b.array_pattern(node.expression.elements),
36-
b.member(b.id('$$slotProps'), b.id(node.name))
36+
b.member(b.id('$$slotProps'), node.name)
3737
),
3838
b.return(b.object(bindings.map((binding) => b.init(binding.node.name, binding.node))))
3939
])
@@ -48,7 +48,7 @@ export function LetDirective(node, context) {
4848

4949
return b.const(
5050
name,
51-
create_derived(context.state, b.thunk(b.member(b.id('$$slotProps'), b.id(node.name))))
51+
create_derived(context.state, b.thunk(b.member(b.id('$$slotProps'), node.name)))
5252
);
5353
}
5454
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ export function MemberExpression(node, context) {
1111
if (node.property.type === 'PrivateIdentifier') {
1212
const field = context.state.private_state.get(node.property.name);
1313
if (field) {
14-
return context.state.in_constructor ? b.member(node, b.id('v')) : b.call('$.get', node);
14+
return context.state.in_constructor ? b.member(node, 'v') : b.call('$.get', node);
1515
}
1616
} else if (node.object.type === 'ThisExpression') {
1717
// rewrite `this.foo` as `this.#foo.v` inside a constructor
1818
if (node.property.type === 'Identifier' && !node.computed) {
1919
const field = context.state.public_state.get(node.property.name);
2020

2121
if (field && context.state.in_constructor) {
22-
return b.member(b.member(b.this, field.id), b.id('v'));
22+
return b.member(b.member(b.this, field.id), 'v');
2323
}
2424
}
2525
}

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,7 @@ export function RegularElement(node, context) {
322322

323323
if (text_content && !text_content.has_state) {
324324
child_state.init.push(
325-
b.stmt(
326-
b.assignment('=', b.member(context.state.node, b.id('textContent')), text_content.value)
327-
)
325+
b.stmt(b.assignment('=', b.member(context.state.node, 'textContent'), text_content.value))
328326
);
329327
} else {
330328
/** @type {Expression} */
@@ -339,7 +337,7 @@ export function RegularElement(node, context) {
339337
if (node.name === 'template') {
340338
needs_reset = true;
341339
child_state.init.push(b.stmt(b.call('$.hydrate_template', arg)));
342-
arg = b.member(arg, b.id('content'));
340+
arg = b.member(arg, 'content');
343341
}
344342

345343
process_children(trimmed, () => b.call('$.child', arg), true, {
@@ -369,9 +367,8 @@ export function RegularElement(node, context) {
369367
if (has_direction_attribute) {
370368
// This fixes an issue with Chromium where updates to text content within an element
371369
// does not update the direction when set to auto. If we just re-assign the dir, this fixes it.
372-
context.state.update.push(
373-
b.stmt(b.assignment('=', b.member(node_id, b.id('dir')), b.member(node_id, b.id('dir'))))
374-
);
370+
const dir = b.member(node_id, 'dir');
371+
context.state.update.push(b.stmt(b.assignment('=', dir, dir)));
375372
}
376373

377374
if (child_locations.length > 0) {
@@ -494,16 +491,16 @@ function build_element_spread_attributes(
494491

495492
const preserve_attribute_case =
496493
element.metadata.svg || element.metadata.mathml || is_custom_element_node(element);
497-
const id = context.state.scope.generate('attributes');
494+
const id = b.id(context.state.scope.generate('attributes'));
498495

499496
const update = b.stmt(
500497
b.assignment(
501498
'=',
502-
b.id(id),
499+
id,
503500
b.call(
504501
'$.set_attributes',
505502
element_id,
506-
b.id(id),
503+
id,
507504
b.object(values),
508505
context.state.analysis.css.hash !== '' && b.literal(context.state.analysis.css.hash),
509506
preserve_attribute_case && b.true,
@@ -523,17 +520,17 @@ function build_element_spread_attributes(
523520

524521
if (needs_select_handling) {
525522
context.state.init.push(
526-
b.stmt(b.call('$.init_select', element_id, b.thunk(b.member(b.id(id), b.id('value')))))
523+
b.stmt(b.call('$.init_select', element_id, b.thunk(b.member(id, 'value'))))
527524
);
528525
context.state.update.push(
529526
b.if(
530-
b.binary('in', b.literal('value'), b.id(id)),
527+
b.binary('in', b.literal('value'), id),
531528
b.block([
532529
// This ensures a one-way street to the DOM in case it's <select {value}>
533530
// and not <select bind:value>. We need it in addition to $.init_select
534531
// because the select value is not reflected as an attribute, so the
535532
// mutation observer wouldn't notice.
536-
b.stmt(b.call('$.select_option', element_id, b.member(b.id(id), b.id('value'))))
533+
b.stmt(b.call('$.select_option', element_id, b.member(id, 'value')))
537534
])
538535
)
539536
);
@@ -596,7 +593,7 @@ function build_element_attribute_update_assignment(element, node_id, attribute,
596593
} else if (name === 'checked') {
597594
update = b.stmt(b.call('$.set_checked', node_id, value));
598595
} else if (is_dom_property(name)) {
599-
update = b.stmt(b.assignment('=', b.member(node_id, b.id(name)), value));
596+
update = b.stmt(b.assignment('=', b.member(node_id, name), value));
600597
} else {
601598
const callee = name.startsWith('xlink') ? '$.set_xlink_attribute' : '$.set_attribute';
602599
update = b.stmt(
@@ -666,9 +663,9 @@ function build_element_special_value_attribute(element, node_id, attribute, cont
666663

667664
const inner_assignment = b.assignment(
668665
'=',
669-
b.member(node_id, b.id('value')),
666+
b.member(node_id, 'value'),
670667
b.conditional(
671-
b.binary('==', b.literal(null), b.assignment('=', b.member(node_id, b.id('__value')), value)),
668+
b.binary('==', b.literal(null), b.assignment('=', b.member(node_id, '__value'), value)),
672669
b.literal(''), // render null/undefined values as empty string to support placeholder options
673670
value
674671
)

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

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

6161
const expression = is_default
6262
? b.call('$.default_slot', b.id('$$props'))
63-
: b.member(b.member(b.id('$$props'), b.id('$$slots')), name, true, true);
63+
: b.member(b.member(b.id('$$props'), '$$slots'), name, true, true);
6464

6565
const slot = b.call('$.slot', context.state.node, expression, props_expression, fallback);
6666
context.state.init.push(b.stmt(slot));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function TitleElement(node, context) {
1414
context.state
1515
);
1616

17-
const statement = b.stmt(b.assignment('=', b.member(b.id('$.document'), b.id('title')), value));
17+
const statement = b.stmt(b.assignment('=', b.id('$.document.title'), value));
1818

1919
if (has_state) {
2020
context.state.update.push(statement);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export function build_component(node, component_name, context, anchor = context.
372372

373373
statements.push(
374374
b.stmt(b.call('$.css_props', anchor, b.thunk(b.object(custom_css_props)))),
375-
b.stmt(fn(b.member(anchor, b.id('lastChild')))),
375+
b.stmt(fn(b.member(anchor, 'lastChild'))),
376376
b.stmt(b.call('$.reset', anchor))
377377
);
378378
} else {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ export function visit_event_attribute(node, context) {
5454

5555
context.state.init.push(
5656
b.stmt(
57-
b.assignment(
58-
'=',
59-
b.member(context.state.node, b.id('__' + event_name)),
60-
delegated_assignment
61-
)
57+
b.assignment('=', b.member(context.state.node, '__' + event_name), delegated_assignment)
6258
)
6359
);
6460
} else {
@@ -143,7 +139,7 @@ export function build_event_handler(node, metadata, context) {
143139
}
144140

145141
// wrap the handler in a function, so the expression is re-evaluated for each event
146-
let call = b.call(b.member(handler, b.id('apply'), false, true), b.this, b.id('$$args'));
142+
let call = b.call(b.member(handler, 'apply', false, true), b.this, b.id('$$args'));
147143

148144
if (dev) {
149145
const loc = locator(/** @type {number} */ (node.start));

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
@@ -49,7 +49,7 @@ export function process_children(nodes, expression, is_element, { visit, state }
4949
} else if (has_state && !within_bound_contenteditable) {
5050
state.update.push(update);
5151
} else {
52-
state.init.push(b.stmt(b.assignment('=', b.member(id, b.id('nodeValue')), value)));
52+
state.init.push(b.stmt(b.assignment('=', b.member(id, 'nodeValue'), value)));
5353
}
5454

5555
expression = (is_text) => b.call('$.sibling', id, is_text && b.true);

0 commit comments

Comments
 (0)