@@ -32,6 +32,8 @@ class ModuleMigrator extends Migrator {
3232
3333 @override
3434 final argParser = ArgParser ()
35+ ..addFlag ('built-in-only' ,
36+ help: 'Migrates global functions without migrating @import.' )
3537 ..addMultiOption ('remove-prefix' ,
3638 abbr: 'p' ,
3739 help: 'Removes PREFIX from all migrated member names.\n '
@@ -77,6 +79,13 @@ class ModuleMigrator extends Migrator {
7779 Map <Uri , String > migrateFile (
7880 ImportCache importCache, Stylesheet stylesheet, Importer importer) {
7981 var forwards = {for (var arg in argResults! ['forward' ]) ForwardType (arg)};
82+ var builtInOnly = argResults! ['built-in-only' ] as bool ;
83+ if (builtInOnly &&
84+ (argResults! .wasParsed ('forward' ) ||
85+ argResults! .wasParsed ('remove-prefix' ))) {
86+ throw MigrationException ('--forward and --remove-prefix may not be '
87+ 'passed with --built-in-only.' );
88+ }
8089 if (forwards.contains (ForwardType .prefixed) &&
8190 ! argResults! .wasParsed ('remove-prefix' )) {
8291 throw MigrationException (
@@ -85,8 +94,10 @@ class ModuleMigrator extends Migrator {
8594 }
8695
8796 var references = References (importCache, stylesheet, importer);
88- var visitor = _ModuleMigrationVisitor (importCache, references,
89- globalResults! ['load-path' ] as List <String >, migrateDependencies,
97+ var visitor = _ModuleMigrationVisitor (
98+ importCache, references, globalResults! ['load-path' ] as List <String >,
99+ migrateDependencies: migrateDependencies,
100+ builtInOnly: builtInOnly,
90101 prefixesToRemove: (argResults! ['remove-prefix' ] as List <String >)
91102 .map ((prefix) => prefix.replaceAll ('_' , '-' )),
92103 forwards: forwards);
@@ -186,9 +197,6 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
186197 /// main migration pass is used.
187198 final References references;
188199
189- /// Cache used to load stylesheets.
190- final ImportCache importCache;
191-
192200 /// List of paths that stylesheets can be loaded from.
193201 final List <String > loadPaths;
194202
@@ -199,6 +207,9 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
199207 /// The values of the --forward flag.
200208 final Set <ForwardType > forwards;
201209
210+ /// Whether to migrate only global functions, leaving `@import` rules as-is.
211+ final bool builtInOnly;
212+
202213 /// Constructs a new module migration visitor.
203214 ///
204215 /// [importCache] must be the same one used by [references] .
@@ -210,22 +221,25 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
210221 /// the module migrator will filter out the dependencies' migration results.
211222 ///
212223 /// This converts the OS-specific relative [loadPaths] to absolute URL paths.
213- _ModuleMigrationVisitor (this .importCache, this .references,
214- List <String > loadPaths, bool migrateDependencies,
215- {Iterable <String > prefixesToRemove = const [], this .forwards = const {}})
224+ _ModuleMigrationVisitor (
225+ super .importCache, this .references, List <String > loadPaths,
226+ {required super .migrateDependencies,
227+ required this .builtInOnly,
228+ Iterable <String > prefixesToRemove = const [],
229+ this .forwards = const {}})
216230 : loadPaths = List .unmodifiable (
217231 loadPaths.map ((path) => p.toUri (p.absolute (path)).path)),
218- prefixesToRemove = UnmodifiableSetView (prefixesToRemove.toSet ()),
219- super (importCache, migrateDependencies);
232+ prefixesToRemove = UnmodifiableSetView (prefixesToRemove.toSet ());
220233
221234 /// Checks which global declarations need to be renamed, then runs the
222235 /// migrator.
223236 @override
224237 Map <Uri , String > run (Stylesheet stylesheet, Importer importer) {
225- references.globalDeclarations.forEach (_renameDeclaration);
238+ if ( ! builtInOnly) references.globalDeclarations.forEach (_renameDeclaration);
226239 var migrated = super .run (stylesheet, importer);
227240
228- if (forwards.contains (ForwardType .importOnly) || _needsImportOnly) {
241+ if (! builtInOnly &&
242+ (forwards.contains (ForwardType .importOnly) || _needsImportOnly)) {
229243 var url = stylesheet.span.sourceUrl! ;
230244 var importOnlyUrl = getImportOnlyUrl (url);
231245 var results = _generateImportOnly (url, importOnlyUrl);
@@ -597,7 +611,10 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
597611 /// Adds a namespace to any function call that requires it.
598612 @override
599613 void visitFunctionExpression (FunctionExpression node) {
600- if (node.namespace != null ) {
614+ var source = references.sources[node];
615+ if (node.namespace != null ||
616+ source is UseSource ||
617+ builtInOnly && source is ! BuiltInSource ) {
601618 super .visitFunctionExpression (node);
602619 return ;
603620 }
@@ -631,12 +648,15 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
631648 // Warn for get-function calls without a static name.
632649 var nameArg =
633650 node.arguments.named['name' ] ?? node.arguments.positional.first;
634- if (nameArg is ! StringExpression || nameArg.text.asPlain == null ) {
651+ if ((nameArg is ! StringExpression || nameArg.text.asPlain == null ) &&
652+ ! builtInOnly) {
635653 emitWarning (
636654 "get-function call may require \$ module parameter" , nameArg.span);
637655 return ;
638656 }
639657
658+ if (builtInOnly && declaration != null ) return ;
659+
640660 _patchNamespaceForFunction (node, declaration, (namespace) {
641661 var beforeParen = node.span.end.offset - 1 ;
642662 addPatch (Patch (node.span.file.span (beforeParen, beforeParen),
@@ -776,14 +796,21 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
776796 }
777797 }
778798 if (ruleUrl != null ) {
779- if (_useAllowed) {
799+ if (builtInOnly) {
800+ if (migrateDependencies) {
801+ _upstreamStylesheets.add (currentUrl);
802+ visitDependency (ruleUrl, import.span);
803+ _upstreamStylesheets.remove (currentUrl);
804+ }
805+ } else if (_useAllowed) {
780806 migratedRules.addAll (_migrateImportToRules (ruleUrl, import.span));
781807 } else {
782808 migratedRules.add (_migrateImportToLoadCss (ruleUrl, import.span)
783809 .replaceAll ('\n ' , '\n $indent ' ));
784810 }
785811 }
786812 }
813+ if (builtInOnly) return ;
787814
788815 rulesText = migratedRules.join ('$semicolon \n $indent ' );
789816 if (rulesText.isEmpty) {
@@ -1071,6 +1098,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
10711098 _useAllowed = false ;
10721099 super .visitIncludeRule (node);
10731100 if (node.namespace != null ) return ;
1101+ if (builtInOnly && references.sources[node] is ! BuiltInSource ) return ;
10741102
10751103 var declaration = references.mixins[node];
10761104 if (declaration == null ) {
@@ -1089,7 +1117,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
10891117 @override
10901118 void visitMixinRule (MixinRule node) {
10911119 _useAllowed = false ;
1092- _renameReference (nameSpan (node), MemberDeclaration (node));
1120+ if ( ! builtInOnly) _renameReference (nameSpan (node), MemberDeclaration (node));
10931121 super .visitMixinRule (node);
10941122 }
10951123
@@ -1119,6 +1147,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
11191147 @override
11201148 void visitVariableExpression (VariableExpression node) {
11211149 if (node.namespace != null ) return ;
1150+ if (builtInOnly && references.sources[node] is ! BuiltInSource ) return ;
11221151 var declaration = references.variables[node];
11231152 if (declaration == null ) {
11241153 // TODO(jathak): Error here as part of fixing #182.
@@ -1145,6 +1174,10 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
11451174 /// renaming or namespacing if necessary.
11461175 @override
11471176 void visitVariableDeclaration (VariableDeclaration node) {
1177+ if (builtInOnly) {
1178+ super .visitVariableDeclaration (node);
1179+ return ;
1180+ }
11481181 var declaration = MemberDeclaration (node);
11491182 var defaultDeclaration =
11501183 references.defaultVariableDeclarations[declaration];
0 commit comments