Skip to content

Commit

Permalink
fix: incorrect reduction of subtraction from zero (#88) (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasminexie authored and evilebottnawi committed Oct 1, 2019
1 parent 29ff26e commit 6260789
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ test(
'calc((100vw - 50em)/2)',
);

test(
'should reduce additions and subtractions (5)',
testValue,
'calc(10px - (100vw - 50em) / 2)',
'calc(10px - (100vw - 50em)/2)',
);

test(
'should ignore value surrounding calc function (1)',
testValue,
Expand Down Expand Up @@ -360,6 +367,34 @@ test(
'calc(-1px + -1em)',
);

test(
'should reduce substracted expression from zero (1)',
testValue,
'calc( 0 - (100vw - 10px) / 2 )',
'calc((-100vw - -10px)/2)',
);

test(
'should reduce substracted expression from zero (2)',
testValue,
'calc( 0px - (100vw - 10px))',
'calc(-100vw - -10px)',
);

test(
'should reduce substracted expression from zero (3)',
testValue,
'calc( 0px - (100vw - 10px) * 2px )',
'calc((-100vw - -10px)*2px)',
);

test(
'should reduce substracted expression from zero (4)',
testValue,
'calc( 0px - (100vw + 10px))',
'calc(-100vw + -10px)',
);

test(
'should reduce nested expression',
testValue,
Expand Down
8 changes: 6 additions & 2 deletions src/lib/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ function flipValue(node) {
if (isValueType(node.type)) {
node.value = -node.value;
} else if (node.type === 'MathExpression') {
node.left = flipValue(node.left);
node.right = flipValue(node.right);
if (node.operator === '*' || node.operator === '/') {
node.left = flipValue(node.left);
} else {
node.left = flipValue(node.left);
node.right = flipValue(node.right);
}
}

return node;
Expand Down

0 comments on commit 6260789

Please sign in to comment.