Skip to content

Commit

Permalink
Fix bugs in calc interpolation migrator (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelalozano16 authored Dec 1, 2023
1 parent 2b5915f commit 0852083
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 11 additions & 5 deletions lib/src/migrators/calc_interpolation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
24 changes: 20 additions & 4 deletions test/migrators/calc_interpolation/calc_remove_interpolation.hrx
Original file line number Diff line number Diff line change
@@ -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)); }
1 change: 1 addition & 0 deletions test/migrators/migrator_dart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import '../utils.dart';

main() {
testMigrator("calc_interpolation");
testMigrator("division");
testMigrator("media_logic");
testMigrator("module");
Expand Down

0 comments on commit 0852083

Please sign in to comment.