Skip to content

Commit 18bb47b

Browse files
authored
fix: preserve calc when extra parentheses are used (#187)
* fix: preserve calc when extra parentheses are used * fix: ensure extra parentheses are removed
1 parent bf8554a commit 18bb47b

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/lib/stringifier.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,17 @@ module.exports = function (calc, node, originalValue, options, result, item) {
7373
let str = stringify(node, options.precision);
7474

7575
const shouldPrintCalc =
76-
node.type === 'MathExpression' || node.type === 'Function';
76+
node.type === 'MathExpression' || node.type === 'Function' ||
77+
node.type === 'ParenthesizedExpression';
7778

7879
if (shouldPrintCalc) {
7980
// if calc expression couldn't be resolved to a single value, re-wrap it as
8081
// a calc()
81-
str = `${calc}(${str})`;
82+
if (node.type === 'ParenthesizedExpression') {
83+
str = `${calc}${str}`;
84+
} else {
85+
str = `${calc}(${str})`;
86+
}
8287

8388
// if the warnWhenCannotResolve option is on, inform the user that the calc
8489
// expression could not be resolved to a single value

test/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,14 @@ test(
768768
)
769769
);
770770

771+
test(
772+
'should preserve calc when extra parentheses are used',
773+
testValue(
774+
'calc((var(--circumference) / var(--number-of-segments)))',
775+
'calc(var(--circumference)/var(--number-of-segments))'
776+
)
777+
);
778+
771779
test('precision for calc', testValue('calc(100% / 3 * 3)', '100%'));
772780

773781
test(

0 commit comments

Comments
 (0)