Skip to content

Commit 4a639e8

Browse files
committed
Add support to dartfix for the remaining pedantic lints (for which a fix is already implemented)
Change-Id: I6d7b0df1ce4fc7377697d4c66b78f2fb0e95b5c4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/129362 Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 9921e20 commit 4a639e8

2 files changed

Lines changed: 150 additions & 3 deletions

File tree

pkg/analysis_server/lib/src/edit/fix/dartfix_info.dart

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,36 @@ a message is displayed and the class is not converted to a mixin.''',
5555
//
5656
// Pedantic lint fixes.
5757
//
58+
// TODO(brianwilkerson) The commented out fixes below involve potentially
59+
// non-local changes, so they can't currently be applied together. I have an
60+
// idea for how to update FixProcessor to support these fixes.
61+
// LintFixInfo.alwaysDeclareReturnTypes,
62+
// LintFixInfo.alwaysRequireNonNullNamedParameters,
63+
LintFixInfo.annotateOverrides,
64+
LintFixInfo.avoidEmptyElse,
65+
LintFixInfo.avoidInitToNull,
66+
LintFixInfo.avoidRelativLibImports,
67+
LintFixInfo.avoidReturnTypesOnSetters,
68+
LintFixInfo.curlyBracesInFlowControlStructures,
69+
LintFixInfo.emptyCatches,
70+
LintFixInfo.emptyConstructorBodies,
71+
LintFixInfo.noDuplicateCaseValues,
5872
LintFixInfo.nullClosures,
73+
LintFixInfo.preferConditionalAssignment,
5974
LintFixInfo.preferEqualForDefaultValues,
75+
LintFixInfo.preferFinalFields,
76+
LintFixInfo.preferForElementsToMapFromIterable,
6077
LintFixInfo.preferIsEmpty,
6178
LintFixInfo.preferIsNotEmpty,
6279
LintFixInfo.preferSingleQuotes,
80+
LintFixInfo.preferSpreadCollections,
81+
LintFixInfo.slashForDocComments,
82+
LintFixInfo.typeInitFormals,
83+
LintFixInfo.unawaitedFutures,
6384
LintFixInfo.unnecessaryConst,
6485
LintFixInfo.unnecessaryNew,
86+
LintFixInfo.unnecessaryThis,
87+
LintFixInfo.useRethrowWhenPossible,
6588
//
6689
// Other fixes
6790
//
@@ -181,6 +204,82 @@ class DartFixInfo {
181204

182205
/// Information about a fix that applies to a lint.
183206
class LintFixInfo extends DartFixInfo {
207+
// TODO(brianwilkerson) Add fixes in FixProcessor for the following pedantic
208+
// lints:
209+
// avoid_null_checks_in_equality_operators
210+
// avoid_shadowing_type_parameters
211+
// avoid_types_as_parameter_names
212+
// camel_case_extensions
213+
// library_names
214+
// omit_local_variable_types
215+
// prefer_adjacent_string_concatenation
216+
// prefer_collection_literals
217+
// prefer_contains
218+
// prefer_generic_function_type_aliases
219+
// prefer_if_null_operators
220+
// prefer_iterable_whereType
221+
// recursive_getters
222+
// unnecessary_null_in_if_null_operators
223+
// unrelated_type_equality_checks
224+
// use_function_type_syntax_for_parameters
225+
// valid_regexps
226+
227+
static final alwaysDeclareReturnTypes = LintFixInfo(
228+
'always_declare_return_types',
229+
DartFixKind.ADD_RETURN_TYPE,
230+
'Add a return type where possible.',
231+
isPedantic: true);
232+
233+
static final alwaysRequireNonNullNamedParameters = LintFixInfo(
234+
'always_require_non_null_named_parameters',
235+
DartFixKind.ADD_REQUIRED,
236+
'Add an @required annotation.',
237+
isPedantic: true);
238+
239+
static final annotateOverrides = LintFixInfo('annotate_overrides',
240+
DartFixKind.ADD_OVERRIDE, 'Add an @override annotation.',
241+
isPedantic: true);
242+
243+
static final avoidEmptyElse = LintFixInfo('avoid_empty_else',
244+
DartFixKind.REMOVE_EMPTY_ELSE, 'Remove the empty else.',
245+
isPedantic: true);
246+
247+
static final avoidInitToNull = LintFixInfo('avoid_init_to_null',
248+
DartFixKind.REMOVE_INITIALIZER, 'Remove the initializer.',
249+
isPedantic: true);
250+
251+
static final avoidRelativLibImports = LintFixInfo(
252+
'avoid_relative_lib_imports',
253+
DartFixKind.CONVERT_TO_PACKAGE_IMPORT,
254+
'Convert the import to a package: import.',
255+
isPedantic: true);
256+
257+
static final avoidReturnTypesOnSetters = LintFixInfo(
258+
'avoid_return_types_on_setters',
259+
DartFixKind.REMOVE_TYPE_ANNOTATION,
260+
'Remove the return type.',
261+
isPedantic: true);
262+
263+
static final curlyBracesInFlowControlStructures = LintFixInfo(
264+
'curly_braces_in_flow_control_structures',
265+
DartFixKind.ADD_CURLY_BRACES,
266+
'Add curly braces.',
267+
isPedantic: true);
268+
269+
static final emptyCatches = LintFixInfo('empty_catches',
270+
DartFixKind.REMOVE_EMPTY_CATCH, 'Remove the empty catch clause.',
271+
isPedantic: true);
272+
273+
static final emptyConstructorBodies = LintFixInfo(
274+
'empty_constructor_bodies',
275+
DartFixKind.REMOVE_EMPTY_CONSTRUCTOR_BODY,
276+
'Remove the empoty catch clause.',
277+
isPedantic: true);
278+
279+
static final noDuplicateCaseValues = LintFixInfo('no_duplicate_case_values',
280+
DartFixKind.REMOVE_DUPLICATE_CASE, 'Remove the duplicate case clause.',
281+
isPedantic: true);
282+
184283
static final nullClosures = LintFixInfo(
185284
'null_closures',
186285
DartFixKind.REPLACE_NULL_WITH_CLOSURE,
@@ -195,6 +294,12 @@ will be converted to
195294
isPedantic: true,
196295
);
197296

297+
static final preferConditionalAssignment = LintFixInfo(
298+
'prefer_conditional_assignment',
299+
DartFixKind.REPLACE_WITH_CONDITIONAL_ASSIGNMENT,
300+
'Replace with a conditional assignment.',
301+
isPedantic: true);
302+
198303
static final preferEqualForDefaultValues = LintFixInfo(
199304
'prefer_equal_for_default_values',
200305
DartFixKind.REPLACE_COLON_WITH_EQUALS,
@@ -209,6 +314,16 @@ will be converted to
209314
isPedantic: true,
210315
);
211316

317+
static final preferFinalFields = LintFixInfo(
318+
'prefer_final_fields', DartFixKind.MAKE_FINAL, 'Make the field final.',
319+
isPedantic: true);
320+
321+
static final preferForElementsToMapFromIterable = LintFixInfo(
322+
'prefer_for_elements_to_map_fromIterable',
323+
DartFixKind.CONVERT_TO_FOR_ELEMENT,
324+
'Convert to a for element.',
325+
isPedantic: true);
326+
212327
static final preferIsEmpty = LintFixInfo(
213328
'prefer_is_empty',
214329
DartFixKind.REPLACE_WITH_IS_EMPTY,
@@ -248,6 +363,26 @@ Convert strings using a dobule quote to use a single quote.''',
248363
isPedantic: true,
249364
);
250365

