Migrate several lib/src/services/correction/dart/
Change-Id: I49f43e801f13dbbfe67be79fea19073990acb658
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194962
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/make_class_abstract.dart b/pkg/analysis_server/lib/src/services/correction/dart/make_class_abstract.dart
index 416e4fa..a06ba35 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/make_class_abstract.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/make_class_abstract.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -11,7 +9,7 @@
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
class MakeClassAbstract extends CorrectionProducer {
- String _className;
+ String _className = '';
@override
List<Object> get fixArguments => [_className];
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart b/pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart
index ab137eb..a2c636a 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart
@@ -2,12 +2,10 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
+import 'package:analysis_server/src/utilities/extensions/ast.dart';
import 'package:analyzer/dart/ast/ast.dart';
-import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
@@ -15,7 +13,7 @@
import 'package:analyzer_plugin/utilities/range_factory.dart';
class MakeFieldNotFinal extends CorrectionProducer {
- String _fieldName;
+ String _fieldName = '';
@override
List<Object> get fixArguments => [_fieldName];
@@ -26,40 +24,72 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
- if (node is SimpleIdentifier &&
- node.writeOrReadElement is PropertyAccessorElement) {
- PropertyAccessorElement getter = node.writeOrReadElement;
- if (getter.isGetter &&
- getter.isSynthetic &&
- !getter.variable.isSynthetic &&
- getter.variable.setter == null &&
- getter.enclosingElement is ClassElement) {
- var declarationResult =
- await sessionHelper.getElementDeclaration(getter.variable);
- var variable = declarationResult.node;
- if (variable is VariableDeclaration &&
- variable.parent is VariableDeclarationList &&
- variable.parent.parent is FieldDeclaration) {
- VariableDeclarationList declarationList = variable.parent;
- var keywordToken = declarationList.keyword;
- if (declarationList.variables.length == 1 &&
- keywordToken.keyword == Keyword.FINAL) {
- await builder.addDartFileEdit(file, (builder) {
- if (declarationList.type != null) {
- builder.addDeletion(
- range.startStart(keywordToken, declarationList.type));
- } else {
- builder.addReplacement(range.startStart(keywordToken, variable),
- (builder) {
- builder.write('var ');
- });
- }
- });
- _fieldName = getter.variable.displayName;
- }
- }
- }
+ if (node is! SimpleIdentifier) {
+ return;
}
+
+ var getter = node.writeOrReadElement;
+ if (getter is! PropertyAccessorElement) {
+ return;
+ }
+
+ // The accessor must be a getter, and it must be synthetic.
+ if (!(getter.isGetter && getter.isSynthetic)) {
+ return;
+ }
+
+ // The variable must be not synthetic, and have no setter yet.
+ var variable = getter.variable;
+ if (variable.isSynthetic || variable.setter != null) {
+ return;
+ }
+
+ // It must be a field declaration.
+ if (getter.enclosingElement is! ClassElement) {
+ return;
+ }
+
+ var declaration = await sessionHelper.getElementDeclaration(variable);
+ var variableNode = declaration?.node;
+ if (variableNode is! VariableDeclaration) {
+ return;
+ }
+
+ // The declaration list must have exactly one variable.
+ var declarationList = variableNode.parent;
+ if (declarationList is! VariableDeclarationList) {
+ return;
+ }
+ if (declarationList.variables.length != 1) {
+ return;
+ }
+
+ // It must be a field declaration.
+ if (declarationList.parent is! FieldDeclaration) {
+ return;
+ }
+
+ var finalKeyword = declarationList.finalKeyword;
+ if (finalKeyword == null) {
+ return;
+ }
+
+ _fieldName = variable.displayName;
+ await builder.addDartFileEdit(file, (builder) {
+ var typeAnnotation = declarationList.type;
+ if (typeAnnotation != null) {
+ builder.addDeletion(
+ range.startStart(finalKeyword, typeAnnotation),
+ );
+ } else {
+ builder.addReplacement(
+ range.startStart(finalKeyword, variableNode),
+ (builder) {
+ builder.write('var ');
+ },
+ );
+ }
+ });
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/make_final.dart b/pkg/analysis_server/lib/src/services/correction/dart/make_final.dart
index f3063c1..3ff063a 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/make_final.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/make_final.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -22,41 +20,47 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
+ var parent = node.parent;
+ var grandParent = parent?.parent;
+
if (node is SimpleIdentifier &&
- node.parent is DeclaredIdentifier &&
- node.parent.parent is ForEachPartsWithDeclaration) {
- var declaration = node.parent as DeclaredIdentifier;
+ parent is DeclaredIdentifier &&
+ grandParent is ForEachPartsWithDeclaration) {
await builder.addDartFileEdit(file, (builder) {
- if (declaration.keyword?.keyword == Keyword.VAR) {
- builder.addSimpleReplacement(
- range.token(declaration.keyword), 'final');
- } else if (declaration.keyword == null) {
- builder.addSimpleInsertion(declaration.offset, 'final ');
+ var keyword = parent.keyword;
+ if (keyword != null && keyword.keyword == Keyword.VAR) {
+ builder.addSimpleReplacement(range.token(keyword), 'final');
+ } else if (keyword == null) {
+ builder.addSimpleInsertion(parent.offset, 'final ');
}
});
return;
}
+
VariableDeclarationList list;
if (node is SimpleIdentifier &&
- node.parent is VariableDeclaration &&
- node.parent.parent is VariableDeclarationList) {
- list = node.parent.parent;
+ parent is VariableDeclaration &&
+ grandParent is VariableDeclarationList) {
+ list = grandParent;
} else if (node is VariableDeclaration &&
- node.parent is VariableDeclarationList) {
- list = node.parent;
+ parent is VariableDeclarationList) {
+ list = parent;
+ } else {
+ return;
}
- if (list != null) {
- if (list.variables.length == 1) {
- await builder.addDartFileEdit(file, (builder) {
- if (list.keyword?.keyword == Keyword.VAR) {
- builder.addSimpleReplacement(range.token(list.keyword), 'final');
- } else if (list.lateKeyword != null) {
- builder.addSimpleInsertion(list.lateKeyword.end, ' final');
- } else if (list.keyword == null) {
- builder.addSimpleInsertion(list.offset, 'final ');
- }
- });
- }
+
+ if (list.variables.length == 1) {
+ await builder.addDartFileEdit(file, (builder) {
+ var keyword = list.keyword;
+ var lateKeyword = list.lateKeyword;
+ if (keyword != null && keyword.keyword == Keyword.VAR) {
+ builder.addSimpleReplacement(range.token(keyword), 'final');
+ } else if (lateKeyword != null) {
+ builder.addSimpleInsertion(lateKeyword.end, ' final');
+ } else if (keyword == null) {
+ builder.addSimpleInsertion(list.offset, 'final ');
+ }
+ });
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/make_return_type_nullable.dart b/pkg/analysis_server/lib/src/services/correction/dart/make_return_type_nullable.dart
index bdc633e..20b6cac 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/make_return_type_nullable.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/make_return_type_nullable.dart
@@ -2,12 +2,11 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
+import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -20,10 +19,16 @@
if (!unit.featureSet.isEnabled(Feature.non_nullable)) {
return;
}
+
+ var node = this.node;
if (node is! Expression) {
return;
}
+
var body = node.thisOrAncestorOfType<FunctionBody>();
+ if (body == null) {
+ return;
+ }
var returnType = _getReturnTypeNode(body);
if (returnType == null) {
@@ -34,7 +39,7 @@
if (returnType is! NamedType) {
return null;
}
- var typeArguments = (returnType as NamedType).typeArguments;
+ var typeArguments = returnType.typeArguments;
if (typeArguments == null) {
return null;
}
@@ -44,20 +49,23 @@
}
returnType = arguments[0];
}
+
if (node is! NullLiteral &&
- !typeSystem.isAssignableTo(returnType.type,
- typeSystem.promoteToNonNull((node as Expression).staticType))) {
+ !typeSystem.isAssignableTo(returnType.typeOrThrow,
+ typeSystem.promoteToNonNull(node.typeOrThrow))) {
return;
}
+
+ final returnType_final = returnType;
await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleInsertion(returnType.end, '?');
+ builder.addSimpleInsertion(returnType_final.end, '?');
});
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static MakeReturnTypeNullable newInstance() => MakeReturnTypeNullable();
- static TypeAnnotation _getReturnTypeNode(FunctionBody body) {
+ static TypeAnnotation? _getReturnTypeNode(FunctionBody body) {
var function = body.parent;
if (function is FunctionExpression) {
function = function.parent;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/make_variable_not_final.dart b/pkg/analysis_server/lib/src/services/correction/dart/make_variable_not_final.dart
index 4eb043f..0ac959f 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/make_variable_not_final.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/make_variable_not_final.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:_fe_analyzer_shared/src/scanner/token.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -15,7 +13,7 @@
import 'package:analyzer_plugin/utilities/range_factory.dart';
class MakeVariableNotFinal extends CorrectionProducer {
- String _variableName;
+ String _variableName = '';
@override
List<Object> get fixArguments => [_variableName];
@@ -26,28 +24,36 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
- if (node is SimpleIdentifier &&
- node.staticElement is LocalVariableElement) {
- LocalVariableElement variable = node.staticElement;
- var id = NodeLocator(variable.nameOffset).searchWithin(unit);
- var decl = id?.parent;
- if (decl is VariableDeclaration &&
- decl.parent is VariableDeclarationList) {
- VariableDeclarationList declarationList = decl.parent;
- var keywordToken = declarationList.keyword;
- if (declarationList.variables.length == 1 &&
- keywordToken.keyword == Keyword.FINAL) {
- await builder.addDartFileEdit(file, (builder) {
- if (declarationList.type != null) {
- builder.addDeletion(
- range.startStart(keywordToken, declarationList.type));
- } else {
- builder.addSimpleReplacement(range.token(keywordToken), 'var');
- }
- });
- declarationList.variables[0].name.name;
- _variableName = declarationList.variables[0].name.name;
- }
+ if (node is! SimpleIdentifier) {
+ return;
+ }
+
+ var variable = node.staticElement;
+ if (variable is! LocalVariableElement) {
+ return;
+ }
+
+ var id = NodeLocator(variable.nameOffset).searchWithin(unit);
+ var declaration = id?.parent;
+ var declarationList = declaration?.parent;
+
+ if (declaration is VariableDeclaration &&
+ declarationList is VariableDeclarationList) {
+ var keywordToken = declarationList.keyword;
+ if (declarationList.variables.length == 1 &&
+ keywordToken != null &&
+ keywordToken.keyword == Keyword.FINAL) {
+ await builder.addDartFileEdit(file, (builder) {
+ var typeAnnotation = declarationList.type;
+ if (typeAnnotation != null) {
+ builder.addDeletion(
+ range.startStart(keywordToken, typeAnnotation),
+ );
+ } else {
+ builder.addSimpleReplacement(range.token(keywordToken), 'var');
+ }
+ });
+ _variableName = variable.name;
}
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/make_variable_nullable.dart b/pkg/analysis_server/lib/src/services/correction/dart/make_variable_nullable.dart
index 62f8696..297bf72 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/make_variable_nullable.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/make_variable_nullable.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/analysis/features.dart';
@@ -12,6 +10,7 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
+import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -19,7 +18,7 @@
class MakeVariableNullable extends CorrectionProducer {
/// The name of the variable whose type is to be made nullable.
- String _variableName;
+ String _variableName = '';
@override
List<Object> get fixArguments => [_variableName];
@@ -39,10 +38,13 @@
await _forFunctionTypedFormalParameter(builder, node, parent);
} else if (node is SimpleIdentifier && parent is FieldFormalParameter) {
await _forFieldFormalParameter(builder, node, parent);
- } else if (parent is AssignmentExpression &&
+ } else if (node is Expression &&
+ parent is AssignmentExpression &&
parent.rightHandSide == node) {
- await _forAssignment(builder, node as Expression, parent);
- } else if (parent is VariableDeclaration && parent.initializer == node) {
+ await _forAssignment(builder, node, parent);
+ } else if (node is Expression &&
+ parent is VariableDeclaration &&
+ parent.initializer == node) {
await _forVariableDeclaration(builder, node, parent);
}
}
@@ -51,54 +53,59 @@
/// Return the list of variable declarations containing the declaration of the
/// given [variable] that is located in the given [block] or in a surrounding
/// block. Return `null` if the declaration can't be found.
- VariableDeclarationList _findDeclaration(
- LocalVariableElement variable, Block block) {
- if (variable == null) {
- return null;
- }
+ VariableDeclarationList? _findDeclaration(
+ LocalVariableElement variable, Block? block) {
var currentBlock = block;
while (currentBlock != null) {
- for (var statement in block.statements) {
+ for (var statement in currentBlock.statements) {
if (statement is VariableDeclarationStatement) {
var variableList = statement.variables;
- if (variableList != null) {
- var variables = variableList.variables;
- for (var declaration in variables) {
- if (declaration.declaredElement == variable) {
- return variableList;
- }
+ for (var declaration in variableList.variables) {
+ if (declaration.declaredElement == variable) {
+ return variableList;
}
}
}
}
- currentBlock = currentBlock.parent.thisOrAncestorOfType<Block>();
+ currentBlock = currentBlock.parent?.thisOrAncestorOfType<Block>();
}
return null;
}
- Future<void> _forAssignment(ChangeBuilder builder, Expression node,
+ Future<void> _forAssignment(ChangeBuilder builder, Expression rightHandSide,
AssignmentExpression parent) async {
var leftHandSide = parent.leftHandSide;
- if (leftHandSide is SimpleIdentifier) {
- var element = leftHandSide.staticElement;
- if (element is LocalVariableElement) {
- var oldType = element.type;
- var newType = node.staticType;
- if (node is NullLiteral) {
- newType = (oldType as InterfaceTypeImpl)
- .withNullability(NullabilitySuffix.question);
- } else if (!typeSystem.isAssignableTo(
- oldType, typeSystem.promoteToNonNull(newType))) {
- return;
- }
- var declarationList =
- _findDeclaration(element, parent.thisOrAncestorOfType<Block>());
- if (declarationList == null || declarationList.variables.length > 1) {
- return;
- }
- await _updateVariableType(builder, declarationList, newType);
- }
+ if (leftHandSide is! SimpleIdentifier) {
+ return;
}
+
+ var element = leftHandSide.staticElement;
+ if (element is! LocalVariableElement) {
+ return;
+ }
+
+ var oldType = element.type;
+ if (oldType is! InterfaceTypeImpl) {
+ return;
+ }
+
+ var newType = rightHandSide.typeOrThrow;
+ if (rightHandSide is NullLiteral) {
+ newType = oldType.withNullability(NullabilitySuffix.question);
+ } else if (!typeSystem.isAssignableTo(
+ oldType, typeSystem.promoteToNonNull(newType))) {
+ return;
+ }
+
+ var declarationList = _findDeclaration(
+ element,
+ parent.thisOrAncestorOfType<Block>(),
+ );
+ if (declarationList == null || declarationList.variables.length > 1) {
+ return;
+ }
+
+ await _updateVariableType(builder, declarationList, newType);
}
/// Makes [parameter] nullable if possible.
@@ -115,12 +122,13 @@
builder.addSimpleInsertion(parameter.endToken.end, '?');
});
} else {
- if (!_typeCanBeMadeNullable(parameter.type)) {
+ var type = parameter.type;
+ if (type == null || !_typeCanBeMadeNullable(type)) {
return;
}
_variableName = parameter.identifier.name;
await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleInsertion(parameter.type.end, '?');
+ builder.addSimpleInsertion(type.end, '?');
});
}
}
@@ -140,43 +148,50 @@
Future<void> _forSimpleFormalParameter(ChangeBuilder builder,
SimpleIdentifier name, SimpleFormalParameter parameter) async {
- if (!_typeCanBeMadeNullable(parameter.type)) {
+ var type = parameter.type;
+ if (type == null || !_typeCanBeMadeNullable(type)) {
return;
}
- _variableName = parameter.identifier.name;
+
+ var identifier = parameter.identifier;
+ if (identifier == null) {
+ return;
+ }
+
+ _variableName = identifier.name;
await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleInsertion(parameter.type.end, '?');
+ builder.addSimpleInsertion(type.end, '?');
});
}
Future<void> _forVariableDeclaration(ChangeBuilder builder, Expression node,
VariableDeclaration parent) async {
- var grandParent = parent.parent;
- if (grandParent is! VariableDeclarationList) {
+ var declarationList = parent.parent;
+ if (declarationList is! VariableDeclarationList) {
return;
}
- var declarationList = grandParent as VariableDeclarationList;
if (declarationList.variables.length > 1) {
return;
}
- var oldType = parent.declaredElement.type;
- var newType = node.staticType;
+
+ var oldType = parent.declaredElement!.type;
+ if (oldType is! InterfaceTypeImpl) {
+ return;
+ }
+
+ var newType = node.typeOrThrow;
if (node is NullLiteral) {
- newType = (oldType as InterfaceTypeImpl)
- .withNullability(NullabilitySuffix.question);
+ newType = oldType.withNullability(NullabilitySuffix.question);
} else if (!typeSystem.isAssignableTo(
oldType, typeSystem.promoteToNonNull(newType))) {
return;
}
+
await _updateVariableType(builder, declarationList, newType);
}
bool _typeCanBeMadeNullable(TypeAnnotation typeAnnotation) {
- // Ensure that there is a type annotation.
- if (typeAnnotation == null) {
- return false;
- }
- if (typeSystem.isNullable(typeAnnotation.type)) {
+ if (typeSystem.isNullable(typeAnnotation.typeOrThrow)) {
return false;
}
return true;
@@ -195,13 +210,14 @@
builder.writeType(newType);
});
} else if (keyword == null) {
- if (declarationList.type == null) {
+ var typeAnnotation = declarationList.type;
+ if (typeAnnotation == null) {
builder.addInsertion(variable.offset, (builder) {
builder.writeType(newType);
builder.write(' ');
});
} else {
- builder.addSimpleInsertion(declarationList.type.end, '?');
+ builder.addSimpleInsertion(typeAnnotation.end, '?');
}
}
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/move_type_arguments_to_class.dart b/pkg/analysis_server/lib/src/services/correction/dart/move_type_arguments_to_class.dart
index 95503c8..0230ec5 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/move_type_arguments_to_class.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/move_type_arguments_to_class.dart
@@ -2,12 +2,11 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
+import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -18,26 +17,29 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (coveredNode is TypeArgumentList) {
- TypeArgumentList typeArguments = coveredNode;
- if (typeArguments.parent is! InstanceCreationExpression) {
- return;
- }
- InstanceCreationExpression creation = typeArguments.parent;
- var typeName = creation.constructorName.type;
- if (typeName.typeArguments != null) {
- return;
- }
- var element = typeName.type.element;
- if (element is ClassElement &&
- element.typeParameters != null &&
- element.typeParameters.length == typeArguments.arguments.length) {
- await builder.addDartFileEdit(file, (builder) {
- var argumentText = utils.getNodeText(typeArguments);
- builder.addSimpleInsertion(typeName.end, argumentText);
- builder.addDeletion(range.node(typeArguments));
- });
- }
+ var typeArguments = coveredNode;
+ if (typeArguments is! TypeArgumentList) {
+ return;
+ }
+
+ var creation = typeArguments.parent;
+ if (creation is! InstanceCreationExpression) {
+ return;
+ }
+
+ var typeName = creation.constructorName.type;
+ if (typeName.typeArguments != null) {
+ return;
+ }
+
+ var element = typeName.typeOrThrow.element;
+ if (element is ClassElement &&
+ element.typeParameters.length == typeArguments.arguments.length) {
+ await builder.addDartFileEdit(file, (builder) {
+ var argumentText = utils.getNodeText(typeArguments);
+ builder.addSimpleInsertion(typeName.end, argumentText);
+ builder.addDeletion(range.node(typeArguments));
+ });
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/organize_imports.dart b/pkg/analysis_server/lib/src/services/correction/dart/organize_imports.dart
index fc2aa60..0bbe355 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/organize_imports.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/organize_imports.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server/src/services/correction/organize_imports.dart';
@@ -18,7 +16,7 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var organizer =
- ImportOrganizer(resolvedResult.content, unit, resolvedResult.errors);
+ ImportOrganizer(resolvedResult.content!, unit, resolvedResult.errors);
// todo (pq): consider restructuring organizer to allow a passed-in change
// builder
for (var edit in organizer.organize()) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/qualify_reference.dart b/pkg/analysis_server/lib/src/services/correction/dart/qualify_reference.dart
index 968562b..4756866 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/qualify_reference.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/qualify_reference.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -11,7 +9,7 @@
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
class QualifyReference extends CorrectionProducer {
- String _qualifiedName;
+ String _qualifiedName = '';
@override
List<Object> get fixArguments => [_qualifiedName];
@@ -21,12 +19,13 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is! SimpleIdentifier) {
+ var memberName = node;
+ if (memberName is! SimpleIdentifier) {
return;
}
- SimpleIdentifier memberName = node;
+
+ AstNode? target;
var parent = node.parent;
- AstNode target;
if (parent is MethodInvocation && node == parent.methodName) {
target = parent.target;
} else if (parent is PropertyAccess && node == parent.propertyName) {
@@ -35,14 +34,22 @@
if (target != null) {
return;
}
- var enclosingElement = memberName.staticElement.enclosingElement;
- if (enclosingElement.library != libraryElement) {
+
+ var memberElement = memberName.staticElement;
+ if (memberElement == null) {
+ return;
+ }
+
+ var enclosingElement = memberElement.enclosingElement;
+ if (enclosingElement == null ||
+ enclosingElement.library != libraryElement) {
// TODO(brianwilkerson) Support qualifying references to members defined
// in other libraries. `DartEditBuilder` currently defines the method
// `writeType`, which is close, but we also need to handle extensions,
// which don't have a type.
return;
}
+
var containerName = enclosingElement.name;
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleInsertion(node.offset, '$containerName.');
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart
index 1ab65d1..17594f4 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -11,9 +9,10 @@
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
+import 'package:collection/collection.dart';
class RemoveAnnotation extends CorrectionProducer {
- String _annotationName;
+ String _annotationName = '';
@override
List<Object> get fixArguments => [_annotationName];
@@ -23,11 +22,11 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- Future<void> addFix(Annotation node) async {
+ Future<void> addFix(Annotation? node) async {
if (node == null) {
return;
}
- var followingToken = node.endToken.next;
+ var followingToken = node.endToken.next!;
followingToken = followingToken.precedingComments ?? followingToken;
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(range.startStart(node, followingToken));
@@ -35,11 +34,10 @@
_annotationName = node.name.name;
}
- Annotation findAnnotation(
- NodeList<Annotation> metadata, String targetName) {
- return metadata.firstWhere(
- (annotation) => annotation.name.name == targetName,
- orElse: () => null);
+ Annotation? findAnnotation(List<Annotation> metadata, String targetName) {
+ return metadata.firstWhereOrNull(
+ (annotation) => annotation.name.name == targetName,
+ );
}
var node = coveredNode;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_argument.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_argument.dart
index d7a3eda..64c4273 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_argument.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_argument.dart
@@ -2,10 +2,9 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
+import 'package:analysis_server/src/services/correction/util.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -21,16 +20,21 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var arg = node;
- if (arg.parent is NamedExpression) {
- arg = arg.parent;
+ if (arg is! Expression) {
+ return;
}
- var argumentList = arg.parent.thisOrAncestorOfType<ArgumentList>();
- if (argumentList != null) {
- await builder.addDartFileEdit(file, (builder) {
- final sourceRange = range.nodeInList(argumentList.arguments, arg);
- builder.addDeletion(sourceRange);
- });
+
+ arg = stepUpNamedExpression(arg);
+
+ var argumentList = arg.parent?.thisOrAncestorOfType<ArgumentList>();
+ if (argumentList == null) {
+ return;
}
+
+ await builder.addDartFileEdit(file, (builder) {
+ final sourceRange = range.nodeInList(argumentList.arguments, arg);
+ builder.addDeletion(sourceRange);
+ });
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_await.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_await.dart
index c25a7ae..b09bb6e 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_await.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_await.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -24,7 +22,7 @@
if (awaitExpression is AwaitExpression) {
final awaitToken = awaitExpression.awaitKeyword;
await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(range.startStart(awaitToken, awaitToken.next));
+ builder.addDeletion(range.startStart(awaitToken, awaitToken.next!));
});
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_comparison.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_comparison.dart
index 617d8e9..77e8d88 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_comparison.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_comparison.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -31,10 +29,10 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is! BinaryExpression) {
+ var binaryExpression = node;
+ if (binaryExpression is! BinaryExpression) {
return;
}
- var binaryExpression = node as BinaryExpression;
var parent = binaryExpression.parent;
if (parent is AssertInitializer && _conditionIsTrue) {
var constructor = parent.parent as ConstructorDeclaration;
@@ -55,10 +53,10 @@
} else if (parent is BinaryExpression) {
if (parent.operator.type == TokenType.AMPERSAND_AMPERSAND &&
_conditionIsTrue) {
- await _removeOperatorAndOperand(builder, parent, node);
+ await _removeOperatorAndOperand(builder, parent, binaryExpression);
} else if (parent.operator.type == TokenType.BAR_BAR &&
_conditionIsFalse) {
- await _removeOperatorAndOperand(builder, parent, node);
+ await _removeOperatorAndOperand(builder, parent, binaryExpression);
}
} else if (parent is IfStatement) {
if (parent.elseStatement == null && _conditionIsTrue) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_const.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_const.dart
index c540cec..0cede77 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_const.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_const.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -37,7 +35,7 @@
Future<void> compute(ChangeBuilder builder) async {
final expression = node;
- Token constToken;
+ Token? constToken;
if (expression is InstanceCreationExpression) {
constToken = expression.keyword;
} else if (expression is TypedLiteralImpl) {
@@ -49,8 +47,14 @@
return;
}
+ final constToken_final = constToken;
await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(range.startStart(constToken, constToken.next));
+ builder.addDeletion(
+ range.startStart(
+ constToken_final,
+ constToken_final.next!,
+ ),
+ );
});
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_dead_code.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_dead_code.dart
index 3fadc64..3c37c95 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_dead_code.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_dead_code.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -18,9 +16,10 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var coveringNode = coveredNode;
- if (coveringNode is Expression) {
- var parent = coveredNode.parent;
+ var coveredNode = this.coveredNode;
+ var parent = coveredNode?.parent;
+
+ if (coveredNode is Expression) {
if (parent is BinaryExpression) {
if (parent.rightOperand == coveredNode) {
await builder.addDartFileEdit(file, (builder) {
@@ -28,10 +27,13 @@
});
}
}
- } else if (coveringNode is Block) {
- var block = coveringNode;
+ } else if (coveredNode is Block) {
+ var block = coveredNode;
var statementsToRemove = <Statement>[];
- var problemMessage = diagnostic.problemMessage;
+ var problemMessage = diagnostic?.problemMessage;
+ if (problemMessage == null) {
+ return;
+ }
var errorRange =
SourceRange(problemMessage.offset, problemMessage.length);
for (var statement in block.statements) {
@@ -45,19 +47,18 @@
builder.addDeletion(rangeToRemove);
});
}
- } else if (coveringNode is Statement) {
+ } else if (coveredNode is Statement) {
var rangeToRemove =
- utils.getLinesRangeStatements(<Statement>[coveringNode]);
+ utils.getLinesRangeStatements(<Statement>[coveredNode]);
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(rangeToRemove);
});
- } else if (coveringNode is CatchClause) {
- TryStatement tryStatement = coveringNode.parent;
- var catchClauses = tryStatement.catchClauses;
- var index = catchClauses.indexOf(coveringNode);
- var previous = index == 0 ? tryStatement.body : catchClauses[index - 1];
+ } else if (coveredNode is CatchClause && parent is TryStatement) {
+ var catchClauses = parent.catchClauses;
+ var index = catchClauses.indexOf(coveredNode);
+ var previous = index == 0 ? parent.body : catchClauses[index - 1];
await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(range.endEnd(previous, coveringNode));
+ builder.addDeletion(range.endEnd(previous, coveredNode));
});
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_dead_if_null.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_dead_if_null.dart
index cff4eb2..4424d19 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_dead_if_null.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_dead_if_null.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -31,7 +29,7 @@
}
/// Finds the dead if-null expression above [node].
- SourceRange findIfNull() {
+ SourceRange? findIfNull() {
var child = node;
var parent = node.parent;
while (parent != null) {
@@ -44,10 +42,11 @@
parent.operator.type == TokenType.QUESTION_QUESTION_EQ &&
parent.rightHandSide == child) {
var assignee = parent.leftHandSide;
- if (parent.parent is ExpressionStatement &&
+ var grandParent = parent.parent;
+ if (grandParent is ExpressionStatement &&
assignee is SimpleIdentifier &&
assignee.staticElement is PromotableElement) {
- return utils.getLinesRange(range.node(parent.parent));
+ return utils.getLinesRange(range.node(grandParent));
} else {
return range.endEnd(parent.leftHandSide, parent.rightHandSide);
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_duplicate_case.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_duplicate_case.dart
index d6146ef..c3e74d0 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_duplicate_case.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_duplicate_case.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -22,20 +20,26 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var node = coveredNode;
- if (node is SwitchCase) {
- var parent = node.parent as SwitchStatement;
- var members = parent.members;
- var index = members.indexOf(node);
- await builder.addDartFileEdit(file, (builder) {
- SourceRange deletionRange;
- if (index > 0 && members[index - 1].statements.isNotEmpty) {
- deletionRange = range.node(node);
- } else {
- deletionRange = range.startEnd(node, node.colon);
- }
- builder.addDeletion(utils.getLinesRange(deletionRange));
- });
+ if (node is! SwitchCase) {
+ return;
}
+
+ var switchStatement = node.parent;
+ if (switchStatement is! SwitchStatement) {
+ return;
+ }
+
+ var members = switchStatement.members;
+ var index = members.indexOf(node);
+ await builder.addDartFileEdit(file, (builder) {
+ SourceRange deletionRange;
+ if (index > 0 && members[index - 1].statements.isNotEmpty) {
+ deletionRange = range.node(node);
+ } else {
+ deletionRange = range.startEnd(node, node.colon);
+ }
+ builder.addDeletion(utils.getLinesRange(deletionRange));
+ });
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_catch.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_catch.dart
index 394ee32..c2ac9e5 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_catch.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_catch.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_constructor_body.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_constructor_body.dart
index b72c2f2..c0d3aa6 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_constructor_body.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_constructor_body.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -20,14 +18,15 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- await builder.addDartFileEdit(file, (builder) {
- if (node is Block && node.parent is BlockFunctionBody) {
+ var parent = node.parent;
+ if (node is Block && parent is BlockFunctionBody) {
+ await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(
- utils.getLinesRange(range.node(node.parent)),
+ utils.getLinesRange(range.node(parent)),
';',
);
- }
- });
+ });
+ }
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_else.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_else.dart
index aab2666..e45ff55 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_else.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_else.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -22,10 +20,17 @@
Future<void> compute(ChangeBuilder builder) async {
var parent = node.parent;
if (parent is IfStatement) {
- await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(utils.getLinesRange(
- range.startEnd(parent.elseKeyword, parent.elseStatement)));
- });
+ var elseKeyword = parent.elseKeyword;
+ var elseStatement = parent.elseStatement;
+ if (elseKeyword != null && elseStatement != null) {
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addDeletion(
+ utils.getLinesRange(
+ range.startEnd(elseKeyword, elseStatement),
+ ),
+ );
+ });
+ }
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_statement.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_statement.dart
index ecd86e3..e45057a 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_statement.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_empty_statement.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_if_null_operator.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_if_null_operator.dart
index 9397912..5d75e08 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_if_null_operator.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_if_null_operator.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_initializer.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_initializer.dart
index c1b1080..ffeab97 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_initializer.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_initializer.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -23,17 +21,24 @@
var parameter = node.thisOrAncestorOfType<DefaultFormalParameter>();
if (parameter != null) {
// Handle formal parameters with default values.
- await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(
- range.endEnd(parameter.identifier, parameter.defaultValue));
- });
+ var identifier = parameter.identifier;
+ var defaultValue = parameter.defaultValue;
+ if (identifier != null && defaultValue != null) {
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addDeletion(
+ range.endEnd(identifier, defaultValue),
+ );
+ });
+ }
} else {
// Handle variable declarations with default values.
var variable = node.thisOrAncestorOfType<VariableDeclaration>();
- if (variable != null) {
+ var initializer = variable?.initializer;
+ if (variable != null && initializer != null) {
await builder.addDartFileEdit(file, (builder) {
- builder
- .addDeletion(range.endEnd(variable.name, variable.initializer));
+ builder.addDeletion(
+ range.endEnd(variable.name, initializer),
+ );
});
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_interpolation_braces.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_interpolation_braces.dart
index 8d2a673..d63185f 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_interpolation_braces.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_interpolation_braces.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -23,7 +21,7 @@
var node = this.node;
if (node is InterpolationExpression) {
var right = node.rightBracket;
- if (node.expression != null && right != null) {
+ if (right != null) {
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(
range.startStart(node, node.expression), r'$');
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_method_declaration.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_method_declaration.dart
index d9c0e81..db3ac40 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_method_declaration.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_method_declaration.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_name_from_combinator.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_name_from_combinator.dart
index 107f650..b6da5d0 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_name_from_combinator.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_name_from_combinator.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -13,7 +11,7 @@
import 'package:analyzer_plugin/utilities/range_factory.dart';
class RemoveNameFromCombinator extends CorrectionProducer {
- String _combinatorKind;
+ String _combinatorKind = '';
@override
List<Object> get fixArguments => [_combinatorKind];
@@ -23,54 +21,9 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- SourceRange rangeForCombinator(Combinator combinator) {
- var parent = combinator.parent;
- if (parent is NamespaceDirective) {
- var combinators = parent.combinators;
- if (combinators.length == 1) {
- var previousToken =
- combinator.parent.findPrevious(combinator.beginToken);
- if (previousToken != null) {
- return range.endEnd(previousToken, combinator);
- }
- return null;
- }
- var index = combinators.indexOf(combinator);
- if (index < 0) {
- return null;
- } else if (index == combinators.length - 1) {
- return range.endEnd(combinators[index - 1], combinator);
- }
- return range.startStart(combinator, combinators[index + 1]);
- }
- return null;
- }
-
- SourceRange rangeForNameInCombinator(
- Combinator combinator, SimpleIdentifier name) {
- NodeList<SimpleIdentifier> names;
- if (combinator is HideCombinator) {
- names = combinator.hiddenNames;
- } else if (combinator is ShowCombinator) {
- names = combinator.shownNames;
- } else {
- return null;
- }
- if (names.length == 1) {
- return rangeForCombinator(combinator);
- }
- var index = names.indexOf(name);
- if (index < 0) {
- return null;
- } else if (index == names.length - 1) {
- return range.endEnd(names[index - 1], name);
- }
- return range.startStart(name, names[index + 1]);
- }
-
var node = coveredNode;
if (node is SimpleIdentifier) {
- var parent = coveredNode.parent;
+ var parent = node.parent;
if (parent is Combinator) {
var rangeToRemove = rangeForNameInCombinator(parent, node);
if (rangeToRemove == null) {
@@ -86,4 +39,49 @@
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static RemoveNameFromCombinator newInstance() => RemoveNameFromCombinator();
+
+ static SourceRange? rangeForCombinator(Combinator combinator) {
+ var parent = combinator.parent;
+ if (parent is NamespaceDirective) {
+ var combinators = parent.combinators;
+ if (combinators.length == 1) {
+ var previousToken =
+ combinator.parent?.findPrevious(combinator.beginToken);
+ if (previousToken != null) {
+ return range.endEnd(previousToken, combinator);
+ }
+ return null;
+ }
+ var index = combinators.indexOf(combinator);
+ if (index < 0) {
+ return null;
+ } else if (index == combinators.length - 1) {
+ return range.endEnd(combinators[index - 1], combinator);
+ }
+ return range.startStart(combinator, combinators[index + 1]);
+ }
+ return null;
+ }
+
+ static SourceRange? rangeForNameInCombinator(
+ Combinator combinator, SimpleIdentifier name) {
+ NodeList<SimpleIdentifier> names;
+ if (combinator is HideCombinator) {
+ names = combinator.hiddenNames;
+ } else if (combinator is ShowCombinator) {
+ names = combinator.shownNames;
+ } else {
+ return null;
+ }
+ if (names.length == 1) {
+ return rangeForCombinator(combinator);
+ }
+ var index = names.indexOf(name);
+ if (index < 0) {
+ return null;
+ } else if (index == names.length - 1) {
+ return range.endEnd(names[index - 1], name);
+ }
+ return range.startStart(name, names[index + 1]);
+ }
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_non_null_assertion.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_non_null_assertion.dart
index 79c8910..75a93be 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_non_null_assertion.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_non_null_assertion.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -25,7 +23,7 @@
var bangToken = expression.operator;
await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(range.startStart(bangToken, bangToken.next));
+ builder.addDeletion(range.startStart(bangToken, bangToken.next!));
});
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_operator.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_operator.dart
index ddbfc3b..1b44daf 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_operator.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_operator.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_parameters_in_getter_declaration.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_parameters_in_getter_declaration.dart
index 0d9cd83..cb17bce 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_parameters_in_getter_declaration.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_parameters_in_getter_declaration.dart
@@ -17,11 +17,11 @@
@override
Future<void> compute(ChangeBuilder builder) async {
+ var node = this.node;
if (node is MethodDeclaration) {
// Support for the analyzer error.
- var method = node as MethodDeclaration;
- var name = method.name;
- var body = method.body;
+ var name = node.name;
+ var body = node.body;
if (name != null && body != null) {
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(range.endStart(name, body), ' ');
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_parentheses_in_getter_invocation.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_parentheses_in_getter_invocation.dart
index cd52486..97e5d0f8 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_parentheses_in_getter_invocation.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_parentheses_in_getter_invocation.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_question_mark.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_question_mark.dart
index cadd199..96e0280 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_question_mark.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_question_mark.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -20,7 +18,7 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var node = this.node;
+ AstNode? node = this.node;
if (node is VariableDeclaration) {
var parent = node.parent;
if (parent is VariableDeclarationList) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_this_expression.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_this_expression.dart
index 30fd8a4..68f1350 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_this_expression.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_this_expression.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -34,10 +32,13 @@
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(range.startEnd(node, node.operator));
});
- } else if (node is MethodInvocation && node.target is ThisExpression) {
- await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(range.startEnd(node, node.operator));
- });
+ } else if (node is MethodInvocation) {
+ var operator = node.operator;
+ if (node.target is ThisExpression && operator != null) {
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addDeletion(range.startEnd(node, operator));
+ });
+ }
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_type_annotation.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_type_annotation.dart
index 997c1ee..34b84f4 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_type_annotation.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_type_annotation.dart
@@ -2,11 +2,10 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
+import 'package:analysis_server/src/utilities/extensions/ast.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
@@ -25,7 +24,7 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- for (var node = this.node; node != null; node = node.parent) {
+ for (var node in this.node.withParents) {
if (node is DeclaredIdentifier) {
return _removeFromDeclaredIdentifier(builder, node);
}
@@ -93,13 +92,13 @@
}
Future<void> _removeTypeAnnotation(
- ChangeBuilder builder, TypeAnnotation type) async {
+ ChangeBuilder builder, TypeAnnotation? type) async {
if (type == null) {
return;
}
await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(range.startStart(type, type.endToken.next));
+ builder.addDeletion(range.startStart(type, type.endToken.next!));
});
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_type_arguments.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_type_arguments.dart
index 9423b86..85c70ba1 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_type_arguments.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_type_arguments.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -17,12 +15,14 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (coveredNode is TypeArgumentList) {
- TypeArgumentList typeArguments = coveredNode;
- await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(range.node(typeArguments));
- });
+ var typeArguments = coveredNode;
+ if (typeArguments is! TypeArgumentList) {
+ return;
}
+
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addDeletion(range.node(typeArguments));
+ });
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_cast.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_cast.dart
index a087d1d..b326680 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_cast.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_cast.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -20,13 +18,14 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (coveredNode is! AsExpression) {
+ var asExpression = coveredNode;
+ if (asExpression is! AsExpression) {
return;
}
- var asExpression = coveredNode as AsExpression;
- var expression = asExpression.expression;
+
// remove 'as T' from 'e as T'
await builder.addDartFileEdit(file, (builder) {
+ var expression = asExpression.expression;
builder.addDeletion(range.endEnd(expression, asExpression));
builder.removeEnclosingParentheses(asExpression);
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_new.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_new.dart
index 86e1bf9..d4a1b0b 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_new.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_new.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -20,13 +18,19 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- final instanceCreationExpression = node;
- if (instanceCreationExpression is InstanceCreationExpression) {
- final newToken = instanceCreationExpression.keyword;
- await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(range.startStart(newToken, newToken.next));
- });
+ final creation = node;
+ if (creation is! InstanceCreationExpression) {
+ return;
}
+
+ final newToken = creation.keyword;
+ if (newToken == null) {
+ return;
+ }
+
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addDeletion(range.startStart(newToken, newToken.next!));
+ });
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_parentheses.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_parentheses.dart
index 39d7d8c..7baa9e0 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_parentheses.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_parentheses.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_string_interpolation.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_string_interpolation.dart
index a6cf0b9..6651bf4 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_string_interpolation.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_string_interpolation.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server/src/services/correction/util.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart
index 700bf1a..ba9b690 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -23,7 +21,12 @@
@override
Future<void> compute(ChangeBuilder builder) async {
final sourceRanges = <SourceRange>[];
+
final referencedNode = node.parent;
+ if (referencedNode == null) {
+ return;
+ }
+
if (referencedNode is ClassDeclaration ||
referencedNode is EnumDeclaration ||
referencedNode is FunctionDeclaration ||
@@ -31,16 +34,19 @@
referencedNode is MethodDeclaration ||
referencedNode is VariableDeclaration) {
final element = referencedNode is Declaration
- ? referencedNode.declaredElement
- : (referencedNode as NamedCompilationUnitMember).declaredElement;
+ ? referencedNode.declaredElement!
+ : (referencedNode as NamedCompilationUnitMember).declaredElement!;
final references = _findAllReferences(unit, element);
// todo (pq): consider filtering for references that are limited to within the class.
if (references.length == 1) {
+ var parent = referencedNode.parent;
+ var grandParent = parent?.parent;
SourceRange sourceRange;
- if (referencedNode is VariableDeclaration) {
- VariableDeclarationList parent = referencedNode.parent;
+ if (referencedNode is VariableDeclaration &&
+ parent is VariableDeclarationList &&
+ grandParent != null) {
if (parent.variables.length == 1) {
- sourceRange = utils.getLinesRange(range.node(parent.parent));
+ sourceRange = utils.getLinesRange(range.node(grandParent));
} else {
sourceRange = range.nodeInList(parent.variables, referencedNode);
}
@@ -73,7 +79,7 @@
return;
}
- final element = (declaration as VariableDeclaration).declaredElement;
+ final element = declaration.declaredElement;
if (element is! FieldElement) {
return;
}
@@ -90,13 +96,20 @@
if (referenceNode == null) {
return;
}
+ var parent = referenceNode.parent;
+ var grandParent = parent?.parent;
SourceRange sourceRange;
- if (referenceNode is VariableDeclaration) {
- sourceRange = _forVariableDeclaration(referenceNode);
+ if (referenceNode is VariableDeclaration &&
+ parent is VariableDeclarationList &&
+ grandParent != null) {
+ sourceRange =
+ _forVariableDeclaration(referenceNode, parent, grandParent);
} else if (referenceNode is ConstructorFieldInitializer) {
sourceRange = _forConstructorFieldInitializer(referenceNode);
} else if (referenceNode is FieldFormalParameter) {
- sourceRange = _forFieldFormalParameter(referenceNode);
+ sourceRange = _forFieldFormalParameter(
+ referenceNode,
+ );
} else {
sourceRange = utils.getLinesRange(range.node(referenceNode));
}
@@ -122,7 +135,8 @@
}
SourceRange _forFieldFormalParameter(FieldFormalParameter node) {
- var parameter = node.parent is DefaultFormalParameter ? node.parent : node;
+ var parent = node.parent;
+ var parameter = parent is DefaultFormalParameter ? parent : node;
var parameterList = parameter.parent as FormalParameterList;
// (node) -> ()
@@ -133,12 +147,12 @@
);
}
- var prevToken = parameter.beginToken.previous;
- var nextToken = parameter.endToken.next;
+ var prevToken = parameter.beginToken.previous!;
+ var nextToken = parameter.endToken.next!;
// (node, tail) -> (tail)
if (nextToken.type == TokenType.COMMA) {
- nextToken = nextToken.next;
+ nextToken = nextToken.next!;
return range.startStart(parameter.beginToken, nextToken);
}
@@ -147,22 +161,22 @@
var isFirstOptional = prevToken.type == TokenType.OPEN_CURLY_BRACKET ||
prevToken.type == TokenType.OPEN_SQUARE_BRACKET;
if (isFirstOptional) {
- prevToken = prevToken.previous;
+ prevToken = prevToken.previous!;
}
if (isFirstOptional) {
var isLastOptional = nextToken.type == TokenType.CLOSE_CURLY_BRACKET ||
nextToken.type == TokenType.CLOSE_SQUARE_BRACKET;
if (isLastOptional) {
- nextToken = nextToken.next;
+ nextToken = nextToken.next!;
}
}
- return range.endStart(prevToken.previous, nextToken);
+ return range.endStart(prevToken.previous!, nextToken);
}
- SourceRange _forVariableDeclaration(VariableDeclaration node) {
- VariableDeclarationList parent = node.parent;
+ SourceRange _forVariableDeclaration(VariableDeclaration node,
+ VariableDeclarationList parent, AstNode grandParent) {
if (parent.variables.length == 1) {
- return utils.getLinesRange(range.node(parent.parent));
+ return utils.getLinesRange(range.node(grandParent));
} else {
return range.nodeInList(parent.variables, node);
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_catch_clause.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_catch_clause.dart
index 5981330..b3a9af7 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_catch_clause.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_catch_clause.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -17,15 +15,20 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is SimpleIdentifier) {
- var catchClause = node.parent;
- if (catchClause is CatchClause &&
- catchClause.exceptionParameter == node) {
- await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(
- range.startStart(catchClause.catchKeyword, catchClause.body));
- });
- }
+ var catchClause = node.parent;
+ if (catchClause is! CatchClause) {
+ return;
+ }
+
+ var catchKeyword = catchClause.catchKeyword;
+ if (catchKeyword == null) {
+ return;
+ }
+
+ if (catchClause.exceptionParameter == node) {
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addDeletion(range.startStart(catchKeyword, catchClause.body));
+ });
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_catch_stack.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_catch_stack.dart
index f03e3c7..7cdc1ac 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_catch_stack.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_catch_stack.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -17,16 +15,20 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is SimpleIdentifier) {
- var catchClause = node.parent;
- if (catchClause is CatchClause &&
- catchClause.stackTraceParameter == node &&
- catchClause.exceptionParameter != null) {
- await builder.addDartFileEdit(file, (builder) {
- builder
- .addDeletion(range.endEnd(catchClause.exceptionParameter, node));
- });
- }
+ var catchClause = node.parent;
+ if (catchClause is! CatchClause) {
+ return;
+ }
+
+ var exceptionParameter = catchClause.exceptionParameter;
+ if (exceptionParameter == null) {
+ return;
+ }
+
+ if (catchClause.stackTraceParameter == node) {
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addDeletion(range.endEnd(exceptionParameter, node));
+ });
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_import.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_import.dart
index d5336a0..3fa7457 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_import.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_import.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_label.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_label.dart
index 9939f3f..8f182e9 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_label.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_label.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -18,7 +16,7 @@
@override
Future<void> compute(ChangeBuilder builder) async {
if (node is Label) {
- var nextToken = node.endToken.next;
+ var nextToken = node.endToken.next!;
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(range.startStart(node, nextToken));
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart
index 9fe3616..f64a7a1 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server/src/services/correction/util.dart';
@@ -24,8 +22,9 @@
if (!(declaration is VariableDeclaration && declaration.name == node)) {
return;
}
- Element element = (declaration as VariableDeclaration).declaredElement;
- if (element is! LocalElement) {
+
+ var element = declaration.declaredElement;
+ if (element is! LocalVariableElement) {
return;
}
@@ -41,7 +40,7 @@
final node = reference.thisOrAncestorMatching((node) =>
node is VariableDeclaration || node is AssignmentExpression);
- SourceRange sourceRange;
+ SourceRange? sourceRange;
if (node is AssignmentExpression) {
sourceRange = _forAssignmentExpression(node);
} else if (node is VariableDeclaration) {
@@ -78,14 +77,15 @@
SourceRange _forAssignmentExpression(AssignmentExpression node) {
// todo (pq): consider node.parent is! ExpressionStatement to handle
// assignments in parens, etc.
- if (node.parent is ArgumentList) {
- return range.startStart(node, node.operator.next);
+ var parent = node.parent!;
+ if (parent is ArgumentList) {
+ return range.startStart(node, node.operator.next!);
} else {
- return utils.getLinesRange(range.node(node.parent));
+ return utils.getLinesRange(range.node(parent));
}
}
- SourceRange _forVariableDeclaration(VariableDeclaration node) {
+ SourceRange? _forVariableDeclaration(VariableDeclaration node) {
var declarationList = node.parent as VariableDeclarationList;
var declarationListParent = declarationList.parent;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_parameter.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_parameter.dart
index 2700eb1..830345f 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_parameter.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_parameter.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -18,72 +16,81 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is FormalParameter) {
- var parameter = node as FormalParameter;
- if (parameter.parent is DefaultFormalParameter) {
- parameter = parameter.parent;
- }
+ var parameter = node;
+ if (parameter is! FormalParameter) {
+ return;
+ }
- var parameterList = parameter.parent as FormalParameterList;
- var parameters = parameterList.parameters;
- var index = parameters.indexOf(parameter);
- await builder.addDartFileEdit(file, (builder) {
- if (index == 0) {
- // Remove the first parameter in the list.
- if (parameters.length == 1) {
- // There is only one parameter. By removing everything inside the
- // parentheses we also remove any square brackets or curly braces.
- builder.addDeletion(range.endStart(
- parameterList.leftParenthesis, parameterList.rightParenthesis));
- } else {
- var following = parameters[1];
- if (parameter.isRequiredPositional &&
- !following.isRequiredPositional) {
- // The parameter to be removed and the following parameter are not
- // of the same kind, so there is a delimiter between them that we
- // can't delete.
- var leftDelimiter = parameterList.leftDelimiter;
- if (leftDelimiter != null) {
- builder.addDeletion(range.startStart(parameter, leftDelimiter));
- } else {
- // Invalid code `C(foo, bar = 1)`.
- builder.addDeletion(range.startStart(parameter, following));
- }
- } else {
- // The parameter to be removed and the following parameter are of
- // the same kind, so there is no delimiter between them.
- builder.addDeletion(range.startStart(parameter, following));
- }
- }
+ var parent = parameter.parent;
+ if (parent is DefaultFormalParameter) {
+ parameter = parent;
+ }
+
+ var parameterList = parameter.parent;
+ if (parameterList is! FormalParameterList) {
+ return;
+ }
+
+ var parameters = parameterList.parameters;
+ var index = parameters.indexOf(parameter);
+ final parameter_final = parameter;
+ await builder.addDartFileEdit(file, (builder) {
+ if (index == 0) {
+ // Remove the first parameter in the list.
+ if (parameters.length == 1) {
+ // There is only one parameter. By removing everything inside the
+ // parentheses we also remove any square brackets or curly braces.
+ builder.addDeletion(range.endStart(
+ parameterList.leftParenthesis, parameterList.rightParenthesis));
} else {
- var preceding = parameters[index - 1];
- if (preceding.isRequiredPositional &&
- !parameter.isRequiredPositional) {
- // The parameter to be removed and the preceding parameter are not
- // of the same kind, so there is a delimiter between them.
- if (index == parameters.length - 1) {
- // The parameter to be removed is the only parameter between the
- // delimiters, so remove the delimiters with the parameter.
- var trailingToken = parameterList.rightParenthesis;
- if (trailingToken.previous.type == TokenType.COMMA) {
- // Leave the trailing comma if there is one.
- trailingToken = trailingToken.previous;
- }
- builder.addDeletion(range.endStart(preceding, trailingToken));
+ var following = parameters[1];
+ if (parameter_final.isRequiredPositional &&
+ !following.isRequiredPositional) {
+ // The parameter to be removed and the following parameter are not
+ // of the same kind, so there is a delimiter between them that we
+ // can't delete.
+ var leftDelimiter = parameterList.leftDelimiter;
+ if (leftDelimiter != null) {
+ builder.addDeletion(range.startStart(parameter, leftDelimiter));
} else {
- // The parameter to be removed is the first inside the delimiters
- // but the delimiters should be left.
- var following = parameters[index + 1];
+ // Invalid code `C(foo, bar = 1)`.
builder.addDeletion(range.startStart(parameter, following));
}
} else {
- // The parameter to be removed and the preceding parameter are of
+ // The parameter to be removed and the following parameter are of
// the same kind, so there is no delimiter between them.
- builder.addDeletion(range.endEnd(preceding, parameter));
+ builder.addDeletion(range.startStart(parameter, following));
}
}
- });
- }
+ } else {
+ var preceding = parameters[index - 1];
+ if (preceding.isRequiredPositional &&
+ !parameter_final.isRequiredPositional) {
+ // The parameter to be removed and the preceding parameter are not
+ // of the same kind, so there is a delimiter between them.
+ if (index == parameters.length - 1) {
+ // The parameter to be removed is the only parameter between the
+ // delimiters, so remove the delimiters with the parameter.
+ var trailingToken = parameterList.rightParenthesis;
+ var previous = trailingToken.previous;
+ if (previous != null && previous.type == TokenType.COMMA) {
+ // Leave the trailing comma if there is one.
+ trailingToken = previous;
+ }
+ builder.addDeletion(range.endStart(preceding, trailingToken));
+ } else {
+ // The parameter to be removed is the first inside the delimiters
+ // but the delimiters should be left.
+ var following = parameters[index + 1];
+ builder.addDeletion(range.startStart(parameter, following));
+ }
+ } else {
+ // The parameter to be removed and the preceding parameter are of
+ // the same kind, so there is no delimiter between them.
+ builder.addDeletion(range.endEnd(preceding, parameter));
+ }
+ }
+ });
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.