Skip to content

Commit 69376a6

Browse files
committed
Fix printing around references
Fixes #765
1 parent 6170e95 commit 69376a6

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/printer.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,10 @@ function printElementFragments(path, opts, print) {
348348
const referenceNode = referencePath.getValue();
349349

350350
return {
351+
type: "reference",
351352
offset: referenceNode.location.startOffset,
353+
startLine: referenceNode.location.startLine,
354+
endLine: referenceNode.location.endLine,
352355
printed: print(referencePath)
353356
};
354357
}, "reference")
@@ -476,6 +479,10 @@ function printElement(path, opts, print) {
476479
]);
477480
}
478481

482+
if (fragments.length === 0) {
483+
return group([...parts, space, "/>"]);
484+
}
485+
479486
// If the only content of this tag is chardata, then use a softline so
480487
// that we won't necessarily break (to allow <foo>bar</foo>).
481488
if (
@@ -490,18 +497,28 @@ function printElement(path, opts, print) {
490497
]);
491498
}
492499

493-
if (fragments.length === 0) {
494-
return group([...parts, space, "/>"]);
500+
let delimiter = hardline;
501+
502+
// If the only content is both chardata and references, then use a softline
503+
// so that we won't necessarily break.
504+
if (
505+
fragments.length ===
506+
content.chardata.filter((chardata) => chardata.TEXT).length +
507+
content.reference.length
508+
) {
509+
delimiter = " ";
495510
}
496511

497-
const docs = [];
512+
const docs = [hardline];
498513
let lastLine = fragments[0].startLine;
499514

500-
fragments.forEach((node) => {
501-
if (node.startLine - lastLine >= 2) {
502-
docs.push(hardline, hardline);
503-
} else {
504-
docs.push(hardline);
515+
fragments.forEach((node, index) => {
516+
if (index !== 0) {
517+
if (node.startLine - lastLine >= 2) {
518+
docs.push(hardline, hardline);
519+
} else {
520+
docs.push(delimiter);
521+
}
505522
}
506523

507524
docs.push(node.printed);

0 commit comments

Comments
 (0)