From 33e621133c48e857244edea7f26cf20793c55aa6 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 6 Aug 2022 11:03:47 +0800 Subject: [PATCH] Remove `prettier.doc.builders.concat` (#13203) --- .eslintrc.cjs | 1 - changelog_unreleased/api/13203.md | 3 + commands.md | 10 --- .../index.js | 1 - .../no-doc-builder-concat.js | 34 --------- .../test.js | 16 ---- src/document/builders.js | 34 ++------- src/document/constants.js | 2 +- src/document/debug.js | 15 ++-- src/document/invalid-doc-error.js | 5 +- src/document/printer.js | 11 ++- src/document/utils.js | 41 +++++------ src/language-handlebars/printer-glimmer.js | 4 +- src/language-html/printer-html.js | 16 ++-- src/language-html/utils/index.js | 4 +- src/language-js/print/binaryish.js | 4 +- src/language-yaml/print/block.js | 3 +- src/language-yaml/printer-yaml.js | 6 +- tests/integration/__tests__/debug-api.js | 9 --- tests/integration/__tests__/doc-builders.js | 73 ++----------------- .../integration/__tests__/doc-mark-as-root.js | 8 +- tests/integration/__tests__/doc-trim.js | 36 ++++----- .../__tests__/doc-utils-clean-doc.js | 58 +++++++-------- tests/integration/__tests__/normalize-doc.js | 18 ++--- 24 files changed, 115 insertions(+), 297 deletions(-) create mode 100644 changelog_unreleased/api/13203.md delete mode 100644 scripts/tools/eslint-plugin-prettier-internal-rules/no-doc-builder-concat.js diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 9f0f0e331..5c7ac1d94 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -299,7 +299,6 @@ module.exports = { "prettier-internal-rules/flat-ast-path-call": "error", "prettier-internal-rules/no-conflicting-comment-check-flags": "error", "prettier-internal-rules/no-doc-index-import": "error", - "prettier-internal-rules/no-doc-builder-concat": "error", "prettier-internal-rules/no-empty-flat-contents-for-if-break": "error", "prettier-internal-rules/no-unnecessary-ast-path-call": "error", "prettier-internal-rules/prefer-ast-path-each": "error", diff --git a/changelog_unreleased/api/13203.md b/changelog_unreleased/api/13203.md new file mode 100644 index 000000000..f0636c4d7 --- /dev/null +++ b/changelog_unreleased/api/13203.md @@ -0,0 +1,3 @@ +#### [BREAKING] `prettier.doc.builders.concat` has been removed (#13203 by @fisker) + +`prettier.doc.builders.concat` [was deprecated in v2.3.0](https://prettier.io/blog/2021/05/09/2.3.0.html#use-arrays-instead-of-concat-9733httpsgithubcomprettierprettierpull9733-by-fiskerhttpsgithubcomfisker-thorn0httpsgithubcomthorn0), now it's removed. diff --git a/commands.md b/commands.md index 51c5c56e9..e10722561 100644 --- a/commands.md +++ b/commands.md @@ -307,16 +307,6 @@ declare const cursor: Doc; This is a placeholder value where the cursor is in the original input in order to find where it would be printed. -### [Deprecated] `concat` - -_This command has been deprecated in v2.3.0, use `Doc[]` instead_ - -```ts -declare function concat(docs: Doc[]): Doc; -``` - -Combine an array into a single doc. - ## Example For an example, here's the implementation of the `ArrayExpression` node type: diff --git a/scripts/tools/eslint-plugin-prettier-internal-rules/index.js b/scripts/tools/eslint-plugin-prettier-internal-rules/index.js index 360eb7de1..53f33d060 100644 --- a/scripts/tools/eslint-plugin-prettier-internal-rules/index.js +++ b/scripts/tools/eslint-plugin-prettier-internal-rules/index.js @@ -9,7 +9,6 @@ module.exports = { "flat-ast-path-call": require("./flat-ast-path-call.js"), "jsx-identifier-case": require("./jsx-identifier-case.js"), "no-conflicting-comment-check-flags": require("./no-conflicting-comment-check-flags.js"), - "no-doc-builder-concat": require("./no-doc-builder-concat.js"), "no-doc-index-import": require("./no-doc-index-import.js"), "no-empty-flat-contents-for-if-break": require("./no-empty-flat-contents-for-if-break.js"), "no-identifier-n": require("./no-identifier-n.js"), diff --git a/scripts/tools/eslint-plugin-prettier-internal-rules/no-doc-builder-concat.js b/scripts/tools/eslint-plugin-prettier-internal-rules/no-doc-builder-concat.js deleted file mode 100644 index b62696b92..000000000 --- a/scripts/tools/eslint-plugin-prettier-internal-rules/no-doc-builder-concat.js +++ /dev/null @@ -1,34 +0,0 @@ -"use strict"; - -const selector = [ - "CallExpression", - ">", - "Identifier.callee", - '[name="concat"]', -].join(""); - -const messageId = "no-doc-builder-concat"; - -module.exports = { - meta: { - type: "suggestion", - docs: { - url: "https://github.com/prettier/prettier/blob/main/scripts/tools/eslint-plugin-prettier-internal-rules/no-doc-builder-concat.js", - }, - messages: { - [messageId]: "Use array directly instead of `concat([])`", - }, - fixable: "code", - }, - create(context) { - return { - [selector](node) { - context.report({ - node, - messageId, - fix: (fixer) => fixer.replaceText(node, ""), - }); - }, - }; - }, -}; diff --git a/scripts/tools/eslint-plugin-prettier-internal-rules/test.js b/scripts/tools/eslint-plugin-prettier-internal-rules/test.js index d248396d0..86c0c4729 100644 --- a/scripts/tools/eslint-plugin-prettier-internal-rules/test.js +++ b/scripts/tools/eslint-plugin-prettier-internal-rules/test.js @@ -252,22 +252,6 @@ test("no-conflicting-comment-check-flags", { ], }); -test("no-doc-builder-concat", { - valid: ["notConcat([])", "concat", "[].concat([])"], - invalid: [ - { - code: "concat(parts)", - output: "(parts)", - errors: 1, - }, - { - code: "concat(['foo', line])", - output: "(['foo', line])", - errors: 1, - }, - ], -}); - test("no-identifier-n", { valid: ["const a = {n: 1}", "const m = 1", "a.n = 1"], invalid: [ diff --git a/src/document/builders.js b/src/document/builders.js index ac5ba5e70..36d062329 100644 --- a/src/document/builders.js +++ b/src/document/builders.js @@ -1,5 +1,4 @@ import { - DOC_TYPE_CONCAT, DOC_TYPE_CURSOR, DOC_TYPE_INDENT, DOC_TYPE_ALIGN, @@ -51,26 +50,6 @@ function assertDoc(val) { throw new Error("Value " + JSON.stringify(val) + " is not a valid document"); } -/** - * @param {Doc[]} parts - * @returns Doc - */ -function concat(parts) { - if (process.env.NODE_ENV !== "production") { - for (const part of parts) { - assertDoc(part); - } - } - - // We cannot do this until we change `printJSXElement` to not - // access the internals of a document directly. - // if(parts.length === 1) { - // // If it's a single document, no need to concat it. - // return parts[0]; - // } - return { type: DOC_TYPE_CONCAT, parts }; -} - /** * @param {Doc} contents * @returns Doc @@ -226,10 +205,8 @@ const literallineWithoutBreakParent = { const line = { type: DOC_TYPE_LINE }; const softline = { type: DOC_TYPE_LINE, soft: true }; -// eslint-disable-next-line prettier-internal-rules/no-doc-builder-concat -const hardline = concat([hardlineWithoutBreakParent, breakParent]); -// eslint-disable-next-line prettier-internal-rules/no-doc-builder-concat -const literalline = concat([literallineWithoutBreakParent, breakParent]); +const hardline = [hardlineWithoutBreakParent, breakParent]; +const literalline = [literallineWithoutBreakParent, breakParent]; const cursor = { type: DOC_TYPE_CURSOR, placeholder: Symbol("cursor") }; @@ -249,8 +226,7 @@ function join(sep, arr) { res.push(arr[i]); } - // eslint-disable-next-line prettier-internal-rules/no-doc-builder-concat - return concat(res); + return res; } /** @@ -278,8 +254,10 @@ function label(label, contents) { return { type: DOC_TYPE_LABEL, label, contents }; } +// TODO: Remove this in v4 +export const concat = (parts) => parts; + export { - concat, join, line, softline, diff --git a/src/document/constants.js b/src/document/constants.js index 29203e2ca..3b4854125 100644 --- a/src/document/constants.js +++ b/src/document/constants.js @@ -1,5 +1,5 @@ export const DOC_TYPE_STRING = "string"; -export const DOC_TYPE_CONCAT = "concat"; +export const DOC_TYPE_ARRAY = "array"; export const DOC_TYPE_CURSOR = "cursor"; export const DOC_TYPE_INDENT = "indent"; export const DOC_TYPE_ALIGN = "align"; diff --git a/src/document/debug.js b/src/document/debug.js index 5949a54b0..a7f6c98d2 100644 --- a/src/document/debug.js +++ b/src/document/debug.js @@ -12,18 +12,17 @@ import { DOC_TYPE_LABEL, DOC_TYPE_BREAK_PARENT, } from "./constants.js"; -import { isConcat, getDocParts } from "./utils.js"; function flattenDoc(doc) { if (!doc) { return ""; } - if (isConcat(doc)) { + if (Array.isArray(doc)) { const res = []; - for (const part of getDocParts(doc)) { - if (isConcat(part)) { - res.push(...flattenDoc(part).parts); + for (const part of doc) { + if (Array.isArray(part)) { + res.push(...flattenDoc(part)); } else { const flattened = flattenDoc(part); if (flattened !== "") { @@ -32,7 +31,7 @@ function flattenDoc(doc) { } } - return { type: "concat", parts: res }; + return res; } if (doc.type === DOC_TYPE_IF_BREAK) { @@ -74,8 +73,8 @@ function printDocToDebug(doc) { return JSON.stringify(doc); } - if (isConcat(doc)) { - const printed = getDocParts(doc).map(printDoc).filter(Boolean); + if (Array.isArray(doc)) { + const printed = doc.map(printDoc).filter(Boolean); return printed.length === 1 ? printed[0] : `[${printed.join(", ")}]`; } diff --git a/src/document/invalid-doc-error.js b/src/document/invalid-doc-error.js index 1deba4fe0..b8af3ed52 100644 --- a/src/document/invalid-doc-error.js +++ b/src/document/invalid-doc-error.js @@ -1,7 +1,7 @@ import getLast from "../utils/get-last.js"; import { DOC_TYPE_STRING, - DOC_TYPE_CONCAT, + DOC_TYPE_ARRAY, DOC_TYPE_CURSOR, DOC_TYPE_INDENT, DOC_TYPE_ALIGN, @@ -19,7 +19,6 @@ import { import { getDocType } from "./utils.js"; const VALID_OBJECT_DOC_TYPE_VALUES = [ - DOC_TYPE_CONCAT, DOC_TYPE_CURSOR, DOC_TYPE_INDENT, DOC_TYPE_ALIGN, @@ -50,7 +49,7 @@ function getDocErrorMessage(doc) { /* istanbul ignore next */ if ( docType === DOC_TYPE_STRING || - docType === DOC_TYPE_CONCAT || + docType === DOC_TYPE_ARRAY || VALID_OBJECT_DOC_TYPE_VALUES.includes(docType) ) { throw new Error("doc is valid."); diff --git a/src/document/printer.js b/src/document/printer.js index 30575d987..1b56d09d1 100644 --- a/src/document/printer.js +++ b/src/document/printer.js @@ -3,7 +3,7 @@ import getLast from "../utils/get-last.js"; import getStringWidth from "../utils/get-string-width.js"; import { DOC_TYPE_STRING, - DOC_TYPE_CONCAT, + DOC_TYPE_ARRAY, DOC_TYPE_CURSOR, DOC_TYPE_INDENT, DOC_TYPE_ALIGN, @@ -205,7 +205,7 @@ function fits(next, restCommands, width, hasLineSuffix, mustBeFlat) { width -= getStringWidth(doc); break; - case DOC_TYPE_CONCAT: + case DOC_TYPE_ARRAY: case DOC_TYPE_FILL: { const parts = getDocParts(doc); for (let i = parts.length - 1; i >= 0; i--) { @@ -301,10 +301,9 @@ function printDocToString(doc, options) { break; } - case DOC_TYPE_CONCAT: { - const parts = getDocParts(doc); - for (let i = parts.length - 1; i >= 0; i--) { - cmds.push({ ind, mode, doc: parts[i] }); + case DOC_TYPE_ARRAY: { + for (let i = doc.length - 1; i >= 0; i--) { + cmds.push({ ind, mode, doc: doc[i] }); } break; } diff --git a/src/document/utils.js b/src/document/utils.js index e69114c2f..395fe1a86 100644 --- a/src/document/utils.js +++ b/src/document/utils.js @@ -1,7 +1,7 @@ import getLast from "../utils/get-last.js"; import { DOC_TYPE_STRING, - DOC_TYPE_CONCAT, + DOC_TYPE_ARRAY, DOC_TYPE_INDENT, DOC_TYPE_ALIGN, DOC_TYPE_GROUP, @@ -15,17 +15,14 @@ import { } from "./constants.js"; import { literalline, join } from "./builders.js"; -const isConcat = (doc) => getDocType(doc) === DOC_TYPE_CONCAT; const getDocParts = (doc) => { if (Array.isArray(doc)) { return doc; } /* istanbul ignore next */ - if (doc.type !== DOC_TYPE_CONCAT && doc.type !== DOC_TYPE_FILL) { - throw new Error( - `Expect doc type to be '${DOC_TYPE_CONCAT}' or '${DOC_TYPE_FILL}'.` - ); + if (doc.type !== DOC_TYPE_FILL) { + throw new Error(`Expect doc to be 'array' or '${DOC_TYPE_FILL}'.`); } return doc.parts; @@ -58,7 +55,7 @@ function traverseDoc(doc, onEnter, onExit, shouldTraverseConditionalGroups) { // the parts need to be pushed onto the stack in reverse order, // so that they are processed in the original order // when the stack is popped. - if (isConcat(doc) || doc.type === DOC_TYPE_FILL) { + if (Array.isArray(doc) || doc.type === DOC_TYPE_FILL) { const parts = getDocParts(doc); for (let ic = parts.length, i = ic - 1; i >= 0; --i) { docsStack.push(parts[i]); @@ -109,7 +106,7 @@ function mapDoc(doc, cb) { return cb(doc.map(rec)); } - if (doc.type === DOC_TYPE_CONCAT || doc.type === DOC_TYPE_FILL) { + if (doc.type === DOC_TYPE_ARRAY || doc.type === DOC_TYPE_FILL) { const parts = doc.parts.map(rec); return cb({ ...doc, parts }); } @@ -244,7 +241,7 @@ function stripDocTrailingHardlineFromDoc(doc) { return doc; } - if (isConcat(doc) || doc.type === DOC_TYPE_FILL) { + if (Array.isArray(doc) || doc.type === DOC_TYPE_FILL) { const parts = getDocParts(doc); while (parts.length > 1 && isHardline(...parts.slice(-2))) { @@ -319,18 +316,16 @@ function cleanDocFn(doc) { break; } - if (!isConcat(doc)) { + if (!Array.isArray(doc)) { return doc; } const parts = []; - for (const part of getDocParts(doc)) { + for (const part of doc) { if (!part) { continue; } - const [currentPart, ...restParts] = isConcat(part) - ? getDocParts(part) - : [part]; + const [currentPart, ...restParts] = Array.isArray(part) ? part : [part]; if (typeof currentPart === "string" && typeof getLast(parts) === "string") { parts[parts.length - 1] += currentPart; } else { @@ -346,11 +341,12 @@ function cleanDocFn(doc) { if (parts.length === 1) { return parts[0]; } - return Array.isArray(doc) ? parts : { ...doc, parts }; + + return parts; } // A safer version of `normalizeDoc` -// - `normalizeDoc` concat strings and flat "concat" in `fill`, while `cleanDoc` don't -// - On `concat` object, `normalizeDoc` always return object with `parts`, `cleanDoc` may return strings +// - `normalizeDoc` concat strings and flat array in `fill`, while `cleanDoc` don't +// - On array, `normalizeDoc` always return object with `parts`, `cleanDoc` may return strings // - `cleanDoc` also remove nested `group`s and empty `fill`/`align`/`indent`/`line-suffix`/`if-break` if possible function cleanDoc(doc) { return mapDoc(doc, (currentDoc) => cleanDocFn(currentDoc)); @@ -367,8 +363,8 @@ function normalizeParts(parts) { continue; } - if (isConcat(part)) { - restParts.unshift(...getDocParts(part)); + if (Array.isArray(part)) { + restParts.unshift(...part); continue; } @@ -410,10 +406,8 @@ function replaceEndOfLine(doc) { ); } -// This function need return array -// TODO: remove `.parts` when we remove `docBuilders.concat()` function replaceTextEndOfLine(text, replacement = literalline) { - return join(replacement, text.split("\n")).parts; + return join(replacement, text.split("\n")); } function canBreakFn(doc) { @@ -432,14 +426,13 @@ function getDocType(doc) { } if (Array.isArray(doc)) { - return DOC_TYPE_CONCAT; + return DOC_TYPE_ARRAY; } return doc?.type; } export { - isConcat, getDocParts, willBreak, traverseDoc, diff --git a/src/language-handlebars/printer-glimmer.js b/src/language-handlebars/printer-glimmer.js index 9592bc64c..105b81bf3 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -9,7 +9,7 @@ import { line, softline, } from "../document/builders.js"; -import { getDocParts, replaceTextEndOfLine } from "../document/utils.js"; +import { replaceTextEndOfLine } from "../document/utils.js"; import { getPreferredQuote, isNonEmptyArray } from "../common/util.js"; import { locStart, locEnd } from "./loc.js"; import clean from "./clean.js"; @@ -651,7 +651,7 @@ function printInverse(path, print, options) { /* TextNode print helpers */ function getTextValueParts(value) { - return getDocParts(join(line, splitByHtmlWhitespace(value))); + return join(line, splitByHtmlWhitespace(value)); } function splitByHtmlWhitespace(string) { diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index cd69fceb5..126d966eb 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -2,14 +2,8 @@ * @typedef {import("../document/builders.js").Doc} Doc */ -import { DOC_TYPE_FILL } from "../document/constants.js"; import { fill, group, hardline, literalline } from "../document/builders.js"; -import { - cleanDoc, - getDocParts, - isConcat, - replaceTextEndOfLine, -} from "../document/utils.js"; +import { cleanDoc, replaceTextEndOfLine } from "../document/utils.js"; import clean from "./clean.js"; import { countChars, @@ -39,7 +33,6 @@ function genericPrint(path, options, print) { if (options.__onHtmlRoot) { options.__onHtmlRoot(node); } - // use original concat to not break stripTrailingHardline return [group(printChildren(path, options, print)), hardline]; case "element": case "ieConditionalComment": { @@ -73,10 +66,11 @@ function genericPrint(path, options, print) { ...getTextValueParts(node), printClosingTagSuffix(node, options), ]); - if (isConcat(printed) || printed.type === DOC_TYPE_FILL) { - return fill(getDocParts(printed)); + + if (Array.isArray(printed)) { + return fill(printed); } - /* istanbul ignore next */ + return printed; } case "docType": diff --git a/src/language-html/utils/index.js b/src/language-html/utils/index.js index acf6e2667..e3546302e 100644 --- a/src/language-html/utils/index.js +++ b/src/language-html/utils/index.js @@ -4,7 +4,7 @@ import { inferParserByLanguage, isFrontMatterNode } from "../../common/util.js"; import { line, hardline, join } from "../../document/builders.js"; -import { getDocParts, replaceTextEndOfLine } from "../../document/utils.js"; +import { replaceTextEndOfLine } from "../../document/utils.js"; import { CSS_DISPLAY_TAGS, CSS_DISPLAY_DEFAULT, @@ -638,7 +638,7 @@ function getTextValueParts(node, value = node.value) { dedentString(htmlTrimPreserveIndentation(value)), hardline ) - : getDocParts(join(line, splitByHtmlWhitespace(value))); + : join(line, splitByHtmlWhitespace(value)); } function isVueScriptTag(node, options) { diff --git a/src/language-js/print/binaryish.js b/src/language-js/print/binaryish.js index bba2e573d..1b4acc4a6 100644 --- a/src/language-js/print/binaryish.js +++ b/src/language-js/print/binaryish.js @@ -10,7 +10,7 @@ import { align, indentIfBreak, } from "../../document/builders.js"; -import { cleanDoc, getDocParts, isConcat } from "../../document/utils.js"; +import { cleanDoc, getDocParts } from "../../document/utils.js"; import { hasLeadingOwnLineComment, isBinaryish, @@ -302,7 +302,7 @@ function printBinaryishExpressions( if (isNested && hasComment(node)) { const printed = cleanDoc(printComments(path, parts, options)); /* istanbul ignore else */ - if (isConcat(printed) || printed.type === DOC_TYPE_FILL) { + if (Array.isArray(printed) || printed.type === DOC_TYPE_FILL) { return getDocParts(printed); } diff --git a/src/language-yaml/print/block.js b/src/language-yaml/print/block.js index 16acd834c..578e2b4d6 100644 --- a/src/language-yaml/print/block.js +++ b/src/language-yaml/print/block.js @@ -10,7 +10,6 @@ import { literalline, markAsRoot, } from "../../document/builders.js"; -import { getDocParts } from "../../document/utils.js"; import { getAncestorCount, getBlockValueLineContents, @@ -51,7 +50,7 @@ function printBlock(path, print, options) { if (index === 0) { contentsParts.push(hardline); } - contentsParts.push(fill(getDocParts(join(line, lineWords)))); + contentsParts.push(fill(join(line, lineWords))); if (index !== lineContents.length - 1) { contentsParts.push( lineWords.length === 0 ? hardline : markAsRoot(literalline) diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js index 71e325415..2a5643352 100644 --- a/src/language-yaml/printer-yaml.js +++ b/src/language-yaml/printer-yaml.js @@ -10,7 +10,7 @@ import { lineSuffix, literalline, } from "../document/builders.js"; -import { getDocParts, replaceTextEndOfLine } from "../document/utils.js"; +import { replaceTextEndOfLine } from "../document/utils.js"; import { isPreviousLineEmpty } from "../common/util.js"; import { insertPragma, isPragma } from "./pragma.js"; import { locStart } from "./loc.js"; @@ -415,9 +415,7 @@ function printFlowScalarContent(nodeType, content, options) { const lineContents = getFlowScalarLineContents(nodeType, content, options); return join( hardline, - lineContents.map((lineContentWords) => - fill(getDocParts(join(line, lineContentWords))) - ) + lineContents.map((lineContentWords) => fill(join(line, lineContentWords))) ); } diff --git a/tests/integration/__tests__/debug-api.js b/tests/integration/__tests__/debug-api.js index a620dbbe2..bbaadbc41 100644 --- a/tests/integration/__tests__/debug-api.js +++ b/tests/integration/__tests__/debug-api.js @@ -32,15 +32,6 @@ describe("API", () => { expect(formatResultFromAST).toBe(formatted); }); - test("prettier.printToDoc", (done) => { - // If it's array, it's a `concat` - if (!Array.isArray(doc)) { - expect(doc.type).toBe("concat"); - expect(Array.isArray(doc.parts)).toBe(true); - } - done(); - }); - test("prettier.printDocToString", async () => { const { formatted: stringFromDoc } = await printDocToString(doc, options); expect(stringFromDoc).toBe(formatted); diff --git a/tests/integration/__tests__/doc-builders.js b/tests/integration/__tests__/doc-builders.js index 3849bed3c..b554f90ad 100644 --- a/tests/integration/__tests__/doc-builders.js +++ b/tests/integration/__tests__/doc-builders.js @@ -1,6 +1,5 @@ import prettier from "../../config/prettier-entry.js"; const { - concat, join, hardline, literalline, @@ -13,53 +12,18 @@ const { align, } = prettier.doc.builders; -// TODO: Make these builders to use array if possible in 3.0.0 describe("doc builders", () => { test.each([ - ["concat", concat(["1", "2"]), { type: "concat", parts: ["1", "2"] }], - [ - "join", - join(concat(["1"]), ["2", concat(["3"])]), - { - type: "concat", - parts: [ - "2", - { type: "concat", parts: ["1"] }, - { type: "concat", parts: ["3"] }, - ], - }, - ], - [ - "join(array)", - join(["1"], ["2", concat(["3"])]), - { type: "concat", parts: ["2", ["1"], { type: "concat", parts: ["3"] }] }, - ], - [ - "hardline", - hardline, - { type: "concat", parts: [{ type: "line", hard: true }, breakParent] }, - ], + ["concat", ["1", "2"], ["1", "2"]], + ["join", join(["1"], ["2", ["3"]]), ["2", ["1"], ["3"]]], + ["hardline", hardline, [{ type: "line", hard: true }, breakParent]], [ "literalline", literalline, - { - type: "concat", - parts: [{ type: "line", hard: true, literal: true }, breakParent], - }, + [{ type: "line", hard: true, literal: true }, breakParent], ], [ "group", - group(concat(["1"])), - { - type: "group", - id: undefined, - contents: { type: "concat", parts: ["1"] }, - break: false, - expandedStates: undefined, - }, - ], - [ - "group (array)", group(["1"]), { type: "group", @@ -72,14 +36,6 @@ describe("doc builders", () => { ["fill", fill(["1", "2"]), { type: "fill", parts: ["1", "2"] }], [ "line-suffix", - lineSuffix(concat(["1"])), - { - type: "line-suffix", - contents: { type: "concat", parts: ["1"] }, - }, - ], - [ - "line-suffix(array)", lineSuffix(["1"]), { type: "line-suffix", @@ -88,24 +44,16 @@ describe("doc builders", () => { ], [ "if-break", - ifBreak(concat(["1"]), ["2"]), + ifBreak(["1"], ["2"]), { type: "if-break", - breakContents: { type: "concat", parts: ["1"] }, + breakContents: ["1"], flatContents: ["2"], groupId: undefined, }, ], [ "indent", - indent(concat(["1"])), - { - type: "indent", - contents: { type: "concat", parts: ["1"] }, - }, - ], - [ - "indent(array)", indent(["1"]), { type: "indent", @@ -114,15 +62,6 @@ describe("doc builders", () => { ], [ "align", - align(" ", concat(["1"])), - { - type: "align", - contents: { type: "concat", parts: ["1"] }, - n: " ", - }, - ], - [ - "align(array)", align(" ", ["1"]), { type: "align", diff --git a/tests/integration/__tests__/doc-mark-as-root.js b/tests/integration/__tests__/doc-mark-as-root.js index 5dfba24b3..000b8eb8a 100644 --- a/tests/integration/__tests__/doc-mark-as-root.js +++ b/tests/integration/__tests__/doc-mark-as-root.js @@ -3,23 +3,23 @@ const docPrinter = prettier.doc.printer; const docBuilders = prettier.doc.builders; const { printDocToString } = docPrinter; -const { concat, hardline, literalline, trim, indent, markAsRoot } = docBuilders; +const { hardline, literalline, trim, indent, markAsRoot } = docBuilders; describe("markAsRoot", () => { test.each([ [ "with hardline will insert a newline with current indentation", - concat([indent(markAsRoot(indent(hardline))), "123"]), + [indent(markAsRoot(indent(hardline))), "123"], "\n 123", ], [ "with literalline will insert a newline with root indentation", - concat([indent(markAsRoot(indent(literalline))), "123"]), + [indent(markAsRoot(indent(literalline))), "123"], "\n 123", ], [ "followed by trim will trims up to the the first column, ignoring indented root", - concat([indent(markAsRoot(indent(literalline))), trim, "123"]), + [indent(markAsRoot(indent(literalline))), trim, "123"], "\n123", ], ])("%s", (_, doc, expected) => { diff --git a/tests/integration/__tests__/doc-trim.js b/tests/integration/__tests__/doc-trim.js index 5a374ab8c..eecc4da84 100644 --- a/tests/integration/__tests__/doc-trim.js +++ b/tests/integration/__tests__/doc-trim.js @@ -5,35 +5,31 @@ const docPrinter = prettier.doc.printer; const docBuilders = prettier.doc.builders; const { printDocToString } = docPrinter; -const { concat, line, trim, group, indent } = docBuilders; +const { line, trim, group, indent } = docBuilders; // These tests don't use `runPrettier` because `trim` is not used by any // bundled parser (only third-party plugins). describe("trim", () => { test.each([ - ["trims the current line", group(concat(["hello ", trim])), "hello"], + ["trims the current line", group(["hello ", trim]), "hello"], [ "trims existing indentation", - group( - concat([ - "function()", + group([ + "function()", + line, + "{", + indent([ line, - "{", - indent( - concat([ - line, - group(concat([trim, "#if DEBUG"])), - line, - "alert(42);", - line, - group(concat([trim, "#endif"])), - ]) - ), + group([trim, "#if DEBUG"]), line, - "}", - ]) - ), + "alert(42);", + line, + group([trim, "#endif"]), + ]), + line, + "}", + ]), outdent` function() { @@ -45,7 +41,7 @@ describe("trim", () => { ], [ "ignores trimmed characters when fitting the line", - group(concat(["hello ", " ", trim, line, "world!"])), + group(["hello ", " ", trim, line, "world!"]), "hello world!", ], ])("%s", (_, doc, expected) => { diff --git a/tests/integration/__tests__/doc-utils-clean-doc.js b/tests/integration/__tests__/doc-utils-clean-doc.js index ae1a4b443..e38f1da69 100644 --- a/tests/integration/__tests__/doc-utils-clean-doc.js +++ b/tests/integration/__tests__/doc-utils-clean-doc.js @@ -3,63 +3,55 @@ const docBuilders = prettier.doc.builders; const docUtils = prettier.doc.utils; const { cleanDoc } = docUtils; -const { group, concat, align, indent, lineSuffix, ifBreak, fill } = docBuilders; +const { group, align, indent, lineSuffix, ifBreak, fill } = docBuilders; describe("cleanDoc", () => { test.each([ [ "fill", - concat([fill(["", ""]), fill([]), fill(["1"]), fill(["2", "3"])]), - concat([fill(["1"]), fill(["2", "3"])]), + [fill(["", ""]), fill([]), fill(["1"]), fill(["2", "3"])], + [fill(["1"]), fill(["2", "3"])], ], ["nested group", group(group("_")), group("_")], [ "empty group", - concat([ + [ group(""), - group(concat([""])), + group([""]), group("_", { id: "id" }), group("_", { shouldBreak: true }), group("_", { expandedStates: ["_"] }), - ]), - concat([ + ], + [ group("_", { id: "id" }), group("_", { shouldBreak: true }), group("_", { expandedStates: ["_"] }), - ]), + ], ], [ "removes empty align/indent/line-suffix", - concat([ - group( - concat([ - align(" ", concat([""])), - indent(concat([""])), - concat([""]), - "", - lineSuffix(concat([""])), - ifBreak("", concat([""])), - ]) - ), + [ + group([ + align(" ", [""]), + indent([""]), + [""], + "", + lineSuffix([""]), + ifBreak("", [""]), + ]), "_", - ]), - "_", - ], - [ - "removes empty string/concat", - concat(["", concat(["", concat([concat(["", "_", ""]), ""])]), ""]), + ], "_", ], + ["removes empty string/", ["", ["", [["", "_", ""], ""]], ""], "_"], [ "concat string & flat concat", - group( - concat([ - group("1"), - concat(["2", "3", group("4"), "5", "6"]), - concat(["7", "8", group("9"), "10", "11"]), - ]) - ), - group(concat([group("1"), "23", group("4"), "5678", group("9"), "1011"])), + group([ + group("1"), + ["2", "3", group("4"), "5", "6"], + ["7", "8", group("9"), "10", "11"], + ]), + group([group("1"), "23", group("4"), "5678", group("9"), "1011"]), ], ])("%s", (_, doc, expected) => { const result = cleanDoc(doc); diff --git a/tests/integration/__tests__/normalize-doc.js b/tests/integration/__tests__/normalize-doc.js index 997c2cfff..4dffca819 100644 --- a/tests/integration/__tests__/normalize-doc.js +++ b/tests/integration/__tests__/normalize-doc.js @@ -3,29 +3,29 @@ const docBuilders = prettier.doc.builders; const docUtils = prettier.doc.utils; const { normalizeDoc } = docUtils; -const { group, concat, fill } = docBuilders; +const { group, fill } = docBuilders; describe("normalizeDoc", () => { test.each([ [ "removes empty strings", - concat(["", "foo", fill(["", "bar", ""]), ""]), - concat(["foo", fill(["bar"])]), + ["", "foo", fill(["", "bar", ""]), ""], + ["foo", fill(["bar"])], ], [ "flattens nested concat", - concat(["foo ", "", concat(["bar ", "", concat(["baz", ""])])]), - concat(["foo bar baz"]), + ["foo ", "", ["bar ", "", ["baz", ""]]], + ["foo bar baz"], ], [ "flattens nested concat in other docs", - group(concat(["foo ", concat(["bar ", "", concat(["baz", ""])])])), - group(concat(["foo bar baz"])), + group(["foo ", ["bar ", "", ["baz", ""]]]), + group(["foo bar baz"]), ], [ "keeps groups", - concat([group("foo"), group("bar"), group("baz")]), - concat([group("foo"), group("bar"), group("baz")]), + [group("foo"), group("bar"), group("baz")], + [group("foo"), group("bar"), group("baz")], ], [ "keeps fills",