Skip to content

Commit 8ecaba5

Browse files
andyjanssonben-eb
authored andcommitted
Fix consecutive additions and substractions
1 parent 1de8227 commit 8ecaba5

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/__tests__/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ test(
4242
'1ex'
4343
)
4444

45+
test(
46+
'should reduce additions and subtractions (1)',
47+
testFixture,
48+
'calc(100% - 10px + 20px)',
49+
'calc(100% + 10px)'
50+
)
51+
52+
test(
53+
'should reduce additions and subtractions (2)',
54+
testFixture,
55+
'calc(100% + 10px - 20px)',
56+
'calc(100% - 10px)'
57+
)
58+
4559
test(
4660
'should ignore value surrounding calc function (1)',
4761
testFixture,

src/lib/reducer.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,32 @@ function reduceAddSubExpression(node, precision) {
113113
return reduce(node, precision)
114114
}
115115
// (something + value) + value => something + (value + value)
116-
// (something - value) + value => something - (value + value)
116+
// (something - value1) + value2 => something - (value2 - value1)
117117
// (something + value) - value => something + (value - value)
118118
// (something - value) - value => something - (value + value)
119119
else if (right.type === left.right.type) {
120120
node = Object.assign({ }, left)
121-
node.right = reduce({
122-
type: 'MathExpression',
123-
operator: op === left.operator && op === '-' ? '+' : op,
124-
left: left.right,
125-
right: right
126-
}, precision)
121+
if (left.operator === '-') {
122+
node.right = reduce({
123+
type: 'MathExpression',
124+
operator: op === left.operator ? '+' : '-',
125+
left: right,
126+
right: left.right
127+
}, precision)
128+
node.operator = op === left.operator ? '-' : '+';
129+
}
130+
else {
131+
node.right = reduce({
132+
type: 'MathExpression',
133+
operator: op,
134+
left: left.right,
135+
right: right
136+
}, precision)
137+
}
138+
if (node.right.value < 0) {
139+
node.right.value *= -1;
140+
node.operator = '-';
141+
}
127142
return reduce(node, precision)
128143
}
129144
}

0 commit comments

Comments
 (0)