5
5
import 'package:analysis_server/src/services/correction/fix/data_driven/add_type_parameter.dart' ;
6
6
import 'package:analysis_server/src/services/correction/fix/data_driven/change.dart' ;
7
7
import 'package:analysis_server/src/services/correction/fix/data_driven/element_descriptor.dart' ;
8
+ import 'package:analysis_server/src/services/correction/fix/data_driven/modify_parameters.dart' ;
8
9
import 'package:analysis_server/src/services/correction/fix/data_driven/parameter_reference.dart' ;
9
10
import 'package:analysis_server/src/services/correction/fix/data_driven/rename.dart' ;
10
11
import 'package:analysis_server/src/services/correction/fix/data_driven/transform.dart' ;
@@ -17,11 +18,13 @@ import 'package:yaml/yaml.dart';
17
18
18
19
/// A parser used to read a transform set from a file.
19
20
class TransformSetParser {
21
+ static const String _argumentValueKey = 'argumentValue' ;
20
22
static const String _changesKey = 'changes' ;
21
23
static const String _classKey = 'class' ;
24
+ static const String _constantKey = 'constant' ;
22
25
static const String _constructorKey = 'constructor' ;
26
+ static const String _defaultValueKey = 'defaultValue' ;
23
27
static const String _elementKey = 'element' ;
24
- static const String _enumConstantKey = 'constant' ;
25
28
static const String _enumKey = 'enum' ;
26
29
static const String _extensionKey = 'extension' ;
27
30
static const String _fieldKey = 'field' ;
@@ -38,6 +41,7 @@ class TransformSetParser {
38
41
static const String _nameKey = 'name' ;
39
42
static const String _newNameKey = 'newName' ;
40
43
static const String _setterKey = 'setter' ;
44
+ static const String _styleKey = 'style' ;
41
45
static const String _titleKey = 'title' ;
42
46
static const String _transformsKey = 'transforms' ;
43
47
static const String _typedefKey = 'typedef' ;
@@ -49,22 +53,28 @@ class TransformSetParser {
49
53
/// the possible containers of that element.
50
54
static const Map <String , List <String >> _containerKeyMap = {
51
55
_constructorKey: [_inClassKey],
52
- _enumConstantKey : [_inEnumKey],
56
+ _constantKey : [_inEnumKey],
53
57
_fieldKey: [_inClassKey, _inExtensionKey, _inMixinKey],
54
58
_getterKey: [_inClassKey, _inExtensionKey, _inMixinKey],
55
59
_methodKey: [_inClassKey, _inExtensionKey, _inMixinKey],
56
60
_setterKey: [_inClassKey, _inExtensionKey, _inMixinKey],
57
61
};
58
62
63
+ static const String _addParameterKind = 'addParameter' ;
59
64
static const String _addTypeParameterKind = 'addTypeParameter' ;
60
65
static const String _argumentKind = 'argument' ;
66
+ static const String _removeParameterKind = 'removeParameter' ;
61
67
static const String _renameKind = 'rename' ;
62
68
63
69
static const int currentVersion = 1 ;
64
70
65
71
/// The error reporter to which diagnostics will be reported.
66
72
final ErrorReporter errorReporter;
67
73
74
+ /// The parameter modifications associated with the current transform, or
75
+ /// `null` if the current transform does not yet have any such modifications.
76
+ List <ParameterModification > _parameterModifications;
77
+
68
78
/// Initialize a newly created parser to report diagnostics to the
69
79
/// [errorReporter] .
70
80
TransformSetParser (this .errorReporter);
@@ -160,6 +170,52 @@ class TransformSetParser {
160
170
return foundKeys[0 ];
161
171
}
162
172
173
+ /// Translate the [node] into a add-parameter modification.
174
+ void _translateAddParameterChange (YamlMap node) {
175
+ _singleKey (node, [_indexKey, _nameKey]);
176
+ _reportUnsupportedKeys (node, const {_indexKey, _kindKey, _nameKey});
177
+ var index = _translateInteger (node.valueAt (_indexKey), _indexKey);
178
+ if (index == null ) {
179
+ return ;
180
+ }
181
+ var name = _translateString (node.valueAt (_nameKey), _nameKey);
182
+ if (name == null ) {
183
+ return ;
184
+ }
185
+ var style = _translateString (node.valueAt (_styleKey), _styleKey);
186
+ if (style == null ) {
187
+ return ;
188
+ }
189
+ var isRequired = style.startsWith ('required_' );
190
+ var isPositional = style.endsWith ('_positional' );
191
+ // TODO(brianwilkerson) I originally thought we'd need a default value, but
192
+ // it seems like we ought to be able to get it from the overridden method,
193
+ // so investigate removing this field.
194
+ var defaultValue = _translateValueExtractor (
195
+ node.valueAt (_defaultValueKey), _defaultValueKey);
196
+ if (isRequired && defaultValue != null ) {
197
+ // TODO(brianwilkerson) Report that required parameters can't have a
198
+ // default value.
199
+ return ;
200
+ }
201
+ var argumentValue = _translateValueExtractor (
202
+ node.valueAt (_argumentValueKey), _argumentValueKey);
203
+ // TODO(brianwilkerson) We really ought to require an argument value for
204
+ // optional positional parameters too for the case where the added
205
+ // parameter is being added before the end of the list and call sites might
206
+ // already be providing a value for subsequent parameters. Unfortunately we
207
+ // can't know at this point whether there are subsequent parameters in
208
+ // order to require it only when it's potentially necessary.
209
+ if (isRequired && argumentValue == null ) {
210
+ // TODO(brianwilkerson) Report that required parameters must have an
211
+ // argument value.
212
+ return ;
213
+ }
214
+ _parameterModifications ?? = [];
215
+ _parameterModifications.add (AddParameter (
216
+ index, name, isRequired, isPositional, defaultValue, argumentValue));
217
+ }
218
+
163
219
/// Translate the [node] into an add-type-parameter change. Return the
164
220
/// resulting change, or `null` if the [node] does not represent a valid
165
221
/// add-type-parameter change.
@@ -219,6 +275,12 @@ class TransformSetParser {
219
275
return _translateAddTypeParameterChange (node);
220
276
} else if (kind == _renameKind) {
221
277
return _translateRenameChange (node);
278
+ } else if (kind == _addParameterKind) {
279
+ _translateAddParameterChange (node);
280
+ return null ;
281
+ } else if (kind == _removeParameterKind) {
282
+ _translateRemoveParameterChange (node);
283
+ return null ;
222
284
}
223
285
// TODO(brianwilkerson) Report the invalid change kind.
224
286
return null ;
@@ -248,7 +310,7 @@ class TransformSetParser {
248
310
}
249
311
var elementKey = _singleKey (node, [
250
312
_classKey,
251
- _enumConstantKey ,
313
+ _constantKey ,
252
314
_constructorKey,
253
315
_enumKey,
254
316
_extensionKey,
@@ -270,7 +332,7 @@ class TransformSetParser {
270
332
var containerName =
271
333
_translateString (node.valueAt (containerKey), containerKey);
272
334
if (containerName == null ) {
273
- if ([_constructorKey, _enumConstantKey , _methodKey, _fieldKey]
335
+ if ([_constructorKey, _constantKey , _methodKey, _fieldKey]
274
336
.contains (elementKey)) {
275
337
// TODO(brianwilkerson) Report that no container was found.
276
338
return null ;
@@ -338,6 +400,25 @@ class TransformSetParser {
338
400
}
339
401
}
340
402
403
+ /// Translate the [node] into a remove-parameter modification.
404
+ void _translateRemoveParameterChange (YamlMap node) {
405
+ _singleKey (node, [_indexKey, _nameKey]);
406
+ _reportUnsupportedKeys (node, const {_indexKey, _kindKey, _nameKey});
407
+ ParameterReference reference;
408
+ var index = _translateInteger (node.valueAt (_indexKey), _indexKey);
409
+ if (index != null ) {
410
+ reference = PositionalParameterReference (index);
411
+ } else {
412
+ var name = _translateString (node.valueAt (_nameKey), _nameKey);
413
+ if (name == null ) {
414
+ return ;
415
+ }
416
+ reference = NamedParameterReference (name);
417
+ }
418
+ _parameterModifications ?? = [];
419
+ _parameterModifications.add (RemoveParameter (reference));
420
+ }
421
+
341
422
/// Translate the [node] into a rename change. Return the resulting change, or
342
423
/// `null` if the [node] does not represent a valid rename change.
343
424
Rename _translateRenameChange (YamlMap node) {
@@ -380,12 +461,16 @@ class TransformSetParser {
380
461
_reportUnsupportedKeys (node, const {_changesKey, _elementKey, _titleKey});
381
462
var title = _translateString (node.valueAt (_titleKey), _titleKey);
382
463
var element = _translateElement (node.valueAt (_elementKey), _elementKey);
383
- var changes = _translateList < Change > (
464
+ var changes = _translateList (
384
465
node.valueAt (_changesKey), _changesKey, _translateChange);
385
466
if (changes == null ) {
386
467
// The error has already been reported.
387
468
return null ;
388
469
}
470
+ if (_parameterModifications != null ) {
471
+ changes.add (ModifyParameters (modifications: _parameterModifications));
472
+ _parameterModifications = null ;
473
+ }
389
474
return Transform (title: title, element: element, changes: changes);
390
475
} else if (node == null ) {
391
476
// TODO(brianwilkerson) Report the missing YAML.
0 commit comments