} */
+ const cache = new Map();
+ /** @type {VisitorKeys} */
+ const result = {};
+
+ for (const [type, keys] of Object.entries(visitorKeys)) {
+ const cacheKey = keys.toSorted().join("\0");
+ if (!cache.has(cacheKey)) {
+ cache.set(cacheKey, keys);
+ }
+ result[type] = cache.get(cacheKey);
+ }
+
+ return result;
+}
+
+export {
+ generateReferenceSharedVisitorKeys,
+ removeNodeTypes,
+ removeVisitorKeys,
+ unionVisitorKeys,
+};
diff --git a/src/utils/whitespace-utils.js b/src/utilities/whitespace-utilities.js
similarity index 98%
rename from src/utils/whitespace-utils.js
rename to src/utilities/whitespace-utilities.js
index 9e9ee9c9be79..7d451467a7ac 100644
--- a/src/utils/whitespace-utils.js
+++ b/src/utilities/whitespace-utilities.js
@@ -1,6 +1,6 @@
import escapeStringRegexp from "escape-string-regexp";
-class WhitespaceUtils {
+class WhitespaceUtilities {
#whitespaceCharacters;
constructor(whitespaceCharacters) {
@@ -151,4 +151,4 @@ class WhitespaceUtils {
}
}
-export default WhitespaceUtils;
+export default WhitespaceUtilities;
diff --git a/src/utils/get-preferred-quote.js b/src/utils/get-preferred-quote.js
deleted file mode 100644
index ec1a85ff7c34..000000000000
--- a/src/utils/get-preferred-quote.js
+++ /dev/null
@@ -1,35 +0,0 @@
-/**
- * @typedef {SINGLE_QUOTE | DOUBLE_QUOTE} Quote
- */
-
-const SINGLE_QUOTE = "'";
-const DOUBLE_QUOTE = '"';
-
-/**
- *
- * @param {string} text
- * @param {Quote | boolean} preferredQuoteOrPreferSingleQuote
- * @returns {Quote}
- */
-function getPreferredQuote(text, preferredQuoteOrPreferSingleQuote) {
- const preferred =
- preferredQuoteOrPreferSingleQuote === true ||
- preferredQuoteOrPreferSingleQuote === SINGLE_QUOTE
- ? SINGLE_QUOTE
- : DOUBLE_QUOTE;
- const alternate = preferred === SINGLE_QUOTE ? DOUBLE_QUOTE : SINGLE_QUOTE;
-
- let preferredQuoteCount = 0;
- let alternateQuoteCount = 0;
- for (const character of text) {
- if (character === preferred) {
- preferredQuoteCount++;
- } else if (character === alternate) {
- alternateQuoteCount++;
- }
- }
-
- return preferredQuoteCount > alternateQuoteCount ? alternate : preferred;
-}
-
-export default getPreferredQuote;
diff --git a/src/utils/html-whitespace-utils.js b/src/utils/html-whitespace-utils.js
deleted file mode 100644
index d87e832fb36f..000000000000
--- a/src/utils/html-whitespace-utils.js
+++ /dev/null
@@ -1,7 +0,0 @@
-import WhitespaceUtils from "./whitespace-utils.js";
-
-// https://infra.spec.whatwg.org/#ascii-whitespace
-const HTML_WHITESPACE_CHARACTERS = ["\t", "\n", "\f", "\r", " "];
-const htmlWhitespaceUtils = new WhitespaceUtils(HTML_WHITESPACE_CHARACTERS);
-
-export default htmlWhitespaceUtils;
diff --git a/tests/config/prettier-plugins/prettier-plugin-dummy-toml/index.js b/tests/config/prettier-plugins/prettier-plugin-dummy-toml/index.js
index 8523216a4d92..217d66ec154e 100644
--- a/tests/config/prettier-plugins/prettier-plugin-dummy-toml/index.js
+++ b/tests/config/prettier-plugins/prettier-plugin-dummy-toml/index.js
@@ -1,5 +1,5 @@
import { outdent } from "outdent";
-import createPlugin from "../../utils/create-plugin.cjs";
+import createPlugin from "../../utilities/create-plugin.cjs";
const plugin = createPlugin({
name: "toml",
diff --git a/tests/config/prettier-plugins/prettier-plugin-uppercase-rocks/index.js b/tests/config/prettier-plugins/prettier-plugin-uppercase-rocks/index.js
index e3d8ea8a3d11..c2fca396b7dc 100644
--- a/tests/config/prettier-plugins/prettier-plugin-uppercase-rocks/index.js
+++ b/tests/config/prettier-plugins/prettier-plugin-uppercase-rocks/index.js
@@ -1,4 +1,4 @@
-import createPlugin from "../../utils/create-plugin.cjs";
+import createPlugin from "../../utilities/create-plugin.cjs";
const plugin = createPlugin({
name: "uppercase-rocks",
diff --git a/tests/config/require-standalone.cjs b/tests/config/require-standalone.cjs
index c02c30979df2..5a0ac7777115 100644
--- a/tests/config/require-standalone.cjs
+++ b/tests/config/require-standalone.cjs
@@ -1,7 +1,7 @@
"use strict";
const vm = require("vm");
const fastGlob = require("fast-glob");
-const createSandBox = require("./utils/create-sandbox.cjs");
+const createSandBox = require("./utilities/create-sandbox.cjs");
const sandbox = createSandBox({
files: fastGlob.sync(["standalone.js", "plugins/*.js"], {
diff --git a/tests/config/run-format-test.js b/tests/config/run-format-test.js
index 64bf003bfe5c..c7fab1beb5e3 100644
--- a/tests/config/run-format-test.js
+++ b/tests/config/run-format-test.js
@@ -3,11 +3,11 @@ import path from "node:path";
import url from "node:url";
import createEsmUtils from "esm-utils";
import getPrettier from "./get-prettier.js";
-import checkParsers from "./utils/check-parsers.js";
-import consistentEndOfLine from "./utils/consistent-end-of-line.js";
-import createSnapshot from "./utils/create-snapshot.js";
-import stringifyOptionsForTitle from "./utils/stringify-options-for-title.js";
-import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
+import checkParsers from "./utilities/check-parsers.js";
+import consistentEndOfLine from "./utilities/consistent-end-of-line.js";
+import createSnapshot from "./utilities/create-snapshot.js";
+import stringifyOptionsForTitle from "./utilities/stringify-options-for-title.js";
+import visualizeEndOfLine from "./utilities/visualize-end-of-line.js";
const { __dirname } = createEsmUtils(import.meta);
diff --git a/tests/config/utils/check-parsers.js b/tests/config/utilities/check-parsers.js
similarity index 100%
rename from tests/config/utils/check-parsers.js
rename to tests/config/utilities/check-parsers.js
diff --git a/tests/config/utils/consistent-end-of-line.js b/tests/config/utilities/consistent-end-of-line.js
similarity index 100%
rename from tests/config/utils/consistent-end-of-line.js
rename to tests/config/utilities/consistent-end-of-line.js
diff --git a/tests/config/utils/create-plugin.cjs b/tests/config/utilities/create-plugin.cjs
similarity index 100%
rename from tests/config/utils/create-plugin.cjs
rename to tests/config/utilities/create-plugin.cjs
diff --git a/tests/config/utils/create-sandbox.cjs b/tests/config/utilities/create-sandbox.cjs
similarity index 100%
rename from tests/config/utils/create-sandbox.cjs
rename to tests/config/utilities/create-sandbox.cjs
diff --git a/tests/config/utils/create-snapshot.js b/tests/config/utilities/create-snapshot.js
similarity index 100%
rename from tests/config/utils/create-snapshot.js
rename to tests/config/utilities/create-snapshot.js
diff --git a/tests/config/utils/stringify-options-for-title.js b/tests/config/utilities/stringify-options-for-title.js
similarity index 100%
rename from tests/config/utils/stringify-options-for-title.js
rename to tests/config/utilities/stringify-options-for-title.js
diff --git a/tests/config/utils/visualize-end-of-line.js b/tests/config/utilities/visualize-end-of-line.js
similarity index 100%
rename from tests/config/utils/visualize-end-of-line.js
rename to tests/config/utilities/visualize-end-of-line.js
diff --git a/tests/config/utils/visualize-range.js b/tests/config/utilities/visualize-range.js
similarity index 100%
rename from tests/config/utils/visualize-range.js
rename to tests/config/utilities/visualize-range.js
diff --git a/tests/format/lwc/attribute/quotes/__snapshots__/format.test.js.snap b/tests/format/lwc/attribute/quotes/__snapshots__/format.test.js.snap
index 19cf51248bab..e1f8a47f2552 100644
--- a/tests/format/lwc/attribute/quotes/__snapshots__/format.test.js.snap
+++ b/tests/format/lwc/attribute/quotes/__snapshots__/format.test.js.snap
@@ -13,10 +13,28 @@ unquoted={data}>
+
+
=====================================output=====================================
-
+
+
+
-
+
================================================================================
`;
@@ -33,10 +51,28 @@ unquoted={data}>
+
+
=====================================output=====================================
+
+
================================================================================
`;
diff --git a/tests/format/lwc/attribute/quotes/quotes.html b/tests/format/lwc/attribute/quotes/quotes.html
index ad41f3f05a27..a254cabdb3e4 100644
--- a/tests/format/lwc/attribute/quotes/quotes.html
+++ b/tests/format/lwc/attribute/quotes/quotes.html
@@ -3,3 +3,12 @@
+
+
diff --git a/tests/format/typescript/union/comments/18379.ts b/tests/format/typescript/union/comments/18379.ts
new file mode 100644
index 000000000000..59fa250e11ed
--- /dev/null
+++ b/tests/format/typescript/union/comments/18379.ts
@@ -0,0 +1,31 @@
+type A1 =
+ (
+ A | B // comment 1
+ ) & (
+ // comment2
+ A | B
+ )
+
+type A2 =
+ (
+ A | B // prettier-ignore
+ ) & (
+ // prettier-ignore
+ A | B
+ )
+
+type A1 =
+ // comment 1
+ (A | B)
+ & (
+ // comment2
+ A | B
+ )
+
+type A1 =
+ // prettier-ignore
+ (A | B)
+ & (
+ // prettier-ignore
+ A | B
+ )
diff --git a/tests/format/typescript/union/comments/18389.ts b/tests/format/typescript/union/comments/18389.ts
new file mode 100644
index 000000000000..b3611ca97a46
--- /dev/null
+++ b/tests/format/typescript/union/comments/18389.ts
@@ -0,0 +1,19 @@
+type A1 = | /**
+ * octahedralRhinocerosTransformer
+ */
+ a
+ | (b | c);
+
+type A2 = | /**
+ * hippopotamicKangarooMutator
+ */
+ (a | b)
+ | c;
+
+type A4 =
+ (
+ A | B // comment 1
+ ) | (
+ // comment2
+ A | B
+ )
diff --git a/tests/format/typescript/union/comments/__snapshots__/format.test.js.snap b/tests/format/typescript/union/comments/__snapshots__/format.test.js.snap
index 093f8c013965..83dc8a974ad8 100644
--- a/tests/format/typescript/union/comments/__snapshots__/format.test.js.snap
+++ b/tests/format/typescript/union/comments/__snapshots__/format.test.js.snap
@@ -95,3 +95,122 @@ type A3 =
================================================================================
`;
+
+exports[`18379.ts format 1`] = `
+====================================options=====================================
+parsers: ["typescript", "flow"]
+printWidth: 80
+ | printWidth
+=====================================input======================================
+type A1 =
+ (
+ A | B // comment 1
+ ) & (
+ // comment2
+ A | B
+ )
+
+type A2 =
+ (
+ A | B // prettier-ignore
+ ) & (
+ // prettier-ignore
+ A | B
+ )
+
+type A1 =
+ // comment 1
+ (A | B)
+ & (
+ // comment2
+ A | B
+ )
+
+type A1 =
+ // prettier-ignore
+ (A | B)
+ & (
+ // prettier-ignore
+ A | B
+ )
+
+=====================================output=====================================
+type A1 = (
+ | A
+ | B // comment 1
+) &
+ // comment2
+ (A | B);
+
+type A2 = (
+ | A
+ | B // prettier-ignore
+) &
+ // prettier-ignore
+ (A | B);
+
+type A1 =
+ // comment 1
+ (A | B) &
+ // comment2
+ (A | B);
+
+type A1 =
+ // prettier-ignore
+ (A | B)
+ & (
+ // prettier-ignore
+ A | B
+ );
+
+================================================================================
+`;
+
+exports[`18389.ts format 1`] = `
+====================================options=====================================
+parsers: ["typescript", "flow"]
+printWidth: 80
+ | printWidth
+=====================================input======================================
+type A1 = | /**
+ * octahedralRhinocerosTransformer
+ */
+ a
+ | (b | c);
+
+type A2 = | /**
+ * hippopotamicKangarooMutator
+ */
+ (a | b)
+ | c;
+
+type A4 =
+ (
+ A | B // comment 1
+ ) | (
+ // comment2
+ A | B
+ )
+
+=====================================output=====================================
+type A1 =
+ | /**
+ * octahedralRhinocerosTransformer
+ */
+ a
+ | (b | c);
+
+type A2 =
+ | /**
+ * hippopotamicKangarooMutator
+ */
+ (a | b)
+ | c;
+
+type A4 =
+ | (A | B) // comment 1
+ // comment2
+ | (A | B);
+
+================================================================================
+`;
diff --git a/tests/integration/__tests__/bundle.js b/tests/integration/__tests__/bundle.js
index 9e60e8708b52..cbba5d3fa3bf 100644
--- a/tests/integration/__tests__/bundle.js
+++ b/tests/integration/__tests__/bundle.js
@@ -1,11 +1,12 @@
import fs from "node:fs/promises";
import path from "node:path";
+import { inspect } from "node:util";
import createEsmUtils from "esm-utils";
import fastGlob from "fast-glob";
import coreOptions from "../../../src/main/core-options.evaluate.js";
import codeSamples from "../../../website/playground/codeSamples.mjs";
import prettier from "../../config/prettier-entry.js";
-import createSandBox from "../../config/utils/create-sandbox.cjs";
+import createSandBox from "../../config/utilities/create-sandbox.cjs";
import { projectRoot } from "../env.js";
const { require, importModule } = createEsmUtils(import.meta);
@@ -70,6 +71,111 @@ describe("standalone", () => {
expect(esmOutput).toBe(umdOutput);
});
}
+
+ const printerVisitorKeysSettings = new Map([
+ [
+ "estree",
+ {
+ sharedVisitorKeys: true,
+ nodes: [
+ { type: "FunctionDeclaration" },
+ { type: "FunctionExpression" },
+ ],
+ },
+ ],
+ [
+ "estree-json",
+ {
+ sharedVisitorKeys: true,
+ nodes: [{ type: "StringLiteral" }, { type: "Identifier" }],
+ },
+ ],
+ [
+ "glimmer",
+ {
+ sharedVisitorKeys: false,
+ nodes: [{ type: "Template" }, { type: "Block" }],
+ },
+ ],
+ [
+ "graphql",
+ {
+ sharedVisitorKeys: false,
+ nodes: [{ kind: "StringValue" }, { kind: "BooleanValue" }],
+ },
+ ],
+ [
+ "html",
+ {
+ sharedVisitorKeys: true,
+ nodes: [{ kind: "comment" }, { kind: "cdata" }],
+ },
+ ],
+ [
+ "mdast",
+ {
+ sharedVisitorKeys: true,
+ nodes: [{ type: "code" }, { type: "image" }],
+ },
+ ],
+ [
+ "postcss",
+ {
+ sharedVisitorKeys: true,
+ nodes: [{ type: "media-query-list" }, { type: "selector-pseudo" }],
+ },
+ ],
+ [
+ "yaml",
+ {
+ sharedVisitorKeys: true,
+ nodes: [{ type: "blockLiteral" }, { type: "quoteSingle" }],
+ },
+ ],
+ ]);
+
+ test("visitor keys with shared reference", () => {
+ for (const [name, printer] of esmPlugins.flatMap((plugin) =>
+ Object.entries(plugin.printers ?? {}),
+ )) {
+ try {
+ expect(printerVisitorKeysSettings.has(name)).toBe(true);
+ } catch {
+ throw new Error(`Missing settings for printer '${name}'.`);
+ }
+ const { getVisitorKeys } = printer;
+ expect(typeof getVisitorKeys).toBe("function");
+ const { sharedVisitorKeys, nodes } = printerVisitorKeysSettings.get(name);
+ expect(
+ typeof sharedVisitorKeys === "boolean" &&
+ Array.isArray(nodes) &&
+ nodes.length > 1,
+ ).toBe(true);
+ const keys = nodes.map((node) => getVisitorKeys(node));
+
+ try {
+ expect(keys.every((keys) => Array.isArray(keys))).toBe(true);
+ } catch {
+ throw new Error(
+ `Missing visitor keys for '${name}' nodes: ${inspect(nodes)}.`,
+ );
+ }
+ const [firstNodeKeys, ...restNodeKeys] = keys;
+ if (sharedVisitorKeys) {
+ // Should be same reference
+ expect(restNodeKeys.every((keys) => keys === firstNodeKeys)).toBe(true);
+ } else {
+ // Should be same, but not same reference
+ expect(
+ restNodeKeys.every(
+ (keys) =>
+ keys !== firstNodeKeys &&
+ JSON.stringify(keys) === JSON.stringify(firstNodeKeys),
+ ),
+ ).toBe(true);
+ }
+ }
+ });
});
test("global objects", async () => {
diff --git a/tests/integration/__tests__/doc-utils-clean-doc.js b/tests/integration/__tests__/doc-utilities-clean-doc.js
similarity index 100%
rename from tests/integration/__tests__/doc-utils-clean-doc.js
rename to tests/integration/__tests__/doc-utilities-clean-doc.js
diff --git a/tests/integration/__tests__/plugin-override-buitin-plugins.js b/tests/integration/__tests__/plugin-override-buitin-plugins.js
index 9bc08ff03073..50d408fbad09 100644
--- a/tests/integration/__tests__/plugin-override-buitin-plugins.js
+++ b/tests/integration/__tests__/plugin-override-buitin-plugins.js
@@ -1,5 +1,5 @@
import prettier from "../../config/prettier-entry.js";
-import createPlugin from "../../config/utils/create-plugin.cjs";
+import createPlugin from "../../config/utilities/create-plugin.cjs";
test("plugins can override builtin plugins", async () => {
const outputWithoutPlugin = await prettier.format("foo()", {
diff --git a/tests/integration/__tests__/util-shared.js b/tests/integration/__tests__/public-utilities.js
similarity index 85%
rename from tests/integration/__tests__/util-shared.js
rename to tests/integration/__tests__/public-utilities.js
index 3cb6eaf08e69..62095c11d9d1 100644
--- a/tests/integration/__tests__/util-shared.js
+++ b/tests/integration/__tests__/public-utilities.js
@@ -1,36 +1,36 @@
import prettier from "../../config/prettier-entry.js";
-const sharedUtil = prettier.util;
+const utilities = prettier.util;
test("shared util has correct structure", () => {
- expect(typeof sharedUtil.getMaxContinuousCount).toBe("function");
- expect(typeof sharedUtil.getStringWidth).toBe("function");
- expect(typeof sharedUtil.getAlignmentSize).toBe("function");
- expect(typeof sharedUtil.getIndentSize).toBe("function");
- expect(typeof sharedUtil.skip).toBe("function");
- expect(typeof sharedUtil.skipWhitespace).toBe("function");
- expect(typeof sharedUtil.skipSpaces).toBe("function");
- expect(typeof sharedUtil.skipToLineEnd).toBe("function");
- expect(typeof sharedUtil.skipEverythingButNewLine).toBe("function");
- expect(typeof sharedUtil.skipInlineComment).toBe("function");
- expect(typeof sharedUtil.skipTrailingComment).toBe("function");
- expect(typeof sharedUtil.skipNewline).toBe("function");
- expect(typeof sharedUtil.hasNewline).toBe("function");
- expect(typeof sharedUtil.hasNewlineInRange).toBe("function");
- expect(typeof sharedUtil.hasSpaces).toBe("function");
- expect(typeof sharedUtil.isNextLineEmpty).toBe("function");
- expect(typeof sharedUtil.isNextLineEmptyAfterIndex).toBe("function");
- expect(typeof sharedUtil.isPreviousLineEmpty).toBe("function");
- expect(typeof sharedUtil.getNextNonSpaceNonCommentCharacter).toBe("function");
- expect(typeof sharedUtil.getNextNonSpaceNonCommentCharacterIndex).toBe(
+ expect(typeof utilities.getMaxContinuousCount).toBe("function");
+ expect(typeof utilities.getStringWidth).toBe("function");
+ expect(typeof utilities.getAlignmentSize).toBe("function");
+ expect(typeof utilities.getIndentSize).toBe("function");
+ expect(typeof utilities.skip).toBe("function");
+ expect(typeof utilities.skipWhitespace).toBe("function");
+ expect(typeof utilities.skipSpaces).toBe("function");
+ expect(typeof utilities.skipToLineEnd).toBe("function");
+ expect(typeof utilities.skipEverythingButNewLine).toBe("function");
+ expect(typeof utilities.skipInlineComment).toBe("function");
+ expect(typeof utilities.skipTrailingComment).toBe("function");
+ expect(typeof utilities.skipNewline).toBe("function");
+ expect(typeof utilities.hasNewline).toBe("function");
+ expect(typeof utilities.hasNewlineInRange).toBe("function");
+ expect(typeof utilities.hasSpaces).toBe("function");
+ expect(typeof utilities.isNextLineEmpty).toBe("function");
+ expect(typeof utilities.isNextLineEmptyAfterIndex).toBe("function");
+ expect(typeof utilities.isPreviousLineEmpty).toBe("function");
+ expect(typeof utilities.getNextNonSpaceNonCommentCharacter).toBe("function");
+ expect(typeof utilities.getNextNonSpaceNonCommentCharacterIndex).toBe(
"function",
);
- expect(typeof sharedUtil.makeString).toBe("function");
- expect(typeof sharedUtil.getPreferredQuote).toBe("function");
+ expect(typeof utilities.makeString).toBe("function");
+ expect(typeof utilities.getPreferredQuote).toBe("function");
});
test("sharedUtil.getMaxContinuousCount", () => {
- const { getMaxContinuousCount } = sharedUtil;
+ const { getMaxContinuousCount } = utilities;
expect(getMaxContinuousCount("|---|--|-|--|---|", "-")).toBe(3);
expect(getMaxContinuousCount("|...|", ".")).toBe(3);
@@ -50,7 +50,7 @@ test("sharedUtil.getMaxContinuousCount", () => {
});
test("sharedUtil.getStringWidth", () => {
- const { getStringWidth } = sharedUtil;
+ const { getStringWidth } = utilities;
// From https://github.com/sindresorhus/string-width/blob/main/test.js
expect(getStringWidth("abcde")).toBe(5);
@@ -93,7 +93,7 @@ test("sharedUtil.getStringWidth", () => {
});
test("sharedUtil.getAlignmentSize", () => {
- const { getAlignmentSize } = sharedUtil;
+ const { getAlignmentSize } = utilities;
expect(getAlignmentSize(" ")).toBe(3);
expect(getAlignmentSize(" ", /* tabWidth */ 2, /* startIndex */ 2)).toBe(1);
expect(getAlignmentSize("\t\t", /* tabWidth */ 2)).toBe(4);
@@ -104,7 +104,7 @@ test("sharedUtil.getAlignmentSize", () => {
});
test("sharedUtil.getIndentSize", () => {
- const { getIndentSize } = sharedUtil;
+ const { getIndentSize } = utilities;
expect(getIndentSize("\n a")).toBe(3);
expect(getIndentSize("\n a", /* tabWidth */ 2)).toBe(3);
expect(getIndentSize("\n\t\ta", /* tabWidth */ 2)).toBe(4);
@@ -118,7 +118,7 @@ test("sharedUtil.getNextNonSpaceNonCommentCharacter and sharedUtil.getNextNonSpa
const {
getNextNonSpaceNonCommentCharacter,
getNextNonSpaceNonCommentCharacterIndex,
- } = sharedUtil;
+ } = utilities;
const FAKE_NODE = { type: "Identifier", name: "a" };
{
@@ -169,7 +169,7 @@ test("sharedUtil.getNextNonSpaceNonCommentCharacter and sharedUtil.getNextNonSpa
test("sharedUtil.isPreviousLineEmpty, sharedUtil.isNextLineEmpty and sharedUtil.isNextLineEmptyAfterIndex", () => {
const { isPreviousLineEmpty, isNextLineEmpty, isNextLineEmptyAfterIndex } =
- sharedUtil;
+ utilities;
const FAKE_NODE_A = { type: "Identifier", name: "a" };
const FAKE_NODE_B = { type: "Identifier", name: "b" };
@@ -213,7 +213,7 @@ test("sharedUtil.isPreviousLineEmpty, sharedUtil.isNextLineEmpty and sharedUtil.
});
test("sharedUtil.makeString", () => {
- const { makeString } = sharedUtil;
+ const { makeString } = utilities;
const DOUBLE_QUOTE = '"';
const SINGLE_QUOTE = "'";
@@ -268,7 +268,7 @@ test("sharedUtil.makeString", () => {
});
test("sharedUtil.getPreferredQuote", () => {
- const { getPreferredQuote } = sharedUtil;
+ const { getPreferredQuote } = utilities;
const DOUBLE_QUOTE = '"';
const SINGLE_QUOTE = "'";
diff --git a/tests/integration/__tests__/schema.js b/tests/integration/__tests__/schema.js
index 31f8b51dbdec..218b9178f045 100644
--- a/tests/integration/__tests__/schema.js
+++ b/tests/integration/__tests__/schema.js
@@ -1,4 +1,4 @@
-import { generateSchemaData } from "../../../scripts/utils/generate-schema.js";
+import { generateSchemaData } from "../../../scripts/utilities/generate-schema.js";
import prettier from "../../config/prettier-entry.js";
test("schema", async () => {
diff --git a/tests/integration/cli/infer-parser/override-builtin-plugin-languages/dummy-js-plugin.js b/tests/integration/cli/infer-parser/override-builtin-plugin-languages/dummy-js-plugin.js
index ed9c1f03cef4..e73ff6e8dcd0 100644
--- a/tests/integration/cli/infer-parser/override-builtin-plugin-languages/dummy-js-plugin.js
+++ b/tests/integration/cli/infer-parser/override-builtin-plugin-languages/dummy-js-plugin.js
@@ -1,5 +1,5 @@
-import createPlugin from "../../../../config/utils/create-plugin.cjs";
+import createPlugin from "../../../../config/utilities/create-plugin.cjs";
const PARSER_NAME = 'dummy-js-parser'
const PRINT_MARK = `formatted by '${PARSER_NAME}' parser`
diff --git a/tests/integration/plugins/automatic/prettier-plugin-baz.js b/tests/integration/plugins/automatic/prettier-plugin-baz.js
index 61fa15fb7aac..a4dd32b3d176 100644
--- a/tests/integration/plugins/automatic/prettier-plugin-baz.js
+++ b/tests/integration/plugins/automatic/prettier-plugin-baz.js
@@ -1,4 +1,4 @@
-import createPlugin from "../../../config/utils/create-plugin.cjs";
+import createPlugin from "../../../config/utilities/create-plugin.cjs";
const plugin = createPlugin({
name: "baz",
diff --git a/tests/integration/plugins/languages/is-supported.js b/tests/integration/plugins/languages/is-supported.js
index 5db7588f85c2..6bea9766523b 100644
--- a/tests/integration/plugins/languages/is-supported.js
+++ b/tests/integration/plugins/languages/is-supported.js
@@ -1,5 +1,5 @@
import path from "node:path";
-import createPlugin from "../../../config/utils/create-plugin.cjs";
+import createPlugin from "../../../config/utilities/create-plugin.cjs";
const PARSER_NAME = "parser-name-inferred-from-language-is-supported";
const PRINT_MARK = `formatted by '${PARSER_NAME}' parser`;
diff --git a/tests/unit/__snapshots__/visitor-keys.js.snap b/tests/unit/__snapshots__/visitor-keys.js.snap
index 0d816d2eb97f..75c96533b074 100644
--- a/tests/unit/__snapshots__/visitor-keys.js.snap
+++ b/tests/unit/__snapshots__/visitor-keys.js.snap
@@ -1668,12 +1668,10 @@ exports[`visitor keys yaml 1`] = `
"trailingComment",
],
"anchor": [
- "anchor",
"endComments",
"indicatorComment",
"leadingComments",
"middleComments",
- "tag",
"trailingComment",
],
"blockFolded": [
@@ -1892,12 +1890,10 @@ exports[`visitor keys yaml 1`] = `
"trailingComment",
],
"tag": [
- "anchor",
"endComments",
"indicatorComment",
"leadingComments",
"middleComments",
- "tag",
"trailingComment",
],
}
diff --git a/tests/unit/get-descendants.js b/tests/unit/get-descendants.js
index 521d5999c6a5..037b15d1b1ed 100644
--- a/tests/unit/get-descendants.js
+++ b/tests/unit/get-descendants.js
@@ -1,4 +1,4 @@
-import { getDescendants } from "../../src/utils/ast-utils.js";
+import { getDescendants } from "../../src/utilities/ast.js";
const tree = {
id: "tree",
diff --git a/tests/unit/get-parser-plugin-by-parser-name.js b/tests/unit/get-parser-plugin-by-parser-name.js
index cf3fdd2a4963..5abf2640486c 100644
--- a/tests/unit/get-parser-plugin-by-parser-name.js
+++ b/tests/unit/get-parser-plugin-by-parser-name.js
@@ -1,6 +1,6 @@
import { ConfigError } from "../../src/common/errors.js";
import { getParserPluginByParserName } from "../../src/main/parser-and-printer.js";
-import createPlugin from "../config/utils/create-plugin.cjs";
+import createPlugin from "../config/utilities/create-plugin.cjs";
describe("unit tests for getParserPluginByParserName", () => {
const getMockPlugins = (name) => [
diff --git a/tests/unit/get-printer-plugin-by-ast-format.js b/tests/unit/get-printer-plugin-by-ast-format.js
index 61f75e735a62..d257293c3678 100644
--- a/tests/unit/get-printer-plugin-by-ast-format.js
+++ b/tests/unit/get-printer-plugin-by-ast-format.js
@@ -1,6 +1,6 @@
import { ConfigError } from "../../src/common/errors.js";
import { getPrinterPluginByAstFormat } from "../../src/main/parser-and-printer.js";
-import createPlugin from "../config/utils/create-plugin.cjs";
+import createPlugin from "../config/utilities/create-plugin.cjs";
describe("unit tests for getPrinterPluginByAstFormat", () => {
const getMockPlugins = (name) => [
diff --git a/tests/unit/make-string.js b/tests/unit/make-string.js
index b8f04bf667cd..206a1efae52f 100644
--- a/tests/unit/make-string.js
+++ b/tests/unit/make-string.js
@@ -1,4 +1,4 @@
-import makeString from "../../src/utils/make-string.js";
+import makeString from "../../src/utilities/make-string.js";
const DOUBLE_QUOTE = '"';
const SINGLE_QUOTE = "'";
diff --git a/tests/unit/visitor-keys.js b/tests/unit/visitor-keys.js
index 5411b42b79f4..cce4eb192892 100644
--- a/tests/unit/visitor-keys.js
+++ b/tests/unit/visitor-keys.js
@@ -1,11 +1,11 @@
-import postcssVisitorKeys from "../../src/language-css/visitor-keys.js";
+import postcssVisitorKeys from "../../src/language-css/visitor-keys.evaluate.js";
import graphqlVisitorKeys from "../../src/language-graphql/visitor-keys.js";
-import glimmerVisitorKeys from "../../src/language-handlebars/visitor-keys.evaluate.js";
-import htmlVisitorKeys from "../../src/language-html/visitor-keys.js";
+import glimmerVisitorKeys from "../../src/language-handlebars/visitor-keys.js";
+import htmlVisitorKeys from "../../src/language-html/visitor-keys.evaluate.js";
import estreeVisitorKeys from "../../src/language-js/traverse/visitor-keys.evaluate.js";
-import jsonVisitorKeys from "../../src/language-json/visitor-keys.js";
-import remarkVisitorKeys from "../../src/language-markdown/visitor-keys.js";
-import yamlVisitorKeys from "../../src/language-yaml/visitor-keys.js";
+import jsonVisitorKeys from "../../src/language-json/visitor-keys.evaluate.js";
+import remarkVisitorKeys from "../../src/language-markdown/visitor-keys.evaluate.js";
+import yamlVisitorKeys from "../../src/language-yaml/visitor-keys.evaluate.js";
// Keep eye on package change
describe("visitor keys", () => {
diff --git a/tests/unit/whitespace-utils.js b/tests/unit/whitespace-utilities.js
similarity index 73%
rename from tests/unit/whitespace-utils.js
rename to tests/unit/whitespace-utilities.js
index 47193ce46b74..b2d22cd37f3b 100644
--- a/tests/unit/whitespace-utils.js
+++ b/tests/unit/whitespace-utilities.js
@@ -1,20 +1,22 @@
-import WhitespaceUtils from "../../src/utils/whitespace-utils.js";
+import WhitespaceUtilities from "../../src/utilities/whitespace-utilities.js";
it("constructor", () => {
- expect(() => new WhitespaceUtils()).toThrow(TypeError);
- expect(() => new WhitespaceUtils(1)).toThrow(TypeError);
- expect(() => new WhitespaceUtils("")).toThrow(TypeError);
- expect(() => new WhitespaceUtils([])).toThrow(TypeError);
- expect(() => new WhitespaceUtils([""])).toThrow(TypeError);
- expect(() => new WhitespaceUtils(["a"])).toThrow(TypeError);
- expect(() => new WhitespaceUtils("a")).toThrow(TypeError);
- expect(() => new WhitespaceUtils(["\r\n"])).toThrow(TypeError);
+ expect(() => new WhitespaceUtilities()).toThrow(TypeError);
+ expect(() => new WhitespaceUtilities(1)).toThrow(TypeError);
+ expect(() => new WhitespaceUtilities("")).toThrow(TypeError);
+ expect(() => new WhitespaceUtilities([])).toThrow(TypeError);
+ expect(() => new WhitespaceUtilities([""])).toThrow(TypeError);
+ expect(() => new WhitespaceUtilities(["a"])).toThrow(TypeError);
+ expect(() => new WhitespaceUtilities("a")).toThrow(TypeError);
+ expect(() => new WhitespaceUtilities(["\r\n"])).toThrow(TypeError);
- expect(new WhitespaceUtils("\r\n")).toBeInstanceOf(WhitespaceUtils);
- expect(new WhitespaceUtils(["\r", "\n"])).toBeInstanceOf(WhitespaceUtils);
+ expect(new WhitespaceUtilities("\r\n")).toBeInstanceOf(WhitespaceUtilities);
+ expect(new WhitespaceUtilities(["\r", "\n"])).toBeInstanceOf(
+ WhitespaceUtilities,
+ );
});
-const utils = new WhitespaceUtils(" ");
+const utils = new WhitespaceUtilities(" ");
describe("trimStart", () => {
for (const [string, expected] of [
["", ""],
diff --git a/website/blog/2023-07-05-3.0.0.md b/website/blog/2023-07-05-3.0.0.md
index fdf99b40ec6c..104a4639fd32 100644
--- a/website/blog/2023-07-05-3.0.0.md
+++ b/website/blog/2023-07-05-3.0.0.md
@@ -2092,14 +2092,14 @@ Align with other tools like ESLint and Stylelint.
```jsx
// Prettier 2.8
Checking formatting...
-[warn] src\utils\create-get-visitor-keys.js
-[warn] src\utils\unexpected-node-error.js
+[warn] src\utilities\create-get-visitor-keys.js
+[warn] src\utilities\unexpected-node-error.js
[warn] Code style issues found in 2 files. Forgot to run Prettier?
// Prettier 3.0
Checking formatting...
-[warn] src/utils/create-get-visitor-keys.js
-[warn] src/utils/unexpected-node-error.js
+[warn] src/utilities/create-get-visitor-keys.js
+[warn] src/utilities/unexpected-node-error.js
[warn] Code style issues found in 2 files. Forgot to run Prettier?
```
diff --git a/website/package.json b/website/package.json
index 257d507f0cf4..5807f05a049f 100644
--- a/website/package.json
+++ b/website/package.json
@@ -34,7 +34,7 @@
"@vitejs/plugin-react": "5.1.1",
"concurrently": "9.2.1",
"vite": "7.2.4",
- "yaml": "2.8.1"
+ "yaml": "2.8.2"
},
"browserslist": {
"production": [
diff --git a/website/playground/Playground.jsx b/website/playground/Playground.jsx
index 5edd3b20125c..190bfa101f42 100644
--- a/website/playground/Playground.jsx
+++ b/website/playground/Playground.jsx
@@ -11,7 +11,14 @@ import { Checkbox } from "./sidebar/inputs.jsx";
import Option from "./sidebar/options.jsx";
import SidebarOptions from "./sidebar/SidebarOptions.jsx";
import * as urlHash from "./urlHash.js";
-import * as util from "./util.js";
+import {
+ buildCliArgs,
+ convertOffsetToSelection,
+ convertSelectionToRange,
+ getAstAutoFold,
+ getCodemirrorMode,
+ getDefaults,
+} from "./utilities.js";
const { React } = window;
@@ -33,6 +40,7 @@ const ENABLED_OPTIONS = [
"printWidth",
"tabWidth",
"useTabs",
+ "endOfLine",
"semi",
"singleQuote",
"bracketSpacing",
@@ -61,10 +69,7 @@ class Playground extends React.Component {
const original = urlHash.read();
- const defaultOptions = util.getDefaults(
- props.availableOptions,
- ENABLED_OPTIONS,
- );
+ const defaultOptions = getDefaults(props.availableOptions, ENABLED_OPTIONS);
const options = Object.assign(defaultOptions, original.options);
@@ -110,13 +115,13 @@ class Playground extends React.Component {
if (this.state.trackCursorOffset) {
this.handleOptionValueChange(
this.cursorOffsetOption,
- util.convertSelectionToRange(selection, this.state.content)[0],
+ convertSelectionToRange(selection, this.state.content)[0],
);
}
};
this.setSelectionAsRange = () => {
const { selection, content, options } = this.state;
- const [rangeStart, rangeEnd] = util.convertSelectionToRange(
+ const [rangeStart, rangeEnd] = convertSelectionToRange(
selection,
content,
);
@@ -182,7 +187,7 @@ class Playground extends React.Component {
"rangeEnd",
"cursorOffset",
]);
- const cliOptions = util.buildCliArgs(orderedOptions, options);
+ const cliOptions = buildCliArgs(orderedOptions, options);
return formatMarkdown({
input: content,
@@ -207,7 +212,7 @@ class Playground extends React.Component {
return this.props.worker
.format(content, {
parser: "__js_expression",
- cursorOffset: util.convertSelectionToRange(selection, content)[0],
+ cursorOffset: convertSelectionToRange(selection, content)[0],
})
.then(({ error, formatted, cursorOffset }) => {
if (error) {
@@ -216,7 +221,7 @@ class Playground extends React.Component {
this.setState({
content: formatted,
- selection: util.convertOffsetToSelection(cursorOffset, formatted),
+ selection: convertOffsetToSelection(cursorOffset, formatted),
});
});
}
@@ -224,13 +229,13 @@ class Playground extends React.Component {
insertDummyId() {
const { content, selection } = this.state;
const dummyId = generateDummyId();
- const range = util.convertSelectionToRange(selection, content);
+ const range = convertSelectionToRange(selection, content);
const modifiedContent =
content.slice(0, range[0]) + dummyId + content.slice(range[1]);
this.setState({
content: modifiedContent,
- selection: util.convertOffsetToSelection(
+ selection: convertOffsetToSelection(
range[0] + dummyId.length,
modifiedContent,
),
@@ -420,7 +425,7 @@ class Playground extends React.Component {
{editorState.showInput ? (
) : null}
{editorState.showPreprocessedAst && !isDocExplorer ? (
) : null}
{editorState.showDoc && !isDocExplorer ? (
@@ -454,12 +459,12 @@ class Playground extends React.Component {
{editorState.showComments && !isDocExplorer ? (
) : null}
{editorState.showOutput ? (
diff --git a/website/playground/index.jsx b/website/playground/index.jsx
index ef86d50d9a9d..f26d4b325468 100644
--- a/website/playground/index.jsx
+++ b/website/playground/index.jsx
@@ -2,7 +2,7 @@ import "codemirror-graphql/cm6-legacy/mode.esm.js";
import "./install-service-worker.js";
import Playground from "./Playground.jsx";
-import { fixPrettierVersion } from "./util.js";
+import { fixPrettierVersion } from "./utilities.js";
import VersionLink from "./VersionLink.jsx";
import WorkerApi from "./WorkerApi.js";
diff --git a/website/playground/sidebar/SidebarOptions.jsx b/website/playground/sidebar/SidebarOptions.jsx
index f3f8bdbc70d1..f3b01d440feb 100644
--- a/website/playground/sidebar/SidebarOptions.jsx
+++ b/website/playground/sidebar/SidebarOptions.jsx
@@ -1,7 +1,7 @@
import { SidebarCategory } from "./components.jsx";
import Option from "./options.jsx";
-// Copied from `/src/cli/utils.js`
+// Copied from `/src/cli/utilities.js`
function groupBy(array, iteratee) {
const result = Object.create(null);
diff --git a/website/playground/util.js b/website/playground/utilities.js
similarity index 100%
rename from website/playground/util.js
rename to website/playground/utilities.js
diff --git a/website/versioned_docs/version-stable/browser.md b/website/versioned_docs/version-stable/browser.md
index 379ac6ccaf28..723421131171 100644
--- a/website/versioned_docs/version-stable/browser.md
+++ b/website/versioned_docs/version-stable/browser.md
@@ -21,7 +21,7 @@ Required options:
- **[`parser`](options.md#parser) (or [`filepath`](options.md#file-path))**: One of these options has to be specified for Prettier to know which parser to use.
-- **`plugins`**: Unlike the `format` function from the [Node.js-based API](api.md#prettierformatsource-options), this function doesn’t load plugins automatically. The `plugins` option is required because all the parsers included in the Prettier package come as plugins (for reasons of file size). These plugins are files in [https://unpkg.com/browse/prettier@3.7.3/plugins](https://unpkg.com/browse/prettier@3.7.3/plugins). Note that `estree` plugin should be loaded when printing JavaScript, TypeScript, Flow, or JSON.
+- **`plugins`**: Unlike the `format` function from the [Node.js-based API](api.md#prettierformatsource-options), this function doesn’t load plugins automatically. The `plugins` option is required because all the parsers included in the Prettier package come as plugins (for reasons of file size). These plugins are files in [https://unpkg.com/browse/prettier@3.7.4/plugins](https://unpkg.com/browse/prettier@3.7.4/plugins). Note that `estree` plugin should be loaded when printing JavaScript, TypeScript, Flow, or JSON.
You need to load the ones that you’re going to use and pass them to `prettier.format` using the `plugins` option.
@@ -32,8 +32,8 @@ See below for examples.
### Global
```html
-
-
+
+