From 0a61c80be3a73c1380b40df55e37e3d145aa6ba7 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 23 Jun 2025 06:50:10 -0400 Subject: [PATCH] tweak test logic to reduce false negatives --- packages/svelte/src/compiler/print/index.js | 2 +- packages/svelte/tests/parser-modern/test.ts | 55 +++++++++++++++------ 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/packages/svelte/src/compiler/print/index.js b/packages/svelte/src/compiler/print/index.js index 45feedff4c..f0c5409870 100644 --- a/packages/svelte/src/compiler/print/index.js +++ b/packages/svelte/src/compiler/print/index.js @@ -16,7 +16,7 @@ export function print(ast) { }); } -/** @type {Visitors} */ +/** @type {Visitors} */ const visitors = { Root(node, context) { if (node.options) { diff --git a/packages/svelte/tests/parser-modern/test.ts b/packages/svelte/tests/parser-modern/test.ts index da9119441a..74fabda633 100644 --- a/packages/svelte/tests/parser-modern/test.ts +++ b/packages/svelte/tests/parser-modern/test.ts @@ -49,26 +49,51 @@ const { test, run } = suite(async (config, cwd) => { fs.writeFileSync(`${cwd}/_actual.svelte`, printed.code); - const actual_cleaned = walk(actual, null, { - _(node, context) { - delete node.loc; - context.next(); - } - }); - delete reparsed.comments; - const reparsed_cleaned = walk(reparsed, null, { - _(node, context) { - delete node.loc; - context.next(); - } - }); - - assert.deepEqual(actual_cleaned, reparsed_cleaned); + assert.deepEqual(clean(actual), clean(reparsed)); } }); +function clean(ast: import('svelte/compiler').AST.SvelteNode) { + return walk(ast, null, { + _(node, context) { + // @ts-ignore + delete node.start; + // @ts-ignore + delete node.end; + // @ts-ignore + delete node.loc; + // @ts-ignore + delete node.leadingComments; + // @ts-ignore + delete node.trailingComments; + + context.next(); + }, + Fragment(node, context) { + return { + ...node, + nodes: node.nodes + .map((child, i) => { + if (child.type === 'Text') { + if (i === 0) { + child = { ...child, data: child.data.trimStart() }; + } + + if (i === node.nodes.length - 1) { + child = { ...child, data: child.data.trimEnd() }; + } + + if (!child.data) return null; + } + }) + .filter(Boolean) + }; + } + }); +} + export { test }; await run(__dirname);