diff --git a/CHANGELOG.md b/CHANGELOG.md index cf85e6e..4ee4893 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/src/migrators/division.dart b/lib/src/migrators/division.dart index 46fa535..75756a1 100644 --- a/lib/src/migrators/division.dart +++ b/lib/src/migrators/division.dart @@ -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)); } @@ -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) { diff --git a/pubspec.yaml b/pubspec.yaml index 6aa9af5..4f2bb1d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 homepage: https://github.com/sass/migrator diff --git a/test/migrators/division/always_division.hrx b/test/migrators/division/always_division.hrx index 2f7a2a7..dd9594c 100644 --- a/test/migrators/division/always_division.hrx +++ b/test/migrators/division/always_division.hrx @@ -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 @@ -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)))); }