Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a parenthesizing bug in calculations #1482

Merged
merged 2 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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