Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simpler string normalization #11954

Merged
merged 3 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-taxis-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: simpler string normalization
Original file line number Diff line number Diff line change
Expand Up @@ -1337,10 +1337,7 @@ function process_children(nodes, expression, is_element, { visit, state }) {
b.assignment(
'=',
b.member(text_id, b.id('nodeValue')),
b.call(
'$.stringify',
/** @type {import('estree').Expression} */ (visit(node.expression))
)
/** @type {import('estree').Expression} */ (visit(node.expression))
)
)
);
Expand Down Expand Up @@ -1524,7 +1521,7 @@ function serialize_template_literal(values, visit, state) {
);
expressions.push(b.call('$.get', id));
} else {
expressions.push(b.call('$.stringify', visit(node.expression, state)));
expressions.push(b.logical('??', visit(node.expression, state), b.literal('')));
}
quasis.push(b.quasi('', i + 1 === values.length));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DEV } from 'esm-env';
import { render_effect, effect, teardown } from '../../../reactivity/effects.js';
import { stringify } from '../../../render.js';
import { listen_to_event_and_reset_event } from './shared.js';
import * as e from '../../../errors.js';
import { get_proxied_value, is } from '../../../proxy.js';
Expand Down Expand Up @@ -43,7 +42,8 @@ export function bind_value(input, get_value, update) {
return;
}

input.value = stringify(value);
// @ts-expect-error the value is coerced on assignment
input.value = value ?? '';
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export {
update_pre_store,
update_store
} from './reactivity/store.js';
export { append_styles, sanitize_slots, set_text, slot, stringify } from './render.js';
export { append_styles, sanitize_slots, set_text, slot } from './render.js';
export {
get,
invalidate_inner_signals,
Expand Down
8 changes: 0 additions & 8 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ export function slot(anchor, slot_fn, slot_props, fallback_fn) {
}
}

/**
* @param {unknown} value
* @returns {string}
*/
export function stringify(value) {
return typeof value === 'string' ? value : value == null ? '' : value + '';
}

/**
* Mounts a component to the given target and returns the exports and potentially the props (if compiled with `accessors: true`) of the component
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export default function Bind_component_snippet($$anchor) {

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

$.template_effect(() => $.set_text(text, ` value: ${$.stringify($.get(value))}`));
$.template_effect(() => $.set_text(text, ` value: ${$.get(value) ?? ""}`));
$.append($$anchor, fragment_1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function Each_string_template($$anchor) {
$.each(node, 1, () => ['foo', 'bar', 'baz'], $.index, ($$anchor, thing, $$index) => {
var text = $.text($$anchor);

$.template_effect(() => $.set_text(text, `${$.stringify($.unwrap(thing))}, `));
$.template_effect(() => $.set_text(text, `${$.unwrap(thing) ?? ""}, `));
$.append($$anchor, text);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function Function_prop_no_getter($$anchor) {
children: ($$anchor, $$slotProps) => {
var text = $.text($$anchor);

$.template_effect(() => $.set_text(text, `clicks: ${$.stringify($.get(count))}`));
$.template_effect(() => $.set_text(text, `clicks: ${$.get(count) ?? ""}`));
$.append($$anchor, text);
},
$$slots: { default: true }
Expand Down
Loading