8000 fix: remove commas (#1379) · sveltejs/svelte.dev@0dd78c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0dd78c6

Browse files
authored
fix: remove commas (#1379)
Commas could be left in, which in certain combinations can lead to multiple commas after each other which is invalid syntax
1 parent 5901b5b commit 0dd78c6

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

packages/repl/src/lib/workers/typescript-strip-types.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ function ts_blank_space(context: Context<any, { ms: MagicString }>, node: any):
5858
}
5959
}
6060

61+
function specifier_end(node: any, i: number, s: any, context: Context<any, { ms: MagicString }>) {
62+
let end = node.specifiers[i + 1]?.start;
63+
if (end === undefined) {
64+
end = s.end;
65+
// Look for a comma after s.end, skipping whitespace
66+
let j = end;
67+
const original = context.state.ms.original;
68+
while (j < original.length && /\s/.test(original[j])) j++;
69+
if (original[j] === ',') {
70+
end = j + 1;
71+
}
72+
}
73+
return end;
74+
}
75+
6176
const visitors: Visitors<any, { ms: MagicString }> = {
6277
_(node, context) {
6378
if (node.typeAnnotation) ts_blank_space(context, node.typeAnnotation);
@@ -85,7 +100,7 @@ const visitors: Visitors<any, { ms: MagicString }> = {
85100

86101
ts_blank_space(context, {
87102
start: s.start,
88-
end: node.specifiers[i + 1]?.start || s.end
103+
end: specifier_end(node, i, s, context)
89104
});
90105
});
91106

@@ -115,7 +130,7 @@ const visitors: Visitors<any, { ms: MagicString }> = {
115130

116131
ts_blank_space(context, {
117132
start: s.start,
118-
end: node.specifiers[i + 1]?.start || s.end
133+
end: specifier_end(node, i, s, context)
119134
});
120135
});
121136

0 commit comments

Comments
 (0)
0