@@ -32,6 +32,8 @@ class ModuleMigrator extends Migrator {
32
32
33
33
@override
34
34
final argParser = ArgParser ()
35
+ ..addFlag ('built-in-only' ,
36
+ help: 'Migrates global functions without migrating @import.' )
35
37
..addMultiOption ('remove-prefix' ,
36
38
abbr: 'p' ,
37
39
help: 'Removes PREFIX from all migrated member names.\n '
@@ -77,6 +79,13 @@ class ModuleMigrator extends Migrator {
77
79
Map <Uri , String > migrateFile (
78
80
ImportCache importCache, Stylesheet stylesheet, Importer importer) {
79
81
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
+ }
80
89
if (forwards.contains (ForwardType .prefixed) &&
81
90
! argResults! .wasParsed ('remove-prefix' )) {
82
91
throw MigrationException (
@@ -85,8 +94,10 @@ class ModuleMigrator extends Migrator {
85
94
}
86
95
87
96
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,
90
101
prefixesToRemove: (argResults! ['remove-prefix' ] as List <String >)
91
102
.map ((prefix) => prefix.replaceAll ('_' , '-' )),
92
103
forwards: forwards);
@@ -186,9 +197,6 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
186
197
/// main migration pass is used.
187
198
final References references;
188
199
189
- /// Cache used to load stylesheets.
190
- final ImportCache importCache;
191
-
192
200
/// List of paths that stylesheets can be loaded from.
193
201
final List <String > loadPaths;
194
202
@@ -199,6 +207,9 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
199
207
/// The values of the --forward flag.
200
208
final Set <ForwardType > forwards;
201
209
210
+ /// Whether to migrate only global functions, leaving `@import` rules as-is.
211
+ final bool builtInOnly;
212
+
202
213
/// Constructs a new module migration visitor.
203
214
///
204
215
/// [importCache] must be the same one used by [references] .
@@ -210,22 +221,25 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
210
221
/// the module migrator will filter out the dependencies' migration results.
211
222
///
212
223
/// 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 {}})
216
230
: loadPaths = List .unmodifiable (
217
231
loadPaths.map ((path) => p.toUri (p.absolute (path)).path)),
218
- prefixesToRemove = UnmodifiableSetView (prefixesToRemove.toSet ()),
219
- super (importCache, migrateDependencies);
232
+ prefixesToRemove = UnmodifiableSetView (prefixesToRemove.toSet ());
220
233
221
234
/// Checks which global declarations need to be renamed, then runs the
222
235
/// migrator.
223
236
@override
224
237
Map <Uri , String > run (Stylesheet stylesheet, Importer importer) {
225
- references.globalDeclarations.forEach (_renameDeclaration);
238
+ if ( ! builtInOnly) references.globalDeclarations.forEach (_renameDeclaration);
226
239
var migrated = super .run (stylesheet, importer);
227
240
228
- if (forwards.contains (ForwardType .importOnly) || _needsImportOnly) {
241
+ if (! builtInOnly &&
242
+ (forwards.contains (ForwardType .importOnly) || _needsImportOnly)) {
229
243
var url = stylesheet.span.sourceUrl! ;
230
244
var importOnlyUrl = getImportOnlyUrl (url);
231
245
var results = _generateImportOnly (url, importOnlyUrl);
@@ -597,7 +611,10 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
597
611
/// Adds a namespace to any function call that requires it.
598
612
@override
599
613
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 ) {
601
618
super .visitFunctionExpression (node);
602
619
return ;
603
620
}
@@ -631,12 +648,15 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
631
648
// Warn for get-function calls without a static name.
632
649
var nameArg =
633
650
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) {
635
653
emitWarning (
636
654
"get-function call may require \$ module parameter" , nameArg.span);
637
655
return ;
638
656
}
639
657
658
+ if (builtInOnly && declaration != null ) return ;
659
+
640
660
_patchNamespaceForFunction (node, declaration, (namespace) {
641
661
var beforeParen = node.span.end.offset - 1 ;
642
662
addPatch (Patch (node.span.file.span (beforeParen, beforeParen),
@@ -776,14 +796,21 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
776
796
}
777
797
}
778
798
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) {
780
806
migratedRules.addAll (_migrateImportToRules (ruleUrl, import.span));
781
807
} else {
782
808
migratedRules.add (_migrateImportToLoadCss (ruleUrl, import.span)
783
809
.replaceAll ('\n ' , '\n $indent ' ));
784
810
}
785
811
}
786
812
}
813
+ if (builtInOnly) return ;
787
814
788
815
rulesText = migratedRules.join ('$semicolon \n $indent ' );
789
816
if (rulesText.isEmpty) {
@@ -1071,6 +1098,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
1071
1098
_useAllowed = false ;
1072
1099
super .visitIncludeRule (node);
1073
1100
if (node.namespace != null ) return ;
1101
+ if (builtInOnly && references.sources[node] is ! BuiltInSource ) return ;
1074
1102
1075
1103
var declaration = references.mixins[node];
1076
1104
if (declaration == null ) {
@@ -1089,7 +1117,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
1089
1117
@override
1090
1118
void visitMixinRule (MixinRule node) {
1091
1119
_useAllowed = false ;
1092
- _renameReference (nameSpan (node), MemberDeclaration (node));
1120
+ if ( ! builtInOnly) _renameReference (nameSpan (node), MemberDeclaration (node));
1093
1121
super .visitMixinRule (node);
1094
1122
}
1095
1123
@@ -1119,6 +1147,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
1119
1147
@override
1120
1148
void visitVariableExpression (VariableExpression node) {
1121
1149
if (node.namespace != null ) return ;
1150
+ if (builtInOnly && references.sources[node] is ! BuiltInSource ) return ;
1122
1151
var declaration = references.variables[node];
1123
1152
if (declaration == null ) {
1124
1153
// TODO(jathak): Error here as part of fixing #182.
@@ -1145,6 +1174,10 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
1145
1174
/// renaming or namespacing if necessary.
1146
1175
@override
1147
1176
void visitVariableDeclaration (VariableDeclaration node) {
1177
+ if (builtInOnly) {
1178
+ super .visitVariableDeclaration (node);
1179
+ return ;
1180
+ }
1148
1181
var declaration = MemberDeclaration (node);
1149
1182
var defaultDeclaration =
1150
1183
references.defaultVariableDeclarations[declaration];
0 commit comments