Skip to content

Commit

Permalink
Replace replaceTextEndOfLine with replaceEndOfLine (#13343)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Aug 27, 2022
1 parent 5c465a7 commit d0f0b09
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 52 deletions.
11 changes: 3 additions & 8 deletions src/document/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,18 +390,14 @@ function normalizeDoc(doc) {
});
}

function replaceEndOfLine(doc) {
function replaceEndOfLine(doc, replacement = literalline) {
return mapDoc(doc, (currentDoc) =>
typeof currentDoc === "string" && currentDoc.includes("\n")
? replaceTextEndOfLine(currentDoc)
typeof currentDoc === "string"
? join(replacement, currentDoc.split("\n"))
: currentDoc
);
}

function replaceTextEndOfLine(text, replacement = literalline) {
return join(replacement, text.split("\n"));
}

function canBreakFn(doc) {
if (doc.type === DOC_TYPE_LINE) {
return true;
Expand All @@ -424,7 +420,6 @@ export {
normalizeParts,
normalizeDoc,
cleanDoc,
replaceTextEndOfLine,
replaceEndOfLine,
canBreak,
getDocType,
Expand Down
4 changes: 2 additions & 2 deletions src/language-handlebars/printer-glimmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
line,
softline,
} from "../document/builders.js";
import { replaceTextEndOfLine } from "../document/utils.js";
import { replaceEndOfLine } from "../document/utils.js";
import { getPreferredQuote, isNonEmptyArray } from "../common/util.js";
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
import { locStart, locEnd } from "./loc.js";
Expand Down Expand Up @@ -224,7 +224,7 @@ function print(path, options, print) {
];
}

return replaceTextEndOfLine(text);
return replaceEndOfLine(text);
}

const whitespacesOnlyRE = /^[\t\n\f\r ]*$/;
Expand Down
6 changes: 3 additions & 3 deletions src/language-html/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
fill,
softline,
} from "../document/builders.js";
import { mapDoc, replaceTextEndOfLine } from "../document/utils.js";
import { mapDoc, replaceEndOfLine } from "../document/utils.js";
import printFrontMatter from "../utils/front-matter/print.js";
import {
printClosingTag,
Expand Down Expand Up @@ -216,7 +216,7 @@ async function printEmbeddedAttributeValue(node, htmlTextToDoc, options) {
const parts = [];
for (const [index, part] of value.split(interpolationRegex).entries()) {
if (index % 2 === 0) {
parts.push(replaceTextEndOfLine(part));
parts.push(replaceEndOfLine(part));
} else {
try {
parts.push(
Expand All @@ -234,7 +234,7 @@ async function printEmbeddedAttributeValue(node, htmlTextToDoc, options) {
])
);
} catch {
parts.push("{{", replaceTextEndOfLine(part), "}}");
parts.push("{{", replaceEndOfLine(part), "}}");
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/language-html/print/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
softline,
hardline,
} from "../../document/builders.js";
import { replaceTextEndOfLine } from "../../document/utils.js";
import { replaceEndOfLine } from "../../document/utils.js";
import { locStart, locEnd } from "../loc.js";
import {
forceBreakChildren,
Expand All @@ -31,7 +31,7 @@ function printChild(childPath, options, print) {
if (hasPrettierIgnore(child)) {
return [
printOpeningTagPrefix(child, options),
...replaceTextEndOfLine(
replaceEndOfLine(
options.originalText.slice(
locStart(child) +
(child.prev && needsToBorrowNextOpeningTagStartMarker(child.prev)
Expand Down
4 changes: 2 additions & 2 deletions src/language-html/print/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
line,
softline,
} from "../../document/builders.js";
import { replaceTextEndOfLine } from "../../document/utils.js";
import { replaceEndOfLine } from "../../document/utils.js";
import getNodeContent from "../get-node-content.js";
import {
shouldPreserveContent,
Expand All @@ -34,7 +34,7 @@ function printElement(path, options, print) {
return [
printOpeningTagPrefix(node, options),
group(printOpeningTag(path, options, print)),
...replaceTextEndOfLine(getNodeContent(node, options)),
replaceEndOfLine(getNodeContent(node, options)),
...printClosingTag(node, options),
printClosingTagSuffix(node, options),
];
Expand Down
4 changes: 2 additions & 2 deletions src/language-html/print/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
softline,
hardline,
} from "../../document/builders.js";
import { replaceTextEndOfLine } from "../../document/utils.js";
import { replaceEndOfLine } from "../../document/utils.js";
import { locStart, locEnd } from "../loc.js";
import {
isTextLikeNode,
Expand Down Expand Up @@ -241,7 +241,7 @@ function printAttributes(path, options, print) {
const printedAttributes = path.map((attributePath) => {
const attribute = attributePath.getValue();
return hasPrettierIgnoreAttribute(attribute)
? replaceTextEndOfLine(
? replaceEndOfLine(
options.originalText.slice(locStart(attribute), locEnd(attribute))
)
: print();
Expand Down
15 changes: 5 additions & 10 deletions src/language-html/printer-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import { fill, group, hardline } from "../document/builders.js";
import { cleanDoc, replaceTextEndOfLine } from "../document/utils.js";
import { cleanDoc, replaceEndOfLine } from "../document/utils.js";
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
import clean from "./clean.js";
import {
Expand All @@ -30,7 +30,7 @@ function genericPrint(path, options, print) {

switch (node.type) {
case "front-matter":
return replaceTextEndOfLine(node.raw);
return replaceEndOfLine(node.raw);
case "root":
if (options.__onHtmlRoot) {
options.__onHtmlRoot(node);
Expand All @@ -57,10 +57,7 @@ function genericPrint(path, options, print) {
const value = hasTrailingNewline
? node.value.replace(trailingNewlineRegex, "")
: node.value;
return [
...replaceTextEndOfLine(value),
hasTrailingNewline ? hardline : "",
];
return [replaceEndOfLine(value), hasTrailingNewline ? hardline : ""];
}

const printed = cleanDoc([
Expand All @@ -87,7 +84,7 @@ function genericPrint(path, options, print) {
case "comment": {
return [
printOpeningTagPrefix(node, options),
...replaceTextEndOfLine(
replaceEndOfLine(
options.originalText.slice(locStart(node), locEnd(node))
),
printClosingTagSuffix(node, options),
Expand All @@ -103,11 +100,9 @@ function genericPrint(path, options, print) {
const quote = singleQuoteCount < doubleQuoteCount ? "'" : '"';
return [
node.rawName,

"=",
quote,

...replaceTextEndOfLine(
replaceEndOfLine(
quote === '"'
? value.replace(/"/g, "&quot;")
: value.replace(/'/g, "&apos;")
Expand Down
6 changes: 3 additions & 3 deletions src/language-html/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { inferParserByLanguage, isFrontMatterNode } from "../../common/util.js";
import { line, hardline, join } from "../../document/builders.js";
import { replaceTextEndOfLine } from "../../document/utils.js";
import { replaceEndOfLine } from "../../document/utils.js";
import {
CSS_DISPLAY_TAGS,
CSS_DISPLAY_DEFAULT,
Expand Down Expand Up @@ -633,8 +633,8 @@ function isVueSfcBindingsAttribute(attribute, options) {
function getTextValueParts(node, value = node.value) {
return node.parent.isWhitespaceSensitive
? node.parent.isIndentationSensitive
? replaceTextEndOfLine(value)
: replaceTextEndOfLine(
? replaceEndOfLine(value)
: replaceEndOfLine(
dedentString(htmlTrimPreserveIndentation(value)),
hardline
)
Expand Down
4 changes: 2 additions & 2 deletions src/language-js/print/comment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hasNewline } from "../../common/util.js";
import { join, hardline } from "../../document/builders.js";
import { replaceTextEndOfLine } from "../../document/utils.js";
import { replaceEndOfLine } from "../../document/utils.js";

import { isLineComment } from "../utils/index.js";
import { locStart, locEnd } from "../loc.js";
Expand Down Expand Up @@ -38,7 +38,7 @@ function printComment(commentPath, options) {
options.originalText.slice(commentEnd - 3, commentEnd) === "*-/";
return [
"/*",
replaceTextEndOfLine(comment.value),
replaceEndOfLine(comment.value),
isInsideFlowComment ? "*-/" : "*/",
];
}
Expand Down
4 changes: 2 additions & 2 deletions src/language-js/print/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assert from "node:assert";
import { printDanglingComments } from "../../main/comments.js";
import { printString, printNumber } from "../../common/util.js";
import { hardline, softline, group, indent } from "../../document/builders.js";
import { replaceTextEndOfLine } from "../../document/utils.js";
import { replaceEndOfLine } from "../../document/utils.js";
import {
getParentExportDeclaration,
isFunctionNotation,
Expand Down Expand Up @@ -269,7 +269,7 @@ function printFlow(path, options, print) {
case "QualifiedTypeIdentifier":
return [print("qualification"), ".", print("id")];
case "StringLiteralTypeAnnotation":
return replaceTextEndOfLine(printString(rawText(node), options));
return replaceEndOfLine(printString(rawText(node), options));
case "NumberLiteralTypeAnnotation":
assert.strictEqual(typeof node.value, "number");
// fall through
Expand Down
6 changes: 3 additions & 3 deletions src/language-js/print/literal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { printString, printNumber } from "../../common/util.js";
import { replaceTextEndOfLine } from "../../document/utils.js";
import { replaceEndOfLine } from "../../document/utils.js";

function printLiteral(path, options /*, print*/) {
const node = path.getNode();
Expand All @@ -13,7 +13,7 @@ function printLiteral(path, options /*, print*/) {
case "NumericLiteral": // Babel 6 Literal split
return printNumber(node.extra.raw);
case "StringLiteral": // Babel 6 Literal split
return replaceTextEndOfLine(printString(node.extra.raw, options));
return replaceEndOfLine(printString(node.extra.raw, options));
case "NullLiteral": // Babel 6 Literal split
return "null";
case "BooleanLiteral": // Babel 6 Literal split
Expand All @@ -40,7 +40,7 @@ function printLiteral(path, options /*, print*/) {
}

if (typeof value === "string") {
return replaceTextEndOfLine(printString(node.raw, options));
return replaceEndOfLine(printString(node.raw, options));
}

return String(value);
Expand Down
4 changes: 2 additions & 2 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
group,
indent,
} from "../document/builders.js";
import { replaceTextEndOfLine } from "../document/utils.js";
import { replaceEndOfLine } from "../document/utils.js";
import embed from "./embed.js";
import clean from "./clean.js";
import { insertPragma } from "./pragma.js";
Expand Down Expand Up @@ -761,7 +761,7 @@ function printPathNoParens(path, options, print, args) {
case "ClassAccessorProperty":
return printClassProperty(path, options, print);
case "TemplateElement":
return replaceTextEndOfLine(node.value.raw);
return replaceEndOfLine(node.value.raw);
case "TemplateLiteral":
return printTemplateLiteral(path, print, options);
case "TaggedTemplateExpression":
Expand Down
15 changes: 6 additions & 9 deletions src/language-markdown/printer-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
group,
hardlineWithoutBreakParent,
} from "../document/builders.js";
import { normalizeDoc, replaceTextEndOfLine } from "../document/utils.js";
import { normalizeDoc, replaceEndOfLine } from "../document/utils.js";
import { printDocToString } from "../document/printer.js";
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
import embed from "./embed.js";
Expand Down Expand Up @@ -243,7 +243,7 @@ function genericPrint(path, options, print) {
const alignment = " ".repeat(4);
return align(alignment, [
alignment,
...replaceTextEndOfLine(node.value, hardline),
replaceEndOfLine(node.value, hardline),
]);
}

Expand All @@ -257,8 +257,7 @@ function genericPrint(path, options, print) {
node.lang || "",
node.meta ? " " + node.meta : "",
hardline,

...replaceTextEndOfLine(
replaceEndOfLine(
getFencedCodeBlockValue(node, options.originalText),
hardline
),
Expand All @@ -274,7 +273,7 @@ function genericPrint(path, options, print) {
: node.value;
const isHtmlComment = /^<!--.*-->$/s.test(value);

return replaceTextEndOfLine(
return replaceEndOfLine(
value,
// @ts-expect-error
isHtmlComment ? hardline : markAsRoot(literalline)
Expand Down Expand Up @@ -427,7 +426,7 @@ function genericPrint(path, options, print) {
? [" ", markAsRoot(literalline)]
: ["\\", hardline];
case "liquidNode":
return replaceTextEndOfLine(node.value, hardline);
return replaceEndOfLine(node.value, hardline);
// MDX
// fallback to the original text if multiparser failed
// or `embeddedLanguageFormatting: "off"`
Expand All @@ -441,9 +440,7 @@ function genericPrint(path, options, print) {
return [
"$$",
hardline,
node.value
? [...replaceTextEndOfLine(node.value, hardline), hardline]
: "",
node.value ? [replaceEndOfLine(node.value, hardline), hardline] : "",
"$$",
];
case "inlineMath": {
Expand Down
4 changes: 2 additions & 2 deletions src/language-yaml/printer-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
line,
lineSuffix,
} from "../document/builders.js";
import { replaceTextEndOfLine } from "../document/utils.js";
import { replaceEndOfLine } from "../document/utils.js";
import { isPreviousLineEmpty } from "../common/util.js";
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
import { insertPragma, isPragma } from "./pragma.js";
Expand Down Expand Up @@ -97,7 +97,7 @@ function genericPrint(path, options, print) {
const parentNode = path.getParentNode();
if (hasPrettierIgnore(path)) {
parts.push(
replaceTextEndOfLine(
replaceEndOfLine(
options.originalText
.slice(node.position.start.offset, node.position.end.offset)
.trimEnd()
Expand Down

0 comments on commit d0f0b09

Please sign in to comment.