Skip to content

Commit 31ea60a

Browse files
Add parenthesis in operations to keep order (#248)
1 parent 0852083 commit 31ea60a

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 1.8.2
2+
3+
### Calc Functions Interpolation Migrator
4+
5+
* Add parentheses in place of interpolation when necessary to preserve the evaluation order.
6+
17
## 1.8.1
28

39
### Calc Functions Interpolation Migrator

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);

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sass_migrator
2-
version: 1.8.1
2+
version: 1.8.2-dev
33
description: A tool for running migrations on Sass files
44
homepage: https://github.com/sass/migrator
55

test/migrators/calc_interpolation/calc_remove_interpolation.hrx

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

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

1015
// Nested
1116
.a { .b: calc(3 + max(#{$c, 2})); }
@@ -17,10 +22,15 @@ $d: 5;
1722
$b: 10;
1823
$c: 1;
1924
$d: 5;
20-
.a { .b: calc($b - $c + 1); }
25+
26+
// Single interpolation
27+
.a { .b: calc($b * ($c + 1)); }
2128

2229
// More than one interpolations
23-
.a { .b: calc($b - $c + 1 + $d); }
30+
.a {
31+
.b: calc($b - ($c + 1) + $d);
32+
.c: calc(100% - ($table-title-height + 2px));
33+
}
2434

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

0 commit comments

Comments
 (0)