Skip to content

Commit 07c6bc6

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Adds remove type fix to ommit type lints
Fixes: #60184 Change-Id: I4fb363f2874bfce2e72777fce55fe6ace6c319c6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/411101 Reviewed-by: Samuel Rawlins <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent f5dc281 commit 07c6bc6

File tree

6 files changed

+428
-112
lines changed

6 files changed

+428
-112
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ class RemoveTypeAnnotation extends ParsedCorrectionProducer {
5555
parameters: node.parameters,
5656
);
5757
}
58-
if (node is TypeAnnotation && diagnostic != null) {
58+
if (node case TypeAnnotation(:var parent) when diagnostic != null) {
59+
if (parent is VariableDeclarationList) {
60+
return _removeFromDeclarationList(builder, parent);
61+
}
5962
return _removeTypeAnnotation(builder, node);
6063
}
6164
if (node is VariableDeclarationList) {
@@ -75,11 +78,11 @@ class RemoveTypeAnnotation extends ParsedCorrectionProducer {
7578
}
7679
// ignore if an incomplete variable declaration
7780
if (declarationList.variables.length == 1 &&
78-
declarationList.variables[0].name.isSynthetic) {
81+
declarationList.variables.first.name.isSynthetic) {
7982
return;
8083
}
8184
// must be not after the name of the variable
82-
var firstVariable = declarationList.variables[0];
85+
var firstVariable = declarationList.variables.first;
8386
if (selectionOffset > firstVariable.name.end) {
8487
return;
8588
}
@@ -129,10 +132,10 @@ class RemoveTypeAnnotation extends ParsedCorrectionProducer {
129132
var keyword = declarationList.keyword;
130133
await builder.addDartFileEdit(file, (builder) {
131134
var typeRange = range.startStart(type, firstVariable);
132-
if (keyword != null && keyword.lexeme != 'var') {
135+
if (keyword != null && keyword.lexeme != Keyword.VAR.lexeme) {
133136
builder.addSimpleReplacement(typeRange, '');
134137
} else {
135-
builder.addSimpleReplacement(typeRange, 'var ');
138+
builder.addSimpleReplacement(typeRange, '${Keyword.VAR.lexeme} ');
136139
}
137140
if (typeArgumentsText != null && typeArgumentsOffset != null) {
138141
builder.addSimpleInsertion(typeArgumentsOffset, typeArgumentsText);
@@ -152,10 +155,10 @@ class RemoveTypeAnnotation extends ParsedCorrectionProducer {
152155
var variableName = declaration.name;
153156
await builder.addDartFileEdit(file, (builder) {
154157
var typeRange = range.startStart(typeNode, variableName);
155-
if (keyword != null && keyword.lexeme != 'var') {
158+
if (keyword != null && keyword.lexeme != Keyword.VAR.lexeme) {
156159
builder.addSimpleReplacement(typeRange, '');
157160
} else {
158-
builder.addSimpleReplacement(typeRange, 'var ');
161+
builder.addSimpleReplacement(typeRange, '${Keyword.VAR.lexeme} ');
159162
}
160163
});
161164
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class ReplaceWithVar extends ResolvedCorrectionProducer {
4848
grandparent is TopLevelVariableDeclaration ||
4949
grandparent is FieldDeclaration)) {
5050
var variables = parent.variables;
51+
// This is the job of RemoveTypeAnnotation fix/assist.
52+
if (parent.isConst || parent.isFinal) {
53+
return;
54+
}
5155
if (variables.length != 1) {
5256
return;
5357
}
@@ -88,17 +92,19 @@ class ReplaceWithVar extends ResolvedCorrectionProducer {
8892
return;
8993
}
9094
await builder.addDartFileEdit(file, (builder) {
91-
if (parent.isConst || parent.isFinal) {
92-
builder.addDeletion(range.startStart(type, variables[0]));
93-
} else {
94-
builder.addSimpleReplacement(range.node(type), 'var');
95-
}
95+
builder.addSimpleReplacement(range.node(type), 'var');
96+
9697
if (typeArgumentsText != null && typeArgumentsOffset != null) {
9798
builder.addSimpleInsertion(typeArgumentsOffset, typeArgumentsText);
9899
}
99100
});
100101
} else if (parent is DeclaredIdentifier &&
101102
grandparent is ForEachPartsWithDeclaration) {
103+
// This is the job of RemoveTypeAnnotation fix/assist.
104+
if (parent.isConst || parent.isFinal) {
105+
return;
106+
}
107+
102108
String? typeArgumentsText;
103109
int? typeArgumentsOffset;
104110
if (type is NamedType) {

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,18 @@ final _builtInLintProducers = <LintCode, List<ProducerGenerator>>{
388388
ReplaceNullCheckWithCast.new,
389389
],
390390
LinterLintCode.null_closures: [ReplaceNullWithClosure.new],
391-
LinterLintCode.omit_local_variable_types: [ReplaceWithVar.new],
392-
LinterLintCode.omit_obvious_local_variable_types: [ReplaceWithVar.new],
393-
LinterLintCode.omit_obvious_property_types: [ReplaceWithVar.new],
391+
LinterLintCode.omit_local_variable_types: [
392+
ReplaceWithVar.new,
393+
RemoveTypeAnnotation.other,
394+
],
395+
LinterLintCode.omit_obvious_local_variable_types: [
396+
ReplaceWithVar.new,
397+
RemoveTypeAnnotation.other,
398+
],
399+
LinterLintCode.omit_obvious_property_types: [
400+
ReplaceWithVar.new,
401+
RemoveTypeAnnotation.other,
402+
],
394403
LinterLintCode.prefer_adjacent_string_concatenation: [RemoveOperator.new],
395404
LinterLintCode.prefer_collection_literals: [
396405
ConvertToMapLiteral.new,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class FixProcessorMapTest {
2323
'prefer_collection_literals',
2424
'prefer_const_constructors',
2525
'prefer_inlined_adds',
26+
'omit_local_variable_types',
27+
'omit_obvious_local_variable_types',
28+
'omit_obvious_property_types',
2629
];
2730

2831
void setUp() {

0 commit comments

Comments
 (0)