Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove prettier.doc.builders.concat #13203

Merged
merged 13 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,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",
Expand Down
3 changes: 3 additions & 0 deletions changelog_unreleased/api/13203.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 0 additions & 10 deletions commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

This file was deleted.

16 changes: 0 additions & 16 deletions scripts/tools/eslint-plugin-prettier-internal-rules/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
31 changes: 3 additions & 28 deletions src/document/builders.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
DOC_TYPE_CONCAT,
DOC_TYPE_CURSOR,
DOC_TYPE_INDENT,
DOC_TYPE_ALIGN,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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") };

Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -279,7 +255,6 @@ function label(label, contents) {
}

export {
concat,
join,
line,
softline,
Expand Down
15 changes: 7 additions & 8 deletions src/document/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== "") {
Expand All @@ -32,7 +31,7 @@ function flattenDoc(doc) {
}
}

return { type: "concat", parts: res };
return res;
}

if (doc.type === DOC_TYPE_IF_BREAK) {
Expand Down Expand Up @@ -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(", ")}]`;
}

Expand Down
1 change: 0 additions & 1 deletion src/document/invalid-doc-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions src/document/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,8 @@ function printDocToString(doc, options) {
}

case DOC_TYPE_CONCAT: {
const parts = getDocParts(doc);
for (let i = parts.length - 1; i >= 0; i--) {
cmds.push({ ind, mode, doc: parts[i] });
for (let i = doc.length - 1; i >= 0; i--) {
cmds.push({ ind, mode, doc: doc[i] });
}
break;
}
Expand Down
35 changes: 14 additions & 21 deletions src/document/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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))) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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));
Expand All @@ -367,8 +363,8 @@ function normalizeParts(parts) {
continue;
}

if (isConcat(part)) {
restParts.unshift(...getDocParts(part));
if (Array.isArray(part)) {
restParts.unshift(...part);
continue;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -439,7 +433,6 @@ function getDocType(doc) {
}

export {
isConcat,
getDocParts,
willBreak,
traverseDoc,
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 { 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";
Expand Down Expand Up @@ -651,7 +651,7 @@ async 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) {
Expand Down
16 changes: 5 additions & 11 deletions src/language-html/printer-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -39,7 +33,6 @@ async function genericPrint(path, options, print) {
if (options.__onHtmlRoot) {
options.__onHtmlRoot(node);
}
// use original concat to not break stripTrailingHardline
return [group(await printChildren(path, options, print)), hardline];
case "element":
case "ieConditionalComment": {
Expand Down Expand Up @@ -73,10 +66,11 @@ async 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":
Expand Down