Skip to content

Commit c55bd7b

Browse files
committed
Fix a crash when trying to divide by a CSS variable.
1 parent 78dd513 commit c55bd7b

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/__tests__/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,27 @@ test(
126126
'calc(10px - 100px * var(--mouseX))'
127127
)
128128

129+
test(
130+
'should ignore calc with css variables (3)',
131+
testFixture,
132+
'calc(10px - (100px + var(--mouseX)))',
133+
'calc(-90px - var(--mouseX))'
134+
)
135+
136+
test(
137+
'should ignore calc with css variables (4)',
138+
testFixture,
139+
'calc(10px - (100px / var(--mouseX)))',
140+
'calc(10px - 100px / var(--mouseX))'
141+
)
142+
143+
test(
144+
'should ignore calc with css variables (5)',
145+
testFixture,
146+
'calc(10px - (100px - var(--mouseX)))',
147+
'calc(-90px - var(--mouseX))'
148+
)
149+
129150
test(
130151
'should reduce calc with newline characters',
131152
testFixture,

src/lib/reducer.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ function convertMathExpression(node, precision) {
6565
function reduceAddSubExpression(node, precision) {
6666
const {left, right, operator: op} = node
6767

68+
if (left.type === 'CssVariable' || right.type === 'CssVariable')
69+
return node
70+
6871
// something + 0 => something
6972
// something - 0 => something
7073
if (right.value === 0)
@@ -125,7 +128,7 @@ function reduceAddSubExpression(node, precision) {
125128
// (expr) <op> value
126129
if (
127130
left.type === 'MathExpression' &&
128-
(left.operator === '+' || left.operator === '-') &&
131+
(left.operator === '+' || left.operator === '-') &&
129132
isValueType(right.type)
130133
) {
131134
// (value + something) + value => (value + value) + something
@@ -176,7 +179,7 @@ function reduceAddSubExpression(node, precision) {
176179
}
177180

178181
function reduceDivisionExpression(node) {
179-
if (node.right.type === 'MathExpression')
182+
if (!isValueType(node.right.type))
180183
return node
181184

182185
if (node.right.type !== 'Value')
@@ -188,7 +191,7 @@ function reduceDivisionExpression(node) {
188191
// (expr) / value
189192
if (node.left.type === 'MathExpression') {
190193
if (
191-
isValueType(node.left.left.type) &&
194+
isValueType(node.left.left.type) &&
192195
isValueType(node.left.right.type)
193196
) {
194197
node.left.left.value /= node.right.value

0 commit comments

Comments
 (0)