Avoid repeated blank lines in unformatted Angular and Vue interpolations (#20001)
Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
1 parent
20e88e6018
commit
bbd3b67851
10 files changed
+248
-5
No files matched your search
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
|
||||
import {
|
||||
breakParent,
|
||||
fill,
|
||||
group,
|
||||
hardline,
|
||||
@@ -30,6 +31,7 @@ import { printChildren } from "./print/children.js";
|
||||
import { printElement } from "./print/element.js";
|
||||
import { printStartTagComment } from "./print/start-tag-comment.js";
|
||||
import {
|
||||
needsToBorrowPrevClosingTagEndMarker,
|
||||
printClosingTagEnd,
|
||||
printClosingTagSuffix,
|
||||
printOpeningTagPrefix,
|
||||
@@ -90,13 +92,30 @@ function genericPrint(path, options, print) {
|
||||
];
|
||||
case "text": {
|
||||
if (node.parent.kind === "interpolation") {
|
||||
let { value } = node;
|
||||
// replace the trailing literalline with hardline for better readability
|
||||
const trailingNewlineRegex = /\n[^\S\n]*$/;
|
||||
const hasTrailingNewline = trailingNewlineRegex.test(node.value);
|
||||
const value = hasTrailingNewline
|
||||
? node.value.replace(trailingNewlineRegex, "")
|
||||
: node.value;
|
||||
return [replaceEndOfLine(value), hasTrailingNewline ? hardline : ""];
|
||||
const hasTrailingNewline = trailingNewlineRegex.test(value);
|
||||
|
||||
if (hasTrailingNewline) {
|
||||
value = value.replace(trailingNewlineRegex, "");
|
||||
}
|
||||
|
||||
const parts = [replaceEndOfLine(value)];
|
||||
|
||||
if (hasTrailingNewline) {
|
||||
// The interpolation's trailing softline already prints this newline.
|
||||
if (
|
||||
node.parent.next &&
|
||||
needsToBorrowPrevClosingTagEndMarker(node.parent.next)
|
||||
) {
|
||||
parts.push(breakParent);
|
||||
} else {
|
||||
parts.push(hardline);
|
||||
}
|
||||
}
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
const prefix = printOpeningTagPrefix(node, options);
|
||||
|
||||
Reference in new issue
Block a user