366+
static final preferSpreadCollections = LintFixInfo(
367+
'prefer_spread_collections',
368+
// TODO(brianwilkerson) There are two possible fixes here, but not under
369+
// user control.
370+
DartFixKind.CONVERT_TO_SPREAD,
371+
'Convert to a spread operator.',
372+
isPedantic: true);
373+
374+
static final slashForDocComments = LintFixInfo('slash_for_doc_comments',
375+
DartFixKind.CONVERT_TO_LINE_COMMENT, 'Convert to a line comment.',
376+
isPedantic: true);
377+
378+
static final typeInitFormals = LintFixInfo('type_init_formals',
379+
DartFixKind.REMOVE_TYPE_ANNOTATION, 'Remove the type annotation.',
380+
isPedantic: true);
381+
382+
static final unawaitedFutures = LintFixInfo(
383+
'unawaited_futures', DartFixKind.ADD_AWAIT, 'Add await.',
384+
isPedantic: true);
385+
251386
static final unnecessaryConst = LintFixInfo(
252387
'unnecessary_const',
253388
DartFixKind.REMOVE_UNNECESSARY_CONST,
@@ -278,6 +413,14 @@ will be converted to
278413
isPedantic: true,
279414
);
280415

416+
static final unnecessaryThis = LintFixInfo(
417+
'unnecessary_this', DartFixKind.REMOVE_THIS_EXPRESSION, 'Remove this.',
418+
isPedantic: true);
419+
420+
static final useRethrowWhenPossible = LintFixInfo('use_rethrow_when_possible',
421+
DartFixKind.USE_RETHROW, 'Replace with rethrow.',
422+
isPedantic: true);
423+
281424
/// The name of the lint to be fixed.
282425
final String lintName;
283426

pkg/analysis_server/test/domain_edit_dartfix_test.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,21 @@ const double myDouble = 42.0;
136136

137137
test_fixNamedConstructorTypeArgs() async {
138138
addTestFile('''
139-
class A<T> { A.from(Object obj) { } }
139+
class A<T> {
140+
A.from(Object obj);
141+
}
140142
main() {
141143
print(A.from<String>([]));
142144
}
143145
''');
144146
createProject();
145147
EditDartfixResult result = await performFix();
146148
expect(result.suggestions, hasLength(1));
147-
expectSuggestion(result.suggestions[0], 'type arguments', 61, 8);
149+
expectSuggestion(result.suggestions[0], 'type arguments', 60, 8);
148150
expectEdits(result.edits, '''
149-
class A<T> { A.from(Object obj) { } }
151+
class A<T> {
152+
A.from(Object obj);
153+
}
150154
main() {
151155
print(A<String>.from([]));
152156
}

0 commit comments

Comments
 (0)