diff --git a/src/language-handlebars/printer-glimmer.js b/src/language-handlebars/printer-glimmer.js index 5c2767aa6..724771bd1 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -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"; diff --git a/src/language-html/print/children.js b/src/language-html/print/children.js index 2a98c5521..9658f2303 100644 --- a/src/language-html/print/children.js +++ b/src/language-html/print/children.js @@ -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 - */ - nextNode.firstChild || - /** - * 123 - */ - nextNode.isSelfClosing || - /** - * 123 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) || /** - * 123 + * 123 */ - (prevNode.kind === "element" && - prevNode.isSelfClosing && - needsToBorrowPrevClosingTagEndMarker(nextNode)) - ? "" - : !nextNode.isLeadingSpaceSensitive || - preferHardlineAsLeadingSpaces(nextNode) || - /** - * Want to write us a letter? Use ourmailing address. - */ - (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 0))) || + /** + * 123 + */ + (prevNode.kind === "element" && + prevNode.isSelfClosing && + needsToBorrowPrevClosingTagEndMarker(nextNode)) + ) { + return ""; + } + + if ( + !nextNode.isLeadingSpaceSensitive || + preferHardlineAsLeadingSpaces(nextNode) || + /** + * Want to write us a letter? Use ourmailing address. + */ + (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) { diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index c60e1fc13..ffd261275 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -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"; diff --git a/src/language-js/print/jsx.js b/src/language-js/print/jsx.js index e265ff172..6426cb746 100644 --- a/src/language-js/print/jsx.js +++ b/src/language-js/print/jsx.js @@ -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"; diff --git a/src/language-markdown/print/mdast.js b/src/language-markdown/print/mdast.js index 33a1a33e5..32c74ede1 100644 --- a/src/language-markdown/print/mdast.js +++ b/src/language-markdown/print/mdast.js @@ -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 { diff --git a/src/main/comments/print.js b/src/main/comments/print.js index 517bc9b11..4db1d31ca 100644 --- a/src/main/comments/print.js +++ b/src/main/comments/print.js @@ -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 { diff --git a/src/utilities/get-preferred-quote.js b/src/utilities/get-preferred-quote.js index dbf2ebac4..7b2df5234 100644 --- a/src/utilities/get-preferred-quote.js +++ b/src/utilities/get-preferred-quote.js @@ -52,4 +52,4 @@ function getPreferredQuote(text, preferredQuoteOrPreferSingleQuote) { .character; } -export default getPreferredQuote; +export { DOUBLE_QUOTE, getPreferredQuote, SINGLE_QUOTE }; diff --git a/src/utilities/make-string.js b/src/utilities/make-string.js index 77dc7b5b8..44178d74a 100644 --- a/src/utilities/make-string.js +++ b/src/utilities/make-string.js @@ -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`. diff --git a/src/utilities/print-string.js b/src/utilities/print-string.js index f8a565a35..e6ac91374 100644 --- a/src/utilities/print-string.js +++ b/src/utilities/print-string.js @@ -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); diff --git a/src/utilities/public.js b/src/utilities/public.js index 444db8690..64b788afd 100644 --- a/src/utilities/public.js +++ b/src/utilities/public.js @@ -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"; diff --git a/tests/config/utilities/create-snapshot.js b/tests/config/utilities/create-snapshot.js index 7b0a79ef0..34ca8f54e 100644 --- a/tests/config/utilities/create-snapshot.js +++ b/tests/config/utilities/create-snapshot.js @@ -10,11 +10,15 @@ function printSeparator(description = "") { } function stringify(value) { - return value === Number.POSITIVE_INFINITY - ? "Infinity" - : Array.isArray(value) - ? `[${value.map((v) => JSON.stringify(v)).join(", ")}]` - : JSON.stringify(value); + if (value === Number.POSITIVE_INFINITY) { + return "Infinity"; + } + + if (Array.isArray(value)) { + return `[${value.map((v) => JSON.stringify(v)).join(", ")}]`; + } + + return JSON.stringify(value); } function printOptions(options) { diff --git a/tests/config/utilities/stringify-options-for-title.js b/tests/config/utilities/stringify-options-for-title.js index d25c671d5..531fc0650 100644 --- a/tests/config/utilities/stringify-options-for-title.js +++ b/tests/config/utilities/stringify-options-for-title.js @@ -1,11 +1,15 @@ function stringifyOptions(options) { - const string = JSON.stringify(options || {}, (key, value) => - key === "plugins" || key === "errors" - ? undefined - : value === Number.POSITIVE_INFINITY - ? "Infinity" - : value, - ); + const string = JSON.stringify(options || {}, (key, value) => { + if (key === "plugins" || key === "errors") { + return; + } + + if (value === Number.POSITIVE_INFINITY) { + return "Infinity"; + } + + return value; + }); return string === "{}" ? "" : string; } diff --git a/tests/integration/__tests__/support-info.js b/tests/integration/__tests__/support-info.js index 3a354aa32..58822b0d7 100644 --- a/tests/integration/__tests__/support-info.js +++ b/tests/integration/__tests__/support-info.js @@ -15,18 +15,20 @@ async function getCoreInfo() { ); const options = Object.fromEntries( - supportInfo.options.map((option) => [ - option.name, - { - type: option.type, - default: option.default, - ...(option.type === "int" - ? { range: option.range } - : option.type === "choice" - ? { choices: option.choices.map((choice) => choice.value) } - : null), - }, - ]), + supportInfo.options.map((rawOption) => { + const option = { + type: rawOption.type, + default: rawOption.default, + }; + + if (rawOption.type === "int") { + option.range = rawOption.range; + } else if (rawOption.type === "choice") { + option.choices = rawOption.choices.map((choice) => choice.value); + } + + return [rawOption.name, option]; + }), ); return { languages, options }; diff --git a/tests/integration/cli-worker.js b/tests/integration/cli-worker.js index a70c802bb..b435e929a 100644 --- a/tests/integration/cli-worker.js +++ b/tests/integration/cli-worker.js @@ -38,23 +38,30 @@ async function mockImplementations(options) { cliMockable.mockImplementations({ clearStreamText(stream, text) { - const streamName = - stream === process.stdout - ? "process.stdout" - : stream === process.stderr - ? "process.stderr" - : "unknown stream"; + let streamName; + if (stream === process.stdout) { + streamName = "process.stdout"; + } else if (stream === process.stderr) { + streamName = "process.stderr"; + } else { + streamName = "unknown stream"; + } stream.write(`\n[[Clear text(${streamName}): ${text}]]\n`); }, // Time measure in format test getTimestamp: () => 0, isCI: () => Boolean(options.ci), - isStreamTTY: (stream) => - stream === process.stdin - ? Boolean(options.isTTY) - : stream === process.stdout - ? Boolean(options.stdoutIsTTY) - : stream.isTTY, + isStreamTTY(stream) { + if (stream === process.stdin) { + return Boolean(options.isTTY); + } + + if (stream === process.stdout) { + return Boolean(options.stdoutIsTTY); + } + + return stream.isTTY; + }, // eslint-disable-next-line require-await async writeFormattedFile(filename, content) { filename = normalizeToPosix(path.relative(process.cwd(), filename)); diff --git a/website/playground/Playground.jsx b/website/playground/Playground.jsx index 91ad47206..a62335c06 100644 --- a/website/playground/Playground.jsx +++ b/website/playground/Playground.jsx @@ -565,11 +565,15 @@ function getReportLink(reportBody) { } function getSecondFormat(formatted, reformatted) { - return formatted === "" - ? "" - : formatted === reformatted - ? "✓ Second format is unchanged." - : (reformatted ?? ""); + if (formatted === "") { + return ""; + } + + if (formatted === reformatted) { + return "✓ Second format is unchanged."; + } + + return reformatted ?? ""; } export default Playground; diff --git a/website/static/worker.mjs b/website/static/worker.mjs index c5c92cf63..52b83cf95 100644 --- a/website/static/worker.mjs +++ b/website/static/worker.mjs @@ -70,14 +70,21 @@ function serializeError(error) { function serializeAst(ast) { return JSON.stringify( ast, - (_, value) => - value instanceof Error - ? serializeError(value) - : typeof value === "bigint" - ? `BigInt('${String(value)}')` - : typeof value === "symbol" - ? String(value) - : value, + (_, value) => { + if (value instanceof Error) { + return serializeError(value); + } + + if (typeof value === "bigint") { + return `BigInt('${String(value)}')`; + } + + if (typeof value === "symbol") { + return String(value); + } + + return value; + }, 2, ); }