Add print pre-check to CSS printer (#13398)
This commit is contained in:
1 parent
30f8825cb0
commit
8d86bc1a78
1 file changed
+21
-26
@@ -21,6 +21,8 @@ import {
|
||||
} from "../document/builders.js";
|
||||
import { removeLines, getDocParts } from "../document/utils.js";
|
||||
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
|
||||
import createPrintPreCheckFunction from "../utils/create-print-pre-check-function.js";
|
||||
import UnexpectedNodeError from "../utils/unexpected-node-error.js";
|
||||
import clean from "./clean.js";
|
||||
import embed from "./embed.js";
|
||||
import { insertPragma } from "./pragma.js";
|
||||
@@ -76,6 +78,9 @@ import {
|
||||
import { locStart, locEnd } from "./loc.js";
|
||||
import printUnit from "./utils/print-unit.js";
|
||||
|
||||
const getVisitorKeys = createGetVisitorKeys(visitorKeys);
|
||||
const ensurePrintingNode = createPrintPreCheckFunction(getVisitorKeys);
|
||||
|
||||
function shouldPrintComma(options) {
|
||||
return options.trailingComma === "es5" || options.trailingComma === "all";
|
||||
}
|
||||
@@ -83,8 +88,8 @@ function shouldPrintComma(options) {
|
||||
function genericPrint(path, options, print) {
|
||||
const node = path.getValue();
|
||||
|
||||
if (typeof node === "string") {
|
||||
return node;
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
ensurePrintingNode(path);
|
||||
}
|
||||
|
||||
switch (node.type) {
|
||||
@@ -139,10 +144,9 @@ function genericPrint(path, options, print) {
|
||||
const isColon = trimmedBetween === ":";
|
||||
const isValueAllSpace =
|
||||
typeof node.value === "string" && /^ *$/.test(node.value);
|
||||
let value = typeof node.value === "string" ? node.value : print("value");
|
||||
|
||||
let value = hasComposesNode(node)
|
||||
? removeLines(print("value"))
|
||||
: print("value");
|
||||
value = hasComposesNode(node) ? removeLines(value) : value;
|
||||
|
||||
if (!isColon && lastLineHasInlineComment(trimmedBetween)) {
|
||||
value = indent([hardline, dedent(value)]);
|
||||
@@ -208,7 +212,7 @@ function genericPrint(path, options, print) {
|
||||
if (node.function) {
|
||||
return [
|
||||
node.name,
|
||||
print("params"),
|
||||
typeof node.params === "string" ? node.params : print("params"),
|
||||
isTemplatePlaceholderNodeWithoutSemiColon ? "" : ";",
|
||||
];
|
||||
}
|
||||
@@ -264,7 +268,7 @@ function genericPrint(path, options, print) {
|
||||
? hardline
|
||||
: " "
|
||||
: " ",
|
||||
print("params"),
|
||||
typeof node.params === "string" ? node.params : print("params"),
|
||||
]
|
||||
: "",
|
||||
node.selector ? indent([" ", print("selector")]) : "",
|
||||
@@ -863,6 +867,10 @@ function genericPrint(path, options, print) {
|
||||
}
|
||||
case "value-paren_group": {
|
||||
const parentNode = path.getParentNode();
|
||||
const printedGroups = path.map(() => {
|
||||
const child = path.getValue();
|
||||
return typeof child === "string" ? child : print();
|
||||
}, "groups");
|
||||
|
||||
if (
|
||||
parentNode &&
|
||||
@@ -876,23 +884,13 @@ function genericPrint(path, options, print) {
|
||||
) {
|
||||
return [
|
||||
node.open ? print("open") : "",
|
||||
join(",", path.map(print, "groups")),
|
||||
join(",", printedGroups),
|
||||
node.close ? print("close") : "",
|
||||
];
|
||||
}
|
||||
|
||||
if (!node.open) {
|
||||
const printed = path.map(print, "groups");
|
||||
const res = [];
|
||||
|
||||
for (let i = 0; i < printed.length; i++) {
|
||||
if (i !== 0) {
|
||||
res.push([",", line]);
|
||||
}
|
||||
res.push(printed[i]);
|
||||
}
|
||||
|
||||
return group(indent(fill(res)));
|
||||
return group(indent(fill(join([",", line], printedGroups))));
|
||||
}
|
||||
|
||||
const isSCSSMapItem = isSCSSMapItemNode(path, options);
|
||||
@@ -1009,11 +1007,6 @@ function genericPrint(path, options, print) {
|
||||
: line,
|
||||
];
|
||||
}
|
||||
// TODO: confirm this code is dead
|
||||
/* istanbul ignore next */
|
||||
case "value-comma": {
|
||||
return [node.value, " "];
|
||||
}
|
||||
case "value-string": {
|
||||
return printString(
|
||||
node.raws.quote + node.value + node.raws.quote,
|
||||
@@ -1029,9 +1022,11 @@ function genericPrint(path, options, print) {
|
||||
case "value-unknown": {
|
||||
return node.value;
|
||||
}
|
||||
|
||||
case "value-comma": // Handled in `value-comma_group`
|
||||
default:
|
||||
/* istanbul ignore next */
|
||||
throw new Error(`Unknown postcss type ${JSON.stringify(node.type)}`);
|
||||
throw new UnexpectedNodeError(node, "PostCSS");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1126,7 +1121,7 @@ const printer = {
|
||||
embed,
|
||||
insertPragma,
|
||||
massageAstNode: clean,
|
||||
getVisitorKeys: createGetVisitorKeys(visitorKeys),
|
||||
getVisitorKeys,
|
||||
};
|
||||
|
||||
export default printer;
|
||||
Reference in new issue
Block a user