Skip to content

Commit

Permalink
fix: preserve calc when extra parentheses are used (#187)
Browse files Browse the repository at this point in the history
* fix: preserve calc when extra parentheses are used

* fix: ensure extra parentheses are removed
  • Loading branch information
tannerdolby authored May 5, 2023
1 parent bf8554a commit 18bb47b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/lib/stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,17 @@ module.exports = function (calc, node, originalValue, options, result, item) {
let str = stringify(node, options.precision);

const shouldPrintCalc =
node.type === 'MathExpression' || node.type === 'Function';
node.type === 'MathExpression' || node.type === 'Function' ||
node.type === 'ParenthesizedExpression';

if (shouldPrintCalc) {
// if calc expression couldn't be resolved to a single value, re-wrap it as
// a calc()
str = `${calc}(${str})`;
if (node.type === 'ParenthesizedExpression') {
str = `${calc}${str}`;
} else {
str = `${calc}(${str})`;
}

// if the warnWhenCannotResolve option is on, inform the user that the calc
// expression could not be resolved to a single value
Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,14 @@ test(
)
);

test(
'should preserve calc when extra parentheses are used',
testValue(
'calc((var(--circumference) / var(--number-of-segments)))',
'calc(var(--circumference)/var(--number-of-segments))'
)
);

test('precision for calc', testValue('calc(100% / 3 * 3)', '100%'));

test(
Expand Down

0 comments on commit 18bb47b

Please sign in to comment.