diff --git a/CHANGELOG.md b/CHANGELOG.md index c1bcb24..69d943e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 1.8.1 + +### Calc Functions Interpolation Migrator + +* Migration for more than one interpolation or expressions in a calc function parameter. + ## 1.8.0 ### Calc Functions Interpolation Migrator diff --git a/lib/src/migrators/calc_interpolation.dart b/lib/src/migrators/calc_interpolation.dart index 0500d99..53a350d 100644 --- a/lib/src/migrators/calc_interpolation.dart +++ b/lib/src/migrators/calc_interpolation.dart @@ -35,16 +35,22 @@ class _CalculationInterpolationVisitor extends MigrationVisitor { @override void visitCalculationExpression(CalculationExpression node) { const calcFunctions = ['calc', 'clamp', 'min', 'max']; - final interpolation = RegExp(r'^#{.*\s*}'); + final interpolation = RegExp(r'\#{\s*[^}]+\s*}'); if (calcFunctions.contains(node.name)) { for (var arg in node.arguments) { + var newArg = arg.toString(); for (var match in interpolation.allMatches(arg.toString())) { - var noInterpolation = match[0].toString().substring(2, match.end - 1); + var noInterpolation = + match[0].toString().substring(2, match[0].toString().length - 1); + newArg = newArg + .toString() + .replaceAll(match[0].toString(), noInterpolation); + } + if (newArg != arg.toString()) { var interpolationSpan = node.span.file.span(arg.span.start.offset, arg.span.end.offset); - if (interpolationSpan.text == match[0].toString()) { - addPatch(Patch(interpolationSpan, noInterpolation)); - } + addPatch(Patch(interpolationSpan, newArg)); + return; } } } diff --git a/pubspec.yaml b/pubspec.yaml index f8a9e0f..512ba70 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: sass_migrator -version: 1.8.0 +version: 1.8.1 description: A tool for running migrations on Sass files homepage: https://github.com/sass/migrator diff --git a/test/migrators/calc_interpolation/calc_remove_interpolation.hrx b/test/migrators/calc_interpolation/calc_remove_interpolation.hrx index 1ca1499..e8af3f8 100644 --- a/test/migrators/calc_interpolation/calc_remove_interpolation.hrx +++ b/test/migrators/calc_interpolation/calc_remove_interpolation.hrx @@ -1,13 +1,29 @@ <==> input/entrypoint.scss +$b: 10; $c: 1; -.a { .b: calc(#{$c + 1}); } +$d: 5; +.a { .b: calc($b - #{$c + 1}); } + +// More than one interpolations +.a { .b: calc($b - #{$c + 1} + #{$d}); } // Nested -.a { .b: calc(max(#{$c, 2})); } +.a { .b: calc(3 + max(#{$c, 2})); } + +// Nested and more interpolations +.a { .b: calc(#{$b} + max(#{$c, 2})); } <==> output/entrypoint.scss +$b: 10; $c: 1; -.a { .b: calc($c + 1); } +$d: 5; +.a { .b: calc($b - $c + 1); } + +// More than one interpolations +.a { .b: calc($b - $c + 1 + $d); } // Nested -.a { .b: calc(max($c, 2)); } +.a { .b: calc(3 + max($c, 2)); } + +// Nested and more interpolations +.a { .b: calc($b + max($c, 2)); } diff --git a/test/migrators/migrator_dart_test.dart b/test/migrators/migrator_dart_test.dart index 93abf96..84fe0e5 100644 --- a/test/migrators/migrator_dart_test.dart +++ b/test/migrators/migrator_dart_test.dart @@ -7,6 +7,7 @@ import '../utils.dart'; main() { + testMigrator("calc_interpolation"); testMigrator("division"); testMigrator("media_logic"); testMigrator("module");