Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove unnecessary parens around args to division
  • Loading branch information
jathak committed Apr 19, 2019
commit df71f3d4aea62261566958f71e56796d397f41e4
19 changes: 19 additions & 0 deletions lib/src/migrators/division.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
var numericResult = false;
if (_shouldMigrate(node)) {
addPatch(patchBefore(node, "divide("));
_patchParensIfAny(node.left);
_patchSlashToComma(node);
_patchParensIfAny(node.right);
addPatch(patchAfter(node, ")"));
numericResult = true;
}
Expand Down Expand Up @@ -90,7 +92,9 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
expression.operator == BinaryOperator.dividedBy) {
if (_shouldMigrate(expression)) {
addPatch(patchBefore(node, "divide"));
_patchParensIfAny(expression.left);
_patchSlashToComma(expression);
_patchParensIfAny(expression.right);
}
super.visitBinaryOperationExpression(expression);
} else {
Expand Down Expand Up @@ -203,6 +207,21 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
addPatch(Patch(start.file.span(start.offset, end.offset), ", "));
}

/// Adds patches removing unnecessary parentheses around [node] if it is a
/// ParenthesizedExpression.
void _patchParensIfAny(SassNode node) {
if (node is! ParenthesizedExpression) return;
var expression = (node as ParenthesizedExpression).expression;
if (expression is BinaryOperationExpression &&
expression.operator == BinaryOperator.dividedBy) {
return;
}
var start = node.span.start;
var end = node.span.end;
addPatch(Patch(start.file.span(start.offset, start.offset + 1), ""));
addPatch(Patch(start.file.span(end.offset - 1, end.offset), ""));
}

/// Runs [operation] with the given context.
void _withContext(void operation(),
{bool isDivisionAllowed, bool expectsNumericResult}) {
Expand Down
4 changes: 2 additions & 2 deletions test/migrators/division/always_division.hrx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ a {
}

a {
b: divide((4px + 2px), 3px);
b: divide(4px + 2px, 3px);
c: divide(6px, 3px) + 1;
d: divide(6px, 3px);
e: divide(divide(6px, (2px + 1px)), 2);
e: divide(divide(6px, 2px + 1px), 2);
$f: divide(6px, 3px);

// * and % always return numbers, so we can assume division here
Expand Down