Handlebars printer: two small refactorings (#10285)

* Refactor generateHardlines

* Move variables

* Fix isWhitespaceOnly regex
This commit is contained in:
Cyrille David authored and GitHub committed 2021-02-09 21:49:31 +02:00
1 parent 03a43ae55f
commit 091e91e50d
1 file changed
+14 -12
+14 -12
View File
@@ -32,6 +32,8 @@ const {
isWhitespaceNode,
} = require("./utils");
const NEWLINES_TO_PRESERVE_MAX = 2;
// Formatter based on @glimmerjs/syntax's built-in test formatter:
// https://github.com/glimmerjs/glimmer-vm/blob/master/packages/%40glimmer/syntax/lib/generation/print.ts
@@ -241,18 +243,20 @@ function print(path, options, print) {
return replaceEndOfLineWith(text, literalline);
}
const whitespacesOnlyRE = /^[\t\n\f\r ]*$/;
const isWhitespaceOnly = whitespacesOnlyRE.test(text);
if (options.htmlWhitespaceSensitivity === "strict") {
// https://infra.spec.whatwg.org/#ascii-whitespace
const leadingWhitespacesRE = /^[\t\n\f\r ]*/;
const trailingWhitespacesRE = /[\t\n\f\r ]*$/;
const whitespacesOnlyRE = /^[\t\n\f\r ]*$/;
if (whitespacesOnlyRE.test(text)) {
if (isWhitespaceOnly) {
let breaks = [line];
const newlines = countNewLines(text);
if (newlines) {
breaks = generateHardlines(newlines, 2);
breaks = generateHardlines(newlines);
}
if (isLastNodeOfSiblings(path)) {
@@ -271,7 +275,7 @@ function print(path, options, print) {
const leadingNewlines = countNewLines(lead);
if (leadingNewlines) {
leadBreaks = generateHardlines(countNewLines(lead) || 1, 2);
leadBreaks = generateHardlines(countNewLines(lead) || 1);
}
text = text.replace(leadingWhitespacesRE, "");
@@ -283,7 +287,7 @@ function print(path, options, print) {
const trailingNewlines = countNewLines(tail);
if (trailingNewlines) {
trailBreaks = generateHardlines(trailingNewlines || 1, 2);
trailBreaks = generateHardlines(trailingNewlines || 1);
if (isLastNodeOfSiblings(path)) {
trailBreaks = trailBreaks.map((hardline) => dedent(hardline));
@@ -296,10 +300,8 @@ function print(path, options, print) {
return [...leadBreaks, fill(getTextValueParts(text)), ...trailBreaks];
}
const maxLineBreaksToPreserve = 2;
const isFirstElement = !getPreviousNode(path);
const isLastElement = !getNextNode(path);
const isWhitespaceOnly = !/\S/.test(text);
const lineBreaksCount = countNewLines(text);
let leadingLineBreaksCount = countLeadingNewLines(text);
@@ -316,7 +318,7 @@ function print(path, options, print) {
if (isWhitespaceOnly && lineBreaksCount) {
leadingLineBreaksCount = Math.min(
lineBreaksCount,
maxLineBreaksToPreserve
NEWLINES_TO_PRESERVE_MAX
);
trailingLineBreaksCount = 0;
} else {
@@ -361,9 +363,9 @@ function print(path, options, print) {
.replace(/[\t\n\f\r ]+$/, trailingSpace);
return [
...generateHardlines(leadingLineBreaksCount, maxLineBreaksToPreserve),
...generateHardlines(leadingLineBreaksCount),
fill(getTextValueParts(text)),
...generateHardlines(trailingLineBreaksCount, maxLineBreaksToPreserve),
...generateHardlines(trailingLineBreaksCount),
];
}
case "MustacheCommentStatement": {
@@ -680,8 +682,8 @@ function countTrailingNewLines(string) {
return countNewLines(newLines);
}
function generateHardlines(number = 0, max = 0) {
return new Array(Math.min(number, max)).fill(hardline);
function generateHardlines(number = 0) {
return new Array(Math.min(number, NEWLINES_TO_PRESERVE_MAX)).fill(hardline);
}
/* StringLiteral print helpers */