Skip to content

Commit 29ca451

Browse files
authored
chore: tidier generated message code (#12062)
* chore: tidier generated message code * lint
1 parent ad75111 commit 29ca451

File tree

6 files changed

+79
-31
lines changed

6 files changed

+79
-31
lines changed

packages/svelte/scripts/process-messages/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,54 @@ function transform(name, dest) {
293293
}
294294
});
295295
},
296+
TemplateLiteral(node, context) {
297+
/** @type {import('estree').TemplateElement} */
298+
let quasi = {
299+
type: 'TemplateElement',
300+
value: {
301+
...node.quasis[0].value
302+
},
303+
tail: node.quasis[0].tail
304+
};
305+
306+
/** @type {import('estree').TemplateLiteral} */
307+
let out = {
308+
type: 'TemplateLiteral',
309+
quasis: [quasi],
310+
expressions: []
311+
};
312+
313+
for (let i = 0; i < node.expressions.length; i += 1) {
314+
const q = node.quasis[i + 1];
315+
const e = node.expressions[i];
316+
317+
if (e.type === 'Literal' && e.value === 'CODE') {
318+
quasi.value.raw += code + q.value.raw;
319+
continue;
320+
}
321+
322+
if (e.type === 'Identifier' && e.name === 'MESSAGE') {
323+
if (message.type === 'Literal') {
324+
const str = /** @type {string} */ (message.value).replace(/(`|\${)/g, '\\$1');
325+
quasi.value.raw += str + q.value.raw;
326+
continue;
327+
}
328+
329+
if (message.type === 'TemplateLiteral') {
330+
quasi.value.raw += message.quasis[0].value.raw + q.value.raw;
331+
out.quasis.push(...message.quasis.slice(1));
332+
out.expressions.push(...message.expressions);
333+
quasi = message.quasis[message.quasis.length - 1];
334+
continue;
335+
}
336+
}
337+
338+
out.quasis.push((quasi = q));
339+
out.expressions.push(/** @type {import('estree').Expression} */ (context.visit(e)));
340+
}
341+
342+
return out;
343+
},
296344
Literal(node) {
297345
if (node.value === 'CODE') {
298346
return {

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { DEV } from 'esm-env';
88
*/
99
export function bind_invalid_checkbox_value() {
1010
if (DEV) {
11-
const error = new Error(`${"bind_invalid_checkbox_value"}\n${"Using `bind:value` together with a checkbox input is not allowed. Use `bind:checked` instead"}`);
11+
const error = new Error(`bind_invalid_checkbox_value\nUsing \`bind:value\` together with a checkbox input is not allowed. Use \`bind:checked\` instead`);
1212

1313
error.name = 'Svelte error';
1414
throw error;
@@ -27,7 +27,7 @@ export function bind_invalid_checkbox_value() {
2727
*/
2828
export function bind_invalid_export(component, key, name) {
2929
if (DEV) {
30-
const error = new Error(`${"bind_invalid_export"}\n${`Component ${component} has an export named \`${key}\` that a consumer component is trying to access using \`bind:${key}\`, which is disallowed. Instead, use \`bind:this\` (e.g. \`<${name} bind:this={component} />\`) and then access the property on the bound component instance (e.g. \`component.${key}\`)`}`);
30+
const error = new Error(`bind_invalid_export\nComponent ${component} has an export named \`${key}\` that a consumer component is trying to access using \`bind:${key}\`, which is disallowed. Instead, use \`bind:this\` (e.g. \`<${name} bind:this={component} />\`) and then access the property on the bound component instance (e.g. \`component.${key}\`)`);
3131

3232
error.name = 'Svelte error';
3333
throw error;
@@ -46,7 +46,7 @@ export function bind_invalid_export(component, key, name) {
4646
*/
4747
export function bind_not_bindable(key, component, name) {
4848
if (DEV) {
49-
const error = new Error(`${"bind_not_bindable"}\n${`A component is attempting to bind to a non-bindable property \`${key}\` belonging to ${component} (i.e. \`<${name} bind:${key}={...}>\`). To mark a property as bindable: \`let { ${key} = $bindable() } = $props()\``}`);
49+
const error = new Error(`bind_not_bindable\nA component is attempting to bind to a non-bindable property \`${key}\` belonging to ${component} (i.e. \`<${name} bind:${key}={...}>\`). To mark a property as bindable: \`let { ${key} = $bindable() } = $props()\``);
5050

5151
error.name = 'Svelte error';
5252
throw error;
@@ -65,7 +65,7 @@ export function bind_not_bindable(key, component, name) {
6565
*/
6666
export function component_api_changed(parent, method, component) {
6767
if (DEV) {
68-
const error = new Error(`${"component_api_changed"}\n${`${parent} called \`${method}\` on an instance of ${component}, which is no longer valid in Svelte 5. See https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes for more information`}`);
68+
const error = new Error(`component_api_changed\n${parent} called \`${method}\` on an instance of ${component}, which is no longer valid in Svelte 5. See https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes for more information`);
6969

7070
error.name = 'Svelte error';
7171
throw error;
@@ -83,7 +83,7 @@ export function component_api_changed(parent, method, component) {
8383
*/
8484
export function component_api_invalid_new(component, name) {
8585
if (DEV) {
86-
const error = new Error(`${"component_api_invalid_new"}\n${`Attempted to instantiate ${component} with \`new ${name}\`, which is no longer valid in Svelte 5. If this component is not under your control, set the \`legacy.componentApi\` compiler option to keep it working. See https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes for more information`}`);
86+
const error = new Error(`component_api_invalid_new\nAttempted to instantiate ${component} with \`new ${name}\`, which is no longer valid in Svelte 5. If this component is not under your control, set the \`legacy.componentApi\` compiler option to keep it working. See https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes for more information`);
8787

8888
error.name = 'Svelte error';
8989
throw error;
@@ -102,7 +102,7 @@ export function component_api_invalid_new(component, name) {
102102
*/
103103
export function each_key_duplicate(a, b, value) {
104104
if (DEV) {
105-
const error = new Error(`${"each_key_duplicate"}\n${value ? `Keyed each block has duplicate key \`${value}\` at indexes ${a} and ${b}` : `Keyed each block has duplicate key at indexes ${a} and ${b}`}`);
105+
const error = new Error(`each_key_duplicate\n${value ? `Keyed each block has duplicate key \`${value}\` at indexes ${a} and ${b}` : `Keyed each block has duplicate key at indexes ${a} and ${b}`}`);
106106

107107
error.name = 'Svelte error';
108108
throw error;
@@ -119,7 +119,7 @@ export function each_key_duplicate(a, b, value) {
119119
*/
120120
export function effect_in_teardown(rune) {
121121
if (DEV) {
122-
const error = new Error(`${"effect_in_teardown"}\n${`\`${rune}\` cannot be used inside an effect cleanup function`}`);
122+
const error = new Error(`effect_in_teardown\n\`${rune}\` cannot be used inside an effect cleanup function`);
123123

124124
error.name = 'Svelte error';
125125
throw error;
@@ -135,7 +135,7 @@ export function effect_in_teardown(rune) {
135135
*/
136136
export function effect_in_unowned_derived() {
137137
if (DEV) {
138-
const error = new Error(`${"effect_in_unowned_derived"}\n${"Effect cannot be created inside a `$derived` value that was not itself created inside an effect"}`);
138+
const error = new Error(`effect_in_unowned_derived\nEffect cannot be created inside a \`$derived\` value that was not itself created inside an effect`);
139139

140140
error.name = 'Svelte error';
141141
throw error;
@@ -152,7 +152,7 @@ export function effect_in_unowned_derived() {
152152
*/
153153
export function effect_orphan(rune) {
154154
if (DEV) {
155-
const error = new Error(`${"effect_orphan"}\n${`\`${rune}\` can only be used inside an effect (e.g. during component initialisation)`}`);
155+
const error = new Error(`effect_orphan\n\`${rune}\` can only be used inside an effect (e.g. during component initialisation)`);
156156

157157
error.name = 'Svelte error';
158158
throw error;
@@ -168,7 +168,7 @@ export function effect_orphan(rune) {
168168
*/
169169
export function effect_update_depth_exceeded() {
170170
if (DEV) {
171-
const error = new Error(`${"effect_update_depth_exceeded"}\n${"Maximum update depth exceeded. This can happen when a reactive block or effect repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops"}`);
171+
const error = new Error(`effect_update_depth_exceeded\nMaximum update depth exceeded. This can happen when a reactive block or effect repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops`);
172172

173173
error.name = 'Svelte error';
174174
throw error;
@@ -184,7 +184,7 @@ export function effect_update_depth_exceeded() {
184184
*/
185185
export function hydration_failed() {
186186
if (DEV) {
187-
const error = new Error(`${"hydration_failed"}\n${"Failed to hydrate the application"}`);
187+
const error = new Error(`hydration_failed\nFailed to hydrate the application`);
188188

189189
error.name = 'Svelte error';
190190
throw error;
@@ -201,7 +201,7 @@ export function hydration_failed() {
201201
*/
202202
export function lifecycle_legacy_only(name) {
203203
if (DEV) {
204-
const error = new Error(`${"lifecycle_legacy_only"}\n${`\`${name}(...)\` cannot be used in runes mode`}`);
204+
const error = new Error(`lifecycle_legacy_only\n\`${name}(...)\` cannot be used in runes mode`);
205205

206206
error.name = 'Svelte error';
207207
throw error;
@@ -218,7 +218,7 @@ export function lifecycle_legacy_only(name) {
218218
*/
219219
export function props_invalid_value(key) {
220220
if (DEV) {
221-
const error = new Error(`${"props_invalid_value"}\n${`Cannot do \`bind:${key}={undefined}\` when \`${key}\` has a fallback value`}`);
221+
const error = new Error(`props_invalid_value\nCannot do \`bind:${key}={undefined}\` when \`${key}\` has a fallback value`);
222222

223223
error.name = 'Svelte error';
224224
throw error;
@@ -235,7 +235,7 @@ export function props_invalid_value(key) {
235235
*/
236236
export function props_rest_readonly(property) {
237237
if (DEV) {
238-
const error = new Error(`${"props_rest_readonly"}\n${`Rest element properties of \`$props()\` such as \`${property}\` are readonly`}`);
238+
const error = new Error(`props_rest_readonly\nRest element properties of \`$props()\` such as \`${property}\` are readonly`);
239239

240240
error.name = 'Svelte error';
241241
throw error;
@@ -252,7 +252,7 @@ export function props_rest_readonly(property) {
252252
*/
253253
export function rune_outside_svelte(rune) {
254254
if (DEV) {
255-
const error = new Error(`${"rune_outside_svelte"}\n${`The \`${rune}\` rune is only available inside \`.svelte\` and \`.svelte.js/ts\` files`}`);
255+
const error = new Error(`rune_outside_svelte\nThe \`${rune}\` rune is only available inside \`.svelte\` and \`.svelte.js/ts\` files`);
256256

257257
error.name = 'Svelte error';
258258
throw error;
@@ -268,7 +268,7 @@ export function rune_outside_svelte(rune) {
268268
*/
269269
export function state_prototype_fixed() {
270270
if (DEV) {
271-
const error = new Error(`${"state_prototype_fixed"}\n${"Cannot set prototype of `$state` object"}`);
271+
const error = new Error(`state_prototype_fixed\nCannot set prototype of \`$state\` object`);
272272

273273
error.name = 'Svelte error';
274274
throw error;
@@ -284,7 +284,7 @@ export function state_prototype_fixed() {
284284
*/
285285
export function state_unsafe_mutation() {
286286
if (DEV) {
287-
const error = new Error(`${"state_unsafe_mutation"}\n${"Updating state inside a derived is forbidden. If the value should not be reactive, declare it without `$state`"}`);
287+
const error = new Error(`state_unsafe_mutation\nUpdating state inside a derived is forbidden. If the value should not be reactive, declare it without \`$state\``);
288288

289289
error.name = 'Svelte error';
290290
throw error;
@@ -300,7 +300,7 @@ export function state_unsafe_mutation() {
300300
*/
301301
export function svelte_component_invalid_this_value() {
302302
if (DEV) {
303-
const error = new Error(`${"svelte_component_invalid_this_value"}\n${"The `this={...}` property of a `<svelte:component>` must be a Svelte component, if defined"}`);
303+
const error = new Error(`svelte_component_invalid_this_value\nThe \`this={...}\` property of a \`<svelte:component>\` must be a Svelte component, if defined`);
304304

305305
error.name = 'Svelte error';
306306
throw error;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var normal = 'font-weight: normal';
1313
*/
1414
export function hydration_attribute_changed(attribute, html, value) {
1515
if (DEV) {
16-
console.warn(`%c[svelte] ${"hydration_attribute_changed"}\n%c${`The \`${attribute}\` attribute on \`${html}\` changed its value between server and client renders. The client value, \`${value}\`, will be ignored in favour of the server value`}`, bold, normal);
16+
console.warn(`%c[svelte] hydration_attribute_changed\n%cThe \`${attribute}\` attribute on \`${html}\` changed its value between server and client renders. The client value, \`${value}\`, will be ignored in favour of the server value`, bold, normal);
1717
} else {
1818
// TODO print a link to the documentation
1919
console.warn("hydration_attribute_changed");
@@ -26,7 +26,7 @@ export function hydration_attribute_changed(attribute, html, value) {
2626
*/
2727
export function hydration_mismatch(location) {
2828
if (DEV) {
29-
console.warn(`%c[svelte] ${"hydration_mismatch"}\n%c${location ? `Hydration failed because the initial UI does not match what was rendered on the server. The error occurred near ${location}` : "Hydration failed because the initial UI does not match what was rendered on the server"}`, bold, normal);
29+
console.warn(`%c[svelte] hydration_mismatch\n%c${location ? `Hydration failed because the initial UI does not match what was rendered on the server. The error occurred near ${location}` : "Hydration failed because the initial UI does not match what was rendered on the server"}`, bold, normal);
3030
} else {
3131
// TODO print a link to the documentation
3232
console.warn("hydration_mismatch");
@@ -38,7 +38,7 @@ export function hydration_mismatch(location) {
3838
*/
3939
export function lifecycle_double_unmount() {
4040
if (DEV) {
41-
console.warn(`%c[svelte] ${"lifecycle_double_unmount"}\n%c${"Tried to unmount a component that was not mounted"}`, bold, normal);
41+
console.warn(`%c[svelte] lifecycle_double_unmount\n%cTried to unmount a component that was not mounted`, bold, normal);
4242
} else {
4343
// TODO print a link to the documentation
4444
console.warn("lifecycle_double_unmount");
@@ -53,7 +53,7 @@ export function lifecycle_double_unmount() {
5353
*/
5454
export function ownership_invalid_binding(parent, child, owner) {
5555
if (DEV) {
56-
console.warn(`%c[svelte] ${"ownership_invalid_binding"}\n%c${`${parent} passed a value to ${child} with \`bind:\`, but the value is owned by ${owner}. Consider creating a binding between ${owner} and ${parent}`}`, bold, normal);
56+
console.warn(`%c[svelte] ownership_invalid_binding\n%c${parent} passed a value to ${child} with \`bind:\`, but the value is owned by ${owner}. Consider creating a binding between ${owner} and ${parent}`, bold, normal);
5757
} else {
5858
// TODO print a link to the documentation
5959
console.warn("ownership_invalid_binding");
@@ -67,7 +67,7 @@ export function ownership_invalid_binding(parent, child, owner) {
6767
*/
6868
export function ownership_invalid_mutation(component, owner) {
6969
if (DEV) {
70-
console.warn(`%c[svelte] ${"ownership_invalid_mutation"}\n%c${component ? `${component} mutated a value owned by ${owner}. This is strongly discouraged. Consider passing values to child components with \`bind:\`, or use a callback instead` : "Mutating a value outside the component that created it is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead"}`, bold, normal);
70+
console.warn(`%c[svelte] ownership_invalid_mutation\n%c${component ? `${component} mutated a value owned by ${owner}. This is strongly discouraged. Consider passing values to child components with \`bind:\`, or use a callback instead` : "Mutating a value outside the component that created it is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead"}`, bold, normal);
7171
} else {
7272
// TODO print a link to the documentation
7373
console.warn("ownership_invalid_mutation");
@@ -80,7 +80,7 @@ export function ownership_invalid_mutation(component, owner) {
8080
*/
8181
export function state_proxy_equality_mismatch(operator) {
8282
if (DEV) {
83-
console.warn(`%c[svelte] ${"state_proxy_equality_mismatch"}\n%c${`Reactive \`$state(...)\` proxies and the values they proxy have different identities. Because of this, comparisons with \`${operator}\` will produce unexpected results. Consider using \`$state.is(a, b)\` instead`}`, bold, normal);
83+
console.warn(`%c[svelte] state_proxy_equality_mismatch\n%cReactive \`$state(...)\` proxies and the values they proxy have different identities. Because of this, comparisons with \`${operator}\` will produce unexpected results. Consider using \`$state.is(a, b)\` instead`, bold, normal);
8484
} else {
8585
// TODO print a link to the documentation
8686
console.warn("state_proxy_equality_mismatch");

packages/svelte/src/internal/server/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @returns {never}
77
*/
88
export function lifecycle_function_unavailable(name) {
9-
const error = new Error(`${"lifecycle_function_unavailable"}\n${`\`${name}(...)\` is not available on the server`}`);
9+
const error = new Error(`lifecycle_function_unavailable\n\`${name}(...)\` is not available on the server`);
1010

1111
error.name = 'Svelte error';
1212
throw error;

packages/svelte/src/internal/shared/errors.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { DEV } from 'esm-env';
99
*/
1010
export function lifecycle_outside_component(name) {
1111
if (DEV) {
12-
const error = new Error(`${"lifecycle_outside_component"}\n${`\`${name}(...)\` can only be used during component initialisation`}`);
12+
const error = new Error(`lifecycle_outside_component\n\`${name}(...)\` can only be used during component initialisation`);
1313

1414
error.name = 'Svelte error';
1515
throw error;
@@ -25,7 +25,7 @@ export function lifecycle_outside_component(name) {
2525
*/
2626
export function render_tag_invalid_argument() {
2727
if (DEV) {
28-
const error = new Error(`${"render_tag_invalid_argument"}\n${"The argument to `{@render ...}` must be a snippet function, not a component or some other kind of function. If you want to dynamically render one snippet or another, use `$derived` and pass its result to `{@render ...}`"}`);
28+
const error = new Error(`render_tag_invalid_argument\nThe argument to \`{@render ...}\` must be a snippet function, not a component or some other kind of function. If you want to dynamically render one snippet or another, use \`$derived\` and pass its result to \`{@render ...}\``);
2929

3030
error.name = 'Svelte error';
3131
throw error;
@@ -41,7 +41,7 @@ export function render_tag_invalid_argument() {
4141
*/
4242
export function snippet_used_as_component() {
4343
if (DEV) {
44-
const error = new Error(`${"snippet_used_as_component"}\n${"A snippet must be rendered with `{@render ...}`"}`);
44+
const error = new Error(`snippet_used_as_component\nA snippet must be rendered with \`{@render ...}\``);
4545

4646
error.name = 'Svelte error';
4747
throw error;
@@ -58,7 +58,7 @@ export function snippet_used_as_component() {
5858
*/
5959
export function store_invalid_shape(name) {
6060
if (DEV) {
61-
const error = new Error(`${"store_invalid_shape"}\n${`\`${name}\` is not a store with a \`subscribe\` method`}`);
61+
const error = new Error(`store_invalid_shape\n\`${name}\` is not a store with a \`subscribe\` method`);
6262

6363
error.name = 'Svelte error';
6464
throw error;
@@ -74,7 +74,7 @@ export function store_invalid_shape(name) {
7474
*/
7575
export function svelte_element_invalid_this_value() {
7676
if (DEV) {
77-
const error = new Error(`${"svelte_element_invalid_this_value"}\n${"The `this` prop on `<svelte:element>` must be a string, if defined"}`);
77+
const error = new Error(`svelte_element_invalid_this_value\nThe \`this\` prop on \`<svelte:element>\` must be a string, if defined`);
7878

7979
error.name = 'Svelte error';
8080
throw error;

packages/svelte/src/internal/shared/warnings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var normal = 'font-weight: normal';
1111
*/
1212
export function dynamic_void_element_content(tag) {
1313
if (DEV) {
14-
console.warn(`%c[svelte] ${"dynamic_void_element_content"}\n%c${`\`<svelte:element this="${tag}">\` is a void element — it cannot have content`}`, bold, normal);
14+
console.warn(`%c[svelte] dynamic_void_element_content\n%c\`<svelte:element this="${tag}">\` is a void element — it cannot have content`, bold, normal);
1515
} else {
1616
// TODO print a link to the documentation
1717
console.warn("dynamic_void_element_content");

0 commit comments

Comments
 (0)