Skip to content

Commit 0a61c80

Browse files
committed
tweak test logic to reduce false negatives
1 parent e41bbe7 commit 0a61c80

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function print(ast) {
1616
});
1717
}
1818

19-
/** @type {Visitors<AST.SvelteNode, any>} */
19+
/** @type {Visitors<AST.SvelteNode>} */
2020
const visitors = {
2121
Root(node, context) {
2222
if (node.options) {

packages/svelte/tests/parser-modern/test.ts

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,51 @@ const { test, run } = suite<ParserTest>(async (config, cwd) => {
4949

5050
fs.writeFileSync(`${cwd}/_actual.svelte`, printed.code);
5151

52-
const actual_cleaned = walk(actual, null, {
53-
_(node, context) {
54-
delete node.loc;
55-
context.next();
56-
}
57-
});
58-
5952
delete reparsed.comments;
6053

61-
const reparsed_cleaned = walk(reparsed, null, {
62-
_(node, context) {
63-
delete node.loc;
64-
context.next();
65-
}
66-
});
67-
68-
assert.deepEqual(actual_cleaned, reparsed_cleaned);
54+
assert.deepEqual(clean(actual), clean(reparsed));
6955
}
7056
});
7157

58+
function clean(ast: import('svelte/compiler').AST.SvelteNode) {
59+
return walk(ast, null, {
60+
_(node, context) {
61+
// @ts-ignore
62+
delete node.start;
63+
// @ts-ignore
64+
delete node.end;
65+
// @ts-ignore
66+
delete node.loc;
67+
// @ts-ignore
68+
delete node.leadingComments;
69+
// @ts-ignore
70+
delete node.trailingComments;
71+
72+
context.next();
73+
},
74+
Fragment(node, context) {
75+
return {
76+
...node,
77+
nodes: node.nodes
78+
.map((child, i) => {
79+
if (child.type === 'Text') {
80+
if (i === 0) {
81+
child = { ...child, data: child.data.trimStart() };
82+
}
83+
84+
if (i === node.nodes.length - 1) {
85+
child = { ...child, data: child.data.trimEnd() };
86+
}
87+
88+
if (!child.data) return null;
89+
}
90+
})
91+
.filter(Boolean)
92+
};
93+
}
94+
});
95+
}
96+
7297
export { test };
7398

7499
await run(__dirname);

0 commit comments

Comments
 (0)