Skip to content

Commit 968bba5

Browse files
authored
feat: simpler string normalization (#11954)
* feat: simpler string normalization * simpler still - no function call necessary
1 parent 64ee32c commit 968bba5

File tree

8 files changed

+13
-19
lines changed

8 files changed

+13
-19
lines changed

.changeset/tiny-taxis-whisper.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
feat: simpler string normalization

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,10 +1337,7 @@ function process_children(nodes, expression, is_element, { visit, state }) {
13371337
b.assignment(
13381338
'=',
13391339
b.member(text_id, b.id('nodeValue')),
1340-
b.call(
1341-
'$.stringify',
1342-
/** @type {import('estree').Expression} */ (visit(node.expression))
1343-
)
1340+
/** @type {import('estree').Expression} */ (visit(node.expression))
13441341
)
13451342
)
13461343
);
@@ -1524,7 +1521,7 @@ function serialize_template_literal(values, visit, state) {
15241521
);
15251522
expressions.push(b.call('$.get', id));
15261523
} else {
1527-
expressions.push(b.call('$.stringify', visit(node.expression, state)));
1524+
expressions.push(b.logical('??', visit(node.expression, state), b.literal('')));
15281525
}
15291526
quasis.push(b.quasi('', i + 1 === values.length));
15301527
}

packages/svelte/src/internal/client/dom/elements/bindings/input.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { DEV } from 'esm-env';
22
import { render_effect, effect, teardown } from '../../../reactivity/effects.js';
3-
import { stringify } from '../../../render.js';
43
import { listen_to_event_and_reset_event } from './shared.js';
54
import * as e from '../../../errors.js';
65
import { get_proxied_value, is } from '../../../proxy.js';
@@ -43,7 +42,8 @@ export function bind_value(input, get_value, update) {
4342
return;
4443
}
4544

46-
input.value = stringify(value);
45+
// @ts-expect-error the value is coerced on assignment
46+
input.value = value ?? '';
4747
});
4848
}
4949

packages/svelte/src/internal/client/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export {
118118
update_pre_store,
119119
update_store
120120
} from './reactivity/store.js';
121-
export { append_styles, sanitize_slots, set_text, slot, stringify } from './render.js';
121+
export { append_styles, sanitize_slots, set_text, slot } from './render.js';
122122
export {
123123
get,
124124
invalidate_inner_signals,

packages/svelte/src/internal/client/render.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,6 @@ export function slot(anchor, slot_fn, slot_props, fallback_fn) {
6666
}
6767
}
6868

69-
/**
70-
* @param {unknown} value
71-
* @returns {string}
72-
*/
73-
export function stringify(value) {
74-
return typeof value === 'string' ? value : value == null ? '' : value + '';
75-
}
76-
7769
/**
7870
* Mounts a component to the given target and returns the exports and potentially the props (if compiled with `accessors: true`) of the component
7971
*

packages/svelte/tests/snapshot/samples/bind-component-snippet/_expected/client/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ export default function Bind_component_snippet($$anchor) {
2828

2929
var text = $.sibling(node, true);
3030

31-
$.template_effect(() => $.set_text(text, ` value: ${$.stringify($.get(value))}`));
31+
$.template_effect(() => $.set_text(text, ` value: ${$.get(value) ?? ""}`));
3232
$.append($$anchor, fragment_1);
3333
}

packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default function Each_string_template($$anchor) {
88
$.each(node, 1, () => ['foo', 'bar', 'baz'], $.index, ($$anchor, thing, $$index) => {
99
var text = $.text($$anchor);
1010

11-
$.template_effect(() => $.set_text(text, `${$.stringify($.unwrap(thing))}, `));
11+
$.template_effect(() => $.set_text(text, `${$.unwrap(thing) ?? ""}, `));
1212
$.append($$anchor, text);
1313
});
1414

packages/svelte/tests/snapshot/samples/function-prop-no-getter/_expected/client/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default function Function_prop_no_getter($$anchor) {
1919
children: ($$anchor, $$slotProps) => {
2020
var text = $.text($$anchor);
2121

22-
$.template_effect(() => $.set_text(text, `clicks: ${$.stringify($.get(count))}`));
22+
$.template_effect(() => $.set_text(text, `clicks: ${$.get(count) ?? ""}`));
2323
$.append($$anchor, text);
2424
},
2525
$$slots: { default: true }

0 commit comments

Comments
 (0)