Skip to content

Commit

Permalink
quick fix for COLON_IN_PLACE_OF_IN
Browse files Browse the repository at this point in the history
See: #55917


Change-Id: I2dbe760dd0d123fc4b1bf493c8f7699baaea376c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/370503
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Jun 11, 2024
1 parent 55a1275 commit 19c5914
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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<void> 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');
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
#
# Stats:
# - 42 "needsEvaluation"
# - 330 "needsFix"
# - 418 "hasFix"
# - 329 "needsFix"
# - 419 "hasFix"
# - 516 "noFix"

AnalysisOptionsErrorCode.INCLUDED_FILE_PARSE_ERROR:
Expand Down Expand Up @@ -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: |-
Expand Down
10 changes: 10 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -1361,6 +1362,9 @@ final _builtInNonLintProducers = <ErrorCode, List<ProducerGenerator>>{
ParserErrorCode.ABSTRACT_STATIC_METHOD: [
RemoveLexeme.modifier,
],
ParserErrorCode.COLON_IN_PLACE_OF_IN: [
ReplaceColonWithIn.new,
],
ParserErrorCode.CONST_CLASS: [
RemoveConst.new,
],
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> test_colonInPlaceOfIn() async {
await resolveTestCode('''
void f() {
for (var _ : []) {}
}
''');
await assertHasFix('''
void f() {
for (var _ in []) {}
}
''');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 19c5914

Please sign in to comment.