fix: don't duplicate comments in attributes (#18636)

An attribute with a comment would be printed again atop the following
attribute because we didn't take that case into account.
pull/18638/head
Simon H 2 weeks ago committed by GitHub
parent 26786e9298
commit 3ed9db4ba7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: don't duplicate comments in attributes

@ -82,6 +82,7 @@ function attributes(node, attributes, context, comments) {
}
const separator = context.new();
let previous_attribute_end = node.start;
const children = attributes.map((attribute) => {
const child_context = context.new();
@ -90,12 +91,16 @@ function attributes(node, attributes, context, comments) {
const comment = comments[comment_index];
if (comment.start < attribute.start) {
if (comment.type === 'Line') {
child_context.write('//' + comment.value);
child_context.newline();
} else {
child_context.write('/*' + comment.value + '*/'); // TODO match indentation?
child_context.append(separator);
// Inside a previous attribute's value can be comments which don't
// advance comment_index, therefore this additional check
if (comment.start >= previous_attribute_end) {
if (comment.type === 'Line') {
child_context.write('//' + comment.value);
child_context.newline();
} else {
child_context.write('/*' + comment.value + '*/'); // TODO match indentation?
child_context.append(separator);
}
}
comment_index += 1;
@ -105,6 +110,7 @@ function attributes(node, attributes, context, comments) {
}
child_context.visit(attribute);
previous_attribute_end = attribute.end;
length += child_context.measure() + 1;

@ -0,0 +1,7 @@
<Button
onclick={() => {
// belongs to onclick
run();
}}
onkeydown={() => run()}
/>

@ -0,0 +1,7 @@
<Button
onclick={() => {
// belongs to onclick
run();
}}
onkeydown={() => run()}
/>
Loading…
Cancel
Save