Skip to content

Commit 60a8ba1

Browse files
asashourCommit Queue
authored and
Commit Queue
committed
[analysis_server] offer fixes for TEXT_DIRECTION_CODE_POINT_*
Fixes #50790 Change-Id: Id8f135e090406f235f488a497ba277032cb1e900 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/276644 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent ba8db19 commit 60a8ba1

File tree

8 files changed

+182
-4
lines changed

8 files changed

+182
-4
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
8+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
9+
import 'package:analyzer_plugin/utilities/range_factory.dart';
10+
11+
class RemoveCharacter extends CorrectionProducer {
12+
String _codePoint = '';
13+
14+
@override
15+
// Not predictably the correct action.
16+
bool get canBeAppliedInBulk => false;
17+
18+
@override
19+
// Not predictably the correct action.
20+
bool get canBeAppliedToFile => false;
21+
22+
@override
23+
List<Object> get fixArguments => [_codePoint];
24+
25+
@override
26+
FixKind get fixKind => DartFixKind.REMOVE_CHARACTER;
27+
28+
@override
29+
Future<void> compute(ChangeBuilder builder) async {
30+
var problemMessage = diagnostic?.problemMessage;
31+
if (problemMessage == null) return;
32+
33+
var offset = problemMessage.offset;
34+
var content = resolvedResult.content;
35+
var codeUnit = content.codeUnitAt(offset);
36+
_codePoint = codeUnit.toRadixString(16).toUpperCase();
37+
await builder.addDartFileEdit(file, (builder) {
38+
builder.addDeletion(range.startOffsetEndOffset(offset, offset + 1));
39+
});
40+
}
41+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
8+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
9+
import 'package:analyzer_plugin/utilities/range_factory.dart';
10+
11+
class ReplaceWithUnicodeEscape extends CorrectionProducer {
12+
@override
13+
// Not predictably the correct action.
14+
bool get canBeAppliedInBulk => false;
15+
16+
@override
17+
// Not predictably the correct action.
18+
bool get canBeAppliedToFile => false;
19+
20+
@override
21+
FixKind get fixKind => DartFixKind.REPLACE_WITH_UNICODE_ESCAPE;
22+
23+
@override
24+
Future<void> compute(ChangeBuilder builder) async {
25+
var problemMessage = diagnostic?.problemMessage;
26+
if (problemMessage == null) return;
27+
28+
var offset = problemMessage.offset;
29+
var content = resolvedResult.content;
30+
var codeUnit = content.codeUnitAt(offset);
31+
var code = codeUnit.toRadixString(16).toUpperCase();
32+
await builder.addDartFileEdit(file, (builder) {
33+
builder.addSimpleReplacement(
34+
range.startOffsetEndOffset(offset, offset + 1), '\\u$code');
35+
});
36+
}
37+
}

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,11 +1642,9 @@ HintCode.UNNECESSARY_TYPE_CHECK_TRUE:
16421642
status: needsFix
16431643
issue: https://github.com/dart-lang/sdk/issues/47793
16441644
HintCode.TEXT_DIRECTION_CODE_POINT_IN_COMMENT:
1645-
status: needsFix
1646-
since: ~2.16
1645+
status: hasFix
16471646
HintCode.TEXT_DIRECTION_CODE_POINT_IN_LITERAL:
1648-
status: needsFix
1649-
since: ~2.16
1647+
status: hasFix
16501648
HintCode.UNUSED_CATCH_CLAUSE:
16511649
status: hasFix
16521650
HintCode.UNUSED_CATCH_STACK:

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,11 @@ class DartFixKind {
924924
DartFixKindPriority.IN_FILE,
925925
'Remove awaits in file',
926926
);
927+
static const REMOVE_CHARACTER = FixKind(
928+
'dart.fix.remove.character',
929+
DartFixKindPriority.DEFAULT,
930+
"Remove the 'U+{0}' code point",
931+
);
927932
static const REMOVE_COMPARISON = FixKind(
928933
'dart.fix.remove.comparison',
929934
DartFixKindPriority.DEFAULT,
@@ -1595,6 +1600,11 @@ class DartFixKind {
15951600
DartFixKindPriority.IN_FILE,
15961601
'Replace function literals with tear-offs everywhere in file',
15971602
);
1603+
static const REPLACE_WITH_UNICODE_ESCAPE = FixKind(
1604+
'dart.fix.replace.withUnicodeEscape',
1605+
DartFixKindPriority.DEFAULT,
1606+
"Replace with Unicode escape",
1607+
);
15981608
static const REPLACE_WITH_VAR = FixKind(
15991609
'dart.fix.replace.withVar',
16001610
DartFixKindPriority.DEFAULT,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_argument.dar
117117
import 'package:analysis_server/src/services/correction/dart/remove_assertion.dart';
118118
import 'package:analysis_server/src/services/correction/dart/remove_assignment.dart';
119119
import 'package:analysis_server/src/services/correction/dart/remove_await.dart';
120+
import 'package:analysis_server/src/services/correction/dart/remove_character.dart';
120121
import 'package:analysis_server/src/services/correction/dart/remove_comparison.dart';
121122
import 'package:analysis_server/src/services/correction/dart/remove_const.dart';
122123
import 'package:analysis_server/src/services/correction/dart/remove_constructor_name.dart';
@@ -192,6 +193,7 @@ import 'package:analysis_server/src/services/correction/dart/replace_with_is_emp
192193
import 'package:analysis_server/src/services/correction/dart/replace_with_not_null_aware.dart';
193194
import 'package:analysis_server/src/services/correction/dart/replace_with_null_aware.dart';
194195
import 'package:analysis_server/src/services/correction/dart/replace_with_tear_off.dart';
196+
import 'package:analysis_server/src/services/correction/dart/replace_with_unicode_escape.dart';
195197
import 'package:analysis_server/src/services/correction/dart/replace_with_var.dart';
196198
import 'package:analysis_server/src/services/correction/dart/sort_child_property_last.dart';
197199
import 'package:analysis_server/src/services/correction/dart/sort_combinators.dart';
@@ -1416,6 +1418,14 @@ class FixProcessor extends BaseProcessor {
14161418
HintCode.OVERRIDE_ON_NON_OVERRIDING_SETTER: [
14171419
RemoveAnnotation.new,
14181420
],
1421+
HintCode.TEXT_DIRECTION_CODE_POINT_IN_COMMENT: [
1422+
RemoveCharacter.new,
1423+
ReplaceWithUnicodeEscape.new,
1424+
],
1425+
HintCode.TEXT_DIRECTION_CODE_POINT_IN_LITERAL: [
1426+
RemoveCharacter.new,
1427+
ReplaceWithUnicodeEscape.new,
1428+
],
14191429
// TODO(brianwilkerson) Add a fix to normalize the path.
14201430
// HintCode.PACKAGE_IMPORT_CONTAINS_DOT_DOT: [],
14211431
HintCode.TYPE_CHECK_IS_NOT_NULL: [
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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:analyzer_plugin/utilities/fixes/fixes.dart';
7+
import 'package:test_reflective_loader/test_reflective_loader.dart';
8+
9+
import 'fix_processor.dart';
10+
11+
void main() {
12+
defineReflectiveSuite(() {
13+
defineReflectiveTests(RemoveCharacterTest);
14+
});
15+
}
16+
17+
@reflectiveTest
18+
class RemoveCharacterTest extends FixProcessorTest {
19+
@override
20+
FixKind get kind => DartFixKind.REMOVE_CHARACTER;
21+
22+
Future<void> test_comment() async {
23+
await resolveTestCode('''
24+
// some\u2066thing
25+
''');
26+
await assertHasFix('''
27+
// something
28+
''');
29+
}
30+
31+
Future<void> test_literal() async {
32+
await resolveTestCode('''
33+
var ch = '\u202A';
34+
''');
35+
await assertHasFix('''
36+
var ch = '';
37+
''');
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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:analyzer_plugin/utilities/fixes/fixes.dart';
7+
import 'package:test_reflective_loader/test_reflective_loader.dart';
8+
9+
import 'fix_processor.dart';
10+
11+
void main() {
12+
defineReflectiveSuite(() {
13+
defineReflectiveTests(ReplaceWithUnicodeEscapeTest);
14+
});
15+
}
16+
17+
@reflectiveTest
18+
class ReplaceWithUnicodeEscapeTest extends FixProcessorTest {
19+
@override
20+
FixKind get kind => DartFixKind.REPLACE_WITH_UNICODE_ESCAPE;
21+
22+
Future<void> test_comment() async {
23+
await resolveTestCode('''
24+
// some\u2066thing
25+
''');
26+
await assertHasFix(r'''
27+
// some\u2066thing
28+
''');
29+
}
30+
31+
Future<void> test_literal() async {
32+
await resolveTestCode('''
33+
var ch = '\u202A';
34+
''');
35+
await assertHasFix(r'''
36+
var ch = '\u202A';
37+
''');
38+
}
39+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ import 'remove_argument_test.dart' as remove_argument;
145145
import 'remove_assertion_test.dart' as remove_assertion;
146146
import 'remove_assignment_test.dart' as remove_assignment;
147147
import 'remove_await_test.dart' as remove_await;
148+
import 'remove_character_test.dart' as remove_character;
148149
import 'remove_comparison_test.dart' as remove_comparison;
149150
import 'remove_const_test.dart' as remove_const;
150151
import 'remove_constructor_name_test.dart' as remove_constructor_name;
@@ -234,6 +235,7 @@ import 'replace_with_is_not_empty_test.dart' as replace_with_is_not_empty;
234235
import 'replace_with_not_null_aware_test.dart' as replace_with_not_null_aware;
235236
import 'replace_with_null_aware_test.dart' as replace_with_null_aware;
236237
import 'replace_with_tear_off_test.dart' as replace_with_tear_off;
238+
import 'replace_with_unicode_escape_test.dart' as replace_with_unicode_escape_;
237239
import 'replace_with_var_test.dart' as replace_with_var;
238240
import 'sort_child_property_last_test.dart' as sort_properties_last;
239241
import 'sort_combinators_test.dart' as sort_combinators_test;
@@ -377,6 +379,7 @@ void main() {
377379
remove_assertion.main();
378380
remove_assignment.main();
379381
remove_await.main();
382+
remove_character.main();
380383
remove_comparison.main();
381384
remove_const.main();
382385
remove_constructor_name.main();
@@ -454,6 +457,7 @@ void main() {
454457
replace_with_not_null_aware.main();
455458
replace_with_null_aware.main();
456459
replace_with_tear_off.main();
460+
replace_with_unicode_escape_.main();
457461
replace_with_var.main();
458462
sort_properties_last.main();
459463
sort_constructor_first_test.main();

0 commit comments

Comments
 (0)