Skip to content

Commit 9836146

Browse files
srawlinsCommit Bot
authored and
Commit Bot
committed
Add remove_leading_underscore fix for lint
Change-Id: I2db240604580f270980296c07296a1336d45ff90 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/234341 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent be7ff1d commit 9836146

File tree

6 files changed

+234
-0
lines changed

6 files changed

+234
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
6+
import 'package:analysis_server/src/services/correction/fix.dart';
7+
import 'package:analysis_server/src/services/correction/util.dart';
8+
import 'package:analyzer/dart/ast/ast.dart';
9+
import 'package:analyzer/dart/element/element.dart';
10+
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
11+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
12+
import 'package:analyzer_plugin/utilities/range_factory.dart';
13+
14+
class RemoveLeadingUnderscore extends CorrectionProducer {
15+
@override
16+
bool get canBeAppliedInBulk => true;
17+
18+
@override
19+
bool get canBeAppliedToFile => true;
20+
21+
@override
22+
FixKind get fixKind => DartFixKind.REMOVE_LEADING_UNDERSCORE;
23+
24+
@override
25+
FixKind get multiFixKind => DartFixKind.REMOVE_LEADING_UNDERSCORE_MULTI;
26+
27+
@override
28+
Future<void> compute(ChangeBuilder builder) async {
29+
var identifier = node;
30+
if (identifier is! SimpleIdentifier) {
31+
return;
32+
}
33+
34+
var name = identifier.name;
35+
if (name.length < 2) {
36+
return;
37+
}
38+
39+
var newName = name.substring(1);
40+
41+
// Find references to the identifier.
42+
List<SimpleIdentifier>? references;
43+
var element = identifier.staticElement;
44+
if (element is LocalVariableElement) {
45+
var root = node.thisOrAncestorOfType<Block>();
46+
if (root != null) {
47+
references = findLocalElementReferences(root, element);
48+
}
49+
} else if (element is ParameterElement) {
50+
if (!element.isNamed) {
51+
print(node.parent.runtimeType);
52+
print(node.parent?.parent.runtimeType);
53+
print(node.parent?.parent?.parent.runtimeType);
54+
var root = node
55+
.thisOrAncestorMatching((node) =>
56+
node.parent is FunctionDeclaration ||
57+
node.parent is MethodDeclaration)
58+
?.parent;
59+
if (root != null) {
60+
references = findLocalElementReferences(root, element);
61+
}
62+
}
63+
}
64+
if (references == null) {
65+
return;
66+
}
67+
68+
// Compute the change.
69+
var references_final = references;
70+
await builder.addDartFileEdit(file, (builder) {
71+
for (var reference in references_final) {
72+
builder.addSimpleReplacement(range.node(reference), newName);
73+
}
74+
});
75+
}
76+
77+
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
78+
static RemoveLeadingUnderscore newInstance() => RemoveLeadingUnderscore();
79+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,16 @@ class DartFixKind {
951951
DartFixKindPriority.IN_FILE,
952952
'Remove unnecessary interpolation braces everywhere in file',
953953
);
954+
static const REMOVE_LEADING_UNDERSCORE = FixKind(
955+
'dart.fix.remove.leadingUnderscore',
956+
DartFixKindPriority.DEFAULT,
957+
'Remove leading underscore',
958+
);
959+
static const REMOVE_LEADING_UNDERSCORE_MULTI = FixKind(
960+
'dart.fix.remove.leadingUnderscore.multi',
961+
DartFixKindPriority.IN_FILE,
962+
'Remove leading underscores in file',
963+
);
954964
static const REMOVE_METHOD_DECLARATION = FixKind(
955965
'dart.fix.remove.methodDeclaration',
956966
DartFixKindPriority.DEFAULT,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_empty_statem
118118
import 'package:analysis_server/src/services/correction/dart/remove_if_null_operator.dart';
119119
import 'package:analysis_server/src/services/correction/dart/remove_initializer.dart';
120120
import 'package:analysis_server/src/services/correction/dart/remove_interpolation_braces.dart';
121+
import 'package:analysis_server/src/services/correction/dart/remove_leading_underscore.dart';
121122
import 'package:analysis_server/src/services/correction/dart/remove_method_declaration.dart';
122123
import 'package:analysis_server/src/services/correction/dart/remove_name_from_combinator.dart';
123124
import 'package:analysis_server/src/services/correction/dart/remove_non_null_assertion.dart';
@@ -452,6 +453,9 @@ class FixProcessor extends BaseProcessor {
452453
LintNames.no_duplicate_case_values: [
453454
RemoveDuplicateCase.newInstance,
454455
],
456+
LintNames.no_leading_underscores_for_local_identifiers: [
457+
RemoveLeadingUnderscore.newInstance,
458+
],
455459
LintNames.non_constant_identifier_names: [
456460
RenameToCamelCase.newInstance,
457461
],

pkg/analysis_server/lib/src/services/linter/lint_names.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class LintNames {
5959
static const String leading_newlines_in_multiline_strings =
6060
'leading_newlines_in_multiline_strings';
6161
static const String no_duplicate_case_values = 'no_duplicate_case_values';
62+
static const String no_leading_underscores_for_local_identifiers =
63+
'no_leading_underscores_for_local_identifiers';
6264
static const String non_constant_identifier_names =
6365
'non_constant_identifier_names';
6466
static const String null_closures = 'null_closures';
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analysis_server/src/services/correction/fix.dart';
6+
import 'package:analysis_server/src/services/linter/lint_names.dart';
7+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
8+
import 'package:test_reflective_loader/test_reflective_loader.dart';
9+
10+
import 'fix_processor.dart';
11+
12+
void main() {
13+
defineReflectiveSuite(() {
14+
defineReflectiveTests(RemoveLeadingUnderscoreBulkTest);
15+
defineReflectiveTests(RemoveLeadingUnderscoreTest);
16+
});
17+
}
18+
19+
@reflectiveTest
20+
class RemoveLeadingUnderscoreBulkTest extends BulkFixProcessorTest {
21+
@override
22+
String get lintCode => LintNames.no_leading_underscores_for_local_identifiers;
23+
24+
Future<void> test_singleFile() async {
25+
await resolveTestCode('''
26+
main() {
27+
int _foo = 42;
28+
print(_foo);
29+
[0, 1, 2].forEach((_bar) {
30+
print(_bar);
31+
});
32+
}
33+
''');
34+
await assertHasFix('''
35+
main() {
36+
int foo = 42;
37+
print(foo);
38+
[0, 1, 2].forEach((bar) {
39+
print(bar);
40+
});
41+
}
42+
''');
43+
}
44+
}
45+
46+
@reflectiveTest
47+
class RemoveLeadingUnderscoreTest extends FixProcessorLintTest {
48+
@override
49+
FixKind get kind => DartFixKind.REMOVE_LEADING_UNDERSCORE;
50+
51+
@override
52+
String get lintCode => LintNames.no_leading_underscores_for_local_identifiers;
53+
54+
Future<void> test_localVariable() async {
55+
await resolveTestCode('''
56+
void f() {
57+
var _foo = 1;
58+
print(_foo);
59+
}
60+
''');
61+
await assertHasFix('''
62+
void f() {
63+
var foo = 1;
64+
print(foo);
65+
}
66+
''');
67+
}
68+
69+
Future<void> test_parameter_closure() async {
70+
await resolveTestCode('''
71+
void f() {
72+
[0, 1, 2].forEach((_foo) {
73+
print(_foo);
74+
});
75+
}
76+
''');
77+
await assertHasFix('''
78+
void f() {
79+
[0, 1, 2].forEach((foo) {
80+
print(foo);
81+
});
82+
}
83+
''');
84+
}
85+
86+
Future<void> test_parameter_function() async {
87+
await resolveTestCode('''
88+
void f(int _foo) {
89+
print(_foo);
90+
}
91+
''');
92+
await assertHasFix('''
93+
void f(int foo) {
94+
print(foo);
95+
}
96+
''');
97+
}
98+
99+
Future<void> test_parameter_method() async {
100+
await resolveTestCode('''
101+
class A {
102+
void f(int _foo) {
103+
print(_foo);
104+
}
105+
}
106+
''');
107+
await assertHasFix('''
108+
class A {
109+
void f(int foo) {
110+
print(foo);
111+
}
112+
}
113+
''');
114+
}
115+
116+
Future<void> test_parameter_optionalNamed() async {
117+
await resolveTestCode('''
118+
void f({int? _foo}) {
119+
print(_foo);
120+
}
121+
''');
122+
await assertNoFix();
123+
}
124+
125+
Future<void> test_parameter_optionalPositional() async {
126+
await resolveTestCode('''
127+
void f([int? _foo]) {
128+
print(_foo);
129+
}
130+
''');
131+
await assertHasFix('''
132+
void f([int? foo]) {
133+
print(foo);
134+
}
135+
''');
136+
}
137+
}

pkg/analysis_server/test/src/services/correction/fix/test_all.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ import 'remove_empty_statement_test.dart' as remove_empty_statement;
144144
import 'remove_if_null_operator_test.dart' as remove_if_null_operator;
145145
import 'remove_initializer_test.dart' as remove_initializer;
146146
import 'remove_interpolation_braces_test.dart' as remove_interpolation_braces;
147+
import 'remove_leading_underscore_test.dart' as remove_leading_underscore;
147148
import 'remove_method_declaration_test.dart' as remove_method_declaration;
148149
import 'remove_name_from_combinator_test.dart' as remove_name_from_combinator;
149150
import 'remove_non_null_assertion_test.dart' as remove_non_null_assertion_test;
@@ -346,6 +347,7 @@ void main() {
346347
remove_if_null_operator.main();
347348
remove_initializer.main();
348349
remove_interpolation_braces.main();
350+
remove_leading_underscore.main();
349351
remove_method_declaration.main();
350352
remove_name_from_combinator.main();
351353
remove_non_null_assertion_test.main();

0 commit comments

Comments
 (0)