diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_colon_with_in.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_colon_with_in.dart new file mode 100644 index 000000000000..038599434806 --- /dev/null +++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_colon_with_in.dart @@ -0,0 +1,34 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:analysis_server/src/services/correction/fix.dart'; +import 'package:analysis_server_plugin/edit/dart/correction_producer.dart'; +import 'package:analyzer/src/generated/source.dart'; +import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; +import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; + +class ReplaceColonWithIn extends ResolvedCorrectionProducer { + ReplaceColonWithIn({required super.context}); + + @override + CorrectionApplicability get applicability => + CorrectionApplicability.automatically; + + @override + FixKind get fixKind => DartFixKind.REPLACE_COLON_WITH_IN; + + @override + FixKind get multiFixKind => DartFixKind.REPLACE_COLON_WITH_IN_MULTI; + + @override + Future compute(ChangeBuilder builder) async { + var diagnostic = this.diagnostic; + if (diagnostic == null) return; + + await builder.addDartFileEdit(file, (builder) { + builder.addSimpleReplacement( + SourceRange(diagnostic.problemMessage.offset, 1), 'in'); + }); + } +} diff --git a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml index f93dd3b8fc37..786b0110f6be 100644 --- a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml +++ b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml @@ -45,8 +45,8 @@ # # Stats: # - 42 "needsEvaluation" -# - 330 "needsFix" -# - 418 "hasFix" +# - 329 "needsFix" +# - 419 "hasFix" # - 516 "noFix" AnalysisOptionsErrorCode.INCLUDED_FILE_PARSE_ERROR: @@ -2568,9 +2568,7 @@ ParserErrorCode.CLASS_IN_CLASS: notes: |- Move the inner class to the top-level. ParserErrorCode.COLON_IN_PLACE_OF_IN: - status: needsFix - notes: |- - Replace the `:` with `in`. + status: hasFix ParserErrorCode.CONFLICTING_MODIFIERS: status: needsFix notes: |- diff --git a/pkg/analysis_server/lib/src/services/correction/fix.dart b/pkg/analysis_server/lib/src/services/correction/fix.dart index 780ee0466687..4437df3792a2 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix.dart @@ -1582,6 +1582,16 @@ class DartFixKind { DartFixKindPriority.IN_FILE, "Replace ':'s with '='s everywhere in file", ); + static const REPLACE_COLON_WITH_IN = FixKind( + 'dart.fix.replace.colonWithIn', + DartFixKindPriority.DEFAULT, + "Replace ':' with 'in'", + ); + static const REPLACE_COLON_WITH_IN_MULTI = FixKind( + 'dart.fix.replace.colonWithIn.multi', + DartFixKindPriority.IN_FILE, + "Replace ':'s with 'in's everywhere in file", + ); static const REPLACE_FINAL_WITH_CONST = FixKind( 'dart.fix.replace.finalWithConst', DartFixKindPriority.DEFAULT, diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart index a567a58aa6e5..e17b1145bf92 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart @@ -188,6 +188,7 @@ import 'package:analysis_server/src/services/correction/dart/rename_to_camel_cas import 'package:analysis_server/src/services/correction/dart/replace_boolean_with_bool.dart'; import 'package:analysis_server/src/services/correction/dart/replace_cascade_with_dot.dart'; import 'package:analysis_server/src/services/correction/dart/replace_colon_with_equals.dart'; +import 'package:analysis_server/src/services/correction/dart/replace_colon_with_in.dart'; import 'package:analysis_server/src/services/correction/dart/replace_container_with_sized_box.dart'; import 'package:analysis_server/src/services/correction/dart/replace_empty_map_pattern.dart'; import 'package:analysis_server/src/services/correction/dart/replace_final_with_const.dart'; @@ -1361,6 +1362,9 @@ final _builtInNonLintProducers = >{ ParserErrorCode.ABSTRACT_STATIC_METHOD: [ RemoveLexeme.modifier, ], + ParserErrorCode.COLON_IN_PLACE_OF_IN: [ + ReplaceColonWithIn.new, + ], ParserErrorCode.CONST_CLASS: [ RemoveConst.new, ], diff --git a/pkg/analysis_server/test/src/services/correction/fix/replace_colon_with_in_test.dart b/pkg/analysis_server/test/src/services/correction/fix/replace_colon_with_in_test.dart new file mode 100644 index 000000000000..3e85c5c02015 --- /dev/null +++ b/pkg/analysis_server/test/src/services/correction/fix/replace_colon_with_in_test.dart @@ -0,0 +1,34 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:analysis_server/src/services/correction/fix.dart'; +import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import 'fix_processor.dart'; + +void main() { + defineReflectiveSuite(() { + defineReflectiveTests(ReplaceColonWithInTest); + }); +} + +@reflectiveTest +class ReplaceColonWithInTest extends FixProcessorTest { + @override + FixKind get kind => DartFixKind.REPLACE_COLON_WITH_IN; + + Future test_colonInPlaceOfIn() async { + await resolveTestCode(''' +void f() { + for (var _ : []) {} +} +'''); + await assertHasFix(''' +void f() { + for (var _ in []) {} +} +'''); + } +} diff --git a/pkg/analysis_server/test/src/services/correction/fix/test_all.dart b/pkg/analysis_server/test/src/services/correction/fix/test_all.dart index 42718890c818..e2c29e0c6321 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/test_all.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/test_all.dart @@ -240,6 +240,7 @@ import 'rename_to_camel_case_test.dart' as rename_to_camel_case; import 'replace_boolean_with_bool_test.dart' as replace_boolean_with_bool; import 'replace_cascade_with_dot_test.dart' as replace_cascade_with_dot; import 'replace_colon_with_equals_test.dart' as replace_colon_with_equals; +import 'replace_colon_with_in_test.dart' as replace_colon_with_in; import 'replace_container_with_sized_box_test.dart' as replace_container_with_sized_box; import 'replace_empty_amp_pattern_test.dart' as replace_empty_amp_pattern; @@ -497,6 +498,7 @@ void main() { replace_boolean_with_bool.main(); replace_cascade_with_dot.main(); replace_colon_with_equals.main(); + replace_colon_with_in.main(); replace_container_with_sized_box.main(); replace_empty_amp_pattern.main(); replace_final_with_const.main();