fix: trim whitespace while migrating blocks (#13941)

Co-authored-by: Oscar Dominguez <dominguez.celada@gmail.com>
pull/13967/head
Paolo Ricciuti 1 week ago committed by GitHub
parent 7be3afb3a6
commit 12fcc7a35a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: trim whitespace while migrating blocks

@ -853,6 +853,20 @@ const instance_script = {
}
};
/**
*
* @param {State} state
* @param {number} start
* @param {number} end
*/
function trim_block(state, start, end) {
const original = state.str.snip(start, end).toString();
const without_parens = original.substring(1, original.length - 1);
if (without_parens.trim().length !== without_parens.length) {
state.str.update(start + 1, end - 1, without_parens.trim());
}
}
/** @type {Visitors<SvelteNode, State>} */
const template = {
Identifier(node, { state, path }) {
@ -1119,6 +1133,46 @@ const template = {
if (migrated !== node.data) {
state.str.overwrite(node.start + '<!--'.length, node.end - '-->'.length, migrated);
}
},
HtmlTag(node, { state, next }) {
trim_block(state, node.start, node.end);
next();
},
ConstTag(node, { state, next }) {
trim_block(state, node.start, node.end);
next();
},
IfBlock(node, { state, next }) {
const start = node.start;
const end = state.str.original.indexOf('}', node.test.end) + 1;
trim_block(state, start, end);
next();
},
AwaitBlock(node, { state, next }) {
const start = node.start;
const end =
state.str.original.indexOf(
'}',
node.pending !== null ? node.expression.end : node.value?.end
) + 1;
trim_block(state, start, end);
if (node.pending !== null) {
const start = state.str.original.lastIndexOf('{', node.value?.start);
const end = state.str.original.indexOf('}', node.value?.end) + 1;
trim_block(state, start, end);
}
if (node.catch !== null) {
const start = state.str.original.lastIndexOf('{', node.error?.start);
const end = state.str.original.indexOf('}', node.error?.end) + 1;
trim_block(state, start, end);
}
next();
},
KeyBlock(node, { state, next }) {
const start = node.start;
const end = state.str.original.indexOf('}', node.expression.end) + 1;
trim_block(state, start, end);
next();
}
};

@ -0,0 +1,27 @@
{ @html "some html" }
{ #if false && {
}.x === 34 }
true
{ :else if false }
false
{/if}
{ #await [] }
{ @const x = 43 }
{x}
{ :then i }
{i}
{ :catch e }
dlkdj
{/await}
{ #await [] then i }
stuff
{/await}
{ #key count }
dlkdj
{/key}

@ -0,0 +1,26 @@
{@html "some html"}
{#if false && {
}.x === 34}
true
{:else if false}
false
{/if}
{#await []}
{@const x = 43}
{x}
{:then i}
{i}
{:catch e}
dlkdj
{/await}
{#await [] then i}
stuff
{/await}
{#key count}
dlkdj
{/key}
Loading…
Cancel
Save