Skip to content

Commit 12fcc7a

Browse files
fix: trim whitespace while migrating blocks (#13941)
Co-authored-by: Oscar Dominguez <[email protected]>
1 parent 7be3afb commit 12fcc7a

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

.changeset/tasty-frogs-boil.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+
fix: trim whitespace while migrating blocks

packages/svelte/src/compiler/migrate/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,20 @@ const instance_script = {
853853
}
854854
};
855855

856+
/**
857+
*
858+
* @param {State} state
859+
* @param {number} start
860+
* @param {number} end
861+
*/
862+
function trim_block(state, start, end) {
863+
const original = state.str.snip(start, end).toString();
864+
const without_parens = original.substring(1, original.length - 1);
865+
if (without_parens.trim().length !== without_parens.length) {
866+
state.str.update(start + 1, end - 1, without_parens.trim());
867+
}
868+
}
869+
856870
/** @type {Visitors<SvelteNode, State>} */
857871
const template = {
858872
Identifier(node, { state, path }) {
@@ -1119,6 +1133,46 @@ const template = {
11191133
if (migrated !== node.data) {
11201134
state.str.overwrite(node.start + '<!--'.length, node.end - '-->'.length, migrated);
11211135
}
1136+
},
1137+
HtmlTag(node, { state, next }) {
1138+
trim_block(state, node.start, node.end);
1139+
next();
1140+
},
1141+
ConstTag(node, { state, next }) {
1142+
trim_block(state, node.start, node.end);
1143+
next();
1144+
},
1145+
IfBlock(node, { state, next }) {
1146+
const start = node.start;
1147+
const end = state.str.original.indexOf('}', node.test.end) + 1;
1148+
trim_block(state, start, end);
1149+
next();
1150+
},
1151+
AwaitBlock(node, { state, next }) {
1152+
const start = node.start;
1153+
const end =
1154+
state.str.original.indexOf(
1155+
'}',
1156+
node.pending !== null ? node.expression.end : node.value?.end
1157+
) + 1;
1158+
trim_block(state, start, end);
1159+
if (node.pending !== null) {
1160+
const start = state.str.original.lastIndexOf('{', node.value?.start);
1161+
const end = state.str.original.indexOf('}', node.value?.end) + 1;
1162+
trim_block(state, start, end);
1163+
}
1164+
if (node.catch !== null) {
1165+
const start = state.str.original.lastIndexOf('{', node.error?.start);
1166+
const end = state.str.original.indexOf('}', node.error?.end) + 1;
1167+
trim_block(state, start, end);
1168+
}
1169+
next();
1170+
},
1171+
KeyBlock(node, { state, next }) {
1172+
const start = node.start;
1173+
const end = state.str.original.indexOf('}', node.expression.end) + 1;
1174+
trim_block(state, start, end);
1175+
next();
11221176
}
11231177
};
11241178

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{ @html "some html" }
2+
3+
{ #if false && {
4+
5+
}.x === 34 }
6+
true
7+
{ :else if false }
8+
false
9+
{/if}
10+
11+
{ #await [] }
12+
{ @const x = 43 }
13+
{x}
14+
{ :then i }
15+
{i}
16+
{ :catch e }
17+
dlkdj
18+
{/await}
19+
20+
{ #await [] then i }
21+
stuff
22+
{/await}
23+
24+
{ #key count }
25+
dlkdj
26+
{/key}
27+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{@html "some html"}
2+
3+
{#if false && {
4+
5+
}.x === 34}
6+
true
7+
{:else if false}
8+
false
9+
{/if}
10+
11+
{#await []}
12+
{@const x = 43}
13+
{x}
14+
{:then i}
15+
{i}
16+
{:catch e}
17+
dlkdj
18+
{/await}
19+
20+
{#await [] then i}
21+
stuff
22+
{/await}
23+
24+
{#key count}
25+
dlkdj
26+
{/key}

0 commit comments

Comments
 (0)