Skip to content

Commit

Permalink
Keep interpolation in var CSS funcion (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelalozano16 authored Jan 16, 2024
1 parent dc0ac49 commit 6d08f6d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
12 changes: 7 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 2.0.1

### Calc Functions Interpolation Migrator

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

## 2.0.0

* **Breaking change**: The `media-logic` migrator has been removed as the
Expand All @@ -8,11 +15,6 @@

* Update to be compatible with the latest version of the Dart Sass AST.

### Calc Functions Interpolation Migrator

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

### Division Migrator

* `/` division should now be left untouched in all CSS calculation functions.
Expand Down
21 changes: 15 additions & 6 deletions lib/src/migrators/calc_interpolation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,28 @@ 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\(#{[a-zA-Z0-9#{$}-]+}\)|var\(\-\-[a-zA-Z0-9\$\#\{\}\-]+\)');
if (calcFunctions.contains(node.name)) {
for (var arg in node.arguments.positional) {
var newArg = arg.toString();
for (var match in interpolation.allMatches(arg.toString())) {
var noInterpolation =
match[0].toString().substring(2, match[0].toString().length - 1);
var varFuncArgs = isVarFunc.allMatches(newArg);
if (varFuncArgs.isNotEmpty) {
newArg = newArg.replaceAll(isVarFunc, 'var()');
}

for (var match in interpolation.allMatches(newArg)) {
var noInterpolation = match[0]!.substring(2, match[0]!.length - 1);
if (hasOperation.hasMatch(noInterpolation)) {
noInterpolation = '(' + noInterpolation + ')';
}
newArg = newArg
.toString()
.replaceAll(match[0].toString(), noInterpolation);
newArg = newArg.toString().replaceAll(match[0]!, noInterpolation);
}

for (var match in varFuncArgs) {
newArg = newArg.replaceFirst('var()', match[0]!);
}

if (newArg != arg.toString()) {
var interpolationSpan =
node.span.file.span(arg.span.start.offset, arg.span.end.offset);
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: 2.0.0
version: 2.0.1
description: A tool for running migrations on Sass files
homepage: https://github.com/sass/migrator

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ $d: 5;
// CSS Custom properties keep interpolation
.a { --test: calc(#{$b} + 1);}

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

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

// CSS Custom properties keep interpolation
.a { --test: calc(#{$b} + 1);}

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

0 comments on commit 6d08f6d

Please sign in to comment.