From 38fa72fe2deebbd6ee82413e071d39d455940aaa Mon Sep 17 00:00:00 2001 From: Pamela Lozano Date: Mon, 20 Nov 2023 16:11:08 -0800 Subject: [PATCH] Add calculation interpolation migrator. --- CONTRIBUTING.md | 5 +- lib/src/migrators/calc_interpolation.dart | 51 +++++++++++++++++++ lib/src/runner.dart | 2 + .../calc_remove_interpolation.hrx | 13 +++++ .../clamp_remove_interpolation.hrx | 7 +++ .../max_remove_interpolation.hrx | 7 +++ .../min_remove_interpolation.hrx | 7 +++ 7 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 lib/src/migrators/calc_interpolation.dart create mode 100644 test/migrators/calc_interpolation/calc_remove_interpolation.hrx create mode 100644 test/migrators/calc_interpolation/clamp_remove_interpolation.hrx create mode 100644 test/migrators/calc_interpolation/max_remove_interpolation.hrx create mode 100644 test/migrators/calc_interpolation/min_remove_interpolation.hrx diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 81b540a..09d3a2f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -55,4 +55,7 @@ non-consensually on open-source code with a variety of licenses, it's not obvious that we have the moral or legal right to redistribute code they generate. - +## Testing +1. Add a directory with the same name as the migrator in `sass/migrator/test/migrators` and + add directory to `test/migrators/migrator_dart_test.dart`. +2. For running all tests: `dart run grinder pkg-compile-snapshot-dev && dart test -x node` diff --git a/lib/src/migrators/calc_interpolation.dart b/lib/src/migrators/calc_interpolation.dart new file mode 100644 index 0000000..37d9991 --- /dev/null +++ b/lib/src/migrators/calc_interpolation.dart @@ -0,0 +1,51 @@ +// Copyright 2022 Google LLC +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file or at +// https://opensource.org/licenses/MIT. + +import 'package:sass_api/sass_api.dart'; + +import '../migration_visitor.dart'; +import '../migrator.dart'; +import '../patch.dart'; + +/// Removes interpolation in calculation functions. +class CalculationInterpolationMigrator extends Migrator { + final name = "calc-interpolation"; + final description = r"Removes interpolation in calculation functions" + r"`calc()`, `clamp()`, `min()`, and `max()`"; + + @override + Map migrateFile( + ImportCache importCache, Stylesheet stylesheet, Importer importer) { + var visitor = _UnaryMigrationVisitor(importCache, migrateDependencies); + var result = visitor.run(stylesheet, importer); + missingDependencies.addAll(visitor.missingDependencies); + return result; + } +} + +class _UnaryMigrationVisitor extends MigrationVisitor { + _UnaryMigrationVisitor(ImportCache importCache, bool migrateDependencies) + : super(importCache, migrateDependencies); + + @override + void visitCalculationExpression(CalculationExpression node) { + const calcFunctions = ['calc', 'clamp', 'min', 'max']; + final interpolation = RegExp(r'^#{.*\s*}'); + if (calcFunctions.contains(node.name)) { + for (var arg in node.arguments) { + for (var match in interpolation.allMatches(arg.toString())) { + var noInterpolation = match[0].toString().substring(2, match.end - 1); + var interpolationSpan = + node.span.file.span(arg.span.start.offset, arg.span.end.offset); + if (interpolationSpan.text == match[0].toString()) { + addPatch(Patch(interpolationSpan, noInterpolation)); + } + } + } + } + super.visitCalculationExpression(node); + } +} diff --git a/lib/src/runner.dart b/lib/src/runner.dart index 610e8ff..5c3d63a 100644 --- a/lib/src/runner.dart +++ b/lib/src/runner.dart @@ -18,6 +18,7 @@ import 'migrators/media_logic.dart'; import 'migrators/module.dart'; import 'migrators/namespace.dart'; import 'migrators/strict_unary.dart'; +import 'migrators/calc_interpolation.dart'; import 'exception.dart'; /// A command runner that runs a migrator based on provided arguments. @@ -59,6 +60,7 @@ class MigratorRunner extends CommandRunner> { addCommand(ModuleMigrator()); addCommand(NamespaceMigrator()); addCommand(StrictUnaryMigrator()); + addCommand(CalculationInterpolationMigrator()); } /// Runs a migrator and then writes the migrated files to disk unless diff --git a/test/migrators/calc_interpolation/calc_remove_interpolation.hrx b/test/migrators/calc_interpolation/calc_remove_interpolation.hrx new file mode 100644 index 0000000..1ca1499 --- /dev/null +++ b/test/migrators/calc_interpolation/calc_remove_interpolation.hrx @@ -0,0 +1,13 @@ +<==> input/entrypoint.scss +$c: 1; +.a { .b: calc(#{$c + 1}); } + +// Nested +.a { .b: calc(max(#{$c, 2})); } + +<==> output/entrypoint.scss +$c: 1; +.a { .b: calc($c + 1); } + +// Nested +.a { .b: calc(max($c, 2)); } diff --git a/test/migrators/calc_interpolation/clamp_remove_interpolation.hrx b/test/migrators/calc_interpolation/clamp_remove_interpolation.hrx new file mode 100644 index 0000000..aeaec2d --- /dev/null +++ b/test/migrators/calc_interpolation/clamp_remove_interpolation.hrx @@ -0,0 +1,7 @@ +<==> input/entrypoint.scss +$c: 1; +.a { .b: clamp(#{$c}); } + +<==> output/entrypoint.scss +$c: 1; +.a { .b: clamp($c); } diff --git a/test/migrators/calc_interpolation/max_remove_interpolation.hrx b/test/migrators/calc_interpolation/max_remove_interpolation.hrx new file mode 100644 index 0000000..f767618 --- /dev/null +++ b/test/migrators/calc_interpolation/max_remove_interpolation.hrx @@ -0,0 +1,7 @@ +<==> input/entrypoint.scss +$c: 1; +.a { .b: max(#{$c, 3}); } + +<==> output/entrypoint.scss +$c: 1; +.a { .b: max($c, 3); } diff --git a/test/migrators/calc_interpolation/min_remove_interpolation.hrx b/test/migrators/calc_interpolation/min_remove_interpolation.hrx new file mode 100644 index 0000000..cb0ecbf --- /dev/null +++ b/test/migrators/calc_interpolation/min_remove_interpolation.hrx @@ -0,0 +1,7 @@ +<==> input/entrypoint.scss +$c: 1; +.a { .b: min(#{$c, 3}); } + +<==> output/entrypoint.scss +$c: 1; +.a { .b: min($c, 3); }