Skip to content

Commit

Permalink
Add parenthesis in operations to keep order
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelalozano16 committed Dec 13, 2023
1 parent 0852083 commit 88e6ace
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 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.2

### Calc Functions Interpolation Migrator

* Add parentheses in place of interpolation when necessary to preserve the evaluation order

## 1.8.1

### Calc Functions Interpolation Migrator
Expand Down
4 changes: 4 additions & 0 deletions lib/src/migrators/calc_interpolation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ class _CalculationInterpolationVisitor extends MigrationVisitor {
void visitCalculationExpression(CalculationExpression node) {
const calcFunctions = ['calc', 'clamp', 'min', 'max'];
final interpolation = RegExp(r'\#{\s*[^}]+\s*}');
final hasOperation = RegExp(r'[-+*/]+');
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[0].toString().length - 1);
if (hasOperation.hasMatch(noInterpolation)) {
noInterpolation = '(' + noInterpolation + ')';
}
newArg = newArg
.toString()
.replaceAll(match[0].toString(), noInterpolation);
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.1
version: 1.8.2-dev
description: A tool for running migrations on Sass files
homepage: https://github.com/sass/migrator

Expand Down
18 changes: 14 additions & 4 deletions test/migrators/calc_interpolation/calc_remove_interpolation.hrx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
$b: 10;
$c: 1;
$d: 5;
.a { .b: calc($b - #{$c + 1}); }

// Single interpolation
.a { .b: calc($b * #{$c + 1}); }

// More than one interpolations
.a { .b: calc($b - #{$c + 1} + #{$d}); }
.a {
.b: calc($b - #{$c + 1} + #{$d});
.c: calc(100% - #{$table_title_height + 2px});
}

// Nested
.a { .b: calc(3 + max(#{$c, 2})); }
Expand All @@ -17,10 +22,15 @@ $d: 5;
$b: 10;
$c: 1;
$d: 5;
.a { .b: calc($b - $c + 1); }

// Single interpolation
.a { .b: calc($b * ($c + 1)); }

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

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

0 comments on commit 88e6ace

Please sign in to comment.