New Lint quick-fix: unnecessary_this (#29469)
This commit is contained in:
committed by
Alexei Eleusis Diaz Vera
parent
b90aad2b2e
commit
7979eadef0
@@ -112,7 +112,8 @@ bool hasFix(ErrorCode errorCode) =>
|
||||
errorCode.name == LintNames.avoid_init_to_null ||
|
||||
errorCode.name == LintNames.prefer_collection_literals ||
|
||||
errorCode.name == LintNames.unnecessary_brace_in_string_interp ||
|
||||
errorCode.name == LintNames.unnecessary_lambdas));
|
||||
errorCode.name == LintNames.unnecessary_lambdas ||
|
||||
errorCode.name == LintNames.unnecessary_this));
|
||||
|
||||
/**
|
||||
* An enumeration of possible quick fix kinds.
|
||||
@@ -208,6 +209,8 @@ class DartFixKind {
|
||||
'REMOVE_PARENTHESIS_IN_GETTER_INVOCATION',
|
||||
50,
|
||||
"Remove parentheses in getter invocation");
|
||||
static const REMOVE_THIS_EXPRESSION =
|
||||
const FixKind('REMOVE_THIS_EXPRESSION', 50, "Remove this expression");
|
||||
static const REMOVE_UNNECESSARY_CAST =
|
||||
const FixKind('REMOVE_UNNECESSARY_CAST', 50, "Remove unnecessary cast");
|
||||
static const REMOVE_UNUSED_CATCH_CLAUSE =
|
||||
|
||||
@@ -22,7 +22,6 @@ import 'package:analysis_server/src/services/correction/namespace.dart';
|
||||
import 'package:analysis_server/src/services/correction/source_buffer.dart';
|
||||
import 'package:analysis_server/src/services/correction/source_range.dart'
|
||||
as rf;
|
||||
import 'package:analysis_server/src/services/correction/source_range.dart';
|
||||
import 'package:analysis_server/src/services/correction/strings.dart';
|
||||
import 'package:analysis_server/src/services/correction/util.dart';
|
||||
import 'package:analysis_server/src/services/search/hierarchy.dart';
|
||||
@@ -394,6 +393,9 @@ class FixProcessor {
|
||||
if (errorCode.name == LintNames.unnecessary_lambdas) {
|
||||
_addFix_replaceWithTearOff();
|
||||
}
|
||||
if (errorCode.name == LintNames.unnecessary_this) {
|
||||
_addFix_removeThisExpression();
|
||||
}
|
||||
}
|
||||
// done
|
||||
return fixes;
|
||||
@@ -686,8 +688,8 @@ class FixProcessor {
|
||||
_addInsertEdit,
|
||||
_addRemoveEdit,
|
||||
_addReplaceEdit,
|
||||
rangeStartLength,
|
||||
rangeNode);
|
||||
rf.rangeStartLength,
|
||||
rf.rangeNode);
|
||||
_addFix(DartFixKind.CONVERT_FLUTTER_CHILD, []);
|
||||
return;
|
||||
}
|
||||
@@ -1844,6 +1846,20 @@ class FixProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
void _addFix_removeThisExpression() {
|
||||
final thisExpression = node is ThisExpression
|
||||
? node
|
||||
: node.getAncestor((node) => node is ThisExpression);
|
||||
final parent = thisExpression.parent;
|
||||
if (parent is PropertyAccess) {
|
||||
_addRemoveEdit(rf.rangeStartEnd(parent.offset, parent.operator.end));
|
||||
_addFix(DartFixKind.REMOVE_THIS_EXPRESSION, []);
|
||||
} else if (parent is MethodInvocation) {
|
||||
_addRemoveEdit(rf.rangeStartEnd(parent.offset, parent.operator.end));
|
||||
_addFix(DartFixKind.REMOVE_THIS_EXPRESSION, []);
|
||||
}
|
||||
}
|
||||
|
||||
void _addFix_removeUnnecessaryCast() {
|
||||
if (coveredNode is! AsExpression) {
|
||||
return;
|
||||
@@ -3111,6 +3127,7 @@ class LintNames {
|
||||
static const String unnecessary_brace_in_string_interp =
|
||||
'unnecessary_brace_in_string_interp';
|
||||
static const String unnecessary_lambdas = 'unnecessary_lambdas';
|
||||
static const String unnecessary_this = 'unnecessary_this';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5986,6 +5986,94 @@ var x;
|
||||
''');
|
||||
}
|
||||
|
||||
test_removeThisExpression_methodInvocation_oneCharacterOperator() async {
|
||||
String src = '''
|
||||
class A {
|
||||
void foo() {
|
||||
/*LINT*/this.foo();
|
||||
}
|
||||
}
|
||||
''';
|
||||
await findLint(src, LintNames.unnecessary_this);
|
||||
|
||||
await applyFix(DartFixKind.REMOVE_THIS_EXPRESSION);
|
||||
|
||||
verifyResult('''
|
||||
class A {
|
||||
void foo() {
|
||||
foo();
|
||||
}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_removeThisExpression_methodInvocation_twoCharactersOperator() async {
|
||||
String src = '''
|
||||
class A {
|
||||
void foo() {
|
||||
/*LINT*/this?.foo();
|
||||
}
|
||||
}
|
||||
''';
|
||||
await findLint(src, LintNames.unnecessary_this);
|
||||
|
||||
await applyFix(DartFixKind.REMOVE_THIS_EXPRESSION);
|
||||
|
||||
verifyResult('''
|
||||
class A {
|
||||
void foo() {
|
||||
foo();
|
||||
}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_removeThisExpression_propertyAccess_oneCharacterOperator() async {
|
||||
String src = '''
|
||||
class A {
|
||||
int x;
|
||||
void foo() {
|
||||
/*LINT*/this.x = 2;
|
||||
}
|
||||
}
|
||||
''';
|
||||
await findLint(src, LintNames.unnecessary_this);
|
||||
|
||||
await applyFix(DartFixKind.REMOVE_THIS_EXPRESSION);
|
||||
|
||||
verifyResult('''
|
||||
class A {
|
||||
int x;
|
||||
void foo() {
|
||||
x = 2;
|
||||
}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_removeThisExpression_propertyAccess_twoCharactersOperator() async {
|
||||
String src = '''
|
||||
class A {
|
||||
int x;
|
||||
void foo() {
|
||||
/*LINT*/this?.x = 2;
|
||||
}
|
||||
}
|
||||
''';
|
||||
await findLint(src, LintNames.unnecessary_this);
|
||||
|
||||
await applyFix(DartFixKind.REMOVE_THIS_EXPRESSION);
|
||||
|
||||
verifyResult('''
|
||||
class A {
|
||||
int x;
|
||||
void foo() {
|
||||
x = 2;
|
||||
}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_replaceWithLiteral_linkedHashMap_withCommentsInGeneric() async {
|
||||
String src = '''
|
||||
import 'dart:collection';
|
||||
|
||||
Reference in New Issue
Block a user