Skip to content

Commit 7de7ab4

Browse files
authored
Fix a parenthesizing bug in calculations (#1482)
See sass/sass#3147 See sass/sass#3148
1 parent 5e4bc45 commit 7de7ab4

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
is a `:host` or `:host-context` and the other is a selector that's guaranteed
1111
to be within the current shadow DOM. The `@extend` logic has been updated
1212
accordingly as well.
13-
13+
14+
* Fix a bug where the right-hand operand of a `-` in a calculation could
15+
incorrectly be stripped of parentheses.
16+
1417
### Dart API
1518

1619
* `SassCalculation.plus()` now allows `SassString` arguments.

lib/src/visitor/serialize.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,14 +498,25 @@ class _SerializeVisitor
498498
var right = value.right;
499499
var parenthesizeRight = right is CalculationInterpolation ||
500500
(right is CalculationOperation &&
501-
(right.operator.precedence < value.operator.precedence ||
502-
value.operator == CalculationOperator.dividedBy));
501+
_parenthesizeCalculationRhs(value.operator, right.operator));
503502
if (parenthesizeRight) _buffer.writeCharCode($lparen);
504503
_writeCalculationValue(right);
505504
if (parenthesizeRight) _buffer.writeCharCode($rparen);
506505
}
507506
}
508507

508+
/// Returns whether the right-hand operation of a calculation should be
509+
/// parenthesized.
510+
///
511+
/// In `a ? (b # c)`, `outer` is `?` and `right` is `#`.
512+
bool _parenthesizeCalculationRhs(
513+
CalculationOperator outer, CalculationOperator right) {
514+
if (outer == CalculationOperator.dividedBy) return true;
515+
if (outer == CalculationOperator.plus) return false;
516+
return right == CalculationOperator.plus ||
517+
right == CalculationOperator.minus;
518+
}
519+
509520
void visitColor(SassColor value) {
510521
// In compressed mode, emit colors in the shortest representation possible.
511522
if (_isCompressed && fuzzyEquals(value.alpha, 1)) {

0 commit comments

Comments
 (0)