Skip to content

Commit 6260789

Browse files
jasminexieevilebottnawi
authored andcommitted
fix: incorrect reduction of subtraction from zero (postcss#88) (postcss#93)
1 parent 29ff26e commit 6260789

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/__tests__/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ test(
118118
'calc((100vw - 50em)/2)',
119119
);
120120

121+
test(
122+
'should reduce additions and subtractions (5)',
123+
testValue,
124+
'calc(10px - (100vw - 50em) / 2)',
125+
'calc(10px - (100vw - 50em)/2)',
126+
);
127+
121128
test(
122129
'should ignore value surrounding calc function (1)',
123130
testValue,
@@ -360,6 +367,34 @@ test(
360367
'calc(-1px + -1em)',
361368
);
362369

370+
test(
371+
'should reduce substracted expression from zero (1)',
372+
testValue,
373+
'calc( 0 - (100vw - 10px) / 2 )',
374+
'calc((-100vw - -10px)/2)',
375+
);
376+
377+
test(
378+
'should reduce substracted expression from zero (2)',
379+
testValue,
380+
'calc( 0px - (100vw - 10px))',
381+
'calc(-100vw - -10px)',
382+
);
383+
384+
test(
385+
'should reduce substracted expression from zero (3)',
386+
testValue,
387+
'calc( 0px - (100vw - 10px) * 2px )',
388+
'calc((-100vw - -10px)*2px)',
389+
);
390+
391+
test(
392+
'should reduce substracted expression from zero (4)',
393+
testValue,
394+
'calc( 0px - (100vw + 10px))',
395+
'calc(-100vw + -10px)',
396+
);
397+
363398
test(
364399
'should reduce nested expression',
365400
testValue,

src/lib/reducer.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ function flipValue(node) {
3434
if (isValueType(node.type)) {
3535
node.value = -node.value;
3636
} else if (node.type === 'MathExpression') {
37-
node.left = flipValue(node.left);
38-
node.right = flipValue(node.right);
37+
if (node.operator === '*' || node.operator === '/') {
38+
node.left = flipValue(node.left);
39+
} else {
40+
node.left = flipValue(node.left);
41+
node.right = flipValue(node.right);
42+
}
3943
}
4044

4145
return node;

0 commit comments

Comments
 (0)