Skip to content

Commit 6ff1f83

Browse files
asashourcommit-bot@chromium.org
authored andcommitted
Add a quick fix for unnecessary_raw_strings
Fixes #47144 Change-Id: I89aba11f2a77e0f87a0ad4f5026c120d16147ccb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212743 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 730806b commit 6ff1f83

File tree

6 files changed

+165
-3
lines changed

6 files changed

+165
-3
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2021, 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:analyzer/source/source_range.dart';
8+
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
9+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
10+
11+
class RemoveUnnecessaryRawString extends CorrectionProducer {
12+
@override
13+
bool get canBeAppliedInBulk => true;
14+
15+
@override
16+
bool get canBeAppliedToFile => true;
17+
18+
@override
19+
FixKind get fixKind => DartFixKind.REMOVE_UNNECESSARY_RAW_STRING;
20+
21+
@override
22+
FixKind get multiFixKind => DartFixKind.REMOVE_UNNECESSARY_RAW_STRING_MULTI;
23+
24+
@override
25+
Future<void> compute(ChangeBuilder builder) async {
26+
var offset = diagnostic?.problemMessage.offset;
27+
if (offset == null) {
28+
return;
29+
}
30+
await builder.addDartFileEdit(file, (builder) {
31+
builder.addDeletion(SourceRange(offset, 1));
32+
});
33+
}
34+
35+
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
36+
static RemoveUnnecessaryRawString newInstance() =>
37+
RemoveUnnecessaryRawString();
38+
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,14 +1009,24 @@ class DartFixKind {
10091009
DartFixKindPriority.IN_FILE,
10101010
'Remove all unnecessary parentheses in file',
10111011
);
1012+
static const REMOVE_UNNECESSARY_RAW_STRING = FixKind(
1013+
'dart.fix.remove.unnecessaryRawString',
1014+
DartFixKindPriority.DEFAULT,
1015+
"Remove unnecessary 'r' in string",
1016+
);
1017+
static const REMOVE_UNNECESSARY_RAW_STRING_MULTI = FixKind(
1018+
'dart.fix.remove.unnecessaryRawString.multi',
1019+
DartFixKindPriority.IN_FILE,
1020+
"Remove unnecessary 'r' in strings in file",
1021+
);
10121022
static const REMOVE_UNNECESSARY_STRING_ESCAPE = FixKind(
10131023
'dart.fix.remove.unnecessaryStringEscape',
10141024
DartFixKindPriority.DEFAULT,
10151025
"Remove unnecessary '\\' in string",
10161026
);
10171027
static const REMOVE_UNNECESSARY_STRING_ESCAPE_MULTI = FixKind(
10181028
'dart.fix.remove.unnecessaryStringEscape.multi',
1019-
DartFixKindPriority.DEFAULT,
1029+
DartFixKindPriority.IN_FILE,
10201030
"Remove unnecessary '\\' in strings in file",
10211031
);
10221032
static const REMOVE_UNNECESSARY_STRING_INTERPOLATION = FixKind(
@@ -1171,7 +1181,7 @@ class DartFixKind {
11711181
);
11721182
static const REPLACE_NULL_WITH_VOID_MULTI = FixKind(
11731183
'dart.fix.replace.nullWithVoid.multi',
1174-
DartFixKindPriority.DEFAULT,
1184+
DartFixKindPriority.IN_FILE,
11751185
"Replace 'Null' with 'void' everywhere in file",
11761186
);
11771187
static const REPLACE_RETURN_TYPE = FixKind(
@@ -1191,7 +1201,7 @@ class DartFixKind {
11911201
);
11921202
static const REPLACE_CONTAINER_WITH_SIZED_BOX_MULTI = FixKind(
11931203
'dart.fix.replace.containerWithSizedBox.multi',
1194-
DartFixKindPriority.DEFAULT,
1204+
DartFixKindPriority.IN_FILE,
11951205
"Replace with 'SizedBox' everywhere in file",
11961206
);
11971207
static const REPLACE_VAR_WITH_DYNAMIC = FixKind(

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_type_argumen
123123
import 'package:analysis_server/src/services/correction/dart/remove_unnecessary_cast.dart';
124124
import 'package:analysis_server/src/services/correction/dart/remove_unnecessary_new.dart';
125125
import 'package:analysis_server/src/services/correction/dart/remove_unnecessary_parentheses.dart';
126+
import 'package:analysis_server/src/services/correction/dart/remove_unnecessary_raw_string.dart';
126127
import 'package:analysis_server/src/services/correction/dart/remove_unnecessary_string_escape.dart';
127128
import 'package:analysis_server/src/services/correction/dart/remove_unnecessary_string_interpolation.dart';
128129
import 'package:analysis_server/src/services/correction/dart/remove_unused.dart';
@@ -588,6 +589,9 @@ class FixProcessor extends BaseProcessor {
588589
LintNames.unnecessary_parenthesis: [
589590
RemoveUnnecessaryParentheses.newInstance,
590591
],
592+
LintNames.unnecessary_raw_strings: [
593+
RemoveUnnecessaryRawString.newInstance,
594+
],
591595
LintNames.unnecessary_string_escapes: [
592596
RemoveUnnecessaryStringEscape.newInstance,
593597
],

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class LintNames {
123123
'unnecessary_nullable_for_final_variable_declarations';
124124
static const String unnecessary_overrides = 'unnecessary_overrides';
125125
static const String unnecessary_parenthesis = 'unnecessary_parenthesis';
126+
static const String unnecessary_raw_strings = 'unnecessary_raw_strings';
126127
static const String unnecessary_string_escapes = 'unnecessary_string_escapes';
127128
static const String unnecessary_string_interpolations =
128129
'unnecessary_string_interpolations';
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright (c) 2021, 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/expect.dart';
9+
import 'package:test_reflective_loader/test_reflective_loader.dart';
10+
11+
import 'fix_processor.dart';
12+
13+
void main() {
14+
defineReflectiveSuite(() {
15+
defineReflectiveTests(RemoveUnnecessaryRawStringBulkTest);
16+
defineReflectiveTests(RemoveUnnecessaryRawStringInFileTest);
17+
defineReflectiveTests(RemoveUnnecessaryRawStringTest);
18+
});
19+
}
20+
21+
@reflectiveTest
22+
class RemoveUnnecessaryRawStringBulkTest extends BulkFixProcessorTest {
23+
@override
24+
String get lintCode => LintNames.unnecessary_raw_strings;
25+
26+
Future<void> test_singleFile() async {
27+
await resolveTestCode('''
28+
var a = r'ace';
29+
var b = r'aid';
30+
''');
31+
await assertHasFix('''
32+
var a = 'ace';
33+
var b = 'aid';
34+
''');
35+
}
36+
}
37+
38+
@reflectiveTest
39+
class RemoveUnnecessaryRawStringInFileTest extends FixInFileProcessorTest {
40+
Future<void> test_file() async {
41+
createAnalysisOptionsFile(lints: [LintNames.unnecessary_raw_strings]);
42+
await resolveTestCode('''
43+
var a = r'ace';
44+
var b = r'aid';
45+
''');
46+
var fixes = await getFixesForFirstError();
47+
expect(fixes, hasLength(1));
48+
assertProduces(fixes.first, r'''
49+
var a = 'ace';
50+
var b = 'aid';
51+
''');
52+
}
53+
}
54+
55+
@reflectiveTest
56+
class RemoveUnnecessaryRawStringTest extends FixProcessorLintTest {
57+
@override
58+
FixKind get kind => DartFixKind.REMOVE_UNNECESSARY_RAW_STRING;
59+
60+
@override
61+
String get lintCode => LintNames.unnecessary_raw_strings;
62+
63+
Future<void> test_double() async {
64+
await resolveTestCode('''
65+
var a = r"ace";
66+
''');
67+
await assertHasFix('''
68+
var a = "ace";
69+
''');
70+
}
71+
72+
Future<void> test_multi_line_double() async {
73+
await resolveTestCode('''
74+
var a = r"""
75+
abc
76+
""";
77+
''');
78+
await assertHasFix('''
79+
var a = """
80+
abc
81+
""";
82+
''');
83+
}
84+
85+
Future<void> test_multi_line_single() async {
86+
await resolveTestCode("""
87+
var a = r'''
88+
abc
89+
''';
90+
""");
91+
await assertHasFix("""
92+
var a = '''
93+
abc
94+
''';
95+
""");
96+
}
97+
98+
Future<void> test_single() async {
99+
await resolveTestCode('''
100+
var a = r'ace';
101+
''');
102+
await assertHasFix('''
103+
var a = 'ace';
104+
''');
105+
}
106+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ import 'remove_unnecessary_const_test.dart' as remove_unnecessary_const;
149149
import 'remove_unnecessary_new_test.dart' as remove_unnecessary_new;
150150
import 'remove_unnecessary_parentheses_test.dart'
151151
as remove_unnecessary_parentheses;
152+
import 'remove_unnecessary_raw_string_test.dart'
153+
as remove_unnecessary_raw_string;
152154
import 'remove_unnecessary_string_escapes_test.dart'
153155
as remove_unnecessary_string_escapes;
154156
import 'remove_unnecessary_string_interpolation_test.dart'
@@ -331,6 +333,7 @@ void main() {
331333
remove_unnecessary_const.main();
332334
remove_unnecessary_new.main();
333335
remove_unnecessary_parentheses.main();
336+
remove_unnecessary_raw_string.main();
334337
remove_unnecessary_string_escapes.main();
335338
remove_unnecessary_string_interpolation.main();
336339
remove_unused_catch_clause.main();

0 commit comments

Comments
 (0)