Rewrite nested ternaries (#18508)

This commit is contained in:
fisker Cheung authored and GitHub committed 2025-12-21 18:07:47 +08:00
1 parent 9bf7f1b971
commit 97f2e81d06
16 files changed
+187 -127

No files matched your search

+1 -1
View File
@@ -10,7 +10,7 @@ import {
replaceEndOfLine,
softline,
} from "../document/index.js";
import getPreferredQuote from "../utilities/get-preferred-quote.js";
import { getPreferredQuote } from "../utilities/get-preferred-quote.js";
import htmlWhitespace from "../utilities/html-whitespace.js";
import isNonEmptyArray from "../utilities/is-non-empty-array.js";
import UnexpectedNodeError from "../utilities/unexpected-node-error.js";
+75 -57
View File
@@ -72,65 +72,83 @@ function printChild(path, options, print) {
}
function printBetweenLine(prevNode, nextNode) {
return isTextLikeNode(prevNode) && isTextLikeNode(nextNode)
? prevNode.isTrailingSpaceSensitive
? prevNode.hasTrailingSpaces
? preferHardlineAsLeadingSpaces(nextNode)
? hardline
: line
: ""
: preferHardlineAsLeadingSpaces(nextNode)
? hardline
: softline
: (needsToBorrowNextOpeningTagStartMarker(prevNode) &&
(hasPrettierIgnore(nextNode) ||
/**
* 123<a
* ~
* ><b>
*/
nextNode.firstChild ||
/**
* 123<!--
* ~
* -->
*/
nextNode.isSelfClosing ||
/**
* 123<span
* ~
* attr
*/
(nextNode.kind === "element" && nextNode.attrs.length > 0))) ||
if (isTextLikeNode(prevNode) && isTextLikeNode(nextNode)) {
if (prevNode.isTrailingSpaceSensitive) {
if (prevNode.hasTrailingSpaces) {
if (preferHardlineAsLeadingSpaces(nextNode)) {
return hardline;
}
return line;
}
return "";
}
if (preferHardlineAsLeadingSpaces(nextNode)) {
return hardline;
}
return softline;
}
if (
(needsToBorrowNextOpeningTagStartMarker(prevNode) &&
(hasPrettierIgnore(nextNode) ||
/**
* <img
* src="long"
* ~
* />123
* 123<a
* ~
* ><b>
*/
(prevNode.kind === "element" &&
prevNode.isSelfClosing &&
needsToBorrowPrevClosingTagEndMarker(nextNode))
? ""
: !nextNode.isLeadingSpaceSensitive ||
preferHardlineAsLeadingSpaces(nextNode) ||
/**
* Want to write us a letter? Use our<a
* ><b><a>mailing address</a></b></a
* ~
* >.
*/
(needsToBorrowPrevClosingTagEndMarker(nextNode) &&
prevNode.lastChild &&
needsToBorrowParentClosingTagStartMarker(prevNode.lastChild) &&
prevNode.lastChild.lastChild &&
needsToBorrowParentClosingTagStartMarker(
prevNode.lastChild.lastChild,
))
? hardline
: nextNode.hasLeadingSpaces
? line
: softline;
nextNode.firstChild ||
/**
* 123<!--
* ~
* -->
*/
nextNode.isSelfClosing ||
/**
* 123<span
* ~
* attr
*/
(nextNode.kind === "element" && nextNode.attrs.length > 0))) ||
/**
* <img
* src="long"
* ~
* />123
*/
(prevNode.kind === "element" &&
prevNode.isSelfClosing &&
needsToBorrowPrevClosingTagEndMarker(nextNode))
) {
return "";
}
if (
!nextNode.isLeadingSpaceSensitive ||
preferHardlineAsLeadingSpaces(nextNode) ||
/**
* Want to write us a letter? Use our<a
* ><b><a>mailing address</a></b></a
* ~
* >.
*/
(needsToBorrowPrevClosingTagEndMarker(nextNode) &&
prevNode.lastChild &&
needsToBorrowParentClosingTagStartMarker(prevNode.lastChild) &&
prevNode.lastChild.lastChild &&
needsToBorrowParentClosingTagStartMarker(prevNode.lastChild.lastChild))
) {
return hardline;
}
if (nextNode.hasLeadingSpaces) {
return line;
}
return softline;
}
function printChildren(path, options, print) {
+1 -1
View File
@@ -10,7 +10,7 @@ import {
line,
replaceEndOfLine,
} from "../document/index.js";
import getPreferredQuote from "../utilities/get-preferred-quote.js";
import { getPreferredQuote } from "../utilities/get-preferred-quote.js";
import htmlWhitespace from "../utilities/html-whitespace.js";
import UnexpectedNodeError from "../utilities/unexpected-node-error.js";
import clean from "./clean.js";
+1 -1
View File
@@ -18,7 +18,7 @@ import {
printComments,
printDanglingComments,
} from "../../main/comments/print.js";
import getPreferredQuote from "../../utilities/get-preferred-quote.js";
import { getPreferredQuote } from "../../utilities/get-preferred-quote.js";
import UnexpectedNodeError from "../../utilities/unexpected-node-error.js";
import WhitespaceUtilities from "../../utilities/whitespace-utilities.js";
import needsParentheses from "../parentheses/needs-parentheses.js";
+1 -1
View File
@@ -16,7 +16,7 @@ import {
} from "../../document/index.js";
import getMaxContinuousCount from "../../utilities/get-max-continuous-count.js";
import getMinNotPresentContinuousCount from "../../utilities/get-min-not-present-continuous-count.js";
import getPreferredQuote from "../../utilities/get-preferred-quote.js";
import { getPreferredQuote } from "../../utilities/get-preferred-quote.js";
import UnexpectedNodeError from "../../utilities/unexpected-node-error.js";
import { locEnd, locStart } from "../loc.js";
import {
+10 -8
View File
@@ -15,7 +15,7 @@ import skipNewline from "../../utilities/skip-newline.js";
/**
* @import AstPath from "../../common/ast-path.js"
* @import {Doc} from "../../document/index.js"
* @import {Doc, HardLine, Line} from "../../document/index.js"
*/
const returnTrue = () => true;
@@ -36,13 +36,15 @@ function printLeadingComment(path, options) {
// Leading block comments should see if they need to stay on the
// same line or not.
if (isBlock) {
const lineBreak = hasNewline(originalText, locEnd(comment))
? hasNewline(originalText, locStart(comment), {
backwards: true,
})
? hardline
: line
: " ";
/** @type {HardLine | Line | " "} */
let lineBreak = " ";
if (hasNewline(originalText, locEnd(comment))) {
if (hasNewline(originalText, locStart(comment), { backwards: true })) {
lineBreak = hardline;
} else {
lineBreak = line;
}
}
parts.push(lineBreak);
} else {
+1 -1
View File
@@ -52,4 +52,4 @@ function getPreferredQuote(text, preferredQuoteOrPreferSingleQuote) {
.character;
}
export default getPreferredQuote;
export { DOUBLE_QUOTE, getPreferredQuote, SINGLE_QUOTE };
+4 -1
View File
@@ -1,3 +1,5 @@
import { DOUBLE_QUOTE, SINGLE_QUOTE } from "./get-preferred-quote.js";
/** @import {Quote} from "./get-preferred-quote.js" */
// Matches _any_ escape and unescaped quotes (both single and double).
@@ -10,7 +12,8 @@ const REGEX = /\\(["'\\])|(["'])/gu;
* @returns {string}
*/
function makeString(rawText, enclosingQuote) {
const otherQuote = enclosingQuote === '"' ? "'" : '"';
const otherQuote =
enclosingQuote === DOUBLE_QUOTE ? SINGLE_QUOTE : DOUBLE_QUOTE;
// Escape and unescape single and double quotes as needed to be able to
// enclose `rawText` with `enclosingQuote`.
+15 -6
View File
@@ -1,5 +1,9 @@
import * as assert from "#universal/assert";
import getPreferredQuote from "./get-preferred-quote.js";
import {
DOUBLE_QUOTE,
getPreferredQuote,
SINGLE_QUOTE,
} from "./get-preferred-quote.js";
import makeString from "./make-string.js";
/** @import {Quote} from "./get-preferred-quote.js" */
@@ -12,7 +16,9 @@ function printString(raw, options) {
const rawContent = raw.slice(1, -1);
/** @type {Quote} */
const enclosingQuote =
let enclosingQuote;
if (
options.parser === "json" ||
options.parser === "jsonc" ||
options.parser === "json-stringify" ||
@@ -25,10 +31,13 @@ function printString(raw, options) {
(options.parser === "json5" &&
options.quoteProps === "preserve" &&
!options.singleQuote)
? '"'
: options.__isInHtmlAttribute
? "'"
: getPreferredQuote(rawContent, options.singleQuote);
) {
enclosingQuote = DOUBLE_QUOTE;
} else if (options.__isInHtmlAttribute) {
enclosingQuote = SINGLE_QUOTE;
} else {
enclosingQuote = getPreferredQuote(rawContent, options.singleQuote);
}
const originalQuote = raw.charAt(0);
+1 -1
View File
@@ -142,7 +142,7 @@ export { default as getAlignmentSize } from "./get-alignment-size.js";
export { default as getIndentSize } from "./get-indent-size.js";
export { default as getMaxContinuousCount } from "./get-max-continuous-count.js";
export { default as getNextNonSpaceNonCommentCharacter } from "./get-next-non-space-non-comment-character.js";
export { default as getPreferredQuote } from "./get-preferred-quote.js";
export { getPreferredQuote } from "./get-preferred-quote.js";
export { default as getStringWidth } from "./get-string-width.js";
export { default as hasNewline } from "./has-newline.js";
export { default as hasNewlineInRange } from "./has-newline-in-range.js";