Skip to content

Commit

Permalink
Properly migrate negated parenthesized expressions (#193)
Browse files Browse the repository at this point in the history
Fixes #192.
  • Loading branch information
jathak authored May 26, 2021
1 parent f39df79 commit acce840
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.4.2

### Division Migrator

* Fix a bug where negated division could be migrated incorrectly.

## 1.4.1

* Globs containing `**` should now be properly resolved when running on Node.
Expand Down
21 changes: 18 additions & 3 deletions lib/src/migrators/division.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,16 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
/// Allows division within this parenthesized expression.
///
/// If these parentheses contain a `/` operation that is migrated to a
/// function call, the now-unnecessary parentheses will be removed.
/// function call and [negated] is false, the now-unnecessary parentheses
/// will be removed.
@override
void visitParenthesizedExpression(ParenthesizedExpression node) {
void visitParenthesizedExpression(ParenthesizedExpression node,
{bool negated = false}) {
_withContext(() {
var expression = node.expression;
if (expression is BinaryOperationExpression &&
expression.operator == BinaryOperator.dividedBy) {
if (_visitSlashOperation(expression)) {
if (_visitSlashOperation(expression) && !negated) {
addPatch(patchDelete(node.span, end: 1));
addPatch(patchDelete(node.span, start: node.span.length - 1));
}
Expand All @@ -187,6 +189,19 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
}, isDivisionAllowed: true);
}

/// Sets [_negatedParenthesized] to true when about to visit a negated
/// parenthesized expression.
@override
void visitUnaryOperationExpression(UnaryOperationExpression node) {
var operand = node.operand;
if (node.operator == UnaryOperator.minus &&
operand is ParenthesizedExpression) {
visitParenthesizedExpression(operand, negated: true);
return;
}
super.visitUnaryOperationExpression(node);
}

/// Allows division within this return rule.
@override
void visitReturnRule(ReturnRule node) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sass_migrator
version: 1.4.1
version: 1.4.2
description: A tool for running migrations on Sass files
author: Jennifer Thakar <[email protected]>
homepage: https://github.com/sass/migrator
Expand Down
10 changes: 10 additions & 0 deletions test/migrators/division/always_division.hrx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ a {
o: identity(6px / 3px);
p: rgba(10, 20, 30/2, 0.5);
q: rgb(10 20 30/2 / 0.5);

// Negation
r: -(4/3);
s: (-4/3);
t: -((-6/3) / -(4/2));
}

<==> output/entrypoint.scss
Expand Down Expand Up @@ -71,4 +76,9 @@ a {
o: identity(math.div(6px, 3px));
p: rgba(10, 20, math.div(30, 2), 0.5);
q: rgb(10, 20, math.div(30, 2), 0.5);

// Negation
r: -(math.div(4, 3));
s: math.div(-4, 3);
t: -(math.div(math.div(-6, 3), -(math.div(4, 2))));
}

0 comments on commit acce840

Please sign in to comment.