Skip to content

Commit 325d9b6

Browse files
author
Dart CI
committed
Version 2.10.0-110.0.dev
Merge commit '4d72ed38b80a7d6d377b92747868dbd392a058c5' into 'dev'
2 parents 5e8b31a + 4d72ed3 commit 325d9b6

File tree

10 files changed

+705
-159
lines changed

10 files changed

+705
-159
lines changed

pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class BulkFixProcessor {
9696
LintNames.prefer_equal_for_default_values:
9797
ReplaceColonWithEquals.newInstance,
9898
LintNames.prefer_final_fields: MakeFinal.newInstance,
99+
LintNames.prefer_final_locals: MakeFinal.newInstance,
99100
LintNames.prefer_for_elements_to_map_fromIterable:
100101
ConvertMapFromIterableToForLiteral.newInstance,
101102
LintNames.prefer_generic_function_type_aliases:

pkg/analysis_server/lib/src/services/correction/fix/data_driven/transform_set_parser.dart

+90-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:analysis_server/src/services/correction/fix/data_driven/add_type_parameter.dart';
66
import 'package:analysis_server/src/services/correction/fix/data_driven/change.dart';
77
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';
89
import 'package:analysis_server/src/services/correction/fix/data_driven/parameter_reference.dart';
910
import 'package:analysis_server/src/services/correction/fix/data_driven/rename.dart';
1011
import 'package:analysis_server/src/services/correction/fix/data_driven/transform.dart';
@@ -17,11 +18,13 @@ import 'package:yaml/yaml.dart';
1718

1819
/// A parser used to read a transform set from a file.
1920
class TransformSetParser {
21+
static const String _argumentValueKey = 'argumentValue';
2022
static const String _changesKey = 'changes';
2123
static const String _classKey = 'class';
24+
static const String _constantKey = 'constant';
2225
static const String _constructorKey = 'constructor';
26+
static const String _defaultValueKey = 'defaultValue';
2327
static const String _elementKey = 'element';
24-
static const String _enumConstantKey = 'constant';
2528
static const String _enumKey = 'enum';
2629
static const String _extensionKey = 'extension';
2730
static const String _fieldKey = 'field';
@@ -38,6 +41,7 @@ class TransformSetParser {
3841
static const String _nameKey = 'name';
3942
static const String _newNameKey = 'newName';
4043
static const String _setterKey = 'setter';
44+
static const String _styleKey = 'style';
4145
static const String _titleKey = 'title';
4246
static const String _transformsKey = 'transforms';
4347
static const String _typedefKey = 'typedef';
@@ -49,22 +53,28 @@ class TransformSetParser {
4953
/// the possible containers of that element.
5054
static const Map<String, List<String>> _containerKeyMap = {
5155
_constructorKey: [_inClassKey],
52-
_enumConstantKey: [_inEnumKey],
56+
_constantKey: [_inEnumKey],
5357
_fieldKey: [_inClassKey, _inExtensionKey, _inMixinKey],
5458
_getterKey: [_inClassKey, _inExtensionKey, _inMixinKey],
5559
_methodKey: [_inClassKey, _inExtensionKey, _inMixinKey],
5660
_setterKey: [_inClassKey, _inExtensionKey, _inMixinKey],
5761
};
5862

63+
static const String _addParameterKind = 'addParameter';
5964
static const String _addTypeParameterKind = 'addTypeParameter';
6065
static const String _argumentKind = 'argument';
66+
static const String _removeParameterKind = 'removeParameter';
6167
static const String _renameKind = 'rename';
6268

6369
static const int currentVersion = 1;
6470

6571
/// The error reporter to which diagnostics will be reported.
6672
final ErrorReporter errorReporter;
6773

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+
6878
/// Initialize a newly created parser to report diagnostics to the
6979
/// [errorReporter].
7080
TransformSetParser(this.errorReporter);
@@ -160,6 +170,52 @@ class TransformSetParser {
160170
return foundKeys[0];
161171
}
162172

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+
163219
/// Translate the [node] into an add-type-parameter change. Return the
164220
/// resulting change, or `null` if the [node] does not represent a valid
165221
/// add-type-parameter change.
@@ -219,6 +275,12 @@ class TransformSetParser {
219275
return _translateAddTypeParameterChange(node);
220276
} else if (kind == _renameKind) {
221277
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;
222284
}
223285
// TODO(brianwilkerson) Report the invalid change kind.
224286
return null;
@@ -248,7 +310,7 @@ class TransformSetParser {
248310
}
249311
var elementKey = _singleKey(node, [
250312
_classKey,
251-
_enumConstantKey,
313+
_constantKey,
252314
_constructorKey,
253315
_enumKey,
254316
_extensionKey,
@@ -270,7 +332,7 @@ class TransformSetParser {
270332
var containerName =
271333
_translateString(node.valueAt(containerKey), containerKey);
272334
if (containerName == null) {
273-
if ([_constructorKey, _enumConstantKey, _methodKey, _fieldKey]
335+
if ([_constructorKey, _constantKey, _methodKey, _fieldKey]
274336
.contains(elementKey)) {
275337
// TODO(brianwilkerson) Report that no container was found.
276338
return null;
@@ -338,6 +400,25 @@ class TransformSetParser {
338400
}
339401
}
340402

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+
341422
/// Translate the [node] into a rename change. Return the resulting change, or
342423
/// `null` if the [node] does not represent a valid rename change.
343424
Rename _translateRenameChange(YamlMap node) {
@@ -380,12 +461,16 @@ class TransformSetParser {
380461
_reportUnsupportedKeys(node, const {_changesKey, _elementKey, _titleKey});
381462
var title = _translateString(node.valueAt(_titleKey), _titleKey);
382463
var element = _translateElement(node.valueAt(_elementKey), _elementKey);
383-
var changes = _translateList<Change>(
464+
var changes = _translateList(
384465
node.valueAt(_changesKey), _changesKey, _translateChange);
385466
if (changes == null) {
386467
// The error has already been reported.
387468
return null;
388469
}
470+
if (_parameterModifications != null) {
471+
changes.add(ModifyParameters(modifications: _parameterModifications));
472+
_parameterModifications = null;
473+
}
389474
return Transform(title: title, element: element, changes: changes);
390475
} else if (node == null) {
391476
// TODO(brianwilkerson) Report the missing YAML.

pkg/analysis_server/lib/src/services/correction/fix/data_driven/value_extractor.dart

+15-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,27 @@ class ArgumentExtractor extends ValueExtractor {
1919

2020
@override
2121
String from(AstNode node, CorrectionUtils utils) {
22-
if (node is InvocationExpression) {
23-
var expression = parameter.argumentFrom(node.argumentList);
22+
var argumentList = _getArgumentList(node);
23+
if (argumentList != null) {
24+
var expression = parameter.argumentFrom(argumentList);
2425
if (expression != null) {
2526
return utils.getNodeText(expression);
2627
}
2728
}
2829
return null;
2930
}
31+
32+
/// Return the argument list associated with the given [node].
33+
ArgumentList _getArgumentList(AstNode node) {
34+
if (node is ArgumentList) {
35+
return node;
36+
} else if (node is InvocationExpression) {
37+
return node.argumentList;
38+
} else if (node is InstanceCreationExpression) {
39+
return node.argumentList;
40+
}
41+
return null;
42+
}
3043
}
3144

3245
/// A value extractor that returns a pre-computed piece of code.

pkg/analysis_server/lib/src/services/correction/fix_internal.dart

+2
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,11 @@ class FixProcessor extends BaseProcessor {
504504
],
505505
CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS: [
506506
AddMissingParameter.newInstance,
507+
DataDriven.newInstance,
507508
],
508509
CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS_COULD_BE_NAMED: [
509510
AddMissingParameter.newInstance,
511+
DataDriven.newInstance,
510512
],
511513
CompileTimeErrorCode.IMPLEMENTS_NON_CLASS: [
512514
DataDriven.newInstance,

pkg/analysis_server/test/src/services/correction/fix/bulk/make_final_test.dart

+22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'bulk_fix_processor.dart';
1010
void main() {
1111
defineReflectiveSuite(() {
1212
defineReflectiveTests(PreferFinalFieldsTest);
13+
defineReflectiveTests(PreferFinalLocalsTest);
1314
});
1415
}
1516

@@ -37,3 +38,24 @@ class C {
3738
''');
3839
}
3940
}
41+
42+
@reflectiveTest
43+
class PreferFinalLocalsTest extends BulkFixProcessorTest {
44+
@override
45+
String get lintCode => LintNames.prefer_final_locals;
46+
47+
Future<void> test_singleFile() async {
48+
await resolveTestUnit('''
49+
f() {
50+
var x = 0;
51+
var y = x;
52+
}
53+
''');
54+
await assertHasFix('''
55+
f() {
56+
final x = 0;
57+
final y = x;
58+
}
59+
''');
60+
}
61+
}

pkg/analysis_server/test/src/services/correction/fix/data_driven/end_to_end_test.dart

+73
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,45 @@ void main() {
1414

1515
@reflectiveTest
1616
class EndToEndTest extends DataDrivenFixProcessorTest {
17+
Future<void> test_addParameter() async {
18+
setPackageContent('''
19+
class C {
20+
void m(int x, int y) {}
21+
}
22+
''');
23+
addPackageDataFile('''
24+
version: 1
25+
transforms:
26+
- title: 'Add parameter'
27+
element:
28+
uris: ['$importUri']
29+
method: 'm'
30+
inClass: 'C'
31+
changes:
32+
- kind: 'addParameter'
33+
index: 1
34+
name: 'y'
35+
style: required_positional
36+
argumentValue:
37+
kind: 'argument'
38+
index: 0
39+
''');
40+
await resolveTestUnit('''
41+
import '$importUri';
42+
43+
void f(C c) {
44+
c.m(0);
45+
}
46+
''');
47+
await assertHasFix('''
48+
import '$importUri';
49+
50+
void f(C c) {
51+
c.m(0, 0);
52+
}
53+
''');
54+
}
55+
1756
Future<void> test_addTypeParameter() async {
1857
setPackageContent('''
1958
class C {
@@ -53,6 +92,40 @@ void f(C c) {
5392
''');
5493
}
5594

95+
Future<void> test_removeParameter() async {
96+
setPackageContent('''
97+
class C {
98+
void m(int x) {}
99+
}
100+
''');
101+
addPackageDataFile('''
102+
version: 1
103+
transforms:
104+
- title: 'Add argument'
105+
element:
106+
uris: ['$importUri']
107+
method: 'm'
108+
inClass: 'C'
109+
changes:
110+
- kind: 'removeParameter'
111+
index: 1
112+
''');
113+
await resolveTestUnit('''
114+
import '$importUri';
115+
116+
void f(C c) {
117+
c.m(0, 1);
118+
}
119+
''');
120+
await assertHasFix('''
121+
import '$importUri';
122+
123+
void f(C c) {
124+
c.m(0);
125+
}
126+
''');
127+
}
128+
56129
Future<void> test_rename() async {
57130
setPackageContent('''
58131
class New {}

0 commit comments

Comments
 (0)