Skip to content

Commit

Permalink
fix: substracted css-variable from zero (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
csr632 authored Aug 7, 2020
1 parent 295b1df commit 0282bdc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,13 @@ test(
'calc(-100vw + -10px)',
);

test(
'should reduce substracted expression from zero (css-variable)',
testValue,
'calc( 0px - (var(--foo, 4px) / 2))',
'calc(0px - var(--foo, 4px)/2)',
);

test(
'should reduce nested expression',
testValue,
Expand Down
13 changes: 12 additions & 1 deletion src/lib/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function reduceAddSubExpression(node, precision) {
isValueType(node.left.type) &&
node.left.value === 0 &&
node.operator === "-" &&
node.right.type !== "Function"
!isCSSFunction(node.right)
) {
return flipValue(node.right);
}
Expand Down Expand Up @@ -330,3 +330,14 @@ function reduce(node, precision) {
}

export default reduce;


function isCSSFunction(node) {
if (node.type === "Function") {
return true;
}
if (node.type === "MathExpression") {
return isCSSFunction(node.left) || isCSSFunction(node.right);
}
return false;
}
2 changes: 1 addition & 1 deletion src/lib/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import selectorParser from 'postcss-selector-parser';
import valueParser from 'postcss-value-parser';
import valueParser from 'postcss-value-parser';

// eslint-disable-next-line import/no-unresolved
import { parser } from '../parser';
Expand Down

0 comments on commit 0282bdc

Please sign in to comment.