Skip to content

Commit 6d08f6d

Browse files
Keep interpolation in var CSS funcion (#250)
1 parent dc0ac49 commit 6d08f6d

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

CHANGELOG.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 2.0.1
2+
3+
### Calc Functions Interpolation Migrator
4+
5+
* Add parentheses in place of interpolation when necessary to preserve the evaluation order.
6+
* Keep interpolation in `var()` CSS functions.
7+
18
## 2.0.0
29

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

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

11-
### Calc Functions Interpolation Migrator
12-
13-
* Add parentheses in place of interpolation when necessary to preserve the
14-
evaluation order.
15-
1618
### Division Migrator
1719

1820
* `/` division should now be left untouched in all CSS calculation functions.

lib/src/migrators/calc_interpolation.dart

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,28 @@ class _CalculationInterpolationVisitor extends MigrationVisitor {
3737
const calcFunctions = ['calc', 'clamp', 'min', 'max'];
3838
final interpolation = RegExp(r'\#{\s*[^}]+\s*}');
3939
final hasOperation = RegExp(r'[-+*/]+');
40+
final isVarFunc = RegExp(
41+
r'var\(#{[a-zA-Z0-9#{$}-]+}\)|var\(\-\-[a-zA-Z0-9\$\#\{\}\-]+\)');
4042
if (calcFunctions.contains(node.name)) {
4143
for (var arg in node.arguments.positional) {
4244
var newArg = arg.toString();
43-
for (var match in interpolation.allMatches(arg.toString())) {
44-
var noInterpolation =
45-
match[0].toString().substring(2, match[0].toString().length - 1);
45+
var varFuncArgs = isVarFunc.allMatches(newArg);
46+
if (varFuncArgs.isNotEmpty) {
47+
newArg = newArg.replaceAll(isVarFunc, 'var()');
48+
}
49+
50+
for (var match in interpolation.allMatches(newArg)) {
51+
var noInterpolation = match[0]!.substring(2, match[0]!.length - 1);
4652
if (hasOperation.hasMatch(noInterpolation)) {
4753
noInterpolation = '(' + noInterpolation + ')';
4854
}
49-
newArg = newArg
50-
.toString()
51-
.replaceAll(match[0].toString(), noInterpolation);
55+
newArg = newArg.toString().replaceAll(match[0]!, noInterpolation);
5256
}
57+
58+
for (var match in varFuncArgs) {
59+
newArg = newArg.replaceFirst('var()', match[0]!);
60+
}
61+
5362
if (newArg != arg.toString()) {
5463
var interpolationSpan =
5564
node.span.file.span(arg.span.start.offset, arg.span.end.offset);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sass_migrator
2-
version: 2.0.0
2+
version: 2.0.1
33
description: A tool for running migrations on Sass files
44
homepage: https://github.com/sass/migrator
55

test/migrators/calc_interpolation/calc_remove_interpolation.hrx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ $d: 5;
2121
// CSS Custom properties keep interpolation
2222
.a { --test: calc(#{$b} + 1);}
2323

24+
// var() nested
25+
.a { .b: calc(var(#{$b}) + #{$c + 2} + var(--a-#{$d}-b)); }
26+
2427
<==> output/entrypoint.scss
2528
$b: 10;
2629
$c: 1;
@@ -43,3 +46,6 @@ $d: 5;
4346

4447
// CSS Custom properties keep interpolation
4548
.a { --test: calc(#{$b} + 1);}
49+
50+
// var() nested
51+
.a { .b: calc(var(#{$b}) + ($c + 2) + var(--a-#{$d}-b)); }

0 commit comments

Comments
 (0)