Skip to content

Commit

Permalink
fix: trim whitespace while migrating blocks (#13941)
Browse files Browse the repository at this point in the history
Co-authored-by: Oscar Dominguez <[email protected]>
  • Loading branch information
paoloricciuti and oscard0m authored Oct 26, 2024
1 parent 7be3afb commit 12fcc7a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-frogs-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: trim whitespace while migrating blocks
54 changes: 54 additions & 0 deletions packages/svelte/src/compiler/migrate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down Expand Up @@ -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();
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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}

Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit 12fcc7a

Please sign in to comment.