From 5979697fb5dd9b95bf834811f8b77e5ccbe33d8e Mon Sep 17 00:00:00 2001 From: "sirui.csr" Date: Fri, 7 Aug 2020 20:56:37 +0800 Subject: [PATCH 1/2] fix: substracted css-variable from zero --- src/__tests__/index.js | 7 +++++++ src/lib/reducer.js | 13 ++++++++++++- src/lib/transform.js | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/__tests__/index.js b/src/__tests__/index.js index b656c61..d40e636 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -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, diff --git a/src/lib/reducer.js b/src/lib/reducer.js index dc50560..a2ae69d 100644 --- a/src/lib/reducer.js +++ b/src/lib/reducer.js @@ -62,7 +62,7 @@ function reduceAddSubExpression(node, precision) { isValueType(node.left.type) && node.left.value === 0 && node.operator === "-" && - node.right.type !== "Function" + !containCSSFunction(node.right) ) { return flipValue(node.right); } @@ -330,3 +330,14 @@ function reduce(node, precision) { } export default reduce; + + +function containCSSFunction(node) { + if (node.type === "Function") { + return true; + } + if (node.type === "MathExpression") { + return containCSSFunction(node.left) || containCSSFunction(node.right); + } + return false; +} diff --git a/src/lib/transform.js b/src/lib/transform.js index 73fc545..2436ed4 100644 --- a/src/lib/transform.js +++ b/src/lib/transform.js @@ -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'; From f91f34d91974f001e9d9c09c663722b5bc746d0a Mon Sep 17 00:00:00 2001 From: "sirui.csr" Date: Fri, 7 Aug 2020 21:13:20 +0800 Subject: [PATCH 2/2] rename containCSSFunction to isCSSFunction --- src/lib/reducer.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/reducer.js b/src/lib/reducer.js index a2ae69d..277a394 100644 --- a/src/lib/reducer.js +++ b/src/lib/reducer.js @@ -62,7 +62,7 @@ function reduceAddSubExpression(node, precision) { isValueType(node.left.type) && node.left.value === 0 && node.operator === "-" && - !containCSSFunction(node.right) + !isCSSFunction(node.right) ) { return flipValue(node.right); } @@ -332,12 +332,12 @@ function reduce(node, precision) { export default reduce; -function containCSSFunction(node) { +function isCSSFunction(node) { if (node.type === "Function") { return true; } if (node.type === "MathExpression") { - return containCSSFunction(node.left) || containCSSFunction(node.right); + return isCSSFunction(node.left) || isCSSFunction(node.right); } return false; }