@@ -72,40 +72,49 @@ class _ColorMigrationVisitor extends MigrationVisitor {
72
72
@override
73
73
void visitFunctionExpression (FunctionExpression node) {
74
74
var source = references.sources[node];
75
- if (source is ! BuiltInSource || source.url != _colorUrl) return ;
75
+ if (source is BuiltInSource && source.url == _colorUrl) {
76
+ var colorPatches = _makeColorPatches (node);
77
+ if (colorPatches.isNotEmpty && node.namespace == null ) {
78
+ addPatch (patchBefore (
79
+ node, '${_getOrAddColorModuleNamespace (node .span .file )}.' ));
80
+ }
81
+ colorPatches.forEach (addPatch);
82
+ }
83
+ super .visitFunctionExpression (node);
84
+ }
85
+
86
+ /// Returns the patches necessary to convert legacy color functions to
87
+ /// `color.adjust` or `color.channel` .
88
+ Iterable <Patch > _makeColorPatches (FunctionExpression node) {
76
89
switch (node.name) {
77
90
case 'red' || 'green' || 'blue' :
78
- _patchChannel (node, 'rgb' );
91
+ return _makeChannelPatches (node, 'rgb' );
79
92
case 'hue' || 'saturation' || 'lightness' :
80
- _patchChannel (node, 'hsl' );
93
+ return _makeChannelPatches (node, 'hsl' );
81
94
case 'whiteness' || 'blackness' :
82
- _patchChannel (node, 'hwb' );
95
+ return _makeChannelPatches (node, 'hwb' );
83
96
case 'alpha' :
84
- _patchChannel (node);
97
+ return _makeChannelPatches (node);
85
98
case 'adjust-hue' :
86
- _patchAdjust (node, channel: 'hue' , space: 'hsl' );
99
+ return _makeAdjustPatches (node, channel: 'hue' , space: 'hsl' );
87
100
case 'saturate'
88
101
when node.arguments.named.length + node.arguments.positional.length !=
89
102
1 :
90
- _patchAdjust (node, channel: 'saturation' , space: 'hsl' );
103
+ return _makeAdjustPatches (node, channel: 'saturation' , space: 'hsl' );
91
104
case 'desaturate' :
92
- _patchAdjust (node, channel: 'saturation' , negate: true , space: 'hsl' );
105
+ return _makeAdjustPatches (node,
106
+ channel: 'saturation' , negate: true , space: 'hsl' );
93
107
case 'transparentize' || 'fade-out' :
94
- _patchAdjust (node, channel: 'alpha' , negate: true );
108
+ return _makeAdjustPatches (node, channel: 'alpha' , negate: true );
95
109
case 'opacify' || 'fade-in' :
96
- _patchAdjust (node, channel: 'alpha' );
110
+ return _makeAdjustPatches (node, channel: 'alpha' );
97
111
case 'lighten' :
98
- _patchAdjust (node, channel: 'lightness' , space: 'hsl' );
112
+ return _makeAdjustPatches (node, channel: 'lightness' , space: 'hsl' );
99
113
case 'darken' :
100
- _patchAdjust (node, channel: 'lightness' , negate: true , space: 'hsl' );
114
+ return _makeAdjustPatches (node,
115
+ channel: 'lightness' , negate: true , space: 'hsl' );
101
116
default :
102
- return ;
103
- }
104
- if (node.namespace == null ) {
105
- addPatch (
106
- patchBefore (
107
- node, '${_getOrAddColorModuleNamespace (node .span .file )}.' ),
108
- beforeExisting: true );
117
+ return [];
109
118
}
110
119
}
111
120
@@ -134,36 +143,38 @@ class _ColorMigrationVisitor extends MigrationVisitor {
134
143
return namespace;
135
144
}
136
145
137
- /// Patches a deprecated channel function to use `color.channel` instead.
138
- void _patchChannel (FunctionExpression node, [String ? colorSpace]) {
139
- addPatch (Patch (node.nameSpan, 'channel' ));
140
-
146
+ /// Returns the patches to make a deprecated channel function use
147
+ /// `color.channel` instead.
148
+ Iterable <Patch > _makeChannelPatches (FunctionExpression node,
149
+ [String ? colorSpace]) sync * {
150
+ yield Patch (node.nameSpan, 'channel' );
141
151
if (node.arguments.named.isEmpty) {
142
- addPatch ( patchAfter (
152
+ yield patchAfter (
143
153
node.arguments.positional.last,
144
154
", '${node .name }'"
145
- "${colorSpace == null ? '' : ', \$ space: $colorSpace ' }" )) ;
155
+ "${colorSpace == null ? '' : ', \$ space: $colorSpace ' }" );
146
156
} else {
147
- addPatch ( patchAfter (
157
+ yield patchAfter (
148
158
[...node.arguments.positional, ...node.arguments.named.values].last,
149
159
", \$ channel: '${node .name }'"
150
- "${colorSpace == null ? '' : ', \$ space: $colorSpace ' }" )) ;
160
+ "${colorSpace == null ? '' : ', \$ space: $colorSpace ' }" );
151
161
}
152
162
}
153
163
154
- /// Patches a deprecated adjustment function to use `color.adjust` instead.
155
- void _patchAdjust (FunctionExpression node,
156
- {required String channel, bool negate = false , String ? space}) {
157
- addPatch (Patch (node.nameSpan, 'adjust' ));
164
+ /// Returns the patches to make a deprecated adjustment function use
165
+ /// `color.adjust` instead.
166
+ Iterable <Patch > _makeAdjustPatches (FunctionExpression node,
167
+ {required String channel, bool negate = false , String ? space}) sync * {
168
+ yield Patch (node.nameSpan, 'adjust' );
158
169
switch (node.arguments) {
159
170
case ArgumentInvocation (positional: [_, var adjustment]):
160
- addPatch ( patchBefore (adjustment, '\$ $channel : ${negate ? '-' : '' }' ) );
171
+ yield patchBefore (adjustment, '\$ $channel : ${negate ? '-' : '' }' );
161
172
if (negate && adjustment.needsParens) {
162
- addPatch ( patchBefore (adjustment, '(' ) );
163
- addPatch ( patchAfter (adjustment, ')' ) );
173
+ yield patchBefore (adjustment, '(' );
174
+ yield patchAfter (adjustment, ')' );
164
175
}
165
176
if (space != null ) {
166
- addPatch ( patchAfter (adjustment, ', \$ space: $space ' ) );
177
+ yield patchAfter (adjustment, ', \$ space: $space ' );
167
178
}
168
179
169
180
case ArgumentInvocation (
@@ -178,16 +189,16 @@ class _ColorMigrationVisitor extends MigrationVisitor {
178
189
.pointSpan ()
179
190
.extendIfMatches ('amount' )
180
191
.extendIfMatches ('degrees' );
181
- addPatch ( Patch (argNameSpan, channel) );
192
+ yield Patch (argNameSpan, channel);
182
193
if (negate) {
183
- addPatch ( patchBefore (adjustment, '-' ) );
194
+ yield patchBefore (adjustment, '-' );
184
195
if (adjustment.needsParens) {
185
- addPatch ( patchBefore (adjustment, '(' ) );
186
- addPatch ( patchAfter (adjustment, ')' ) );
196
+ yield patchBefore (adjustment, '(' );
197
+ yield patchAfter (adjustment, ')' );
187
198
}
188
199
}
189
200
if (space != null ) {
190
- addPatch ( patchAfter (adjustment, ', \$ space: $space ' ) );
201
+ yield patchAfter (adjustment, ', \$ space: $space ' );
191
202
}
192
203
193
204
default :
0 commit comments