Skip to content

Commit

Permalink
Add calculation interpolation migrator.
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelalozano16 committed Nov 21, 2023
1 parent 4dc4179 commit 38fa72f
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
51 changes: 51 additions & 0 deletions lib/src/migrators/calc_interpolation.dart
Original file line number Diff line number Diff line change
@@ -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<Uri, String> 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);
}
}
2 changes: 2 additions & 0 deletions lib/src/runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -59,6 +60,7 @@ class MigratorRunner extends CommandRunner<Map<Uri, String>> {
addCommand(ModuleMigrator());
addCommand(NamespaceMigrator());
addCommand(StrictUnaryMigrator());
addCommand(CalculationInterpolationMigrator());
}

/// Runs a migrator and then writes the migrated files to disk unless
Expand Down
13 changes: 13 additions & 0 deletions test/migrators/calc_interpolation/calc_remove_interpolation.hrx
Original file line number Diff line number Diff line change
@@ -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)); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<==> input/entrypoint.scss
$c: 1;
.a { .b: clamp(#{$c}); }

<==> output/entrypoint.scss
$c: 1;
.a { .b: clamp($c); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<==> input/entrypoint.scss
$c: 1;
.a { .b: max(#{$c, 3}); }

<==> output/entrypoint.scss
$c: 1;
.a { .b: max($c, 3); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<==> input/entrypoint.scss
$c: 1;
.a { .b: min(#{$c, 3}); }

<==> output/entrypoint.scss
$c: 1;
.a { .b: min($c, 3); }

0 comments on commit 38fa72f

Please sign in to comment.