Skip to content

Commit

Permalink
fix(UX-and-I#209) fixes issue with parsing of nested variables
Browse files Browse the repository at this point in the history
  • Loading branch information
fastndead committed Mar 18, 2024
1 parent 3f0f0cf commit 2d160cc
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions addon/src/parsers/postcss.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,36 +184,24 @@ function determineTokenValue(
): string {
rawValue = rawValue.replace(/!default/g, "").replace(/!global/g, "");

const cssVars = "(var\\(([a-zA-Z0-9-_]+)\\))";
const scssVars = "(\\$([a-zA-Z0-9-_]+))";
const lessVars = "(\\@([a-zA-Z0-9-_]+))";

const vars = [!preserveCSSVars && cssVars, scssVars, lessVars].filter(
Boolean
) as string[];

const referencedVariableResult = new RegExp(`^(${vars.join("|")})$`).exec(
rawValue
);

const referencedVariable =
referencedVariableResult?.[3] ||
referencedVariableResult?.[5] ||
referencedVariableResult?.[7];

if (referencedVariable) {
const value =
const regex = /\bvar\(([^)]+)\)|(\$[a-zA-Z0-9-_]+|@[a-zA-Z0-9-_]+)/g;
let match;
let replacedString = rawValue;
while ((match = regex.exec(rawValue)) !== null) {
const variableMatch = match[0];
const variableName = variableMatch.replace(/\(|\)|var\(|@|\$/g, "");
const replacement =
declarations.find(
(declaration) =>
declaration.prop === referencedVariable ||
declaration.prop === `$${referencedVariable}` ||
declaration.prop === `@${referencedVariable}`
declaration.prop === variableName ||
declaration.prop === `$${variableName}` ||
declaration.prop === `@${variableName}`
)?.value || "";

return determineTokenValue(value, declarations, preserveCSSVars);
replacedString = replacedString.replace(variableMatch, replacement);
}

return rawValue;
return replacedString;
}

async function getNodes(files: File[]): Promise<{
Expand Down

0 comments on commit 2d160cc

Please sign in to comment.