Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
jathak committed Oct 10, 2024
1 parent ddbf9d6 commit 9fe3c66
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Module Migrator

* Add a new `--builtin-only` flag, which migrates global functions to their
* Add a new `--built-in-only` flag, which migrates global functions to their
`sass:` module equivalents, while leaving `@import` rules unchanged.

* Fix bug where some functions (`opacity`, `is-bracketed`, and
Expand Down
21 changes: 13 additions & 8 deletions lib/src/migrators/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ModuleMigrator extends Migrator {

@override
final argParser = ArgParser()
..addFlag('builtin-only',
..addFlag('built-in-only',
help: 'Migrates global functions without migrating @import.')
..addMultiOption('remove-prefix',
abbr: 'p',
Expand Down Expand Up @@ -79,12 +79,12 @@ class ModuleMigrator extends Migrator {
Map<Uri, String> migrateFile(
ImportCache importCache, Stylesheet stylesheet, Importer importer) {
var forwards = {for (var arg in argResults!['forward']) ForwardType(arg)};
var builtInOnly = argResults!['builtin-only'] as bool;
var builtInOnly = argResults!['built-in-only'] as bool;
if (builtInOnly &&
(argResults!.wasParsed('forward') ||
argResults!.wasParsed('remove-prefix'))) {
throw MigrationException('--forward and --remove-prefix may not be '
'passed with --builtin-only.');
'passed with --built-in-only.');
}
if (forwards.contains(ForwardType.prefixed) &&
!argResults!.wasParsed('remove-prefix')) {
Expand Down Expand Up @@ -630,7 +630,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
});
}

if (node.name == "get-function" && !builtInOnly) {
if (node.name == "get-function") {
var declaration = references.getFunctionReferences[node];
if (declaration != null) {
_unreferencable.check(declaration, node);
Expand All @@ -648,12 +648,15 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
// Warn for get-function calls without a static name.
var nameArg =
node.arguments.named['name'] ?? node.arguments.positional.first;
if (nameArg is! StringExpression || nameArg.text.asPlain == null) {
if ((nameArg is! StringExpression || nameArg.text.asPlain == null) &&
!builtInOnly) {
emitWarning(
"get-function call may require \$module parameter", nameArg.span);
return;
}

if (builtInOnly && declaration != null) return;

_patchNamespaceForFunction(node, declaration, (namespace) {
var beforeParen = node.span.end.offset - 1;
addPatch(Patch(node.span.file.span(beforeParen, beforeParen),
Expand Down Expand Up @@ -794,9 +797,11 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
}
if (ruleUrl != null) {
if (builtInOnly) {
_upstreamStylesheets.add(currentUrl);
if (migrateDependencies) visitDependency(ruleUrl, import.span);
_upstreamStylesheets.remove(currentUrl);
if (migrateDependencies) {
_upstreamStylesheets.add(currentUrl);
visitDependency(ruleUrl, import.span);
_upstreamStylesheets.remove(currentUrl);
}
} else if (_useAllowed) {
migratedRules.addAll(_migrateImportToRules(ruleUrl, import.span));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<==> arguments
--builtin-only
--built-in-only

<==> input/entrypoint.scss
@import "library";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<==> arguments
--built-in-only

<==> input/entrypoint.scss
@import "library";

a {
b: call(get-function(mix), red, blue);
c: call(get-function(fn), green);
}

<==> input/_library.scss
@function fn($x) {
@return $x;
}

<==> output/entrypoint.scss
@use "sass:color";
@use "sass:meta";
@import "library";

a {
b: meta.call(meta.get-function(mix, $module: "color"), red, blue);
c: meta.call(meta.get-function(fn), green);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<==> arguments
--builtin-only
--built-in-only

<==> input/entrypoint.scss
@import "library";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<==> arguments
--builtin-only --migrate-deps
--built-in-only --migrate-deps

<==> input/entrypoint.scss
@import "library";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<==> arguments
--builtin-only
--built-in-only

<==> input/entrypoint.scss
@use "sass:color";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<==> arguments
--builtin-only --migrate-deps
--built-in-only --migrate-deps

<==> input/entrypoint.scss
@import "a", "b";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<==> arguments
--builtin-only
--built-in-only

<==> input/entrypoint.scss
@use "sass:math";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<==> arguments
--builtin-only
--built-in-only

<==> input/entrypoint.scss
@import "library";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<==> arguments
--builtin-only
--built-in-only

<==> input/entrypoint.scss
@use "library" as *;
Expand Down
2 changes: 2 additions & 0 deletions test/migrators/module/built_in_functions/namespacing.hrx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
$a: mix(red, blue);
$b: str-length("hello");
$c: scale-color(blue, $lightness: -10%);
$d: call(get-function(mix), red, blue);

@function fn($args...) {
@return keywords($args);
Expand All @@ -14,6 +15,7 @@ $c: scale-color(blue, $lightness: -10%);
$a: color.mix(red, blue);
$b: string.length("hello");
$c: color.scale(blue, $lightness: -10%);
$d: meta.call(meta.get-function(mix, $module: "color"), red, blue);

@function fn($args...) {
@return meta.keywords($args);
Expand Down

0 comments on commit 9fe3c66

Please sign in to comment.