Skip to content

Commit 2b5915f

Browse files
Add calculation interpolation migrator. (#245)
1 parent 4dc4179 commit 2b5915f

10 files changed

+101
-3
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 1.8.0
2+
3+
### Calc Functions Interpolation Migrator
4+
5+
* Removes interpolation in calculation functions `calc()`, `clamp()`, `min()`, and `max()`.
6+
See the [scss/function-calculation-no-interpolation](https://github.com/stylelint-scss/stylelint-scss/tree/master/src/rules/function-calculation-no-interpolation) rule for more information.
7+
18
## 1.7.3
29

310
* Fixes a bug where path arguments on the command line were incorrectly treated

CONTRIBUTING.md

-2
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,3 @@ review. In addition, because these models were trained indiscriminately and
5454
non-consensually on open-source code with a variety of licenses, it's not
5555
obvious that we have the moral or legal right to redistribute code they
5656
generate.
57-
58-
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Use of this source code is governed by an MIT-style
4+
// license that can be found in the LICENSE file or at
5+
// https://opensource.org/licenses/MIT.
6+
7+
import 'package:sass_api/sass_api.dart';
8+
9+
import '../migration_visitor.dart';
10+
import '../migrator.dart';
11+
import '../patch.dart';
12+
13+
/// Removes interpolation in calculation functions.
14+
class CalculationInterpolationMigrator extends Migrator {
15+
final name = "calc-interpolation";
16+
final description = r"Removes interpolation in calculation functions"
17+
r"`calc()`, `clamp()`, `min()`, and `max()`";
18+
19+
@override
20+
Map<Uri, String> migrateFile(
21+
ImportCache importCache, Stylesheet stylesheet, Importer importer) {
22+
var visitor =
23+
_CalculationInterpolationVisitor(importCache, migrateDependencies);
24+
var result = visitor.run(stylesheet, importer);
25+
missingDependencies.addAll(visitor.missingDependencies);
26+
return result;
27+
}
28+
}
29+
30+
class _CalculationInterpolationVisitor extends MigrationVisitor {
31+
_CalculationInterpolationVisitor(
32+
ImportCache importCache, bool migrateDependencies)
33+
: super(importCache, migrateDependencies);
34+
35+
@override
36+
void visitCalculationExpression(CalculationExpression node) {
37+
const calcFunctions = ['calc', 'clamp', 'min', 'max'];
38+
final interpolation = RegExp(r'^#{.*\s*}');
39+
if (calcFunctions.contains(node.name)) {
40+
for (var arg in node.arguments) {
41+
for (var match in interpolation.allMatches(arg.toString())) {
42+
var noInterpolation = match[0].toString().substring(2, match.end - 1);
43+
var interpolationSpan =
44+
node.span.file.span(arg.span.start.offset, arg.span.end.offset);
45+
if (interpolationSpan.text == match[0].toString()) {
46+
addPatch(Patch(interpolationSpan, noInterpolation));
47+
}
48+
}
49+
}
50+
}
51+
super.visitCalculationExpression(node);
52+
}
53+
}

lib/src/runner.dart

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:source_span/source_span.dart';
1313
import 'package:term_glyph/term_glyph.dart' as glyph;
1414

1515
import 'io.dart';
16+
import 'migrators/calc_interpolation.dart';
1617
import 'migrators/division.dart';
1718
import 'migrators/media_logic.dart';
1819
import 'migrators/module.dart';
@@ -54,6 +55,7 @@ class MigratorRunner extends CommandRunner<Map<Uri, String>> {
5455
abbr: 'v', help: 'Print more information.', negatable: false)
5556
..addFlag('version',
5657
help: 'Print the version of the Sass migrator.', negatable: false);
58+
addCommand(CalculationInterpolationMigrator());
5759
addCommand(DivisionMigrator());
5860
addCommand(MediaLogicMigrator());
5961
addCommand(ModuleMigrator());

pubspec.yaml

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

test/migrators/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ not contain a file if it would not be changed by the migrator.
2828
The migration's expected standard output should be included in a file named
2929
`log.txt`. Its standard error should be included in a file called `error.txt` if
3030
the migration fails, or `warning.txt` if it completes successfully.
31+
32+
## Testing
33+
Run `dart run grinder pkg-compile-snapshot-dev` for compiling the latest changes of code,
34+
`dart test` for running all tests and `dart test -x node` to run all tests except the node tests.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<==> input/entrypoint.scss
2+
$c: 1;
3+
.a { .b: calc(#{$c + 1}); }
4+
5+
// Nested
6+
.a { .b: calc(max(#{$c, 2})); }
7+
8+
<==> output/entrypoint.scss
9+
$c: 1;
10+
.a { .b: calc($c + 1); }
11+
12+
// Nested
13+
.a { .b: calc(max($c, 2)); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<==> input/entrypoint.scss
2+
$c: 1;
3+
.a { .b: clamp(#{$c}); }
4+
5+
<==> output/entrypoint.scss
6+
$c: 1;
7+
.a { .b: clamp($c); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<==> input/entrypoint.scss
2+
$c: 1;
3+
.a { .b: max(#{$c, 3}); }
4+
5+
<==> output/entrypoint.scss
6+
$c: 1;
7+
.a { .b: max($c, 3); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<==> input/entrypoint.scss
2+
$c: 1;
3+
.a { .b: min(#{$c, 3}); }
4+
5+
<==> output/entrypoint.scss
6+
$c: 1;
7+
.a { .b: min($c, 3); }

0 commit comments

Comments
 (0)