Skip to content

Commit

Permalink
change approach
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyab committed Mar 13, 2023
1 parent 2fcf543 commit 4d4d2a6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 34 deletions.
30 changes: 2 additions & 28 deletions src/language-css/parser-postcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ const isSCSS = require("./utils/is-scss.js");
const isSCSSNestedPropertyNode = require("./utils/is-scss-nested-property-node.js");
const isSCSSVariable = require("./utils/is-scss-variable.js");
const isModuleRuleName = require("./utils/is-module-rule-name.js");

const getHighestAncestor = (node) => {
while (node.parent) {
node = node.parent;
}
return node;
};
const getHighestAncestor = require("./utils/get-highest-ancestor.js");

function parseValueNode(valueNode, options) {
const { nodes } = valueNode;
Expand Down Expand Up @@ -156,24 +150,6 @@ function flattenGroups(node) {
return node;
}

function purgeNodes(node) {
if (!node || typeof node !== "object") {
return;
}
if (node.type?.startsWith("selector-")) {
return;
}
if ("nodes" in node) {
delete node.nodes;
}
for (const key in node) {
if (key === "parent") {
continue;
}
purgeNodes(node[key]);
}
}

function addTypePrefix(node, prefix, skipPrefix) {
if (node && typeof node === "object") {
delete node.parent;
Expand Down Expand Up @@ -212,10 +188,10 @@ function parseNestedValue(node, options) {
parseNestedValue(node[key], options);
if (key === "nodes") {
node.group = flattenGroups(parseValueNode(node, options));
delete node[key];
}
}
}
delete node.parent;
}
return node;
}
Expand All @@ -238,8 +214,6 @@ function parseValue(value, options) {

const parsedResult = parseNestedValue(result, options);

purgeNodes(parsedResult);

return addTypePrefix(parsedResult, "value-", /^selector-/);
}

Expand Down
10 changes: 10 additions & 0 deletions src/language-css/utils/get-highest-ancestor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

const getHighestAncestor = (node) => {
while (node.parent) {
node = node.parent;
}
return node;
};

module.exports = getHighestAncestor;
13 changes: 7 additions & 6 deletions src/language-css/utils/stringify-func-param.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"use strict";

const getHighestAncestor = require("./get-highest-ancestor.js");

/**
* @param {*} node
* @returns {string}
*/
function stringifyFuncParam(node) {
const text = node.toString();
const innerText = text
.trim()
.replace(new RegExp(`^${node.value}\\(`), "")
.replace(/\)$/, "")
.trim();
const text = getHighestAncestor(node).text.slice(
node.group.open.sourceIndex + 1,
node.group.close.sourceIndex
);
const innerText = text.trim();
return innerText;
}

Expand Down

0 comments on commit 4d4d2a6

Please sign in to comment.