Skip to content

Commit

Permalink
Keep interpolation in var CSS funcion
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelalozano16 committed Dec 28, 2023
1 parent 31ea60a commit 848716d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Calc Functions Interpolation Migrator

* Add parentheses in place of interpolation when necessary to preserve the evaluation order.
* Keep interpolation in `var()` CSS functions.

## 1.8.1

Expand Down
13 changes: 12 additions & 1 deletion lib/src/migrators/calc_interpolation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ class _CalculationInterpolationVisitor extends MigrationVisitor {
const calcFunctions = ['calc', 'clamp', 'min', 'max'];
final interpolation = RegExp(r'\#{\s*[^}]+\s*}');
final hasOperation = RegExp(r'[-+*/]+');
final isVarFunc = RegExp(r'var\(\#{\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 varFuncArgs;
if (isVarFunc.hasMatch(newArg)) {
varFuncArgs = isVarFunc.allMatches(newArg);
newArg = newArg.replaceAll(isVarFunc, 'var()');
}
for (var match in interpolation.allMatches(newArg)) {
var noInterpolation =
match[0].toString().substring(2, match[0].toString().length - 1);
if (hasOperation.hasMatch(noInterpolation)) {
Expand All @@ -50,6 +56,11 @@ class _CalculationInterpolationVisitor extends MigrationVisitor {
.toString()
.replaceAll(match[0].toString(), noInterpolation);
}
if (varFuncArgs != null) {
for (var match in varFuncArgs) {
newArg = newArg.replaceFirst('var()', match[0].toString());
}
}
if (newArg != arg.toString()) {
var interpolationSpan =
node.span.file.span(arg.span.start.offset, arg.span.end.offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ $d: 5;
// Nested and more interpolations
.a { .b: calc(#{$b} + max(#{$c, 2})); }

// var() nested
.a { .b: calc(var(#{$b}) + #{$c + 2} + var(#{$d})); }

<==> output/entrypoint.scss
$b: 10;
$c: 1;
Expand All @@ -37,3 +40,6 @@ $d: 5;

// Nested and more interpolations
.a { .b: calc($b + max($c, 2)); }

// var() nested
.a { .b: calc(var(#{$b}) + ($c + 2) + var(#{$d})); }

0 comments on commit 848716d

Please sign in to comment.