Skip to content

Commit

Permalink
Fix a parenthesizing bug in calculations (#1482)
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Sep 14, 2021
1 parent 5e4bc45 commit 7de7ab4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
is a `:host` or `:host-context` and the other is a selector that's guaranteed
to be within the current shadow DOM. The `@extend` logic has been updated
accordingly as well.


* Fix a bug where the right-hand operand of a `-` in a calculation could
incorrectly be stripped of parentheses.

### Dart API

* `SassCalculation.plus()` now allows `SassString` arguments.
Expand Down
15 changes: 13 additions & 2 deletions lib/src/visitor/serialize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,25 @@ class _SerializeVisitor
var right = value.right;
var parenthesizeRight = right is CalculationInterpolation ||
(right is CalculationOperation &&
(right.operator.precedence < value.operator.precedence ||
value.operator == CalculationOperator.dividedBy));
_parenthesizeCalculationRhs(value.operator, right.operator));
if (parenthesizeRight) _buffer.writeCharCode($lparen);
_writeCalculationValue(right);
if (parenthesizeRight) _buffer.writeCharCode($rparen);
}
}

/// Returns whether the right-hand operation of a calculation should be
/// parenthesized.
///
/// In `a ? (b # c)`, `outer` is `?` and `right` is `#`.
bool _parenthesizeCalculationRhs(
CalculationOperator outer, CalculationOperator right) {
if (outer == CalculationOperator.dividedBy) return true;
if (outer == CalculationOperator.plus) return false;
return right == CalculationOperator.plus ||
right == CalculationOperator.minus;
}

void visitColor(SassColor value) {
// In compressed mode, emit colors in the shortest representation possible.
if (_isCompressed && fuzzyEquals(value.alpha, 1)) {
Expand Down

0 comments on commit 7de7ab4

Please sign in to comment.