Skip to content

Commit acce840

Browse files
authored
Properly migrate negated parenthesized expressions (#193)
Fixes #192.
1 parent f39df79 commit acce840

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 1.4.2
2+
3+
### Division Migrator
4+
5+
* Fix a bug where negated division could be migrated incorrectly.
6+
17
## 1.4.1
28

39
* Globs containing `**` should now be properly resolved when running on Node.

lib/src/migrators/division.dart

+18-3
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,16 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
170170
/// Allows division within this parenthesized expression.
171171
///
172172
/// If these parentheses contain a `/` operation that is migrated to a
173-
/// function call, the now-unnecessary parentheses will be removed.
173+
/// function call and [negated] is false, the now-unnecessary parentheses
174+
/// will be removed.
174175
@override
175-
void visitParenthesizedExpression(ParenthesizedExpression node) {
176+
void visitParenthesizedExpression(ParenthesizedExpression node,
177+
{bool negated = false}) {
176178
_withContext(() {
177179
var expression = node.expression;
178180
if (expression is BinaryOperationExpression &&
179181
expression.operator == BinaryOperator.dividedBy) {
180-
if (_visitSlashOperation(expression)) {
182+
if (_visitSlashOperation(expression) && !negated) {
181183
addPatch(patchDelete(node.span, end: 1));
182184
addPatch(patchDelete(node.span, start: node.span.length - 1));
183185
}
@@ -187,6 +189,19 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
187189
}, isDivisionAllowed: true);
188190
}
189191

192+
/// Sets [_negatedParenthesized] to true when about to visit a negated
193+
/// parenthesized expression.
194+
@override
195+
void visitUnaryOperationExpression(UnaryOperationExpression node) {
196+
var operand = node.operand;
197+
if (node.operator == UnaryOperator.minus &&
198+
operand is ParenthesizedExpression) {
199+
visitParenthesizedExpression(operand, negated: true);
200+
return;
201+
}
202+
super.visitUnaryOperationExpression(node);
203+
}
204+
190205
/// Allows division within this return rule.
191206
@override
192207
void visitReturnRule(ReturnRule node) {

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sass_migrator
2-
version: 1.4.1
2+
version: 1.4.2
33
description: A tool for running migrations on Sass files
44
author: Jennifer Thakar <[email protected]>
55
homepage: https://github.com/sass/migrator

test/migrators/division/always_division.hrx

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ a {
3434
o: identity(6px / 3px);
3535
p: rgba(10, 20, 30/2, 0.5);
3636
q: rgb(10 20 30/2 / 0.5);
37+
38+
// Negation
39+
r: -(4/3);
40+
s: (-4/3);
41+
t: -((-6/3) / -(4/2));
3742
}
3843

3944
<==> output/entrypoint.scss
@@ -71,4 +76,9 @@ a {
7176
o: identity(math.div(6px, 3px));
7277
p: rgba(10, 20, math.div(30, 2), 0.5);
7378
q: rgb(10, 20, math.div(30, 2), 0.5);
79+
80+
// Negation
81+
r: -(math.div(4, 3));
82+
s: math.div(-4, 3);
83+
t: -(math.div(math.div(-6, 3), -(math.div(4, 2))));
7484
}

0 commit comments

Comments
 (0)