Rich Harris 3 months ago
parent 0a61c80be3
commit 5db4ea878a

@ -4,6 +4,7 @@ import { parse, print } from 'svelte/compiler';
import { try_load_json } from '../helpers.js'; import { try_load_json } from '../helpers.js';
import { suite, type BaseTest } from '../suite.js'; import { suite, type BaseTest } from '../suite.js';
import { walk } from 'zimmerframe'; import { walk } from 'zimmerframe';
import type { AST } from 'svelte/compiler';
interface ParserTest extends BaseTest {} interface ParserTest extends BaseTest {}
@ -55,7 +56,7 @@ const { test, run } = suite<ParserTest>(async (config, cwd) => {
} }
}); });
function clean(ast: import('svelte/compiler').AST.SvelteNode) { function clean(ast: AST.SvelteNode) {
return walk(ast, null, { return walk(ast, null, {
_(node, context) { _(node, context) {
// @ts-ignore // @ts-ignore
@ -72,24 +73,27 @@ function clean(ast: import('svelte/compiler').AST.SvelteNode) {
context.next(); context.next();
}, },
Fragment(node, context) { Fragment(node, context) {
return { const nodes: AST.SvelteNode[] = [];
...node,
nodes: node.nodes for (let i = 0; i < node.nodes.length; i += 1) {
.map((child, i) => { let child = node.nodes[i];
if (child.type === 'Text') {
if (i === 0) { if (child.type === 'Text') {
child = { ...child, data: child.data.trimStart() }; if (i === 0) {
} child = { ...child, data: child.data.trimStart() };
}
if (i === node.nodes.length - 1) {
child = { ...child, data: child.data.trimEnd() }; if (i === node.nodes.length - 1) {
} child = { ...child, data: child.data.trimEnd() };
}
if (!child.data) return null;
} if (!child.data) continue;
}) }
.filter(Boolean)
}; nodes.push(context.visit(child));
}
return { ...node, nodes } as AST.Fragment;
} }
}); });
} }

Loading…
Cancel
Save