Skip to content

Commit

Permalink
[DAS] Adds remove type fix to ommit type lints
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
FMorschel authored and Commit Queue committed Feb 26, 2025
1 parent f5dc281 commit 07c6bc6
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ class RemoveTypeAnnotation extends ParsedCorrectionProducer {
parameters: node.parameters,
);
}
if (node is TypeAnnotation && diagnostic != null) {
if (node case TypeAnnotation(:var parent) when diagnostic != null) {
if (parent is VariableDeclarationList) {
return _removeFromDeclarationList(builder, parent);
}
return _removeTypeAnnotation(builder, node);
}
if (node is VariableDeclarationList) {
Expand All @@ -75,11 +78,11 @@ class RemoveTypeAnnotation extends ParsedCorrectionProducer {
}
// ignore if an incomplete variable declaration
if (declarationList.variables.length == 1 &&
declarationList.variables[0].name.isSynthetic) {
declarationList.variables.first.name.isSynthetic) {
return;
}
// must be not after the name of the variable
var firstVariable = declarationList.variables[0];
var firstVariable = declarationList.variables.first;
if (selectionOffset > firstVariable.name.end) {
return;
}
Expand Down Expand Up @@ -129,10 +132,10 @@ class RemoveTypeAnnotation extends ParsedCorrectionProducer {
var keyword = declarationList.keyword;
await builder.addDartFileEdit(file, (builder) {
var typeRange = range.startStart(type, firstVariable);
if (keyword != null && keyword.lexeme != 'var') {
if (keyword != null && keyword.lexeme != Keyword.VAR.lexeme) {
builder.addSimpleReplacement(typeRange, '');
} else {
builder.addSimpleReplacement(typeRange, 'var ');
builder.addSimpleReplacement(typeRange, '${Keyword.VAR.lexeme} ');
}
if (typeArgumentsText != null && typeArgumentsOffset != null) {
builder.addSimpleInsertion(typeArgumentsOffset, typeArgumentsText);
Expand All @@ -152,10 +155,10 @@ class RemoveTypeAnnotation extends ParsedCorrectionProducer {
var variableName = declaration.name;
await builder.addDartFileEdit(file, (builder) {
var typeRange = range.startStart(typeNode, variableName);
if (keyword != null && keyword.lexeme != 'var') {
if (keyword != null && keyword.lexeme != Keyword.VAR.lexeme) {
builder.addSimpleReplacement(typeRange, '');
} else {
builder.addSimpleReplacement(typeRange, 'var ');
builder.addSimpleReplacement(typeRange, '${Keyword.VAR.lexeme} ');
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class ReplaceWithVar extends ResolvedCorrectionProducer {
grandparent is TopLevelVariableDeclaration ||
grandparent is FieldDeclaration)) {
var variables = parent.variables;
// This is the job of RemoveTypeAnnotation fix/assist.
if (parent.isConst || parent.isFinal) {
return;
}
if (variables.length != 1) {
return;
}
Expand Down Expand Up @@ -88,17 +92,19 @@ class ReplaceWithVar extends ResolvedCorrectionProducer {
return;
}
await builder.addDartFileEdit(file, (builder) {
if (parent.isConst || parent.isFinal) {
builder.addDeletion(range.startStart(type, variables[0]));
} else {
builder.addSimpleReplacement(range.node(type), 'var');
}
builder.addSimpleReplacement(range.node(type), 'var');

if (typeArgumentsText != null && typeArgumentsOffset != null) {
builder.addSimpleInsertion(typeArgumentsOffset, typeArgumentsText);
}
});
} else if (parent is DeclaredIdentifier &&
grandparent is ForEachPartsWithDeclaration) {
// This is the job of RemoveTypeAnnotation fix/assist.
if (parent.isConst || parent.isFinal) {
return;
}

String? typeArgumentsText;
int? typeArgumentsOffset;
if (type is NamedType) {
Expand Down
15 changes: 12 additions & 3 deletions pkg/analysis_server/lib/src/services/correction/fix_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,18 @@ final _builtInLintProducers = <LintCode, List<ProducerGenerator>>{
ReplaceNullCheckWithCast.new,
],
LinterLintCode.null_closures: [ReplaceNullWithClosure.new],
LinterLintCode.omit_local_variable_types: [ReplaceWithVar.new],
LinterLintCode.omit_obvious_local_variable_types: [ReplaceWithVar.new],
LinterLintCode.omit_obvious_property_types: [ReplaceWithVar.new],
LinterLintCode.omit_local_variable_types: [
ReplaceWithVar.new,
RemoveTypeAnnotation.other,
],
LinterLintCode.omit_obvious_local_variable_types: [
ReplaceWithVar.new,
RemoveTypeAnnotation.other,
],
LinterLintCode.omit_obvious_property_types: [
ReplaceWithVar.new,
RemoveTypeAnnotation.other,
],
LinterLintCode.prefer_adjacent_string_concatenation: [RemoveOperator.new],
LinterLintCode.prefer_collection_literals: [
ConvertToMapLiteral.new,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class FixProcessorMapTest {
'prefer_collection_literals',
'prefer_const_constructors',
'prefer_inlined_adds',
'omit_local_variable_types',
'omit_obvious_local_variable_types',
'omit_obvious_property_types',
];

void setUp() {
Expand Down
Loading

0 comments on commit 07c6bc6

Please sign in to comment.