Refactor: Import document/*.js directly (#12739)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-04-29 14:04:17 +08:00
1 parent 88c036a869
commit 2bcee631a2
67 files changed
+503 -460

No files matched your search

+1
View File
@@ -271,6 +271,7 @@ module.exports = {
"prettier-internal-rules/consistent-negative-index-access": "error",
"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",
+1 -1
View File
@@ -1,4 +1,4 @@
The core of the algorithm is implemented in `src/document/doc-{printer,builders,utils}.js`. The printer uses the basic formatting abstractions provided to construct a format when printing a node.
The core of the algorithm is implemented in `src/document/{printer,builders,utils}.js`. The printer uses the basic formatting abstractions provided to construct a format when printing a node.
## Prettier's intermediate representation: `Doc`
+1 -1
View File
@@ -52,7 +52,7 @@ const config = {
collectCoverageFrom: ["<rootDir>/src/**/*.js", "<rootDir>/bin/**/*.js"],
coveragePathIgnorePatterns: [
"<rootDir>/src/standalone.js",
"<rootDir>/src/document/doc-debug.js",
"<rootDir>/src/document/debug.js",
],
coverageReporters: ["text", "lcov"],
moduleNameMapper: {
@@ -10,6 +10,7 @@ module.exports = {
"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"),
"no-legacy-format-test-fixtures": require("./no-legacy-format-test-fixtures.js"),
@@ -0,0 +1,129 @@
"use strict";
const path = require("path");
const selector = [
":matches(ImportDeclaration, ExportNamedDeclaration, ImportExpression)",
" > ",
"Literal.source",
].join("");
const messageId = "no-doc-index-import";
const docIndexFile = path.join(__dirname, "../../../src/document/index.js");
const ignored = new Set([
path.join(__dirname, "../../../src/index.js"),
path.join(__dirname, "../../../src/standalone.js"),
]);
const docProperties = new Set(["builders", "debug", "printer", "utils"]);
function fix(source, context) {
// only fix `import doc from './document/index.js'`
if (
!(
source.parent.type === "ImportDeclaration" &&
source.parent.specifiers.length === 1 &&
source.parent.specifiers[0].type === "ImportDefaultSpecifier" &&
source.parent.specifiers[0].local.type === "Identifier" &&
source.parent.specifiers[0].local.name === "doc"
)
) {
return;
}
const variables = context.getDeclaredVariables(source.parent);
if (variables.length !== 1 || variables[0].name !== "doc") {
return;
}
const [{ references }] = variables;
if (references.length !== 1) {
return;
}
// Only fix `const {builders: {}} = doc`
const [{ identifier }] = references;
if (
!(
identifier.parent.type === "VariableDeclarator" &&
identifier.parent.init === identifier &&
identifier.parent.id.type === "ObjectPattern" &&
identifier.parent.id.properties.every(
(property) =>
property.type === "Property" &&
!property.computed &&
property.key.type === "Identifier" &&
docProperties.has(property.key.name)
) &&
identifier.parent.parent.type === "VariableDeclaration" &&
identifier.parent.parent.kind === "const" &&
identifier.parent.parent.declarations.length === 1 &&
identifier.parent.parent.declarations[0] === identifier.parent
)
) {
return;
}
return function* (fixer) {
const sourceCode = context.getSourceCode();
const text = identifier.parent.id.properties
.map((property) => {
const propertyName = property.key.name;
return `import ${sourceCode.getText(
property.value
)} from "${source.value.replace(
"/document/index.js",
`/document/${propertyName}.js`
)}";`;
})
.join("\n");
yield fixer.replaceText(source.parent, text);
yield fixer.remove(identifier.parent.parent);
};
}
module.exports = {
meta: {
type: "suggestion",
docs: {
url: "https://github.com/prettier/prettier/blob/main/scripts/tools/eslint-plugin-prettier-internal-rules/no-doc-index-import.js",
},
messages: {
[messageId]: "Do not {{type}} document/index.js file",
},
fixable: "code",
},
create(context) {
const file = context.getPhysicalFilename();
if (ignored.has(file)) {
return {};
}
const dir = path.dirname(file);
return {
[selector](node) {
const { value } = node;
if (
!value.startsWith(".") ||
!value.endsWith("/document/index.js") ||
path.join(dir, value) !== docIndexFile
) {
return;
}
context.report({
node,
messageId,
data: {
type: node.parent.type.slice(0, 6).toLowerCase(),
},
fix: fix(node, context),
});
},
};
},
};
File renamed without changes.
@@ -1,4 +1,4 @@
import { isConcat, getDocParts } from "./doc-utils.js";
import { isConcat, getDocParts } from "./utils.js";
function flattenDoc(doc) {
if (!doc) {
+5 -5
View File
@@ -1,10 +1,10 @@
import * as builders from "./doc-builders.js";
import * as printer from "./doc-printer.js";
import * as utils from "./doc-utils.js";
import * as debug from "./doc-debug.js";
import * as builders from "./builders.js";
import * as printer from "./printer.js";
import * as utils from "./utils.js";
import * as debug from "./debug.js";
/**
* @typedef {import("./doc-builders").Doc} Doc
* @typedef {import("./builders").Doc} Doc
*/
const doc = {
@@ -1,8 +1,8 @@
import { convertEndOfLineToChars } from "../common/end-of-line.js";
import getLast from "../utils/get-last.js";
import getStringWidth from "../utils/get-string-width.js";
import { fill, cursor, indent } from "./doc-builders.js";
import { isConcat, getDocParts } from "./doc-utils.js";
import { fill, cursor, indent } from "./builders.js";
import { isConcat, getDocParts } from "./utils.js";
/** @type {Record<symbol, typeof MODE_BREAK | typeof MODE_FLAT>} */
let groupModeMap;
@@ -1,5 +1,5 @@
import getLast from "../utils/get-last.js";
import { literalline, join } from "./doc-builders.js";
import { literalline, join } from "./builders.js";
const isConcat = (doc) => Array.isArray(doc) || (doc && doc.type === "concat");
const getDocParts = (doc) => {
+1 -5
View File
@@ -1,10 +1,6 @@
import doc from "../document/index.js";
import { hardline } from "../document/builders.js";
import printFrontMatter from "../utils/front-matter/print.js";
const {
builders: { hardline },
} = doc;
function embed(path, print, textToDoc /*, options */) {
const node = path.getValue();
+13 -17
View File
@@ -7,7 +7,19 @@ import {
isNextLineEmpty,
isNonEmptyArray,
} from "../common/util.js";
import doc from "../document/index.js";
import {
join,
line,
hardline,
softline,
group,
fill,
indent,
dedent,
ifBreak,
breakParent,
} from "../document/builders.js";
import { removeLines, getDocParts } from "../document/utils.js";
import clean from "./clean.js";
import embed from "./embed.js";
import { insertPragma } from "./pragma.js";
@@ -62,22 +74,6 @@ import {
import { locStart, locEnd } from "./loc.js";
import printUnit from "./utils/print-unit.js";
const {
builders: {
join,
line,
hardline,
softline,
group,
fill,
indent,
dedent,
ifBreak,
breakParent,
},
utils: { removeLines, getDocParts },
} = doc;
function shouldPrintComma(options) {
return options.trailingComma === "es5" || options.trailingComma === "all";
}
+9 -5
View File
@@ -1,12 +1,16 @@
import doc from "../document/index.js";
import {
join,
hardline,
line,
softline,
group,
indent,
ifBreak,
} from "../document/builders.js";
import { isNextLineEmpty, isNonEmptyArray } from "../common/util.js";
import { insertPragma } from "./pragma.js";
import { locStart, locEnd } from "./loc.js";
const {
builders: { join, hardline, line, softline, group, indent, ifBreak },
} = doc;
function genericPrint(path, options, print) {
const node = path.getValue();
if (!node) {
+12 -16
View File
@@ -1,4 +1,15 @@
import doc from "../document/index.js";
import {
dedent,
fill,
group,
hardline,
ifBreak,
indent,
join,
line,
softline,
} from "../document/builders.js";
import { getDocParts, replaceTextEndOfLine } from "../document/utils.js";
import { getPreferredQuote, isNonEmptyArray } from "../common/util.js";
import { locStart, locEnd } from "./loc.js";
import clean from "./clean.js";
@@ -15,21 +26,6 @@ import {
isWhitespaceNode,
} from "./utils.js";
const {
builders: {
dedent,
fill,
group,
hardline,
ifBreak,
indent,
join,
line,
softline,
},
utils: { getDocParts, replaceTextEndOfLine },
} = doc;
const NEWLINES_TO_PRESERVE_MAX = 2;
// Formatter based on @glimmerjs/syntax's built-in test formatter:
+10 -6
View File
@@ -1,4 +1,13 @@
import doc from "../document/index.js";
import {
breakParent,
group,
hardline,
indent,
line,
fill,
softline,
} from "../document/builders.js";
import { mapDoc, replaceTextEndOfLine } from "../document/utils.js";
import printFrontMatter from "../utils/front-matter/print.js";
import {
printClosingTag,
@@ -26,11 +35,6 @@ import {
} from "./utils/index.js";
import getNodeContent from "./get-node-content.js";
const {
builders: { breakParent, group, hardline, indent, line, fill, softline },
utils: { mapDoc, replaceTextEndOfLine },
} = doc;
function printEmbeddedAttributeValue(node, htmlTextToDoc, options) {
const isKeyMatched = (patterns) =>
new RegExp(patterns.join("|")).test(node.fullName);
+9 -6
View File
@@ -1,4 +1,12 @@
import doc from "../../document/index.js";
import {
breakParent,
group,
ifBreak,
line,
softline,
hardline,
} from "../../document/builders.js";
import { replaceTextEndOfLine } from "../../document/utils.js";
import { locStart, locEnd } from "../loc.js";
import {
forceBreakChildren,
@@ -17,11 +25,6 @@ import {
needsToBorrowParentClosingTagStartMarker,
} from "./tag.js";
const {
builders: { breakParent, group, ifBreak, line, softline, hardline },
utils: { replaceTextEndOfLine },
} = doc;
function printChild(childPath, options, print) {
const child = childPath.getValue();
+11 -15
View File
@@ -1,4 +1,14 @@
import doc from "../../document/index.js";
import {
breakParent,
dedentToRoot,
group,
ifBreak,
indentIfBreak,
indent,
line,
softline,
} from "../../document/builders.js";
import { replaceTextEndOfLine } from "../../document/utils.js";
import getNodeContent from "../get-node-content.js";
import {
shouldPreserveContent,
@@ -17,20 +27,6 @@ import {
} from "./tag.js";
import { printChildren } from "./children.js";
const {
builders: {
breakParent,
dedentToRoot,
group,
ifBreak,
indentIfBreak,
indent,
line,
softline,
},
utils: { replaceTextEndOfLine },
} = doc;
function printElement(path, options, print) {
const node = path.getValue();
+8 -6
View File
@@ -4,7 +4,14 @@
import assert from "node:assert";
import { isNonEmptyArray } from "../../common/util.js";
import doc from "../../document/index.js";
import {
indent,
join,
line,
softline,
hardline,
} from "../../document/builders.js";
import { replaceTextEndOfLine } from "../../document/utils.js";
import { locStart, locEnd } from "../loc.js";
import {
isTextLikeNode,
@@ -14,11 +21,6 @@ import {
shouldPreserveContent,
} from "../utils/index.js";
const {
builders: { indent, join, line, softline, hardline },
utils: { replaceTextEndOfLine },
} = doc;
function printClosingTag(node, options) {
return [
node.isSelfClosing ? "" : printClosingTagStart(node, options),
+7 -6
View File
@@ -2,7 +2,13 @@
* @typedef {import("../document").Doc} Doc
*/
import doc from "../document/index.js";
import { fill, group, hardline, literalline } from "../document/builders.js";
import {
cleanDoc,
getDocParts,
isConcat,
replaceTextEndOfLine,
} from "../document/utils.js";
import clean from "./clean.js";
import {
countChars,
@@ -22,11 +28,6 @@ import {
import { printElement } from "./print/element.js";
import { printChildren } from "./print/children.js";
const {
builders: { fill, group, hardline, literalline },
utils: { cleanDoc, getDocParts, isConcat, replaceTextEndOfLine },
} = doc;
function genericPrint(path, options, print) {
const node = path.getValue();
+1 -5
View File
@@ -1,9 +1,5 @@
import parseSrcset from "parse-srcset";
import doc from "../document/index.js";
const {
builders: { ifBreak, join, line },
} = doc;
import { ifBreak, join, line } from "../document/builders.js";
function printImgSrcset(value) {
const srcset = parseSrcset(value, {
+1 -4
View File
@@ -1,7 +1,4 @@
import doc from "../document/index.js";
const {
builders: { group },
} = doc;
import { group } from "../document/builders.js";
/**
* v-for="... in ..."
+2 -6
View File
@@ -3,7 +3,8 @@
*/
import { inferParserByLanguage, isFrontMatterNode } from "../../common/util.js";
import doc from "../../document/index.js";
import { line, hardline, join } from "../../document/builders.js";
import { getDocParts, replaceTextEndOfLine } from "../../document/utils.js";
import {
CSS_DISPLAY_TAGS,
CSS_DISPLAY_DEFAULT,
@@ -12,11 +13,6 @@ import {
} from "../constants.evaluate.js";
import isUnknownNamespace from "./is-unknown-namespace.js";
const {
builders: { line, hardline, join },
utils: { getDocParts, replaceTextEndOfLine },
} = doc;
// https://infra.spec.whatwg.org/#ascii-whitespace
const HTML_WHITESPACE = new Set(["\t", "\n", "\f", "\r", " "]);
const htmlTrimStart = (string) => string.replace(/^[\t\n\f\r ]+/, "");
+2 -6
View File
@@ -1,12 +1,8 @@
import { isNonEmptyArray } from "../../common/util.js";
import doc from "../../document/index.js";
import { indent, hardline, softline } from "../../document/builders.js";
import { mapDoc, replaceEndOfLine, cleanDoc } from "../../document/utils.js";
import { printTemplateExpressions } from "../print/template-literal.js";
const {
builders: { indent, hardline, softline },
utils: { mapDoc, replaceEndOfLine, cleanDoc },
} = doc;
function format(path, print, textToDoc) {
const node = path.getValue();
+1 -5
View File
@@ -1,13 +1,9 @@
import doc from "../../document/index.js";
import { indent, join, hardline } from "../../document/builders.js";
import {
escapeTemplateCharacters,
printTemplateExpressions,
} from "../print/template-literal.js";
const {
builders: { indent, join, hardline },
} = doc;
function format(path, print, textToDoc) {
const node = path.getValue();
+2 -6
View File
@@ -1,14 +1,10 @@
import doc from "../../document/index.js";
import { indent, line, hardline, group } from "../../document/builders.js";
import { mapDoc } from "../../document/utils.js";
import {
printTemplateExpressions,
uncookTemplateElementValue,
} from "../print/template-literal.js";
const {
builders: { indent, line, hardline, group },
utils: { mapDoc },
} = doc;
// The counter is needed to distinguish nested embeds.
let htmlTemplateLiteralCounter = 0;
function format(path, print, textToDoc, options, { parser }) {
+6 -5
View File
@@ -1,10 +1,11 @@
import doc from "../../document/index.js";
import {
indent,
softline,
literalline,
dedentToRoot,
} from "../../document/builders.js";
import { escapeTemplateCharacters } from "../print/template-literal.js";
const {
builders: { indent, softline, literalline, dedentToRoot },
} = doc;
function format(path, print, textToDoc) {
const node = path.getValue();
let text = node.quasis[0].value.raw.replace(
+1 -5
View File
@@ -1,11 +1,7 @@
import doc from "../../document/index.js";
import { join, line, group } from "../../document/builders.js";
import { hasNode, hasComment, getComments } from "../utils/index.js";
import { printBinaryishExpression } from "./binaryish.js";
const {
builders: { join, line, group },
} = doc;
/** @typedef {import("../../common/ast-path.js").default} AstPath */
function printAngular(path, options, print) {
+9 -5
View File
@@ -1,5 +1,13 @@
import { printDanglingComments } from "../../main/comments.js";
import doc from "../../document/index.js";
import {
line,
softline,
hardline,
group,
indent,
ifBreak,
fill,
} from "../../document/builders.js";
import { getLast, hasNewline } from "../../common/util.js";
import {
shouldPrintComma,
@@ -13,10 +21,6 @@ import { locStart } from "../loc.js";
import { printOptionalToken, printTypeAnnotation } from "./misc.js";
const {
builders: { line, softline, hardline, group, indent, ifBreak, fill },
} = doc;
/** @typedef {import("../../document").Doc} Doc */
function printArray(path, options, print) {
+8 -6
View File
@@ -1,5 +1,12 @@
import { isNonEmptyArray, getStringWidth } from "../../common/util.js";
import doc from "../../document/index.js";
import {
line,
group,
indent,
indentIfBreak,
lineSuffixBoundary,
} from "../../document/builders.js";
import { cleanDoc, willBreak, canBreak } from "../../document/utils.js";
import {
hasLeadingOwnLineComment,
isBinaryish,
@@ -17,11 +24,6 @@ import {
import { shouldInlineLogicalExpression } from "./binaryish.js";
import { printCallExpression } from "./call-expression.js";
const {
builders: { line, group, indent, indentIfBreak, lineSuffixBoundary },
utils: { cleanDoc, willBreak, canBreak },
} = doc;
function printAssignment(
path,
options,
+11 -15
View File
@@ -1,6 +1,16 @@
import { printComments } from "../../main/comments.js";
import { getLast } from "../../common/util.js";
import doc from "../../document/index.js";
import {
join,
line,
softline,
group,
indent,
align,
ifBreak,
indentIfBreak,
} from "../../document/builders.js";
import { cleanDoc, getDocParts, isConcat } from "../../document/utils.js";
import {
hasLeadingOwnLineComment,
isBinaryish,
@@ -14,20 +24,6 @@ import {
isEnabledHackPipeline,
} from "../utils/index.js";
const {
builders: {
join,
line,
softline,
group,
indent,
align,
ifBreak,
indentIfBreak,
},
utils: { cleanDoc, getDocParts, isConcat },
} = doc;
/** @typedef {import("../../document").Doc} Doc */
let uid = 0;
+1 -5
View File
@@ -1,6 +1,6 @@
import { printDanglingComments } from "../../main/comments.js";
import { isNonEmptyArray } from "../../common/util.js";
import doc from "../../document/index.js";
import { hardline, indent } from "../../document/builders.js";
import {
hasComment,
CommentCheckFlags,
@@ -10,10 +10,6 @@ import { printHardlineAfterHeritage } from "./class.js";
import { printBody } from "./statement.js";
const {
builders: { hardline, indent },
} = doc;
/** @typedef {import("../../document").Doc} Doc */
function printBlock(path, options, print) {
+11 -15
View File
@@ -16,25 +16,21 @@ import {
isObjectProperty,
} from "../utils/index.js";
import doc from "../../document/index.js";
import {
line,
hardline,
softline,
group,
indent,
conditionalGroup,
ifBreak,
breakParent,
} from "../../document/builders.js";
import { willBreak } from "../../document/utils.js";
import { ArgExpansionBailout } from "../../common/errors.js";
import { isConciselyPrintedArray } from "./array.js";
const {
builders: {
line,
hardline,
softline,
group,
indent,
conditionalGroup,
ifBreak,
breakParent,
},
utils: { willBreak },
} = doc;
function printCallArguments(path, options, print) {
const node = path.getValue();
const isDynamicImport = node.type === "ImportExpression";
+1 -5
View File
@@ -1,4 +1,4 @@
import doc from "../../document/index.js";
import { join, group } from "../../document/builders.js";
import pathNeedsParens from "../needs-parens.js";
import {
getCallArguments,
@@ -14,10 +14,6 @@ import printMemberChain from "./member-chain.js";
import printCallArguments from "./call-arguments.js";
import { printOptionalToken, printFunctionTypeParameters } from "./misc.js";
const {
builders: { join, group },
} = doc;
function printCallExpression(path, options, print) {
const node = path.getValue();
const parentNode = path.getParentNode();
+9 -5
View File
@@ -1,6 +1,14 @@
import { isNonEmptyArray, createGroupIdMapper } from "../../common/util.js";
import { printComments, printDanglingComments } from "../../main/comments.js";
import doc from "../../document/index.js";
import {
join,
line,
hardline,
softline,
group,
indent,
ifBreak,
} from "../../document/builders.js";
import { hasComment, CommentCheckFlags } from "../utils/index.js";
import { getTypeParametersGroupId } from "./type-parameters.js";
import { printMethod } from "./function.js";
@@ -13,10 +21,6 @@ import { printPropertyKey } from "./property.js";
import { printAssignment } from "./assignment.js";
import { printClassMemberDecorators } from "./decorators.js";
const {
builders: { join, line, hardline, softline, group, indent, ifBreak },
} = doc;
function printClass(path, options, print) {
const node = path.getValue();
const parts = [];
+2 -6
View File
@@ -1,15 +1,11 @@
import { hasNewline } from "../../common/util.js";
import doc from "../../document/index.js";
import { join, hardline } from "../../document/builders.js";
import { replaceTextEndOfLine } from "../../document/utils.js";
import { isLineComment } from "../utils/index.js";
import { locStart, locEnd } from "../loc.js";
import isBlockComment from "../utils/is-block-comment.js";
const {
builders: { join, hardline },
utils: { replaceTextEndOfLine },
} = doc;
function printComment(commentPath, options) {
const comment = commentPath.getValue();
+7 -5
View File
@@ -1,12 +1,14 @@
import { isNonEmptyArray, hasNewline } from "../../common/util.js";
import doc from "../../document/index.js";
import {
line,
hardline,
join,
breakParent,
group,
} from "../../document/builders.js";
import { locStart, locEnd } from "../loc.js";
import { getParentExportDeclaration } from "../utils/index.js";
const {
builders: { line, hardline, join, breakParent, group },
} = doc;
function printClassMemberDecorators(path, options, print) {
const node = path.getValue();
return group([
+1 -5
View File
@@ -3,7 +3,7 @@
import assert from "node:assert";
import { printDanglingComments } from "../../main/comments.js";
import { printString, printNumber } from "../../common/util.js";
import doc from "../../document/index.js";
import { hardline, softline, group, indent } from "../../document/builders.js";
import {
getParentExportDeclaration,
isFunctionNotation,
@@ -34,10 +34,6 @@ import {
printRestSpread,
} from "./misc.js";
const {
builders: { hardline, softline, group, indent },
} = doc;
function printFlow(path, options, print) {
const node = path.getValue();
const semi = options.semi ? ";" : "";
+9 -6
View File
@@ -1,6 +1,14 @@
import { getNextNonSpaceNonCommentCharacter } from "../../common/util.js";
import { printDanglingComments } from "../../main/comments.js";
import doc from "../../document/index.js";
import {
line,
hardline,
softline,
group,
indent,
ifBreak,
} from "../../document/builders.js";
import { removeLines, willBreak } from "../../document/utils.js";
import {
getFunctionParameters,
iterateFunctionParametersPath,
@@ -18,11 +26,6 @@ import { locEnd } from "../loc.js";
import { ArgExpansionBailout } from "../../common/errors.js";
import { printFunctionTypeParameters } from "./misc.js";
const {
builders: { line, hardline, softline, group, indent, ifBreak },
utils: { removeLines, willBreak },
} = doc;
function printFunctionParameters(
path,
print,
+12 -16
View File
@@ -1,4 +1,4 @@
/** @typedef {import("../../document/doc-builders").Doc} Doc */
/** @typedef {import("../../document/builders").Doc} Doc */
import assert from "node:assert";
import {
@@ -7,7 +7,17 @@ import {
} from "../../main/comments.js";
import getLast from "../../utils/get-last.js";
import { getNextNonSpaceNonCommentCharacterIndex } from "../../common/util.js";
import doc from "../../document/index.js";
import {
line,
softline,
group,
indent,
ifBreak,
hardline,
join,
indentIfBreak,
} from "../../document/builders.js";
import { removeLines, willBreak } from "../../document/utils.js";
import { ArgExpansionBailout } from "../../common/errors.js";
import {
getFunctionParameters,
@@ -36,20 +46,6 @@ import {
import { printPropertyKey } from "./property.js";
import { printFunctionTypeParameters } from "./misc.js";
const {
builders: {
line,
softline,
group,
indent,
ifBreak,
hardline,
join,
indentIfBreak,
},
utils: { removeLines, willBreak },
} = doc;
function printFunction(path, print, options, args) {
const node = path.getValue();
+7 -5
View File
@@ -1,8 +1,10 @@
import doc from "../../document/index.js";
const {
builders: { join, line, group, softline, indent },
} = doc;
import {
join,
line,
group,
softline,
indent,
} from "../../document/builders.js";
function printHtmlBinding(path, options, print) {
const node = path.getValue();
+1 -5
View File
@@ -1,13 +1,9 @@
import { isNonEmptyArray } from "../../common/util.js";
import doc from "../../document/index.js";
import { join, line, group, indent, ifBreak } from "../../document/builders.js";
import { hasComment, identity, CommentCheckFlags } from "../utils/index.js";
import { getTypeParametersGroupId } from "./type-parameters.js";
import { printTypeScriptModifiers } from "./misc.js";
const {
builders: { join, line, group, indent, ifBreak },
} = doc;
function printInterface(path, options, print) {
const node = path.getValue();
const parts = [];
+13 -17
View File
@@ -1,5 +1,17 @@
import { printComments, printDanglingComments } from "../../main/comments.js";
import doc from "../../document/index.js";
import {
line,
hardline,
softline,
group,
indent,
conditionalGroup,
fill,
ifBreak,
lineSuffixBoundary,
join,
} from "../../document/builders.js";
import { willBreak } from "../../document/utils.js";
import { getLast, getPreferredQuote } from "../../common/util.js";
import {
@@ -16,22 +28,6 @@ import {
import pathNeedsParens from "../needs-parens.js";
import { willPrintOwnComments } from "../comments.js";
const {
builders: {
line,
hardline,
softline,
group,
indent,
conditionalGroup,
fill,
ifBreak,
lineSuffixBoundary,
join,
},
utils: { willBreak },
} = doc;
const isEmptyStringOrAnyLine = (doc) =>
doc === "" || doc === line || doc === hardline || doc === softline;
+10 -14
View File
@@ -19,7 +19,16 @@ import {
} from "../utils/index.js";
import { locEnd } from "../loc.js";
import doc from "../../document/index.js";
import {
join,
hardline,
group,
indent,
conditionalGroup,
breakParent,
label,
} from "../../document/builders.js";
import { willBreak } from "../../document/utils.js";
import printCallArguments from "./call-arguments.js";
import { printMemberLookup } from "./member.js";
import {
@@ -28,19 +37,6 @@ import {
printBindExpressionCallee,
} from "./misc.js";
const {
builders: {
join,
hardline,
group,
indent,
conditionalGroup,
breakParent,
label,
},
utils: { willBreak },
} = doc;
// We detect calls on member expressions specially to format a
// common pattern better. The pattern we are looking for is this:
//
+1 -5
View File
@@ -1,4 +1,4 @@
import doc from "../../document/index.js";
import { softline, group, indent, label } from "../../document/builders.js";
import {
isNumericLiteral,
isMemberExpression,
@@ -6,10 +6,6 @@ import {
} from "../utils/index.js";
import { printOptionalToken } from "./misc.js";
const {
builders: { softline, group, indent, label },
} = doc;
function printMemberExpression(path, options, print) {
const node = path.getValue();
+1 -5
View File
@@ -1,11 +1,7 @@
import { isNonEmptyArray } from "../../common/util.js";
import doc from "../../document/index.js";
import { indent, join, line } from "../../document/builders.js";
import { isFlowAnnotationComment } from "../utils/index.js";
const {
builders: { indent, join, line },
} = doc;
function printOptionalToken(path) {
const node = path.getValue();
if (
+9 -5
View File
@@ -1,5 +1,13 @@
import { isNonEmptyArray } from "../../common/util.js";
import doc from "../../document/index.js";
import {
softline,
group,
indent,
join,
line,
ifBreak,
hardline,
} from "../../document/builders.js";
import { printDanglingComments } from "../../main/comments.js";
import {
@@ -16,10 +24,6 @@ import {
printDecoratorsBeforeExport,
} from "./decorators.js";
const {
builders: { softline, group, indent, join, line, ifBreak, hardline },
} = doc;
/**
* @typedef {import("../../document").Doc} Doc
*/
+8 -5
View File
@@ -1,5 +1,12 @@
import { printDanglingComments } from "../../main/comments.js";
import doc from "../../document/index.js";
import {
line,
softline,
group,
indent,
ifBreak,
hardline,
} from "../../document/builders.js";
import {
getLast,
hasNewlineInRange,
@@ -20,10 +27,6 @@ import { shouldHugFunctionParameters } from "./function-parameters.js";
import { shouldHugType } from "./type-annotation.js";
import { printHardlineAfterHeritage } from "./class.js";
const {
builders: { line, softline, group, indent, ifBreak, hardline },
} = doc;
/** @typedef {import("../../document").Doc} Doc */
function printObject(path, options, print) {
+1 -5
View File
@@ -1,4 +1,4 @@
import doc from "../../document/index.js";
import { hardline } from "../../document/builders.js";
import pathNeedsParens from "../needs-parens.js";
import {
getLeftSidePathName,
@@ -11,10 +11,6 @@ import {
} from "../utils/index.js";
import { shouldPrintParamsWithoutParens } from "./function.js";
const {
builders: { hardline },
} = doc;
/**
* @typedef {import("../../document").Doc} Doc
* @typedef {import("../../common/ast-path")} AstPath
+12 -16
View File
@@ -1,6 +1,17 @@
import getLast from "../../utils/get-last.js";
import { getStringWidth, getIndentSize } from "../../common/util.js";
import doc from "../../document/index.js";
import {
join,
hardline,
softline,
group,
indent,
align,
lineSuffixBoundary,
addAlignmentToDoc,
} from "../../document/builders.js";
import { printDocToString } from "../../document/printer.js";
import { mapDoc } from "../../document/utils.js";
import {
isBinaryish,
isJestEachTemplateLiteral,
@@ -9,21 +20,6 @@ import {
isMemberExpression,
} from "../utils/index.js";
const {
builders: {
join,
hardline,
softline,
group,
indent,
align,
lineSuffixBoundary,
addAlignmentToDoc,
},
printer: { printDocToString },
utils: { mapDoc },
} = doc;
function printTemplateLiteral(path, print, options) {
const node = path.getValue();
const isTemplateLiteral = node.type === "TemplateLiteral";
+10 -14
View File
@@ -7,20 +7,16 @@ import {
} from "../utils/index.js";
import { locStart, locEnd } from "../loc.js";
import isBlockComment from "../utils/is-block-comment.js";
import doc from "../../document/index.js";
const {
builders: {
line,
softline,
group,
indent,
align,
ifBreak,
dedent,
breakParent,
},
} = doc;
import {
line,
softline,
group,
indent,
align,
ifBreak,
dedent,
breakParent,
} from "../../document/builders.js";
/**
* @typedef {import("../../document").Doc} Doc
+9 -5
View File
@@ -1,6 +1,14 @@
import { printComments, printDanglingComments } from "../../main/comments.js";
import { isNonEmptyArray } from "../../common/util.js";
import doc from "../../document/index.js";
import {
group,
join,
line,
softline,
indent,
align,
ifBreak,
} from "../../document/builders.js";
import pathNeedsParens from "../needs-parens.js";
import { locStart } from "../loc.js";
import {
@@ -17,10 +25,6 @@ import {
} from "./function-parameters.js";
import { printArrayItems } from "./array.js";
const {
builders: { group, join, line, softline, indent, align, ifBreak },
} = doc;
function shouldHugType(node) {
if (isSimpleType(node) || isObjectType(node)) {
return true;
+9 -5
View File
@@ -1,5 +1,13 @@
import { printDanglingComments } from "../../main/comments.js";
import doc from "../../document/index.js";
import {
join,
line,
hardline,
softline,
group,
indent,
ifBreak,
} from "../../document/builders.js";
import {
isTestCall,
hasComment,
@@ -13,10 +21,6 @@ import { createGroupIdMapper } from "../../common/util.js";
import { shouldHugType } from "./type-annotation.js";
import { isArrowFunctionVariableDeclarator } from "./assignment.js";
const {
builders: { join, line, hardline, softline, group, indent, ifBreak },
} = doc;
const getTypeParametersGroupId = createGroupIdMapper("typeParameters");
function printTypeParameters(path, options, print, paramsKey) {
+10 -14
View File
@@ -1,6 +1,15 @@
import { printDanglingComments } from "../../main/comments.js";
import { hasNewlineInRange } from "../../common/util.js";
import doc from "../../document/index.js";
import {
join,
line,
hardline,
softline,
group,
indent,
conditionalGroup,
ifBreak,
} from "../../document/builders.js";
import {
isLiteral,
getTypeScriptMappedTypeModifier,
@@ -35,19 +44,6 @@ import {
printIndexedAccessType,
} from "./type-annotation.js";
const {
builders: {
join,
line,
hardline,
softline,
group,
indent,
conditionalGroup,
ifBreak,
},
} = doc;
function printTypescript(path, options, print) {
const node = path.getValue();
+1 -5
View File
@@ -1,10 +1,6 @@
import doc from "../document/index.js";
import { hardline, indent, join } from "../document/builders.js";
import preprocess from "./print-preprocess.js";
const {
builders: { hardline, indent, join },
} = doc;
function genericPrint(path, options, print) {
const node = path.getValue();
switch (node.type) {
+9 -6
View File
@@ -3,7 +3,15 @@
// TODO(azz): anything that imports from main shouldn't be in a `language-*` dir.
import { printDanglingComments } from "../main/comments.js";
import { hasNewline } from "../common/util.js";
import doc from "../document/index.js";
import {
join,
line,
hardline,
softline,
group,
indent,
} from "../document/builders.js";
import { replaceTextEndOfLine } from "../document/utils.js";
import embed from "./embed.js";
import clean from "./clean.js";
import { insertPragma } from "./pragma.js";
@@ -79,11 +87,6 @@ import { printComment } from "./print/comment.js";
import { printLiteral } from "./print/literal.js";
import { printDecorators } from "./print/decorators.js";
const {
builders: { join, line, hardline, softline, group, indent },
utils: { replaceTextEndOfLine },
} = doc;
function genericPrint(path, options, print, args) {
const printed = printPathNoParens(path, options, print, args);
if (!printed) {
+2 -6
View File
@@ -2,15 +2,11 @@ import {
inferParserByLanguage,
getMaxContinuousCount,
} from "../common/util.js";
import doc from "../document/index.js";
import { hardline, markAsRoot } from "../document/builders.js";
import { replaceEndOfLine } from "../document/utils.js";
import printFrontMatter from "../utils/front-matter/print.js";
import { getFencedCodeBlockValue } from "./utils.js";
const {
builders: { hardline, markAsRoot },
utils: { replaceEndOfLine },
} = doc;
function embed(path, print, textToDoc, options) {
const node = path.getValue();
+17 -21
View File
@@ -5,7 +5,23 @@ import {
getStringWidth,
isNonEmptyArray,
} from "../common/util.js";
import doc from "../document/index.js";
import {
breakParent,
join,
line,
literalline,
markAsRoot,
hardline,
softline,
ifBreak,
fill,
align,
indent,
group,
hardlineWithoutBreakParent,
} from "../document/builders.js";
import { normalizeDoc, replaceTextEndOfLine } from "../document/utils.js";
import { printDocToString } from "../document/printer.js";
import embed from "./embed.js";
import { insertPragma } from "./pragma.js";
import { locStart, locEnd } from "./loc.js";
@@ -21,26 +37,6 @@ import {
isAutolink,
} from "./utils.js";
const {
builders: {
breakParent,
join,
line,
literalline,
markAsRoot,
hardline,
softline,
ifBreak,
fill,
align,
indent,
group,
hardlineWithoutBreakParent,
},
utils: { normalizeDoc, replaceTextEndOfLine },
printer: { printDocToString },
} = doc;
/**
* @typedef {import("../document").Doc} Doc
*/
+11 -15
View File
@@ -1,6 +1,16 @@
/** @typedef {import("../../document").Doc} Doc */
import doc from "../../document/index.js";
import {
dedent,
dedentToRoot,
fill,
hardline,
join,
line,
literalline,
markAsRoot,
} from "../../document/builders.js";
import { getDocParts } from "../../document/utils.js";
import {
getAncestorCount,
getBlockValueLineContents,
@@ -10,20 +20,6 @@ import {
} from "../utils.js";
import { alignWithSpaces } from "./misc.js";
const {
builders: {
dedent,
dedentToRoot,
fill,
hardline,
join,
line,
literalline,
markAsRoot,
},
utils: { getDocParts },
} = doc;
function printBlock(path, print, options) {
const node = path.getValue();
const parentIndent = getAncestorCount(path, (ancestorNode) =>
@@ -1,11 +1,13 @@
import doc from "../../document/index.js";
import {
ifBreak,
line,
softline,
hardline,
join,
} from "../../document/builders.js";
import { isEmptyNode, getLast, hasEndComments } from "../utils.js";
import { printNextEmptyLine, alignWithSpaces } from "./misc.js";
const {
builders: { ifBreak, line, softline, hardline, join },
} = doc;
function printFlowMapping(path, print, options) {
const node = path.getValue();
const isMapping = node.type === "flowMapping";
+8 -5
View File
@@ -1,6 +1,13 @@
/** @typedef {import("../../document").Doc} Doc */
import doc from "../../document/index.js";
import {
conditionalGroup,
group,
hardline,
ifBreak,
join,
line,
} from "../../document/builders.js";
import {
hasLeadingComments,
hasMiddleComments,
@@ -12,10 +19,6 @@ import {
} from "../utils.js";
import { alignWithSpaces } from "./misc.js";
const {
builders: { conditionalGroup, group, hardline, ifBreak, join, line },
} = doc;
function printMappingItem(node, parentNode, path, print, options) {
const { key, value } = node;
+1 -5
View File
@@ -1,10 +1,6 @@
import doc from "../../document/index.js";
import { softline, align } from "../../document/builders.js";
import { hasEndComments, isNextLineEmpty, isNode } from "../utils.js";
const {
builders: { softline, align },
} = doc;
const printedEmptyLineCache = new WeakMap();
function printNextEmptyLine(path, originalText) {
const node = path.getValue();
+11 -15
View File
@@ -1,6 +1,16 @@
/** @typedef {import("../document").Doc} Doc */
import doc from "../document/index.js";
import {
breakParent,
fill,
group,
hardline,
join,
line,
lineSuffix,
literalline,
} from "../document/builders.js";
import { getDocParts, replaceTextEndOfLine } from "../document/utils.js";
import { isPreviousLineEmpty } from "../common/util.js";
import { insertPragma, isPragma } from "./pragma.js";
import { locStart } from "./loc.js";
@@ -30,20 +40,6 @@ import {
import printMappingItem from "./print/mapping-item.js";
import printBlock from "./print/block.js";
const {
builders: {
breakParent,
fill,
group,
hardline,
join,
line,
lineSuffix,
literalline,
},
utils: { getDocParts, replaceTextEndOfLine },
} = doc;
function genericPrint(path, options, print) {
const node = path.getValue();
/** @type {Doc[]} */
+2 -6
View File
@@ -1,13 +1,9 @@
import AstPath from "../common/ast-path.js";
import doc from "../document/index.js";
import { hardline, addAlignmentToDoc } from "../document/builders.js";
import { propagateBreaks } from "../document/utils.js";
import { printComments } from "./comments.js";
import { printSubtree } from "./multiparser.js";
const {
builders: { hardline, addAlignmentToDoc },
utils: { propagateBreaks },
} = doc;
/**
* Takes an abstract syntax tree (AST) and recursively converts it to a
* document (series of printing primitives).
+9 -5
View File
@@ -1,6 +1,14 @@
import assert from "node:assert";
import doc from "../document/index.js";
import {
line,
hardline,
breakParent,
indent,
lineSuffix,
join,
cursor,
} from "../document/builders.js";
import {
hasNewline,
@@ -12,10 +20,6 @@ import {
addTrailingComment,
} from "../common/util.js";
const {
builders: { line, hardline, breakParent, indent, lineSuffix, join, cursor },
} = doc;
const childNodesCache = new WeakMap();
function getSortedChildNodes(node, options, resultArray) {
if (!node) {
+2 -6
View File
@@ -1,6 +1,7 @@
import { diffArrays } from "diff";
import doc from "../document/index.js";
import { printDocToString } from "../document/printer.js";
import { printDocToDebug } from "../document/debug.js";
import { getAlignmentSize } from "../common/util.js";
import {
guessEndOfLine,
@@ -15,11 +16,6 @@ import { parse, resolveParser } from "./parser.js";
import printAstToDoc from "./ast-to-doc.js";
import { calculateRange, findNodeAtOffset } from "./range-util.js";
const {
printer: { printDocToString },
debug: { printDocToDebug },
} = doc;
const BOM = "\uFEFF";
const CURSOR = Symbol("cursor");
+2 -6
View File
@@ -1,12 +1,8 @@
import doc from "../document/index.js";
import { stripTrailingHardline } from "../document/utils.js";
import { normalize } from "./options.js";
import { ensureAllCommentsPrinted, attach } from "./comments.js";
import { parse } from "./parser.js";
const {
utils: { stripTrailingHardline },
} = doc;
function printSubtree(path, print, options, printAstToDoc) {
if (options.printer.embed && options.embeddedLanguageFormatting === "auto") {
return options.printer.embed(
@@ -59,7 +55,7 @@ function textToDoc(
ensureAllCommentsPrinted(astComments);
if (shouldStripTrailingHardline) {
// TODO: move this to `stripTrailingHardline` function in `/src/document/doc-utils.js`
// TODO: move this to `stripTrailingHardline` function in `/src/document/utils.js`
if (typeof doc === "string") {
return doc.replace(/(?:\r?\n)*$/, "");
}
+1 -5
View File
@@ -1,8 +1,4 @@
import doc from "../../document/index.js";
const {
builders: { hardline, markAsRoot },
} = doc;
import { hardline, markAsRoot } from "../../document/builders.js";
function print(node, textToDoc) {
if (node.lang === "yaml") {