Refactor CSS utils (#12680)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-04-28 11:24:33 +08:00
1 parent 9fbdcda5b1
commit a1dee86841
4 files changed
+44 -91

No files matched your search

@@ -1,16 +1,9 @@
"use strict";
const isNonEmptyArray = require("../../utils/is-non-empty-array.js");
function hasStringOrFunction(groupList) {
if (isNonEmptyArray(groupList)) {
for (let i = 0; i < groupList.length; i++) {
if (groupList[i].type === "string" || groupList[i].type === "func") {
return true;
}
}
}
return false;
return groupList.some(
(group) => group.type === "string" || group.type === "func"
);
}
module.exports = hasStringOrFunction;
+30 -63
View File
@@ -51,24 +51,18 @@ function getAncestorNode(path, typeOrTypes) {
function getPropOfDeclNode(path) {
const declAncestorNode = getAncestorNode(path, "css-decl");
return (
declAncestorNode &&
declAncestorNode.prop &&
declAncestorNode.prop.toLowerCase()
);
return declAncestorNode?.prop?.toLowerCase();
}
const wideKeywords = new Set(["initial", "inherit", "unset", "revert"]);
function isWideKeywords(value) {
return ["initial", "inherit", "unset", "revert"].includes(
value.toLowerCase()
);
return wideKeywords.has(value.toLowerCase());
}
function isKeyframeAtRuleKeywords(path, value) {
const atRuleAncestorNode = getAncestorNode(path, "css-atrule");
return (
atRuleAncestorNode &&
atRuleAncestorNode.name &&
atRuleAncestorNode?.name &&
atRuleAncestorNode.name.toLowerCase().endsWith("keyframes") &&
["from", "to"].includes(value.toLowerCase())
);
@@ -88,22 +82,16 @@ function maybeToLowerCase(value) {
function insideValueFunctionNode(path, functionName) {
const funcAncestorNode = getAncestorNode(path, "value-func");
return (
funcAncestorNode &&
funcAncestorNode.value &&
funcAncestorNode.value.toLowerCase() === functionName
);
return funcAncestorNode?.value?.toLowerCase() === functionName;
}
function insideICSSRuleNode(path) {
const ruleAncestorNode = getAncestorNode(path, "css-rule");
const selector = ruleAncestorNode?.raws?.selector;
return (
ruleAncestorNode &&
ruleAncestorNode.raws &&
ruleAncestorNode.raws.selector &&
(ruleAncestorNode.raws.selector.startsWith(":import") ||
ruleAncestorNode.raws.selector.startsWith(":export"))
selector &&
(selector.startsWith(":import") || selector.startsWith(":export"))
);
}
@@ -124,8 +112,7 @@ function insideURLFunctionInImportAtRuleNode(path) {
const atRuleAncestorNode = getAncestorNode(path, "css-atrule");
return (
atRuleAncestorNode &&
atRuleAncestorNode.name === "import" &&
atRuleAncestorNode?.name === "import" &&
node.groups[0].value === "url" &&
node.groups.length === 2
);
@@ -136,28 +123,23 @@ function isURLFunctionNode(node) {
}
function isLastNode(path, node) {
const parentNode = path.getParentNode();
/* istanbul ignore next */
if (!parentNode) {
return false;
}
const { nodes } = parentNode;
const nodes = path.getParentNode()?.nodes;
return nodes && nodes.indexOf(node) === nodes.length - 1;
}
function isDetachedRulesetDeclarationNode(node) {
const { selector } = node;
// If a Less file ends up being parsed with the SCSS parser, Less
// variable declarations will be parsed as atrules with names ending
// with a colon, so keep the original case then.
/* istanbul ignore next */
if (!node.selector) {
if (!selector) {
return false;
}
return (
(typeof node.selector === "string" && /^@.+:.*$/.test(node.selector)) ||
(node.selector.value && /^@.+:.*$/.test(node.selector.value))
(typeof selector === "string" && /^@.+:.*$/.test(selector)) ||
(selector.value && /^@.+:.*$/.test(selector.value))
);
}
@@ -226,7 +208,7 @@ function isSCSSControlDirectiveNode(node) {
}
function isDetachedRulesetCallNode(node) {
return node.raws && node.raws.params && /^\(\s*\)$/.test(node.raws.params);
return node.raws?.params && /^\(\s*\)$/.test(node.raws.params);
}
function isTemplatePlaceholderNode(node) {
@@ -241,51 +223,42 @@ function isPostcssSimpleVarNode(currentNode, nextNode) {
return (
currentNode.value === "$$" &&
currentNode.type === "value-func" &&
nextNode &&
nextNode.type === "value-word" &&
nextNode?.type === "value-word" &&
!nextNode.raws.before
);
}
function hasComposesNode(node) {
return (
node.value &&
node.value.type === "value-root" &&
node.value.group &&
node.value.group.type === "value-value" &&
node.value?.type === "value-root" &&
node.value.group?.type === "value-value" &&
node.prop.toLowerCase() === "composes"
);
}
function hasParensAroundNode(node) {
return (
node.value &&
node.value.group &&
node.value.group.group &&
node.value.group.group.type === "value-paren_group" &&
node.value?.group?.group?.type === "value-paren_group" &&
node.value.group.group.open !== null &&
node.value.group.group.close !== null
);
}
function hasEmptyRawBefore(node) {
return node.raws && node.raws.before === "";
return node.raws?.before === "";
}
function isKeyValuePairNode(node) {
return (
node.type === "value-comma_group" &&
node.groups &&
node.groups[1] &&
node.groups[1].type === "value-colon"
node.groups?.[1]?.type === "value-colon"
);
}
function isKeyValuePairInParenGroupNode(node) {
return (
node.type === "value-paren_group" &&
node.groups &&
node.groups[0] &&
node.groups?.[0] &&
isKeyValuePairNode(node.groups[0])
);
}
@@ -311,7 +284,7 @@ function isSCSSMapItemNode(path) {
const declNode = getAncestorNode(path, "css-decl");
// SCSS map declaration (i.e. `$map: (key: value, other-key: other-value)`)
if (declNode && declNode.prop && declNode.prop.startsWith("$")) {
if (declNode?.prop?.startsWith("$")) {
return true;
}
@@ -349,7 +322,7 @@ function isWordNode(node) {
}
function isColonNode(node) {
return node && node.type === "value-colon";
return node?.type === "value-colon";
}
function isKeyInValuePairNode(node, parentNode) {
@@ -385,18 +358,15 @@ function lastLineHasInlineComment(text) {
function isAtWordPlaceholderNode(node) {
return (
node &&
node.type === "value-atword" &&
node?.type === "value-atword" &&
node.value.startsWith("prettier-placeholder-")
);
}
function isConfigurationNode(node, parentNode) {
if (
!node.open ||
node.open.value !== "(" ||
!node.close ||
node.close.value !== ")" ||
node.open?.value !== "(" ||
node.close?.value !== ")" ||
node.groups.some((group) => group.type !== "value-comma_group")
) {
return false;
@@ -405,8 +375,7 @@ function isConfigurationNode(node, parentNode) {
const prevIdx = parentNode.groups.indexOf(node) - 1;
const maybeWithNode = parentNode.groups[prevIdx];
if (
maybeWithNode &&
maybeWithNode.type === "value-word" &&
maybeWithNode?.type === "value-word" &&
maybeWithNode.value === "with"
) {
return true;
@@ -418,10 +387,8 @@ function isConfigurationNode(node, parentNode) {
function isParenGroupNode(node) {
return (
node.type === "value-paren_group" &&
node.open &&
node.open.value === "(" &&
node.close &&
node.close.value === ")"
node.open?.value === "(" &&
node.close?.value === ")"
);
}
+1 -1
View File
@@ -1,7 +1,7 @@
"use strict";
function isSCSSVariable(node) {
return Boolean(node && node.type === "word" && node.value.startsWith("$"));
return Boolean(node?.type === "word" && node.value.startsWith("$"));
}
module.exports = isSCSSVariable;
+10 -17
View File
@@ -2,29 +2,22 @@
function stringifyNode(node) {
if (node.groups) {
const open = node.open && node.open.value ? node.open.value : "";
const groups = node.groups.reduce(
(previousValue, currentValue, index) =>
previousValue +
stringifyNode(currentValue) +
(node.groups[0].type === "comma_group" &&
index !== node.groups.length - 1
? ","
: ""),
""
);
const close = node.close && node.close.value ? node.close.value : "";
const open = node.open?.value || "";
const groups = node.groups
.map((currentValue) => stringifyNode(currentValue))
.join(node.groups[0]?.type === "comma_group" ? "," : "");
const close = node.close?.value || "";
return open + groups + close;
}
const before = node.raws && node.raws.before ? node.raws.before : "";
const quote = node.raws && node.raws.quote ? node.raws.quote : "";
const before = node.raws?.before || "";
const quote = node.raws?.quote || "";
const atword = node.type === "atword" ? "@" : "";
const value = node.value ? node.value : "";
const unit = node.unit ? node.unit : "";
const value = node.value || "";
const unit = node.unit || "";
const group = node.group ? stringifyNode(node.group) : "";
const after = node.raws && node.raws.after ? node.raws.after : "";
const after = node.raws?.after || "";
return before + quote + atword + value + quote + unit + group + after;
}