Skip to content

Commit 0852083

Browse files
Fix bugs in calc interpolation migrator (#247)
1 parent 2b5915f commit 0852083

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 1.8.1
2+
3+
### Calc Functions Interpolation Migrator
4+
5+
* Migration for more than one interpolation or expressions in a calc function parameter.
6+
17
## 1.8.0
28

39
### Calc Functions Interpolation Migrator

lib/src/migrators/calc_interpolation.dart

+11-5
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,22 @@ class _CalculationInterpolationVisitor extends MigrationVisitor {
3535
@override
3636
void visitCalculationExpression(CalculationExpression node) {
3737
const calcFunctions = ['calc', 'clamp', 'min', 'max'];
38-
final interpolation = RegExp(r'^#{.*\s*}');
38+
final interpolation = RegExp(r'\#{\s*[^}]+\s*}');
3939
if (calcFunctions.contains(node.name)) {
4040
for (var arg in node.arguments) {
41+
var newArg = arg.toString();
4142
for (var match in interpolation.allMatches(arg.toString())) {
42-
var noInterpolation = match[0].toString().substring(2, match.end - 1);
43+
var noInterpolation =
44+
match[0].toString().substring(2, match[0].toString().length - 1);
45+
newArg = newArg
46+
.toString()
47+
.replaceAll(match[0].toString(), noInterpolation);
48+
}
49+
if (newArg != arg.toString()) {
4350
var interpolationSpan =
4451
node.span.file.span(arg.span.start.offset, arg.span.end.offset);
45-
if (interpolationSpan.text == match[0].toString()) {
46-
addPatch(Patch(interpolationSpan, noInterpolation));
47-
}
52+
addPatch(Patch(interpolationSpan, newArg));
53+
return;
4854
}
4955
}
5056
}

pubspec.yaml

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

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
<==> input/entrypoint.scss
2+
$b: 10;
23
$c: 1;
3-
.a { .b: calc(#{$c + 1}); }
4+
$d: 5;
5+
.a { .b: calc($b - #{$c + 1}); }
6+
7+
// More than one interpolations
8+
.a { .b: calc($b - #{$c + 1} + #{$d}); }
49

510
// Nested
6-
.a { .b: calc(max(#{$c, 2})); }
11+
.a { .b: calc(3 + max(#{$c, 2})); }
12+
13+
// Nested and more interpolations
14+
.a { .b: calc(#{$b} + max(#{$c, 2})); }
715

816
<==> output/entrypoint.scss
17+
$b: 10;
918
$c: 1;
10-
.a { .b: calc($c + 1); }
19+
$d: 5;
20+
.a { .b: calc($b - $c + 1); }
21+
22+
// More than one interpolations
23+
.a { .b: calc($b - $c + 1 + $d); }
1124

1225
// Nested
13-
.a { .b: calc(max($c, 2)); }
26+
.a { .b: calc(3 + max($c, 2)); }
27+
28+
// Nested and more interpolations
29+
.a { .b: calc($b + max($c, 2)); }

test/migrators/migrator_dart_test.dart

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import '../utils.dart';
88

99
main() {
10+
testMigrator("calc_interpolation");
1011
testMigrator("division");
1112
testMigrator("media_logic");
1213
testMigrator("module");

0 commit comments

Comments
 (0)