-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add calculation interpolation migrator.
- Loading branch information
1 parent
4dc4179
commit 85ef97d
Showing
8 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2023 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 = _CalculationInterpolationVisitor(importCache, migrateDependencies); | ||
var result = visitor.run(stylesheet, importer); | ||
missingDependencies.addAll(visitor.missingDependencies); | ||
return result; | ||
} | ||
} | ||
|
||
class _CalculationInterpolationVisitor extends MigrationVisitor { | ||
_CalculationInterpolationVisitor(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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
test/migrators/calc_interpolation/calc_remove_interpolation.hrx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); } |
7 changes: 7 additions & 0 deletions
7
test/migrators/calc_interpolation/clamp_remove_interpolation.hrx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } |
7 changes: 7 additions & 0 deletions
7
test/migrators/calc_interpolation/max_remove_interpolation.hrx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } |
7 changes: 7 additions & 0 deletions
7
test/migrators/calc_interpolation/min_remove_interpolation.hrx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } |