Skip to content

Commit a74a811

Browse files
Add parenthesis in operations to keep order
1 parent 0852083 commit a74a811

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/src/migrators/calc_interpolation.dart

+4
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ class _CalculationInterpolationVisitor extends MigrationVisitor {
3636
void visitCalculationExpression(CalculationExpression node) {
3737
const calcFunctions = ['calc', 'clamp', 'min', 'max'];
3838
final interpolation = RegExp(r'\#{\s*[^}]+\s*}');
39+
final hasOperation = RegExp(r'[-+*/]+');
3940
if (calcFunctions.contains(node.name)) {
4041
for (var arg in node.arguments) {
4142
var newArg = arg.toString();
4243
for (var match in interpolation.allMatches(arg.toString())) {
4344
var noInterpolation =
4445
match[0].toString().substring(2, match[0].toString().length - 1);
46+
if (hasOperation.hasMatch(noInterpolation)) {
47+
noInterpolation = '(' + noInterpolation + ')';
48+
}
4549
newArg = newArg
4650
.toString()
4751
.replaceAll(match[0].toString(), noInterpolation);

test/migrators/calc_interpolation/calc_remove_interpolation.hrx

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
$b: 10;
33
$c: 1;
44
$d: 5;
5-
.a { .b: calc($b - #{$c + 1}); }
5+
//Single interpolation
6+
.a { .b: calc($b * #{$c + 1}); }
67

78
// More than one interpolations
8-
.a { .b: calc($b - #{$c + 1} + #{$d}); }
9+
.a {
10+
.b: calc($b - #{$c + 1} + #{$d});
11+
.c: calc(100% - #{$table_title_height + 2px});
12+
}
913

1014
// Nested
1115
.a { .b: calc(3 + max(#{$c, 2})); }
@@ -17,10 +21,14 @@ $d: 5;
1721
$b: 10;
1822
$c: 1;
1923
$d: 5;
20-
.a { .b: calc($b - $c + 1); }
24+
//Single interpolation
25+
.a { .b: calc($b * ($c + 1)); }
2126

2227
// More than one interpolations
23-
.a { .b: calc($b - $c + 1 + $d); }
28+
.a {
29+
.b: calc($b - ($c + 1) + $d);
30+
.c: calc(100% - ($table-title-height + 2px));
31+
}
2432

2533
// Nested
2634
.a { .b: calc(3 + max($c, 2)); }

0 commit comments

Comments
 (0)