From 12fcc7a35a9fedfa5cf3cb85b4cc904b75f2aff5 Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Sat, 26 Oct 2024 18:20:37 +0200 Subject: [PATCH] fix: trim whitespace while migrating blocks (#13941) Co-authored-by: Oscar Dominguez --- .changeset/tasty-frogs-boil.md | 5 ++ packages/svelte/src/compiler/migrate/index.js | 54 +++++++++++++++++++ .../remove-blocks-whitespace/input.svelte | 27 ++++++++++ .../remove-blocks-whitespace/output.svelte | 26 +++++++++ 4 files changed, 112 insertions(+) create mode 100644 .changeset/tasty-frogs-boil.md create mode 100644 packages/svelte/tests/migrate/samples/remove-blocks-whitespace/input.svelte create mode 100644 packages/svelte/tests/migrate/samples/remove-blocks-whitespace/output.svelte diff --git a/.changeset/tasty-frogs-boil.md b/.changeset/tasty-frogs-boil.md new file mode 100644 index 0000000000..26b33db998 --- /dev/null +++ b/.changeset/tasty-frogs-boil.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: trim whitespace while migrating blocks diff --git a/packages/svelte/src/compiler/migrate/index.js b/packages/svelte/src/compiler/migrate/index.js index ceb2ad5da2..0bf3ac08b2 100644 --- a/packages/svelte/src/compiler/migrate/index.js +++ b/packages/svelte/src/compiler/migrate/index.js @@ -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} */ const template = { Identifier(node, { state, path }) { @@ -1119,6 +1133,46 @@ const template = { if (migrated !== node.data) { state.str.overwrite(node.start + ''.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(); } }; diff --git a/packages/svelte/tests/migrate/samples/remove-blocks-whitespace/input.svelte b/packages/svelte/tests/migrate/samples/remove-blocks-whitespace/input.svelte new file mode 100644 index 0000000000..d1ff0668a8 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/remove-blocks-whitespace/input.svelte @@ -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} + diff --git a/packages/svelte/tests/migrate/samples/remove-blocks-whitespace/output.svelte b/packages/svelte/tests/migrate/samples/remove-blocks-whitespace/output.svelte new file mode 100644 index 0000000000..e2cb616020 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/remove-blocks-whitespace/output.svelte @@ -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} \ No newline at end of file