Attention is currently required from: Brian Wilkerson.
To view, visit change 194540. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brian Wilkerson.
Konstantin Shcheglov would like Brian Wilkerson to review this change.
Migrate several lib/src/services/correction/dart/
Change-Id: I8c50c6e7664a7bd8ee7b0d881d9adeaf9c544f61
---
M pkg/analysis_server/lib/src/services/correction/dart/abstract_producer.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_await.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_const.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_diagnostic_property_reference.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_explicit_cast.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_field_formal_parameters.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_late.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_missing_enum_case_clauses.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_missing_parameter.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_missing_parameter_named.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_ne_null.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_not_null_assert.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_null_check.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_override.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_required.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_required_keyword.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_return_type.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_static.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_super_constructor_invocation.dart
M pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart
M pkg/analysis_server/lib/src/services/correction/dart/assign_to_local_variable.dart
M pkg/analysis_server/lib/src/services/correction/dart/change_argument_name.dart
M pkg/analysis_server/lib/src/services/correction/dart/change_to.dart
M pkg/analysis_server/lib/src/services/correction/dart/change_to_nearest_precise_value.dart
M pkg/analysis_server/lib/src/services/correction/dart/change_to_static_access.dart
M pkg/analysis_server/lib/src/services/correction/dart/change_type_annotation.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_add_all_to_spread.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_mixin.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_conditional_expression_to_if_element.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_documentation_into_block.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_documentation_into_line.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_flutter_child.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_flutter_children.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_into_async_body.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_into_block_body.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_into_final_field.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_into_for_index.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_into_getter.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_into_is_not.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_into_is_not_empty.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_map_from_iterable_to_for_literal.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_part_of_to_uri.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_quotes.dart
M pkg/analysis_server/lib/src/services/correction/executable_parameters.dart
M pkg/analysis_server/lib/src/utilities/extensions/ast.dart
M pkg/analysis_server/test/src/services/correction/fix/add_null_check_test.dart
46 files changed, 708 insertions(+), 563 deletions(-)
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/abstract_producer.dart b/pkg/analysis_server/lib/src/services/correction/dart/abstract_producer.dart
index 425dc82..a39cc31 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/abstract_producer.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/abstract_producer.dart
@@ -293,6 +293,12 @@
}
}
+abstract class CorrectionProducerWithDiagnostic extends CorrectionProducer {
+ /// TODO(migration) Consider providing it via constructor.
+ @override
+ Diagnostic get diagnostic => super.diagnostic!;
+}
+
/// An object that can dynamically compute multiple corrections (fixes or
/// assists).
abstract class MultiCorrectionProducer extends _AbstractCorrectionProducer {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_await.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_await.dart
index 7acd73c..6e72eae 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_await.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_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_plugin/utilities/change_builder/change_builder_core.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_const.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_const.dart
index d4b2642..84311ad 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_const.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_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';
@@ -19,15 +17,14 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var node = this.node;
+ AstNode? node = this.node;
if (node is SimpleIdentifier) {
node = node.parent;
}
if (node is ConstructorDeclaration) {
+ var node_final = node;
await builder.addDartFileEdit(file, (builder) {
- final offset = (node as ConstructorDeclaration)
- .firstTokenAfterCommentAndMetadata
- .offset;
+ final offset = node_final.firstTokenAfterCommentAndMetadata.offset;
builder.addSimpleInsertion(offset, 'const ');
});
return;
@@ -39,9 +36,10 @@
node = node.parent;
}
if (node is InstanceCreationExpression) {
- if ((node as InstanceCreationExpression).keyword == null) {
+ if (node.keyword == null) {
+ var node_final = node;
await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleInsertion(node.offset, 'const ');
+ builder.addSimpleInsertion(node_final.offset, 'const ');
});
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_diagnostic_property_reference.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_diagnostic_property_reference.dart
index 649cfbc..7dd8989 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_diagnostic_property_reference.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_diagnostic_property_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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -14,7 +12,6 @@
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
-import 'package:meta/meta.dart';
class AddDiagnosticPropertyReference extends CorrectionProducer {
@override
@@ -36,12 +33,11 @@
var classDeclaration = node.thisOrAncestorOfType<ClassOrMixinDeclaration>();
if (classDeclaration == null ||
- !flutter.isDiagnosticable(classDeclaration.declaredElement.thisType)) {
+ !flutter.isDiagnosticable(classDeclaration.declaredElement!.thisType)) {
return;
}
- SimpleIdentifier name = node;
- final parent = node.parent;
+ final parent = node.parent!;
var type = _getReturnType(parent);
if (type == null) {
@@ -49,7 +45,7 @@
}
String constructorId;
- List<DartType> typeArgs;
+ List<DartType>? typeArgs;
var constructorName = '';
if (type is FunctionType) {
@@ -81,8 +77,8 @@
void writePropertyReference(
DartEditBuilder builder, {
- @required String prefix,
- @required String builderName,
+ required String prefix,
+ required String builderName,
}) {
builder.write('$prefix$builderName.add($constructorId');
if (typeArgs != null) {
@@ -90,7 +86,7 @@
builder.writeTypes(typeArgs);
builder.write('>');
} else if (type.isDynamic) {
- TypeAnnotation declType;
+ TypeAnnotation? declType;
final decl = node.thisOrAncestorOfType<VariableDeclarationList>();
if (decl != null) {
declType = decl.type;
@@ -108,14 +104,18 @@
}
}
}
- builder.writeln("$constructorName('${name.name}', ${name.name}));");
+ builder.writeln("$constructorName('${node.name}', ${node.name}));");
}
final debugFillProperties =
classDeclaration.getMethod('debugFillProperties');
if (debugFillProperties == null) {
- final insertOffset =
- utils.prepareNewMethodLocation(classDeclaration).offset;
+ var location = utils.prepareNewMethodLocation(classDeclaration);
+ if (location == null) {
+ return;
+ }
+
+ final insertOffset = location.offset;
await builder.addDartFileEdit(file, (builder) {
builder.addInsertion(utils.getLineNext(insertOffset), (builder) {
final declPrefix =
@@ -149,14 +149,19 @@
prefix = utils.getLinePrefix(offset);
}
- var parameters = debugFillProperties.parameters.parameters;
- String propertiesBuilderName;
- for (var parameter in parameters) {
+ var parameterList = debugFillProperties.parameters;
+ if (parameterList == null) {
+ return;
+ }
+
+ String? propertiesBuilderName;
+ for (var parameter in parameterList.parameters) {
if (parameter is SimpleFormalParameter) {
final type = parameter.type;
- if (type is TypeName) {
+ final identifier = parameter.identifier;
+ if (type is TypeName && identifier != null) {
if (type.name.name == 'DiagnosticPropertiesBuilder') {
- propertiesBuilderName = parameter.identifier.name;
+ propertiesBuilderName = identifier.name;
break;
}
}
@@ -166,17 +171,18 @@
return null;
}
+ final final_propertiesBuilderName = propertiesBuilderName;
await builder.addDartFileEdit(file, (builder) {
builder.addInsertion(utils.getLineNext(offset), (builder) {
writePropertyReference(builder,
- prefix: prefix, builderName: propertiesBuilderName);
+ prefix: prefix, builderName: final_propertiesBuilderName);
});
});
}
}
/// Return the return type of the given [node].
- DartType _getReturnType(AstNode node) {
+ DartType? _getReturnType(AstNode node) {
if (node is MethodDeclaration) {
// Getter.
var element = node.declaredElement;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_explicit_cast.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_explicit_cast.dart
index aaaf687..612635d 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_explicit_cast.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_explicit_cast.dart
@@ -2,14 +2,13 @@
// 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/precedence.dart';
import 'package:analyzer/dart/element/type.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';
@@ -19,14 +18,12 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- Expression target;
- if (coveredNode is Expression) {
- target = coveredNode;
- } else {
+ var target = coveredNode;
+ if (target is! Expression) {
return;
}
- var fromType = target.staticType;
+ var fromType = target.typeOrThrow;
if (fromType == typeProvider.nullType) {
// There would only be a diagnostic if the `toType` is not nullable, in
// which case a cast won't fix the problem.
@@ -39,9 +36,9 @@
parent = target.parent;
}
if (parent is AssignmentExpression && target == parent.rightHandSide) {
- toType = parent.writeType;
+ toType = parent.writeType!;
} else if (parent is VariableDeclaration && target == parent.initializer) {
- toType = parent.declaredElement.type;
+ toType = parent.declaredElement!.type;
} else {
// TODO(brianwilkerson) Handle function arguments.
return;
@@ -56,7 +53,7 @@
if (target.isToListMethodInvocation) {
var targetTarget = (target as MethodInvocation).target;
if (targetTarget != null) {
- var targetTargetType = targetTarget.staticType;
+ var targetTargetType = targetTarget.typeOrThrow;
if (targetTargetType.isDartCoreIterable ||
targetTargetType.isDartCoreList ||
targetTargetType.isDartCoreMap ||
@@ -70,55 +67,65 @@
// TODO(brianwilkerson) Consider updating the right operand.
return;
}
+
+ final target_final = target;
+
var needsParentheses = target.precedence < Precedence.postfix;
if (((fromType.isDartCoreIterable || fromType.isDartCoreList) &&
+ toType is InterfaceType &&
toType.isDartCoreList) ||
- (fromType.isDartCoreSet && toType.isDartCoreSet)) {
+ (fromType.isDartCoreSet &&
+ toType is InterfaceType &&
+ toType.isDartCoreSet)) {
if (target.isCastMethodInvocation) {
// TODO(brianwilkerson) Consider updating the type arguments to the
// `cast` invocation.
return;
}
+ final toType_final = toType;
await builder.addDartFileEdit(file, (builder) {
if (needsParentheses) {
- builder.addSimpleInsertion(target.offset, '(');
+ builder.addSimpleInsertion(target_final.offset, '(');
}
- builder.addInsertion(target.end, (builder) {
+ builder.addInsertion(target_final.end, (builder) {
if (needsParentheses) {
builder.write(')');
}
builder.write('.cast<');
- builder.writeType((toType as InterfaceType).typeArguments[0]);
+ builder.writeType(toType_final.typeArguments[0]);
builder.write('>()');
});
});
- } else if (fromType.isDartCoreMap && toType.isDartCoreMap) {
+ } else if (fromType.isDartCoreMap &&
+ toType is InterfaceType &&
+ toType.isDartCoreMap) {
if (target.isCastMethodInvocation) {
// TODO(brianwilkerson) Consider updating the type arguments to the
// `cast` invocation.
return;
}
+ final toType_final = toType;
await builder.addDartFileEdit(file, (builder) {
if (needsParentheses) {
- builder.addSimpleInsertion(target.offset, '(');
+ builder.addSimpleInsertion(target_final.offset, '(');
}
- builder.addInsertion(target.end, (builder) {
+ builder.addInsertion(target_final.end, (builder) {
if (needsParentheses) {
builder.write(')');
}
builder.write('.cast<');
- builder.writeType((toType as InterfaceType).typeArguments[0]);
+ builder.writeType(toType_final.typeArguments[0]);
builder.write(', ');
- builder.writeType((toType as InterfaceType).typeArguments[1]);
+ builder.writeType(toType_final.typeArguments[1]);
builder.write('>()');
});
});
} else {
await builder.addDartFileEdit(file, (builder) {
if (needsParentheses) {
- builder.addSimpleInsertion(target.offset, '(');
+ builder.addSimpleInsertion(target_final.offset, '(');
}
- builder.addInsertion(target.end, (builder) {
+ builder.addInsertion(target_final.end, (builder) {
if (needsParentheses) {
builder.write(')');
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_field_formal_parameters.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_field_formal_parameters.dart
index e4ef282..4a1c1e4 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_field_formal_parameters.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_field_formal_parameters.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,14 +16,21 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is! SimpleIdentifier || node.parent is! ConstructorDeclaration) {
+ var constructor = node.parent;
+ if (node is! SimpleIdentifier || constructor is! ConstructorDeclaration) {
return;
}
- ConstructorDeclaration constructor = node.parent;
List<FormalParameter> parameters = constructor.parameters.parameters;
- ClassDeclaration classNode = constructor.parent;
- var superType = classNode.declaredElement.supertype;
+ var classNode = constructor.parent;
+ if (classNode is! ClassDeclaration) {
+ return;
+ }
+
+ var superType = classNode.declaredElement!.supertype;
+ if (superType == null) {
+ return;
+ }
// Compute uninitialized final fields.
var fields = ErrorVerifier.computeNotInitializedFields(constructor);
@@ -51,7 +56,7 @@
}
// Prepare the last required parameter.
- FormalParameter lastRequiredParameter;
+ FormalParameter? lastRequiredParameter;
for (var parameter in parameters) {
if (parameter.isRequiredPositional) {
lastRequiredParameter = parameter;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_late.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_late.dart
index 06bb9e0..cc3293b 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_late.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_late.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,14 +23,15 @@
}
var node = this.node;
if (node is SimpleIdentifier) {
- if (node.parent is VariableDeclaration &&
- node.parent.parent is VariableDeclarationList) {
- var list = node.parent.parent as VariableDeclarationList;
- if (!list.isLate) {
- if (list.type == null) {
- var keyword = list.keyword;
+ var variable = node.parent;
+ var variableList = variable?.parent;
+ if (variable is VariableDeclaration &&
+ variableList is VariableDeclarationList) {
+ if (!variableList.isLate) {
+ if (variableList.type == null) {
+ var keyword = variableList.keyword;
if (keyword == null) {
- await _insertAt(builder, list.variables[0].offset);
+ await _insertAt(builder, variableList.variables[0].offset);
// TODO(brianwilkerson) Consider converting this into an assist and
// expand it to support converting `var` to `late` as well as
// working anywhere a non-late local variable or field is selected.
@@ -41,14 +40,14 @@
// builder.addSimpleReplacement(range.token(keyword), 'late');
// });
} else if (keyword.type != Keyword.CONST) {
- await _insertAt(builder, list.variables[0].offset);
+ await _insertAt(builder, variableList.variables[0].offset);
}
} else {
- var keyword = list.keyword;
+ var keyword = variableList.keyword;
if (keyword != null) {
await _insertAt(builder, keyword.offset);
} else {
- var type = list.type;
+ var type = variableList.type;
if (type != null) {
await _insertAt(builder, type.offset);
}
@@ -65,13 +64,17 @@
getter.enclosingElement is ClassElement) {
var declarationResult =
await sessionHelper.getElementDeclaration(getter.variable);
+ if (declarationResult == null) {
+ return;
+ }
var variable = declarationResult.node;
+ var variableList = variable.parent;
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 &&
+ variableList is VariableDeclarationList &&
+ variableList.parent is FieldDeclaration) {
+ var keywordToken = variableList.keyword;
+ if (variableList.variables.length == 1 &&
+ keywordToken != null &&
keywordToken.keyword == Keyword.FINAL) {
await _insertAt(builder, keywordToken.offset,
source: declarationResult.element.source);
@@ -83,7 +86,7 @@
}
Future<void> _insertAt(ChangeBuilder builder, int offset,
- {Source source}) async {
+ {Source? source}) async {
await builder.addDartFileEdit(source?.fullName ?? file, (builder) {
builder.addSimpleInsertion(offset, 'late ');
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_missing_enum_case_clauses.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_missing_enum_case_clauses.dart
index b4825c1..434f13d 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_missing_enum_case_clauses.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_missing_enum_case_clauses.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 statement = node as SwitchStatement;
- String enumName;
+ String? enumName;
var enumConstantNames = <String>[];
var expressionType = statement.expression.staticType;
if (expressionType is InterfaceType) {
@@ -58,13 +56,14 @@
var statementIndent = utils.getLinePrefix(statement.offset);
var singleIndent = utils.getIndent(1);
+ final enumName_final = enumName;
await builder.addDartFileEdit(file, (builder) {
builder.addInsertion(utils.getLineThis(statement.end), (builder) {
for (var constantName in enumConstantNames) {
builder.write(statementIndent);
builder.write(singleIndent);
builder.write('case ');
- builder.write(enumName);
+ builder.write(enumName_final);
builder.write('.');
builder.write(constantName);
builder.writeln(':');
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_missing_parameter.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_missing_parameter.dart
index 4d7c4db..1f11731 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_missing_parameter.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_missing_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/executable_parameters.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -15,11 +13,17 @@
@override
Iterable<CorrectionProducer> get producers sync* {
// node is the unmatched argument.
- if (node.parent is! ArgumentList) {
+ var argumentList = node.parent;
+ if (argumentList is! ArgumentList) {
return;
}
- var context =
- ExecutableParameters.forInvocation(sessionHelper, node.parent.parent);
+
+ var invocation = argumentList.parent;
+ if (invocation == null) {
+ return;
+ }
+
+ var context = ExecutableParameters.forInvocation(sessionHelper, invocation);
if (context == null) {
return;
}
@@ -54,7 +58,7 @@
await _addParameter(builder, prevNode?.end, prefix, ']');
} else {
var parameterList = await context.getParameterList();
- var offset = parameterList?.leftParenthesis?.end;
+ var offset = parameterList?.leftParenthesis.end;
await _addParameter(builder, offset, prefix, ']');
}
}
@@ -68,9 +72,12 @@
_AddMissingParameter(this.context);
Future<void> _addParameter(
- ChangeBuilder builder, int offset, String prefix, String suffix) async {
+ ChangeBuilder builder, int? offset, String prefix, String suffix) async {
// node is the unmatched argument.
- ArgumentList argumentList = node.parent;
+ var argumentList = node.parent;
+ if (argumentList is! ArgumentList) {
+ return;
+ }
List<Expression> arguments = argumentList.arguments;
var numRequired = context.required.length;
if (numRequired >= arguments.length) {
@@ -106,7 +113,7 @@
await _addParameter(builder, prevNode?.end, ', ', '');
} else {
var parameterList = await context.getParameterList();
- var offset = parameterList?.leftParenthesis?.end;
+ var offset = parameterList?.leftParenthesis.end;
var suffix = context.executable.parameters.isNotEmpty ? ', ' : '';
await _addParameter(builder, offset, '', suffix);
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_missing_parameter_named.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_missing_parameter_named.dart
index cc53b3f..da82a33 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_missing_parameter_named.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_missing_parameter_named.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/executable_parameters.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -12,7 +10,7 @@
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
class AddMissingParameterNamed extends CorrectionProducer {
- String _parameterName;
+ String _parameterName = '';
@override
List<Object> get fixArguments => [_parameterName];
@@ -23,23 +21,23 @@
@override
Future<void> compute(ChangeBuilder builder) async {
// Prepare the name of the missing parameter.
- if (this.node is! SimpleIdentifier) {
+ var node = this.node;
+ if (node is! SimpleIdentifier) {
return;
}
- SimpleIdentifier node = this.node;
_parameterName = node.name;
// We expect that the node is part of a NamedExpression.
- if (node.parent?.parent is! NamedExpression) {
+ var namedExpression = node.parent?.parent;
+ if (namedExpression is! NamedExpression) {
return;
}
- NamedExpression namedExpression = node.parent.parent;
// We should be in an ArgumentList.
- if (namedExpression.parent is! ArgumentList) {
+ var argumentList = namedExpression.parent;
+ if (argumentList is! ArgumentList) {
return;
}
- var argumentList = namedExpression.parent;
// Prepare the invoked element.
var context =
@@ -53,7 +51,7 @@
return;
}
- Future<void> addParameter(int offset, String prefix, String suffix) async {
+ Future<void> addParameter(int? offset, String prefix, String suffix) async {
if (offset != null) {
await builder.addDartFileEdit(context.file, (builder) {
builder.addInsertion(offset, (builder) {
@@ -74,7 +72,7 @@
await addParameter(prevNode?.end, ', {', '}');
} else {
var parameterList = await context.getParameterList();
- await addParameter(parameterList?.leftParenthesis?.end, '{', '}');
+ await addParameter(parameterList?.leftParenthesis.end, '{', '}');
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_ne_null.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_ne_null.dart
index cd75a2b..d5facaf 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_ne_null.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_ne_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/analysis/features.dart';
@@ -12,7 +10,7 @@
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
-class AddNeNull extends CorrectionProducer {
+class AddNeNull extends CorrectionProducerWithDiagnostic {
@override
FixKind get fixKind => DartFixKind.ADD_NE_NULL;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_not_null_assert.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_not_null_assert.dart
index fe5c8f6..ac57617 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_not_null_assert.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_not_null_assert.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 'dart:math';
import 'package:_fe_analyzer_shared/src/scanner/token.dart';
@@ -20,53 +18,63 @@
@override
Future<void> compute(ChangeBuilder builder) async {
final identifier = node;
- if (identifier is SimpleIdentifier) {
- if (identifier.parent is FormalParameter) {
- final exp = identifier.parent.thisOrAncestorMatching(
- (node) => node is FunctionExpression || node is MethodDeclaration);
- FunctionBody body;
- if (exp is FunctionExpression) {
- body = exp.body;
- } else if (exp is MethodDeclaration) {
- body = exp.body;
- }
- if (body is BlockFunctionBody) {
- var blockBody = body;
- // Check for an obvious pre-existing assertion.
- for (var statement in blockBody.block.statements) {
- if (statement is AssertStatement) {
- final condition = statement.condition;
- if (condition is BinaryExpression) {
- final leftOperand = condition.leftOperand;
- if (leftOperand is SimpleIdentifier) {
- if (leftOperand.staticElement == identifier.staticElement &&
- condition.operator.type == TokenType.BANG_EQ &&
- condition.rightOperand is NullLiteral) {
- return;
- }
- }
+ if (identifier is! SimpleIdentifier) {
+ return;
+ }
+
+ var formalParameter = identifier.parent;
+ if (formalParameter is! FormalParameter) {
+ return;
+ }
+
+ final executable = formalParameter.thisOrAncestorMatching(
+ (node) => node is FunctionExpression || node is MethodDeclaration);
+ if (executable == null) {
+ return;
+ }
+
+ FunctionBody? body;
+ if (executable is FunctionExpression) {
+ body = executable.body;
+ } else if (executable is MethodDeclaration) {
+ body = executable.body;
+ }
+
+ if (body is BlockFunctionBody) {
+ // Check for an obvious pre-existing assertion.
+ for (var statement in body.block.statements) {
+ if (statement is AssertStatement) {
+ final condition = statement.condition;
+ if (condition is BinaryExpression) {
+ final leftOperand = condition.leftOperand;
+ if (leftOperand is SimpleIdentifier) {
+ if (leftOperand.staticElement == identifier.staticElement &&
+ condition.operator.type == TokenType.BANG_EQ &&
+ condition.rightOperand is NullLiteral) {
+ return;
}
}
}
-
- await builder.addDartFileEdit(file, (builder) {
- final id = identifier.name;
- final prefix = utils.getNodePrefix(exp);
- final indent = utils.getIndent(1);
- // todo (pq): follow-ups:
- // 1. if the end token is on the same line as the body
- // we should add an `eol` before the assert as well.
- // 2. also, consider asking the block for the list of statements and
- // adding the statement to the beginning of the list, special casing
- // when there are no statements (or when there's a single statement
- // and the whole block is on the same line).
- var offset = min(utils.getLineNext(blockBody.beginToken.offset),
- blockBody.endToken.offset);
- builder.addSimpleInsertion(
- offset, '$prefix${indent}assert($id != null);$eol');
- });
}
}
+
+ final body_final = body;
+ await builder.addDartFileEdit(file, (builder) {
+ final id = identifier.name;
+ final prefix = utils.getNodePrefix(executable);
+ final indent = utils.getIndent(1);
+ // todo (pq): follow-ups:
+ // 1. if the end token is on the same line as the body
+ // we should add an `eol` before the assert as well.
+ // 2. also, consider asking the block for the list of statements and
+ // adding the statement to the beginning of the list, special casing
+ // when there are no statements (or when there's a single statement
+ // and the whole block is on the same line).
+ var offset = min(utils.getLineNext(body_final.beginToken.offset),
+ body_final.endToken.offset);
+ builder.addSimpleInsertion(
+ offset, '$prefix${indent}assert($id != null);$eol');
+ });
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_null_check.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_null_check.dart
index b7b4810..11bf784 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_null_check.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_null_check.dart
@@ -2,14 +2,13 @@
// 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/dart/ast/precedence.dart';
import 'package:analyzer/dart/element/type.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';
@@ -23,8 +22,9 @@
// Don't suggest a feature that isn't supported.
return;
}
- Expression target;
- var coveredNodeParent = coveredNode.parent;
+ Expression? target;
+ var coveredNode = this.coveredNode;
+ var coveredNodeParent = coveredNode?.parent;
if (coveredNode is SimpleIdentifier) {
if (coveredNodeParent is MethodInvocation) {
target = coveredNodeParent.realTarget;
@@ -38,59 +38,66 @@
target = coveredNode;
}
} else if (coveredNode is IndexExpression) {
- target = (coveredNode as IndexExpression).realTarget;
- } else if (coveredNodeParent is FunctionExpressionInvocation) {
+ target = coveredNode.realTarget;
+ } else if (coveredNode is Expression &&
+ coveredNodeParent is FunctionExpressionInvocation) {
target = coveredNode;
} else if (coveredNodeParent is AssignmentExpression) {
target = coveredNodeParent.rightHandSide;
} else if (coveredNode is PostfixExpression) {
- target = (coveredNode as PostfixExpression).operand;
+ target = coveredNode.operand;
} else if (coveredNode is PrefixExpression) {
- target = (coveredNode as PrefixExpression).operand;
+ target = coveredNode.operand;
} else if (coveredNode is BinaryExpression) {
- target = (coveredNode as BinaryExpression).leftOperand;
- } else {
+ target = coveredNode.leftOperand;
+ }
+
+ if (target == null) {
return;
}
var fromType = target.staticType;
+ if (fromType == null) {
+ return;
+ }
+
if (fromType == typeProvider.nullType) {
// Adding a null check after an explicit `null` is pointless.
return;
}
- DartType toType;
+ DartType? toType;
var parent = target.parent;
if (parent is AssignmentExpression && target == parent.rightHandSide) {
toType = parent.writeType;
} else if (parent is VariableDeclaration && target == parent.initializer) {
- toType = parent.declaredElement.type;
+ toType = parent.declaredElement?.type;
} else if (parent is ArgumentList) {
- toType = target.staticParameterElement.type;
+ toType = target.staticParameterElement?.type;
} else if (parent is IndexExpression) {
- toType = parent.realTarget.staticType;
+ toType = parent.realTarget.typeOrThrow;
} else if (parent is ForEachPartsWithDeclaration) {
toType =
- typeProvider.iterableType(parent.loopVariable.declaredElement.type);
+ typeProvider.iterableType(parent.loopVariable.declaredElement!.type);
} else if (parent is ForEachPartsWithIdentifier) {
- toType = typeProvider.iterableType(parent.identifier.staticType);
+ toType = typeProvider.iterableType(parent.identifier.typeOrThrow);
} else if (parent is SpreadElement) {
var literal = parent.thisOrAncestorOfType<TypedLiteral>();
if (literal is ListLiteral) {
- toType = literal.staticType.asInstanceOf(typeProvider.iterableElement);
+ toType = literal.typeOrThrow.asInstanceOf(typeProvider.iterableElement);
} else if (literal is SetOrMapLiteral) {
- toType = literal.staticType.isDartCoreSet
- ? literal.staticType.asInstanceOf(typeProvider.iterableElement)
- : literal.staticType.asInstanceOf(typeProvider.mapElement);
+ toType = literal.typeOrThrow.isDartCoreSet
+ ? literal.typeOrThrow.asInstanceOf(typeProvider.iterableElement)
+ : literal.typeOrThrow.asInstanceOf(typeProvider.mapElement);
}
} else if (parent is YieldStatement) {
var enclosingExecutable =
- parent.thisOrAncestorOfType<FunctionBody>().parent;
+ parent.thisOrAncestorOfType<FunctionBody>()?.parent;
if (enclosingExecutable is FunctionDeclaration) {
toType = enclosingExecutable.returnType?.type;
} else if (enclosingExecutable is MethodDeclaration) {
toType = enclosingExecutable.returnType?.type;
} else if (enclosingExecutable is FunctionExpression) {
- toType = enclosingExecutable.declaredElement.returnType;
+ toType = enclosingExecutable.declaredElement!.returnType;
}
} else if ((parent is PrefixedIdentifier && target == parent.prefix) ||
parent is PostfixExpression ||
@@ -113,12 +120,14 @@
// problem.
return;
}
+
+ final target_final = target;
var needsParentheses = target.precedence < Precedence.postfix;
await builder.addDartFileEdit(file, (builder) {
if (needsParentheses) {
- builder.addSimpleInsertion(target.offset, '(');
+ builder.addSimpleInsertion(target_final.offset, '(');
}
- builder.addInsertion(target.end, (builder) {
+ builder.addInsertion(target_final.end, (builder) {
if (needsParentheses) {
builder.write(')');
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_override.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_override.dart
index 5f066c3..22c6924 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_override.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_override.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';
@@ -32,7 +30,7 @@
// Handle doc comments.
var token = member.beginToken;
if (token is CommentToken) {
- token = (token as CommentToken).parent;
+ token = token.parent!;
}
var exitPosition = Position(file, token.offset - 1);
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_required.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_required.dart
index 81a242b..0f61891 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_required.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_required.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:analyzer/dart/ast/ast.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -18,9 +17,12 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleInsertion(node.parent.offset, '@required ');
- });
+ var parameter = node.parent;
+ if (parameter is FormalParameter) {
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addSimpleInsertion(parameter.offset, '@required ');
+ });
+ }
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_required_keyword.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_required_keyword.dart
index 4160520..cf83196 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_required_keyword.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_required_keyword.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,24 +16,27 @@
@override
Future<void> compute(ChangeBuilder builder) async {
await builder.addDartFileEdit(file, (builder) {
- var insertOffset = node.parent.offset;
-
- var parent = node.parent;
- if (parent is FormalParameter) {
- var metadata = parent.metadata;
- // Check for redundant `@required` annotations.
- if (metadata.isNotEmpty) {
- for (var annotation in metadata) {
- if (annotation.elementAnnotation.isRequired) {
- var length = annotation.endToken.next.offset -
- annotation.beginToken.offset;
- builder.addDeletion(SourceRange(annotation.offset, length));
- break;
- }
- }
- insertOffset = metadata.endToken.next.offset;
- }
+ var parameter = node.parent;
+ if (parameter is! FormalParameter) {
+ return;
}
+
+ var insertOffset = parameter.offset;
+
+ // Check for redundant `@required` annotations.
+ var metadata = parameter.metadata;
+ if (metadata.isNotEmpty) {
+ for (var annotation in metadata) {
+ if (annotation.elementAnnotation!.isRequired) {
+ var length =
+ annotation.endToken.next!.offset - annotation.beginToken.offset;
+ builder.addDeletion(SourceRange(annotation.offset, length));
+ break;
+ }
+ }
+ insertOffset = metadata.endToken!.next!.offset;
+ }
+
builder.addSimpleInsertion(insertOffset, 'required ');
});
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_return_type.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_return_type.dart
index dbc3d8d..7e49c67 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_return_type.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_return_type.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -13,6 +11,7 @@
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/dart/element/type_system.dart';
+import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -26,8 +25,8 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- SyntacticEntity insertBeforeEntity;
- FunctionBody body;
+ SyntacticEntity? insertBeforeEntity;
+ FunctionBody? body;
if (node is SimpleIdentifier) {
var executable = node.parent;
if (executable is MethodDeclaration && executable.name == node) {
@@ -55,13 +54,18 @@
}
}
+ if (insertBeforeEntity == null || body == null) {
+ return;
+ }
+
var returnType = _inferReturnType(body);
if (returnType == null) {
return null;
}
+ final insertBeforeEntity_final = insertBeforeEntity;
await builder.addDartFileEdit(file, (builder) {
- builder.addInsertion(insertBeforeEntity.offset, (builder) {
+ builder.addInsertion(insertBeforeEntity_final.offset, (builder) {
if (returnType.isDynamic) {
builder.write('dynamic');
} else {
@@ -74,10 +78,10 @@
/// Return the type of value returned by the function [body], or `null` if a
/// type can't be inferred.
- DartType _inferReturnType(FunctionBody body) {
- DartType baseType;
+ DartType? _inferReturnType(FunctionBody body) {
+ DartType? baseType;
if (body is ExpressionFunctionBody) {
- baseType = body.expression.staticType;
+ baseType = body.expression.typeOrThrow;
} else if (body is BlockFunctionBody) {
var computer = _ReturnTypeComputer(resolvedResult.typeSystem);
body.block.accept(computer);
@@ -124,7 +128,7 @@
class _ReturnTypeComputer extends RecursiveAstVisitor<void> {
final TypeSystem typeSystem;
- DartType returnType;
+ DartType? returnType;
/// A flag indicating whether at least one return statement was found.
bool hasReturn = false;
@@ -143,18 +147,19 @@
return;
}
// prepare type
- var type = expression.staticType;
+ var type = expression.typeOrThrow;
if (type.isBottom) {
return;
}
// combine types
- if (returnType == null) {
+ var current = returnType;
+ if (current == null) {
returnType = type;
} else {
- if (returnType is InterfaceType && type is InterfaceType) {
- returnType = InterfaceType.getSmartLeastUpperBound(returnType, type);
+ if (current is InterfaceType && type is InterfaceType) {
+ returnType = InterfaceType.getSmartLeastUpperBound(current, type);
} else {
- returnType = typeSystem.leastUpperBound(returnType, type);
+ returnType = typeSystem.leastUpperBound(current, type);
}
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_static.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_static.dart
index f165efc..4ee5960 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_static.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_static.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/add_super_constructor_invocation.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_super_constructor_invocation.dart
index cce07f3..092f566 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_super_constructor_invocation.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_super_constructor_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:analysis_server/src/services/correction/util.dart';
@@ -16,14 +14,22 @@
class AddSuperConstructorInvocation extends MultiCorrectionProducer {
@override
Iterable<CorrectionProducer> get producers sync* {
- if (node.parent is! ConstructorDeclaration ||
- node.parent.parent is! ClassDeclaration) {
+ var targetConstructor = node.parent;
+ if (targetConstructor is! ConstructorDeclaration) {
return;
}
- var targetConstructor = node.parent as ConstructorDeclaration;
- var targetClassNode = targetConstructor.parent as ClassDeclaration;
- var targetClassElement = targetClassNode.declaredElement;
+
+ var targetClassNode = targetConstructor.parent;
+ if (targetClassNode is! ClassDeclaration) {
+ return;
+ }
+
+ var targetClassElement = targetClassNode.declaredElement!;
var superType = targetClassElement.supertype;
+ if (superType == null) {
+ return;
+ }
+
var initializers = targetConstructor.initializers;
int insertOffset;
String prefix;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart
index ef04b02..08bd321 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.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:_fe_analyzer_shared/src/scanner/token.dart';
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/dart/element/type.dart';
import 'package:analyzer_plugin/src/utilities/change_builder/change_builder_core.dart';
@@ -36,7 +35,8 @@
return;
}
}
- while (node != null) {
+
+ for (var node in this.node.withParents) {
if (node is VariableDeclarationList) {
await _forVariableDeclaration(builder, node);
return;
@@ -47,8 +47,7 @@
var forLoopParts = node.forLoopParts;
if (forLoopParts is ForEachParts) {
var offset = this.node.offset;
- if (forLoopParts.iterable != null &&
- offset < forLoopParts.iterable.offset) {
+ if (offset < forLoopParts.iterable.offset) {
if (forLoopParts is ForEachPartsWithDeclaration) {
await _forDeclaredIdentifier(builder, forLoopParts.loopVariable);
}
@@ -56,7 +55,6 @@
}
return;
}
- node = node.parent;
}
}
@@ -78,7 +76,7 @@
if (declaredIdentifier.type != null) {
return;
}
- var type = declaredIdentifier.declaredElement.type;
+ var type = declaredIdentifier.declaredElement!.type;
if (type is! InterfaceType && type is! FunctionType) {
return;
}
@@ -88,7 +86,7 @@
var validChange = true;
await builder.addDartFileEdit(file, (builder) {
var keyword = declaredIdentifier.keyword;
- if (keyword.keyword == Keyword.VAR) {
+ if (keyword != null && keyword.keyword == Keyword.VAR) {
builder.addReplacement(range.token(keyword), (builder) {
validChange = builder.writeType(type);
});
@@ -114,7 +112,7 @@
return;
}
// Prepare the type.
- var type = parameter.declaredElement.type;
+ var type = parameter.declaredElement!.type;
// TODO(scheglov) If the parameter is in a method declaration, and if the
// method overrides a method that has a type for the corresponding
// parameter, it would be nice to copy down the type from the overridden
@@ -175,7 +173,7 @@
var validChange = true;
await builder.addDartFileEdit(file, (builder) {
var keyword = declarationList.keyword;
- if (keyword?.keyword == Keyword.VAR) {
+ if (keyword != null && keyword.keyword == Keyword.VAR) {
builder.addReplacement(range.token(keyword), (builder) {
validChange = builder.writeType(type);
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/assign_to_local_variable.dart b/pkg/analysis_server/lib/src/services/correction/dart/assign_to_local_variable.dart
index 54e77cf..5aa14c3 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/assign_to_local_variable.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/assign_to_local_variable.dart
@@ -2,13 +2,13 @@
// 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/protocol_server.dart';
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/name_suggestion.dart';
+import 'package:analysis_server/src/utilities/extensions/ast.dart';
import 'package:analyzer/dart/ast/ast.dart';
+import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
@@ -20,8 +20,8 @@
@override
Future<void> compute(ChangeBuilder builder) async {
// prepare enclosing ExpressionStatement
- ExpressionStatement expressionStatement;
- for (var node = this.node; node != null; node = node.parent) {
+ ExpressionStatement? expressionStatement;
+ for (var node in this.node.withParents) {
if (node is ExpressionStatement) {
expressionStatement = node;
break;
@@ -41,7 +41,7 @@
var expression = expressionStatement.expression;
var offset = expression.offset;
// prepare expression type
- var type = expression.staticType;
+ var type = expression.typeOrThrow;
if (type.isVoid) {
return;
}
@@ -78,9 +78,9 @@
var index = statements.indexOf(statement);
if (index > 0) {
var precedingStatement = statements[index - 1];
- if (precedingStatement is ExpressionStatement &&
- precedingStatement.semicolon.isSynthetic) {
- return true;
+ if (precedingStatement is ExpressionStatement) {
+ var semicolon = precedingStatement.semicolon;
+ return semicolon != null && semicolon.isSynthetic;
} else if (precedingStatement is VariableDeclarationStatement &&
precedingStatement.semicolon.isSynthetic) {
return true;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/change_argument_name.dart b/pkg/analysis_server/lib/src/services/correction/dart/change_argument_name.dart
index c7ff25f..75c187f 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/change_argument_name.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/change_argument_name.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/executable_parameters.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -20,18 +18,25 @@
@override
Iterable<CorrectionProducer> get producers sync* {
- var names = _getNamedParameterNames();
- if (names == null || names.isEmpty) {
+ var namedContext = _getNamedParameterNames();
+ if (namedContext == null) {
return;
}
- SimpleIdentifier argumentName = node;
- var invalidName = argumentName.name;
+
+ var names = namedContext.names;
+ if (names.isEmpty) {
+ return;
+ }
+
+ var currentNameNode = namedContext.identifier;
+ var currentName = currentNameNode.name;
+
for (var proposedName in names) {
- var distance = _computeDistance(invalidName, proposedName);
+ var distance = _computeDistance(currentName, proposedName);
if (distance <= _maxDistance) {
// TODO(brianwilkerson) Create a way to use the distance as part of the
// computation of the priority (so that closer names sort first).
- yield _ChangeName(argumentName, proposedName);
+ yield _ChangeName(currentNameNode, proposedName);
}
}
}
@@ -47,15 +52,20 @@
return levenshtein(current, proposal, _maxDistance, caseSensitive: false);
}
- List<String> _getNamedParameterNames() {
- var namedExpression = node?.parent?.parent;
+ _NamedExpressionContext? _getNamedParameterNames() {
+ var node = this.node;
+ var namedExpression = node.parent?.parent;
if (node is SimpleIdentifier &&
namedExpression is NamedExpression &&
- namedExpression.name == node.parent &&
- namedExpression.parent is ArgumentList) {
- var parameters = ExecutableParameters.forInvocation(
- sessionHelper, namedExpression.parent.parent);
- return parameters?.namedNames;
+ namedExpression.name == node.parent) {
+ var argumentList = namedExpression.parent;
+ if (argumentList is ArgumentList) {
+ var parameters = ExecutableParameters.forInvocation(
+ sessionHelper, argumentList.parent);
+ if (parameters != null) {
+ return _NamedExpressionContext(node, parameters.namedNames);
+ }
+ }
}
return null;
}
@@ -88,3 +98,10 @@
});
}
}
+
+class _NamedExpressionContext {
+ final SimpleIdentifier identifier;
+ final List<String> names;
+
+ _NamedExpressionContext(this.identifier, this.names);
+}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/change_to.dart b/pkg/analysis_server/lib/src/services/correction/dart/change_to.dart
index 7ba2aa2..0f81d93 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/change_to.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/change_to.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/levenshtein.dart';
@@ -23,7 +21,7 @@
final _ReplacementKind _kind;
/// The name to which the undefined name will be changed.
- String _proposedName;
+ String _proposedName = '';
/// Initialize a newly created instance that will propose elements of the
/// given [_kind].
@@ -58,7 +56,7 @@
var node = this.node;
if (node is Annotation) {
var name = node.name;
- if (name != null && name.staticElement == null) {
+ if (name.staticElement == null) {
if (node.arguments != null) {
await _proposeClassOrMixin(builder, name);
}
@@ -68,12 +66,12 @@
Future<void> _proposeClassOrMixin(ChangeBuilder builder, AstNode node) async {
// Prepare the optional import prefix name.
- String prefixName;
+ String? prefixName;
if (node is PrefixedIdentifier &&
node.parent is TypeName &&
node.prefix.staticElement is PrefixElement) {
- prefixName = (node as PrefixedIdentifier).prefix.name;
- node = (node as PrefixedIdentifier).identifier;
+ prefixName = node.prefix.name;
+ node = node.identifier;
}
// Process if looks like a type.
if (mightBeTypeIdentifier(node)) {
@@ -95,34 +93,34 @@
}
}
// If we have a close enough element, suggest to use it.
- if (finder._element != null) {
- _proposedName = finder._element.name;
- if (_proposedName != null) {
- await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleReplacement(range.node(node), _proposedName);
- });
- }
+ var foundElementName = finder._element?.name;
+ if (foundElementName != null) {
+ _proposedName = foundElementName;
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addSimpleReplacement(range.node(node), _proposedName);
+ });
}
}
}
Future<void> _proposeClassOrMixinMember(ChangeBuilder builder,
- Expression target, _ElementPredicate predicate) async {
+ Expression? target, _ElementPredicate predicate) async {
+ var node = this.node;
+ var targetIdentifierElement =
+ target is Identifier ? target.staticElement : null;
if (node is SimpleIdentifier) {
- var name = (node as SimpleIdentifier).name;
- var finder = _ClosestElementFinder(name, predicate);
+ var finder = _ClosestElementFinder(node.name, predicate);
// unqualified invocation
if (target == null) {
var clazz = node.thisOrAncestorOfType<ClassDeclaration>();
if (clazz != null) {
- var classElement = clazz.declaredElement;
+ var classElement = clazz.declaredElement!;
_updateFinderWithClassMembers(finder, classElement);
}
} else if (target is ExtensionOverride) {
_updateFinderWithExtensionMembers(finder, target.staticElement);
- } else if (target is Identifier &&
- target.staticElement is ExtensionElement) {
- _updateFinderWithExtensionMembers(finder, target.staticElement);
+ } else if (targetIdentifierElement is ExtensionElement) {
+ _updateFinderWithExtensionMembers(finder, targetIdentifierElement);
} else {
var classElement = getTargetClassElement(target);
if (classElement != null) {
@@ -130,8 +128,9 @@
}
}
// if we have close enough element, suggest to use it
- if (finder._element != null) {
- _proposedName = finder._element.displayName;
+ var foundElementName = finder._element?.displayName;
+ if (foundElementName != null) {
+ _proposedName = foundElementName;
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(range.node(node), _proposedName);
});
@@ -143,7 +142,7 @@
var node = this.node;
if (node is SimpleIdentifier) {
// Prepare the optional import prefix name.
- String prefixName;
+ String? prefixName;
{
var invocation = node.parent;
if (invocation is MethodInvocation && invocation.methodName == node) {
@@ -171,8 +170,9 @@
}
}
// If we have a close enough element, suggest to use it.
- if (finder._element != null) {
- _proposedName = finder._element.name;
+ var foundElementName = finder._element?.name;
+ if (foundElementName != null) {
+ _proposedName = foundElementName;
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(range.node(node), _proposedName);
});
@@ -184,11 +184,12 @@
var node = this.node;
if (node is SimpleIdentifier) {
// prepare target
- Expression target;
- if (node.parent is PrefixedIdentifier) {
- target = (node.parent as PrefixedIdentifier).prefix;
- } else if (node.parent is PropertyAccess) {
- target = (node.parent as PropertyAccess).target;
+ Expression? target;
+ var parent = node.parent;
+ if (parent is PrefixedIdentifier) {
+ target = parent.prefix;
+ } else if (parent is PropertyAccess) {
+ target = parent.target;
}
// find getter or setter
var wantGetter = node.inGetterContext();
@@ -216,14 +217,12 @@
void _updateFinderWithClassMembers(
_ClosestElementFinder finder, ClassElement clazz) {
- if (clazz != null) {
- var members = getMembers(clazz);
- finder._updateList(members);
- }
+ var members = getMembers(clazz);
+ finder._updateList(members);
}
void _updateFinderWithExtensionMembers(
- _ClosestElementFinder finder, ExtensionElement element) {
+ _ClosestElementFinder finder, ExtensionElement? element) {
if (element != null) {
finder._updateList(getExtensionMembers(element));
}
@@ -265,16 +264,19 @@
int _distance = _maxDistance;
- Element _element;
+ Element? _element;
_ClosestElementFinder(this._targetName, this._predicate);
void _update(Element element) {
if (_predicate(element)) {
- var memberDistance = levenshtein(element.name, _targetName, _distance);
- if (memberDistance < _distance) {
- _element = element;
- _distance = memberDistance;
+ var name = element.name;
+ if (name != null) {
+ var memberDistance = levenshtein(name, _targetName, _distance);
+ if (memberDistance < _distance) {
+ _element = element;
+ _distance = memberDistance;
+ }
}
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/change_to_nearest_precise_value.dart b/pkg/analysis_server/lib/src/services/correction/dart/change_to_nearest_precise_value.dart
index ef2e3ba..4cd612f 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/change_to_nearest_precise_value.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/change_to_nearest_precise_value.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,11 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- IntegerLiteral integer = node;
+ var integer = node;
+ if (integer is! IntegerLiteral) {
+ return;
+ }
+
var lexeme = integer.literal.lexeme;
var precise = BigInt.from(IntegerLiteralImpl.nearestValidDouble(lexeme));
_correction = lexeme.toLowerCase().contains('x')
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/change_to_static_access.dart b/pkg/analysis_server/lib/src/services/correction/dart/change_to_static_access.dart
index 69aad4b..d6b1d16 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/change_to_static_access.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/change_to_static_access.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 ChangeToStaticAccess extends CorrectionProducer {
- String _className;
+ String _className = '';
@override
List<Object> get fixArguments => [_className];
@@ -23,31 +21,48 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- Expression target;
- Element invokedElement;
- if (node is SimpleIdentifier && node.parent is MethodInvocation) {
- var invocation = node.parent as MethodInvocation;
- if (invocation.methodName == node) {
- target = invocation.target;
- invokedElement = invocation.methodName.staticElement;
- }
- } else if (node is SimpleIdentifier && node.parent is PrefixedIdentifier) {
- var prefixed = node.parent as PrefixedIdentifier;
- if (prefixed.identifier == node) {
- target = prefixed.prefix;
- invokedElement = prefixed.identifier.staticElement;
+ Expression? target;
+ Element? invokedElement;
+ var identifier = node;
+ if (identifier is SimpleIdentifier) {
+ var parent = identifier.parent;
+ if (parent is MethodInvocation) {
+ if (parent.methodName == identifier) {
+ target = parent.target;
+ invokedElement = identifier.staticElement;
+ }
+ } else if (parent is PrefixedIdentifier) {
+ if (parent.identifier == identifier) {
+ target = parent.prefix;
+ invokedElement = identifier.staticElement;
+ }
}
}
- if (target == null) {
+ if (target == null || invokedElement is! ExecutableElement) {
return;
}
+
+ final target_final = target;
var declaringElement = invokedElement.enclosingElement;
- await builder.addDartFileEdit(file, (builder) {
- builder.addReplacement(range.node(target), (builder) {
- builder.writeReference(declaringElement);
+
+ if (declaringElement is ClassElement) {
+ _className = declaringElement.name;
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addReplacement(range.node(target_final), (builder) {
+ builder.writeReference(declaringElement);
+ });
});
- });
- _className = declaringElement.name;
+ } else if (declaringElement is ExtensionElement) {
+ var extensionName = declaringElement.name;
+ if (extensionName != null) {
+ _className = extensionName;
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addReplacement(range.node(target_final), (builder) {
+ builder.writeReference(declaringElement);
+ });
+ });
+ }
+ }
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/change_type_annotation.dart b/pkg/analysis_server/lib/src/services/correction/dart/change_type_annotation.dart
index 9aef73e..f1e0afd 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/change_type_annotation.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/change_type_annotation.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/type.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';
@@ -26,24 +25,29 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var declaration = coveredNode?.parent;
- if (declaration is VariableDeclaration &&
- declaration.initializer == coveredNode) {
- var variableList = declaration.parent;
- if (variableList is VariableDeclarationList &&
- variableList.variables.length == 1) {
- var typeNode = variableList.type;
- if (typeNode != null) {
- Expression initializer = coveredNode;
- var newType = initializer.staticType;
- if (newType is InterfaceType || newType is FunctionType) {
- _oldAnnotation = displayStringForType(typeNode.type);
- _newAnnotation = displayStringForType(newType);
- await builder.addDartFileEdit(file, (builder) {
- builder.addReplacement(range.node(typeNode), (builder) {
- builder.writeType(newType);
- });
+ if (declaration is! VariableDeclaration) {
+ return;
+ }
+
+ var initializer = declaration.initializer;
+ if (initializer == null || initializer != coveredNode) {
+ return;
+ }
+
+ var variableList = declaration.parent;
+ if (variableList is VariableDeclarationList &&
+ variableList.variables.length == 1) {
+ var typeNode = variableList.type;
+ if (typeNode != null) {
+ var newType = initializer.typeOrThrow;
+ if (newType is InterfaceType || newType is FunctionType) {
+ _oldAnnotation = displayStringForType(typeNode.typeOrThrow);
+ _newAnnotation = displayStringForType(newType);
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addReplacement(range.node(typeNode), (builder) {
+ builder.writeType(newType);
});
- }
+ });
}
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_add_all_to_spread.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_add_all_to_spread.dart
index 06de8b9..c4fcbab 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_add_all_to_spread.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_add_all_to_spread.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
@@ -16,7 +14,7 @@
class ConvertAddAllToSpread extends CorrectionProducer {
/// The arguments used to compose the message.
- List<String> _args;
+ List<String> _args = [];
/// A flag indicating whether the change that was built is one that inlines
/// the elements of another list into the target list.
@@ -45,22 +43,31 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var node = this.node;
- if (node is! SimpleIdentifier || node.parent is! MethodInvocation) {
+ var name = node;
+ if (name is! SimpleIdentifier) {
return;
}
- SimpleIdentifier name = node;
- MethodInvocation invocation = node.parent;
+
+ var invocation = name.parent;
+ if (invocation is! MethodInvocation) {
+ return;
+ }
+
if (name != invocation.methodName ||
name.name != 'addAll' ||
!invocation.isCascaded ||
invocation.argumentList.arguments.length != 1) {
return;
}
+
var cascade = invocation.thisOrAncestorOfType<CascadeExpression>();
+ if (cascade == null) {
+ return;
+ }
+
var sections = cascade.cascadeSections;
- var target = cascade.target;
- if (target is! ListLiteral || sections[0] != invocation) {
+ var targetList = cascade.target;
+ if (targetList is! ListLiteral || sections[0] != invocation) {
// TODO(brianwilkerson) Consider extending this to handle set literals.
return;
}
@@ -68,9 +75,8 @@
bool isEmptyListLiteral(Expression expression) =>
expression is ListLiteral && expression.elements.isEmpty;
- ListLiteral list = target;
var argument = invocation.argumentList.arguments[0];
- String elementText;
+ String? elementText;
if (argument is BinaryExpression &&
argument.operator.type == TokenType.QUESTION_QUESTION) {
var right = argument.rightOperand;
@@ -105,13 +111,20 @@
}
elementText ??= '...${utils.getNodeText(argument)}';
+ final elementText_final = elementText;
await builder.addDartFileEdit(file, (builder) {
- if (list.elements.isNotEmpty) {
+ if (targetList.elements.isNotEmpty) {
// ['a']..addAll(['b', 'c']);
- builder.addSimpleInsertion(list.elements.last.end, ', $elementText');
+ builder.addSimpleInsertion(
+ targetList.elements.last.end,
+ ', $elementText_final',
+ );
} else {
// []..addAll(['b', 'c']);
- builder.addSimpleInsertion(list.leftBracket.end, elementText);
+ builder.addSimpleInsertion(
+ targetList.leftBracket.end,
+ elementText_final,
+ );
}
builder.addDeletion(range.node(invocation));
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_mixin.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_mixin.dart
index d54b755..f3184e4 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_mixin.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_mixin.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -38,7 +36,7 @@
var superclassConstraints = <InterfaceType>[];
var interfaces = <InterfaceType>[];
- var classElement = classDeclaration.declaredElement;
+ var classElement = classDeclaration.declaredElement!;
for (var type in classElement.mixins) {
if (referencedClasses.contains(type.element)) {
superclassConstraints.add(type);
@@ -46,12 +44,13 @@
interfaces.add(type);
}
}
- var extendsClause = classDeclaration.extendsClause;
- if (extendsClause != null) {
+
+ var superType = classElement.supertype;
+ if (classDeclaration.extendsClause != null && superType != null) {
if (referencedClasses.length > superclassConstraints.length) {
- superclassConstraints.insert(0, classElement.supertype);
+ superclassConstraints.insert(0, superType);
} else {
- interfaces.insert(0, classElement.supertype);
+ interfaces.insert(0, superType);
}
}
interfaces.addAll(classElement.interfaces);
@@ -63,8 +62,7 @@
classDeclaration.leftBracket), (builder) {
builder.write('mixin ');
builder.write(classDeclaration.name.name);
- builder.writeTypeParameters(
- classDeclaration.declaredElement.typeParameters);
+ builder.writeTypeParameters(classElement.typeParameters);
builder.writeTypes(superclassConstraints, prefix: ' on ');
builder.writeTypes(interfaces, prefix: ' implements ');
builder.write(' ');
@@ -100,7 +98,7 @@
return super.visitSuperExpression(node);
}
- void _addElement(Element element) {
+ void _addElement(Element? element) {
if (element is ExecutableElement) {
var enclosingElement = element.enclosingElement;
if (enclosingElement is ClassElement) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_conditional_expression_to_if_element.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_conditional_expression_to_if_element.dart
index 64feabda..e6294e5 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_conditional_expression_to_if_element.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_conditional_expression_to_if_element.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -25,18 +23,19 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- AstNode node = this.node.thisOrAncestorOfType<ConditionalExpression>();
- if (node == null) {
+ var conditional = node.thisOrAncestorOfType<ConditionalExpression>();
+ if (conditional == null) {
return null;
}
- var nodeToReplace = node;
- var parent = node.parent;
+
+ AstNode nodeToReplace = conditional;
+ var parent = conditional.parent;
while (parent is ParenthesizedExpression) {
nodeToReplace = parent;
parent = parent.parent;
}
- if (parent is ListLiteral || (parent is SetOrMapLiteral && parent.isSet)) {
- ConditionalExpression conditional = node;
+
+ if (parent is ListLiteral || parent is SetOrMapLiteral && parent.isSet) {
var condition = conditional.condition.unParenthesized;
var thenExpression = conditional.thenExpression.unParenthesized;
var elseExpression = conditional.elseExpression.unParenthesized;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_documentation_into_block.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_documentation_into_block.dart
index d3e57cf..7068e13 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_documentation_into_block.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_documentation_into_block.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_documentation_into_line.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_documentation_into_line.dart
index 5b7b37a..f82eeca 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_documentation_into_line.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_documentation_into_line.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_flutter_child.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_flutter_child.dart
index b767896..3556de0 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_flutter_child.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_flutter_child.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/convert_flutter_children.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_flutter_children.dart
index 5e29768..c105653 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_flutter_children.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_flutter_children.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,26 +15,26 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var node = this.node;
- if (node is SimpleIdentifier &&
- node.name == 'children' &&
- node.parent?.parent is NamedExpression) {
- NamedExpression named = node.parent?.parent;
- var expression = named.expression;
- if (expression is ListLiteral && expression.elements.length == 1) {
- var widget = expression.elements[0];
- if (flutter.isWidgetExpression(widget)) {
- var widgetText = utils.getNodeText(widget);
- var indentOld = utils.getLinePrefix(widget.offset);
- var indentNew = utils.getLinePrefix(named.offset);
- widgetText = _replaceSourceIndent(widgetText, indentOld, indentNew);
+ var identifier = node;
+ if (identifier is SimpleIdentifier && identifier.name == 'children') {
+ var namedExpression = identifier.parent?.parent;
+ if (namedExpression is NamedExpression) {
+ var expression = namedExpression.expression;
+ if (expression is ListLiteral && expression.elements.length == 1) {
+ var widget = expression.elements[0];
+ if (flutter.isWidgetExpression(widget)) {
+ var widgetText = utils.getNodeText(widget);
+ var indentOld = utils.getLinePrefix(widget.offset);
+ var indentNew = utils.getLinePrefix(namedExpression.offset);
+ widgetText = _replaceSourceIndent(widgetText, indentOld, indentNew);
- await builder.addDartFileEdit(file, (builder) {
- builder.addReplacement(range.node(named), (builder) {
- builder.write('child: ');
- builder.write(widgetText);
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addReplacement(range.node(namedExpression), (builder) {
+ builder.write('child: ');
+ builder.write(widgetText);
+ });
});
- });
+ }
}
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_async_body.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_async_body.dart
index 5373777..1ccaff4 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_async_body.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_async_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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_block_body.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_block_body.dart
index 20c3d7c..d674907 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_block_body.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_block_body.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:analyzer/dart/ast/ast.dart';
+import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -23,7 +22,7 @@
return;
}
- var returnValue = (body as ExpressionFunctionBody).expression;
+ var returnValue = body.expression;
// Return expressions can be quite large, e.g. Flutter build() methods.
// It is surprising to see this Quick Assist deep in the function body.
@@ -31,10 +30,10 @@
return;
}
- var returnValueType = returnValue.staticType;
+ var returnValueType = returnValue.typeOrThrow;
var returnValueCode = utils.getNodeText(returnValue);
// prepare prefix
- var prefix = utils.getNodePrefix(body.parent);
+ var prefix = utils.getNodePrefix(body.parent!);
var indent = utils.getIndent(1);
await builder.addDartFileEdit(file, (builder) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_final_field.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_final_field.dart
index 0cd856c..4e9ee8e 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_final_field.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_final_field.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
+import 'package:analysis_server/src/utilities/extensions/ast.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
@@ -19,8 +18,8 @@
@override
Future<void> compute(ChangeBuilder builder) async {
// Find the enclosing getter.
- MethodDeclaration getter;
- for (var n = node; n != null; n = n.parent) {
+ MethodDeclaration? getter;
+ for (var n in node.withParents) {
if (n is MethodDeclaration) {
getter = n;
break;
@@ -32,9 +31,15 @@
}
break;
}
- if (getter == null || !getter.isGetter) {
+ if (getter == null) {
return;
}
+
+ var propertyKeywordGet = getter.propertyKeywordGet;
+ if (propertyKeywordGet == null) {
+ return;
+ }
+
// Check that there is no corresponding setter.
{
var element = getter.declaredElement;
@@ -49,7 +54,7 @@
}
}
// Try to find the returned expression.
- Expression expression;
+ Expression? expression;
{
var body = getter.body;
if (body is ExpressionFunctionBody) {
@@ -66,9 +71,10 @@
}
// Use the returned expression as the field initializer.
if (expression != null) {
+ var returnType = getter.returnType;
var code = 'final';
- if (getter.returnType != null) {
- code += ' ' + utils.getNodeText(getter.returnType);
+ if (returnType != null) {
+ code += ' ' + utils.getNodeText(returnType);
}
code += ' ' + utils.getNodeText(getter.name);
if (expression is! NullLiteral) {
@@ -76,7 +82,7 @@
}
code += ';';
var replacementRange =
- range.startEnd(getter.returnType ?? getter.propertyKeyword, getter);
+ range.startEnd(returnType ?? propertyKeywordGet, getter);
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(replacementRange, code);
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_for_index.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_for_index.dart
index 39c4e84..f7df358 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_for_index.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_for_index.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -20,15 +18,18 @@
@override
Future<void> compute(ChangeBuilder builder) async {
// find enclosing ForEachStatement
- var forEachStatement = node.thisOrAncestorMatching(
- (node) => node is ForStatement && node.forLoopParts is ForEachParts)
- as ForStatement;
- if (forEachStatement == null) {
+ var forStatement = node.thisOrAncestorOfType<ForStatement>();
+ if (forStatement is! ForStatement) {
return;
}
- ForEachParts forEachParts = forEachStatement.forLoopParts;
- if (selectionOffset < forEachStatement.offset ||
- forEachStatement.rightParenthesis.end < selectionOffset) {
+
+ var forEachParts = forStatement.forLoopParts;
+ if (forEachParts is! ForEachParts) {
+ return;
+ }
+
+ if (selectionOffset < forStatement.offset ||
+ forStatement.rightParenthesis.end < selectionOffset) {
return;
}
// loop should declare variable
@@ -56,15 +57,15 @@
}
}
// body should be Block
- if (forEachStatement.body is! Block) {
+ var body = forStatement.body;
+ if (body is! Block) {
return;
}
- Block body = forEachStatement.body;
// prepare a name for the index variable
String indexName;
{
var conflicts =
- utils.findPossibleLocalVariableConflicts(forEachStatement.offset);
+ utils.findPossibleLocalVariableConflicts(forStatement.offset);
if (!conflicts.contains('i')) {
indexName = 'i';
} else if (!conflicts.contains('j')) {
@@ -76,14 +77,14 @@
}
}
// prepare environment
- var prefix = utils.getNodePrefix(forEachStatement);
+ var prefix = utils.getNodePrefix(forStatement);
var indent = utils.getIndent(1);
var firstBlockLine = utils.getLineContentEnd(body.leftBracket.end);
// add change
await builder.addDartFileEdit(file, (builder) {
// TODO(brianwilkerson) Create linked positions for the loop variable.
builder.addSimpleReplacement(
- range.startEnd(forEachStatement, forEachStatement.rightParenthesis),
+ range.startEnd(forStatement, forStatement.rightParenthesis),
'for (int $indexName = 0; $indexName < $listName.length; $indexName++)');
builder.addSimpleInsertion(firstBlockLine,
'$prefix$indent$loopVariable = $listName[$indexName];$eol');
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_getter.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_getter.dart
index 75b47ca..a1f4d53 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_getter.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_getter.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.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';
@@ -18,8 +17,8 @@
@override
Future<void> compute(ChangeBuilder builder) async {
// Find the enclosing field declaration.
- FieldDeclaration fieldDeclaration;
- for (var n = node; n != null; n = n.parent) {
+ FieldDeclaration? fieldDeclaration;
+ for (var n in node.withParents) {
if (n is FieldDeclaration) {
fieldDeclaration = n;
break;
@@ -38,7 +37,8 @@
}
// The field must be final and has only one variable.
var fieldList = fieldDeclaration.fields;
- if (!fieldList.isFinal || fieldList.variables.length != 1) {
+ var finalKeyword = fieldList.keyword;
+ if (finalKeyword == null || fieldList.variables.length != 1) {
return;
}
var field = fieldList.variables.first;
@@ -49,14 +49,15 @@
}
// Add proposal.
var code = '';
- if (fieldList.type != null) {
- code += utils.getNodeText(fieldList.type) + ' ';
+ var typeAnnotation = fieldList.type;
+ if (typeAnnotation != null) {
+ code += utils.getNodeText(typeAnnotation) + ' ';
}
code += 'get';
code += ' ' + utils.getNodeText(field.name);
code += ' => ' + utils.getNodeText(initializer);
code += ';';
- var replacementRange = range.startEnd(fieldList.keyword, fieldDeclaration);
+ var replacementRange = range.startEnd(finalKeyword, fieldDeclaration);
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(replacementRange, code);
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_is_not.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_is_not.dart
index d39a67d..e84c81f 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_is_not.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_is_not.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/util.dart';
@@ -26,13 +24,17 @@
var node = this.node;
if (node is PrefixExpression) {
var operand = node.operand;
- if (operand is ParenthesizedExpression &&
- operand.expression is IsExpression) {
- isExpression = operand.expression as IsExpression;
+ if (operand is ParenthesizedExpression) {
+ var expression = operand.expression;
+ if (expression is IsExpression) {
+ isExpression = expression;
+ }
}
- } else if (node is ParenthesizedExpression &&
- node.expression is IsExpression) {
- isExpression = node.expression as IsExpression;
+ } else if (node is ParenthesizedExpression) {
+ var expression = node.expression;
+ if (expression is IsExpression) {
+ isExpression = expression;
+ }
}
}
if (isExpression == null) {
@@ -42,21 +44,20 @@
return;
}
// prepare enclosing ()
- var parent = isExpression.parent;
- if (parent is! ParenthesizedExpression) {
+ var parExpression = isExpression.parent;
+ if (parExpression is! ParenthesizedExpression) {
return;
}
- var parExpression = parent as ParenthesizedExpression;
// prepare enclosing !()
- var parent2 = parent.parent;
- if (parent2 is! PrefixExpression) {
+ var prefExpression = parExpression.parent;
+ if (prefExpression is! PrefixExpression) {
return;
}
- var prefExpression = parent2 as PrefixExpression;
if (prefExpression.operator.type != TokenType.BANG) {
return;
}
+ final isExpression_final = isExpression;
await builder.addDartFileEdit(file, (builder) {
if (getExpressionParentPrecedence(prefExpression) >=
Precedence.relational) {
@@ -67,7 +68,7 @@
builder.addDeletion(
range.startEnd(parExpression.rightParenthesis, prefExpression));
}
- builder.addSimpleInsertion(isExpression.isOperator.end, '!');
+ builder.addSimpleInsertion(isExpression_final.isOperator.end, '!');
});
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_is_not_empty.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_is_not_empty.dart
index e236dea..2e7cecb 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_is_not_empty.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_is_not_empty.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
@@ -20,8 +18,8 @@
@override
Future<void> compute(ChangeBuilder builder) async {
// prepare "expr.isEmpty"
- AstNode isEmptyAccess;
- SimpleIdentifier isEmptyIdentifier;
+ SimpleIdentifier? isEmptyIdentifier;
+ AstNode? isEmptyAccess;
if (node is SimpleIdentifier) {
var identifier = node as SimpleIdentifier;
var parent = identifier.parent;
@@ -36,7 +34,7 @@
isEmptyAccess = parent;
}
}
- if (isEmptyIdentifier == null) {
+ if (isEmptyIdentifier == null || isEmptyAccess == null) {
return;
}
// should be "isEmpty"
@@ -60,10 +58,12 @@
return;
}
+ final isEmptyIdentifier_final = isEmptyIdentifier;
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(
range.startStart(prefixExpression, prefixExpression.operand));
- builder.addSimpleReplacement(range.node(isEmptyIdentifier), 'isNotEmpty');
+ builder.addSimpleReplacement(
+ range.node(isEmptyIdentifier_final), 'isNotEmpty');
});
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_map_from_iterable_to_for_literal.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_map_from_iterable_to_for_literal.dart
index 20985e4..c6b8268 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_map_from_iterable_to_for_literal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_map_from_iterable_to_for_literal.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -51,41 +49,10 @@
var secondArg = arguments[1];
var thirdArg = arguments[2];
- Expression extractBody(FunctionExpression expression) {
- var body = expression.body;
- if (body is ExpressionFunctionBody) {
- return body.expression;
- } else if (body is BlockFunctionBody) {
- var statements = body.block.statements;
- if (statements.length == 1) {
- var statement = statements[0];
- if (statement is ReturnStatement) {
- return statement.expression;
- }
- }
- }
- return null;
- }
-
- FunctionExpression extractClosure(String name, Expression argument) {
- if (argument is NamedExpression && argument.name.label.name == name) {
- var expression = argument.expression.unParenthesized;
- if (expression is FunctionExpression) {
- var parameters = expression.parameters.parameters;
- if (parameters.length == 1 && parameters[0].isRequiredPositional) {
- if (extractBody(expression) != null) {
- return expression;
- }
- }
- }
- }
- return null;
- }
-
var keyClosure =
- extractClosure('key', secondArg) ?? extractClosure('key', thirdArg);
- var valueClosure =
- extractClosure('value', thirdArg) ?? extractClosure('value', secondArg);
+ _extractClosure('key', secondArg) ?? _extractClosure('key', thirdArg);
+ var valueClosure = _extractClosure('value', thirdArg) ??
+ _extractClosure('value', secondArg);
if (keyClosure == null || valueClosure == null) {
return null;
}
@@ -93,26 +60,23 @@
// Compute the loop variable name and convert the key and value closures if
// necessary.
//
- SimpleFormalParameter keyParameter = keyClosure.parameters.parameters[0];
- var keyParameterName = keyParameter.identifier.name;
- SimpleFormalParameter valueParameter =
- valueClosure.parameters.parameters[0];
- var valueParameterName = valueParameter.identifier.name;
- var keyBody = extractBody(keyClosure);
- var keyExpressionText = utils.getNodeText(keyBody);
- var valueBody = extractBody(valueClosure);
- var valueExpressionText = utils.getNodeText(valueBody);
+ var keyParameter = keyClosure.parameter;
+ var keyParameterName = keyClosure.parameterIdentifier.name;
+ var valueParameter = valueClosure.parameter;
+ var valueParameterName = valueClosure.parameterIdentifier.name;
+ var keyExpressionText = utils.getNodeText(keyClosure.body);
+ var valueExpressionText = utils.getNodeText(valueClosure.body);
String loopVariableName;
if (keyParameterName == valueParameterName) {
loopVariableName = keyParameterName;
} else {
- var keyFinder = _ParameterReferenceFinder(keyParameter.declaredElement);
- keyBody.accept(keyFinder);
+ var keyFinder = _ParameterReferenceFinder(keyParameter.declaredElement!);
+ keyClosure.body.accept(keyFinder);
var valueFinder =
- _ParameterReferenceFinder(valueParameter.declaredElement);
- valueBody.accept(valueFinder);
+ _ParameterReferenceFinder(valueParameter.declaredElement!);
+ valueClosure.body.accept(valueFinder);
String computeUnusedVariableName() {
var candidate = 'e';
@@ -131,7 +95,7 @@
// referenced in the value expression.
loopVariableName = computeUnusedVariableName();
keyExpressionText = keyFinder.replaceName(
- keyExpressionText, loopVariableName, keyBody.offset);
+ keyExpressionText, loopVariableName, keyClosure.body.offset);
} else {
loopVariableName = keyParameterName;
}
@@ -142,7 +106,7 @@
// referenced in the key expression.
loopVariableName = computeUnusedVariableName();
valueExpressionText = valueFinder.replaceName(
- valueExpressionText, loopVariableName, valueBody.offset);
+ valueExpressionText, loopVariableName, valueClosure.body.offset);
} else {
loopVariableName = valueParameterName;
}
@@ -152,9 +116,9 @@
// either the key or value expressions.
loopVariableName = computeUnusedVariableName();
keyExpressionText = keyFinder.replaceName(
- keyExpressionText, loopVariableName, keyBody.offset);
+ keyExpressionText, loopVariableName, keyClosure.body.offset);
valueExpressionText = valueFinder.replaceName(
- valueExpressionText, loopVariableName, valueBody.offset);
+ valueExpressionText, loopVariableName, valueClosure.body.offset);
}
}
//
@@ -178,6 +142,56 @@
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static ConvertMapFromIterableToForLiteral newInstance() =>
ConvertMapFromIterableToForLiteral();
+
+ static Expression? _extractBody(FunctionExpression expression) {
+ var body = expression.body;
+ if (body is ExpressionFunctionBody) {
+ return body.expression;
+ } else if (body is BlockFunctionBody) {
+ var statements = body.block.statements;
+ if (statements.length == 1) {
+ var statement = statements[0];
+ if (statement is ReturnStatement) {
+ return statement.expression;
+ }
+ }
+ }
+ return null;
+ }
+
+ static _Closure? _extractClosure(String name, Expression argument) {
+ if (argument is NamedExpression && argument.name.label.name == name) {
+ var expression = argument.expression.unParenthesized;
+ if (expression is FunctionExpression) {
+ var parameterList = expression.parameters;
+ if (parameterList != null) {
+ var parameters = parameterList.parameters;
+ if (parameters.length == 1) {
+ var parameter = parameters[0];
+ if (parameter is SimpleFormalParameter &&
+ parameter.isRequiredPositional) {
+ var parameterIdentifier = parameter.identifier;
+ if (parameterIdentifier != null) {
+ var body = _extractBody(expression);
+ if (body != null) {
+ return _Closure(parameter, parameterIdentifier, body);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ return null;
+ }
+}
+
+class _Closure {
+ final SimpleFormalParameter parameter;
+ final SimpleIdentifier parameterIdentifier;
+ final Expression body;
+
+ _Closure(this.parameter, this.parameterIdentifier, this.body);
}
/// A visitor that can be used to find references to a parameter.
@@ -195,7 +209,7 @@
final Set<String> otherNames = <String>{};
/// Initialize a newly created finder to find references to the [parameter].
- _ParameterReferenceFinder(this.parameter) : assert(parameter != null);
+ _ParameterReferenceFinder(this.parameter);
/// Return `true` if the parameter is unreferenced in the nodes that have been
/// visited.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_part_of_to_uri.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_part_of_to_uri.dart
index e532273..0f22239 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_part_of_to_uri.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_part_of_to_uri.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -19,14 +17,20 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var directive = node.thisOrAncestorOfType<PartOfDirective>();
- if (directive == null || directive.libraryName == null) {
+ if (directive == null) {
return;
}
+
+ var libraryName = directive.libraryName;
+ if (libraryName == null) {
+ return;
+ }
+
var libraryPath = resolvedResult.libraryElement.source.fullName;
- var partPath = resolvedResult.path;
+ var partPath = resolvedResult.path!;
var relativePath = relative(libraryPath, from: dirname(partPath));
var uri = Uri.file(relativePath).toString();
- var replacementRange = range.node(directive.libraryName);
+ var replacementRange = range.node(libraryName);
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(replacementRange, "'$uri'");
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_quotes.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_quotes.dart
index c853070..5e45165 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_quotes.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_quotes.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -22,56 +20,72 @@
@override
Future<void> compute(ChangeBuilder builder) async {
+ var node = this.node;
if (node is SimpleStringLiteral) {
- SimpleStringLiteral literal = node;
- if (_fromDouble ? !literal.isSingleQuoted : literal.isSingleQuoted) {
- var newQuote = literal.isMultiline
- ? (_fromDouble ? "'''" : '"""')
- : (_fromDouble ? "'" : '"');
- var quoteLength = literal.isMultiline ? 3 : 1;
- var token = literal.literal;
- if (!token.isSynthetic && !token.lexeme.contains(newQuote)) {
- await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleReplacement(
- SourceRange(
- literal.offset + (literal.isRaw ? 1 : 0), quoteLength),
- newQuote);
- builder.addSimpleReplacement(
- SourceRange(literal.end - quoteLength, quoteLength), newQuote);
- });
- }
- }
- } else if (node is InterpolationString || node is StringInterpolation) {
- StringInterpolation stringNode =
- node is StringInterpolation ? node : node.parent;
- if (_fromDouble
- ? !stringNode.isSingleQuoted
- : stringNode.isSingleQuoted) {
- var newQuote = stringNode.isMultiline
- ? (_fromDouble ? "'''" : '"""')
- : (_fromDouble ? "'" : '"');
- var quoteLength = stringNode.isMultiline ? 3 : 1;
- var elements = stringNode.elements;
- for (var i = 0; i < elements.length; i++) {
- var element = elements[i];
- if (element is InterpolationString) {
- var token = element.contents;
- if (token.isSynthetic || token.lexeme.contains(newQuote)) {
- return null;
- }
- }
- }
+ await _simpleStringLiteral(builder, node);
+ } else if (node is StringInterpolation) {
+ await _stringInterpolation(builder, node);
+ } else if (node is InterpolationString) {
+ await _stringInterpolation(builder, node.parent as StringInterpolation);
+ }
+ }
+
+ Future<void> _simpleStringLiteral(
+ ChangeBuilder builder,
+ SimpleStringLiteral node,
+ ) async {
+ if (_fromDouble ? !node.isSingleQuoted : node.isSingleQuoted) {
+ var newQuote = node.isMultiline
+ ? (_fromDouble ? "'''" : '"""')
+ : (_fromDouble ? "'" : '"');
+ var quoteLength = node.isMultiline ? 3 : 1;
+ var token = node.literal;
+ if (!token.isSynthetic && !token.lexeme.contains(newQuote)) {
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(
- SourceRange(
- stringNode.offset + (stringNode.isRaw ? 1 : 0), quoteLength),
- newQuote);
+ SourceRange(node.offset + (node.isRaw ? 1 : 0), quoteLength),
+ newQuote,
+ );
builder.addSimpleReplacement(
- SourceRange(stringNode.end - quoteLength, quoteLength), newQuote);
+ SourceRange(node.end - quoteLength, quoteLength),
+ newQuote,
+ );
});
}
}
}
+
+ Future<void> _stringInterpolation(
+ ChangeBuilder builder,
+ StringInterpolation node,
+ ) async {
+ if (_fromDouble ? !node.isSingleQuoted : node.isSingleQuoted) {
+ var newQuote = node.isMultiline
+ ? (_fromDouble ? "'''" : '"""')
+ : (_fromDouble ? "'" : '"');
+ var quoteLength = node.isMultiline ? 3 : 1;
+ var elements = node.elements;
+ for (var i = 0; i < elements.length; i++) {
+ var element = elements[i];
+ if (element is InterpolationString) {
+ var token = element.contents;
+ if (token.isSynthetic || token.lexeme.contains(newQuote)) {
+ return;
+ }
+ }
+ }
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addSimpleReplacement(
+ SourceRange(node.offset + (node.isRaw ? 1 : 0), quoteLength),
+ newQuote,
+ );
+ builder.addSimpleReplacement(
+ SourceRange(node.end - quoteLength, quoteLength),
+ newQuote,
+ );
+ });
+ }
+ }
}
class ConvertToDoubleQuotes extends ConvertQuotes {
diff --git a/pkg/analysis_server/lib/src/services/correction/executable_parameters.dart b/pkg/analysis_server/lib/src/services/correction/executable_parameters.dart
index 30c2497..7200eec 100644
--- a/pkg/analysis_server/lib/src/services/correction/executable_parameters.dart
+++ b/pkg/analysis_server/lib/src/services/correction/executable_parameters.dart
@@ -67,7 +67,7 @@
}
static ExecutableParameters? forInvocation(
- AnalysisSessionHelper sessionHelper, AstNode invocation) {
+ AnalysisSessionHelper sessionHelper, AstNode? invocation) {
Element? element;
// This doesn't handle FunctionExpressionInvocation.
if (invocation is Annotation) {
diff --git a/pkg/analysis_server/lib/src/utilities/extensions/ast.dart b/pkg/analysis_server/lib/src/utilities/extensions/ast.dart
index c6d2078..3448861 100644
--- a/pkg/analysis_server/lib/src/utilities/extensions/ast.dart
+++ b/pkg/analysis_server/lib/src/utilities/extensions/ast.dart
@@ -4,6 +4,7 @@
import 'package:analysis_server/src/utilities/extensions/element.dart';
import 'package:analyzer/dart/ast/ast.dart';
+import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/element.dart';
extension AstNodeExtensions on AstNode {
@@ -102,3 +103,19 @@
this is EmptyFunctionBody ||
(this is BlockFunctionBody && beginToken.isSynthetic);
}
+
+extension MethodDeclarationExtension on MethodDeclaration {
+ Token? get propertyKeywordGet {
+ var propertyKeyword = this.propertyKeyword;
+ return propertyKeyword != null && propertyKeyword.keyword == Keyword.GET
+ ? propertyKeyword
+ : null;
+ }
+}
+
+extension VariableDeclarationListExtension on VariableDeclarationList {
+ Token? get finalKeyword {
+ var keyword = this.keyword;
+ return keyword != null && keyword.keyword == Keyword.FINAL ? keyword : null;
+ }
+}
diff --git a/pkg/analysis_server/test/src/services/correction/fix/add_null_check_test.dart b/pkg/analysis_server/test/src/services/correction/fix/add_null_check_test.dart
index eaeb488..44c8028 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/add_null_check_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/add_null_check_test.dart
@@ -249,9 +249,7 @@
await resolveTestCode('''
f(int? i) => i++;
''');
- await assertHasFix('''
-f(int? i) => i!++;
-''');
+ await assertNoFix();
}
Future<void> test_prefixedIdentifier() async {
To view, visit change 194540. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Konstantin Shcheglov.
Patch set 1:Code-Review +1
1 comment:
File pkg/analysis_server/lib/src/services/correction/dart/add_missing_enum_case_clauses.dart:
Patch Set #1, Line 59: final enumName_final = enumName;
I don't understand the value of defining this local variable.
To view, visit change 194540. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brian Wilkerson.
1 comment:
File pkg/analysis_server/lib/src/services/correction/dart/add_missing_enum_case_clauses.dart:
Patch Set #1, Line 59: final enumName_final = enumName;
I don't understand the value of defining this local variable.
At the line `24` we declare `enumName` as nullable.
Then we promote it to non-nullable at the line `38`.
However promotions are not visible inside closures, so at the line `66`.
We need a different variable with a non-nullable type.
Unfortunately this is a limitation of the flow analysis promotion.
Would be great to improve it.
To view, visit change 194540. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brian Wilkerson.
Patch set 1:Commit-Queue +2
commi...@chromium.org submitted this change.
Migrate several lib/src/services/correction/dart/
Change-Id: I8c50c6e7664a7bd8ee7b0d881d9adeaf9c544f61
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194540
Reviewed-by: Brian Wilkerson <brianwi...@google.com>
Commit-Queue: Konstantin Shcheglov <sche...@google.com>
To view, visit change 194540. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brian Wilkerson.
To view, visit change 194546. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brian Wilkerson.
Konstantin Shcheglov would like Brian Wilkerson to review this change.
Migrate several lib/src/services/correction/dart/
Change-Id: I0db19c94d26c7c604d71663e2f2c944286898c0b
---
M pkg/analysis_server/lib/src/services/correction/assist_internal.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_contains.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_expression_function_body.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_field_parameter.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_generic_function_syntax.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_if_null.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_int_literal.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_list_literal.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_map_literal.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_multiline_string.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_named_arguments.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_normal_parameter.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware_spread.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_package_import.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_relative_import.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_set_literal.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_where_type.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_class.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_constructor.dart
20 files changed, 373 insertions(+), 270 deletions(-)
diff --git a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
index b5a1027..ef6a1ef 100644
--- a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
@@ -107,7 +107,7 @@
ConvertPartOfToUri.newInstance,
ConvertToDoubleQuotes.newInstance,
ConvertToFieldParameter.newInstance,
- ConvertToMutilineString.newInstance,
+ ConvertToMultilineString.newInstance,
ConvertToNormalParameter.newInstance,
ConvertToSingleQuotes.newInstance,
ConvertToExpressionFunctionBody.newInstance,
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_contains.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_contains.dart
index 7665de9..2501a07 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_contains.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_contains.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';
@@ -29,6 +27,9 @@
var rightOperand = comparison.rightOperand;
if (leftOperand is MethodInvocation && _isInteger(rightOperand)) {
var value = _integerValue(rightOperand);
+ if (value == null) {
+ return;
+ }
var methodName = leftOperand.methodName;
var deletionRange = range.endEnd(leftOperand, rightOperand);
var notOffset = -1;
@@ -48,6 +49,9 @@
});
} else if (_isInteger(leftOperand) && rightOperand is MethodInvocation) {
var value = _integerValue(leftOperand);
+ if (value == null) {
+ return;
+ }
var methodName = rightOperand.methodName;
var deletionRange = range.startStart(leftOperand, rightOperand);
var notOffset = -1;
@@ -71,17 +75,19 @@
/// Return the value of the given [expression], given that [_isInteger]
/// returned `true`.
- int _integerValue(Expression expression) {
+ int? _integerValue(Expression expression) {
if (expression is IntegerLiteral) {
return expression.value;
} else if (expression is PrefixExpression &&
expression.operator.type == TokenType.MINUS) {
var operand = expression.operand;
if (operand is IntegerLiteral) {
- return -operand.value;
+ var value = operand.value;
+ if (value != null) {
+ return -value;
+ }
}
}
- throw StateError('invalid integer value');
}
TokenType _invertedTokenType(TokenType type) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_expression_function_body.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_expression_function_body.dart
index fd38a12..ed84716e 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_expression_function_body.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_expression_function_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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -31,13 +29,13 @@
return;
}
// prepare return statement
- List<Statement> statements = (body as BlockFunctionBody).block.statements;
+ List<Statement> statements = body.block.statements;
if (statements.length != 1) {
return;
}
var onlyStatement = statements.first;
// prepare returned expression
- Expression returnExpression;
+ Expression? returnExpression;
if (onlyStatement is ReturnStatement) {
returnExpression = onlyStatement.expression;
} else if (onlyStatement is ExpressionStatement) {
@@ -53,15 +51,17 @@
return;
}
+ final returnExpression_final = returnExpression;
await builder.addDartFileEdit(file, (builder) {
builder.addReplacement(range.node(body), (builder) {
if (body.isAsynchronous) {
builder.write('async ');
}
builder.write('=> ');
- builder.write(utils.getNodeText(returnExpression));
- if (body.parent is! FunctionExpression ||
- body.parent.parent is FunctionDeclaration) {
+ builder.write(utils.getNodeText(returnExpression_final));
+ var parent = body.parent;
+ if (parent is! FunctionExpression ||
+ parent.parent is FunctionDeclaration) {
builder.write(';');
}
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_field_parameter.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_field_parameter.dart
index ef999da..6a2b348 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_field_parameter.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_field_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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -19,87 +17,137 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node == null) {
- return;
- }
- // prepare ConstructorDeclaration
- var constructor = node.thisOrAncestorOfType<ConstructorDeclaration>();
- if (constructor == null) {
- return;
- }
- var parameterList = constructor.parameters;
- List<ConstructorInitializer> initializers = constructor.initializers;
- // prepare parameter
- SimpleFormalParameter parameter;
- if (node.parent is SimpleFormalParameter &&
- node.parent.parent is FormalParameterList &&
- node.parent.parent.parent is ConstructorDeclaration) {
- parameter = node.parent;
- }
- if (node is SimpleIdentifier &&
- node.parent is ConstructorFieldInitializer) {
- var name = (node as SimpleIdentifier).name;
- ConstructorFieldInitializer initializer = node.parent;
- if (initializer.expression == node) {
- for (var formalParameter in parameterList.parameters) {
- if (formalParameter is SimpleFormalParameter &&
- formalParameter.identifier.name == name) {
- parameter = formalParameter;
- }
- }
- }
- }
- // analyze parameter
- if (parameter != null) {
- var parameterName = parameter.identifier.name;
- var parameterElement = parameter.declaredElement;
- // check number of references
- var visitor = _ReferenceCounter(parameterElement);
- for (var initializer in initializers) {
- initializer.accept(visitor);
- }
- if (visitor.count != 1) {
- return;
- }
- // find the field initializer
- ConstructorFieldInitializer parameterInitializer;
- for (var initializer in initializers) {
- if (initializer is ConstructorFieldInitializer) {
- var expression = initializer.expression;
- if (expression is SimpleIdentifier &&
- expression.name == parameterName) {
- parameterInitializer = initializer;
- }
- }
- }
- if (parameterInitializer == null) {
- return;
- }
- var fieldName = parameterInitializer.fieldName.name;
+ // // prepare ConstructorDeclaration
+ // var node = this.node;
+ // var constructor = node.thisOrAncestorOfType<ConstructorDeclaration>();
+ // if (constructor == null) {
+ // return;
+ // }
+ // var parameterList = constructor.parameters;
+ // List<ConstructorInitializer> initializers = constructor.initializers;
- await builder.addDartFileEdit(file, (builder) {
- // replace parameter
- builder.addSimpleReplacement(range.node(parameter), 'this.$fieldName');
- // remove initializer
- var initializerIndex = initializers.indexOf(parameterInitializer);
- if (initializers.length == 1) {
- builder
- .addDeletion(range.endEnd(parameterList, parameterInitializer));
- } else {
- if (initializerIndex == 0) {
- var next = initializers[initializerIndex + 1];
- builder.addDeletion(range.startStart(parameterInitializer, next));
- } else {
- var prev = initializers[initializerIndex - 1];
- builder.addDeletion(range.endEnd(prev, parameterInitializer));
- }
- }
- });
+ // prepare parameter
+ var context = _findParameter(node);
+ if (context == null) {
+ return;
}
+
+ // analyze parameter
+ var parameterName = context.identifier.name;
+ var parameterElement = context.parameter.declaredElement!;
+ var initializers = context.constructor.initializers;
+
+ // check number of references
+ var visitor = _ReferenceCounter(parameterElement);
+ for (var initializer in initializers) {
+ initializer.accept(visitor);
+ }
+ if (visitor.count != 1) {
+ return;
+ }
+ // find the field initializer
+ ConstructorFieldInitializer? parameterInitializer;
+ for (var initializer in initializers) {
+ if (initializer is ConstructorFieldInitializer) {
+ var expression = initializer.expression;
+ if (expression is SimpleIdentifier &&
+ expression.name == parameterName) {
+ parameterInitializer = initializer;
+ }
+ }
+ }
+ if (parameterInitializer == null) {
+ return;
+ }
+ var fieldName = parameterInitializer.fieldName.name;
+
+ final context_final = context;
+ final parameterInitializer_final = parameterInitializer;
+ await builder.addDartFileEdit(file, (builder) {
+ // replace parameter
+ builder.addSimpleReplacement(
+ range.node(context_final.parameter),
+ 'this.$fieldName',
+ );
+ // remove initializer
+ var initializerIndex = initializers.indexOf(parameterInitializer_final);
+ if (initializers.length == 1) {
+ builder.addDeletion(
+ range.endEnd(
+ context_final.constructor.parameters,
+ parameterInitializer_final,
+ ),
+ );
+ } else {
+ if (initializerIndex == 0) {
+ var next = initializers[initializerIndex + 1];
+ builder.addDeletion(
+ range.startStart(parameterInitializer_final, next),
+ );
+ } else {
+ var prev = initializers[initializerIndex - 1];
+ builder.addDeletion(
+ range.endEnd(prev, parameterInitializer_final),
+ );
+ }
+ }
+ });
}
/// Return an instance of this class. Used as a tear-off in `AssistProcessor`.
static ConvertToFieldParameter newInstance() => ConvertToFieldParameter();
+
+ static _Context? _findParameter(AstNode node) {
+ var parent = node.parent;
+ if (parent is SimpleFormalParameter) {
+ var identifier = parent.identifier;
+ if (identifier == null) return null;
+
+ var formalParameterList = parent.parent;
+ if (formalParameterList is! FormalParameterList) return null;
+
+ var constructor = formalParameterList.parent;
+ if (constructor is! ConstructorDeclaration) return null;
+
+ return _Context(
+ parameter: parent,
+ identifier: identifier,
+ constructor: constructor,
+ );
+ }
+
+ if (node is SimpleIdentifier && parent is ConstructorFieldInitializer) {
+ var constructor = parent.parent;
+ if (constructor is! ConstructorDeclaration) return null;
+
+ if (parent.expression == node) {
+ for (var formalParameter in constructor.parameters.parameters) {
+ if (formalParameter is SimpleFormalParameter) {
+ var identifier = formalParameter.identifier;
+ if (identifier != null && identifier.name == node.name) {
+ return _Context(
+ parameter: formalParameter,
+ identifier: identifier,
+ constructor: constructor,
+ );
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+class _Context {
+ final SimpleFormalParameter parameter;
+ final SimpleIdentifier identifier;
+ final ConstructorDeclaration constructor;
+
+ _Context({
+ required this.parameter,
+ required this.identifier,
+ required this.constructor,
+ });
}
class _ReferenceCounter extends RecursiveAstVisitor<void> {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_generic_function_syntax.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_generic_function_syntax.dart
index 7b197a4..76b0ad1 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_generic_function_syntax.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_generic_function_syntax.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';
@@ -27,8 +26,7 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var node = this.node;
- while (node != null) {
+ for (var node in this.node.withParents) {
if (node is FunctionTypeAlias) {
return _convertFunctionTypeAlias(builder, node);
} else if (node is FunctionTypedFormalParameter) {
@@ -38,7 +36,6 @@
// when the selection is inside a parameter list.
return null;
}
- node = node.parent;
}
}
@@ -47,7 +44,7 @@
bool _allParametersHaveTypes(FormalParameterList parameters) {
for (var parameter in parameters.parameters) {
if (parameter is DefaultFormalParameter) {
- parameter = (parameter as DefaultFormalParameter).parameter;
+ parameter = parameter.parameter;
}
if (parameter is SimpleFormalParameter) {
if (parameter.type == null) {
@@ -65,10 +62,13 @@
if (!_allParametersHaveTypes(node.parameters)) {
return;
}
- String returnType;
- if (node.returnType != null) {
- returnType = utils.getNodeText(node.returnType);
+
+ String? returnType;
+ var returnTypeNode = node.returnType;
+ if (returnTypeNode != null) {
+ returnType = utils.getNodeText(returnTypeNode);
}
+
var functionName = utils.getRangeText(
range.startEnd(node.name, node.typeParameters ?? node.name));
var parameters = utils.getNodeText(node.parameters);
@@ -81,7 +81,7 @@
// add change
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(
- range.startStart(node.typedefKeyword.next, node.semicolon),
+ range.startStart(node.typedefKeyword.next!, node.semicolon),
replacement);
});
}
@@ -93,8 +93,9 @@
}
var required = node.requiredKeyword != null ? 'required ' : '';
var covariant = node.covariantKeyword != null ? 'covariant ' : '';
+ var returnTypeNode = node.returnType;
var returnType =
- node.returnType != null ? '${utils.getNodeText(node.returnType)} ' : '';
+ returnTypeNode != null ? '${utils.getNodeText(returnTypeNode)} ' : '';
var functionName = utils.getRangeText(range.startEnd(
node.identifier, node.typeParameters ?? node.identifier));
var parameters = utils.getNodeText(node.parameters);
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_if_null.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_if_null.dart
index 19e52d8..d8ca1d2 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_if_null.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_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';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_int_literal.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_int_literal.dart
index ed675e7..1a9f163 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_int_literal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_int_literal.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -25,16 +23,18 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is! DoubleLiteral) {
+ var literal = node;
+ if (literal is! DoubleLiteral) {
return;
}
- DoubleLiteral literal = node;
- int intValue;
+
+ int? intValue;
try {
- intValue = literal.value?.truncate();
+ intValue = literal.value.truncate();
} catch (e) {
// Double cannot be converted to int
}
+
if (intValue == null || intValue != literal.value) {
return;
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_list_literal.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_list_literal.dart
index ba140fa..45ae0af 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_list_literal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_list_literal.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/assist.dart';
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/type.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -29,9 +28,14 @@
// Ensure that this is the default constructor defined on `List`.
//
var creation = node.thisOrAncestorOfType<InstanceCreationExpression>();
- if (creation == null ||
- node.offset > creation.argumentList.offset ||
- creation.staticType.element != typeProvider.listElement ||
+ if (creation == null) {
+ return;
+ }
+
+ var type = creation.staticType;
+ if (node.offset > creation.argumentList.offset ||
+ type is! InterfaceType ||
+ type.element != typeProvider.listElement ||
creation.constructorName.name != null ||
creation.argumentList.arguments.isNotEmpty) {
return;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_map_literal.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_map_literal.dart
index 76221ea..5919c43 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_map_literal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_map_literal.dart
@@ -2,13 +2,12 @@
// 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:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
+import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -31,11 +30,16 @@
// `LinkedHashMap`.
//
var creation = node.thisOrAncestorOfType<InstanceCreationExpression>();
- if (creation == null ||
- node.offset > creation.argumentList.offset ||
+ if (creation == null) {
+ return;
+ }
+
+ var type = creation.staticType;
+ if (node.offset > creation.argumentList.offset ||
creation.constructorName.name != null ||
creation.argumentList.arguments.isNotEmpty ||
- !_isMapClass(creation.staticType.element)) {
+ type is! InterfaceType ||
+ !_isMapClass(type.element)) {
return;
}
//
@@ -57,11 +61,10 @@
/// Return `true` if the [element] represents either the class `Map` or
/// `LinkedHashMap`.
- bool _isMapClass(Element element) =>
- element is ClassElement &&
- (element == typeProvider.mapElement ||
- (element.name == 'LinkedHashMap' &&
- element.library.name == 'dart.collection'));
+ bool _isMapClass(ClassElement element) =>
+ element == typeProvider.mapElement ||
+ (element.name == 'LinkedHashMap' &&
+ element.library.name == 'dart.collection');
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static ConvertToMapLiteral newInstance() => ConvertToMapLiteral();
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_multiline_string.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_multiline_string.dart
index 0dd99aa..51861d9 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_multiline_string.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_multiline_string.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -11,7 +9,7 @@
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
-class ConvertToMutilineString extends CorrectionProducer {
+class ConvertToMultilineString extends CorrectionProducer {
@override
AssistKind get assistKind => DartAssistKind.CONVERT_TO_MULTILINE_STRING;
@@ -19,7 +17,7 @@
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
if (node is InterpolationElement) {
- node = (node as InterpolationElement).parent;
+ node = node.parent!;
}
if (node is SingleStringLiteral) {
var literal = node;
@@ -42,5 +40,5 @@
}
/// Return an instance of this class. Used as a tear-off in `AssistProcessor`.
- static ConvertToMutilineString newInstance() => ConvertToMutilineString();
+ static ConvertToMultilineString newInstance() => ConvertToMultilineString();
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_named_arguments.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_named_arguments.dart
index a4af1c4..228d9f9 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_named_arguments.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_named_arguments.dart
@@ -2,13 +2,12 @@
// 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/dart/element/type.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';
@@ -22,7 +21,7 @@
var argumentList = node.parent;
if (argumentList is ArgumentList) {
// Prepare parameters.
- List<ParameterElement> parameters;
+ List<ParameterElement>? parameters;
var parent = argumentList.parent;
if (parent is FunctionExpressionInvocation) {
var invokeType = parent.staticInvokeType;
@@ -61,10 +60,10 @@
argumentList.arguments.skip(numberOfPositionalParameters);
for (var argument in extraArguments) {
if (argument is! NamedExpression) {
- ParameterElement uniqueNamedParameter;
+ ParameterElement? uniqueNamedParameter;
for (var namedParameter in namedParameters) {
if (typeSystem.isSubtypeOf(
- argument.staticType, namedParameter.type)) {
+ argument.typeOrThrow, namedParameter.type)) {
if (uniqueNamedParameter == null) {
uniqueNamedParameter = namedParameter;
} else {
@@ -84,8 +83,9 @@
}
await builder.addDartFileEdit(file, (builder) {
- for (var argument in argumentToParameter.keys) {
- var parameter = argumentToParameter[argument];
+ for (var entry in argumentToParameter.entries) {
+ var argument = entry.key;
+ var parameter = entry.value;
builder.addSimpleInsertion(argument.offset, '${parameter.name}: ');
}
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_normal_parameter.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_normal_parameter.dart
index b220afd..d7a7c65 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_normal_parameter.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_normal_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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -17,38 +15,41 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is SimpleIdentifier &&
- node.parent is FieldFormalParameter &&
- node.parent.parent is FormalParameterList &&
- node.parent.parent.parent is ConstructorDeclaration) {
- ConstructorDeclaration constructor = node.parent.parent.parent;
- FormalParameterList parameterList = node.parent.parent;
- FieldFormalParameter parameter = node.parent;
- var parameterElement = parameter.declaredElement;
- var name = (node as SimpleIdentifier).name;
- // prepare type
- var type = parameterElement.type;
+ var identifier = node;
+ if (identifier is! SimpleIdentifier) return;
- await builder.addDartFileEdit(file, (builder) {
- // replace parameter
- if (type.isDynamic) {
- builder.addSimpleReplacement(range.node(parameter), name);
- } else {
- builder.addReplacement(range.node(parameter), (builder) {
- builder.writeType(type);
- builder.write(' ');
- builder.write(name);
- });
- }
- // add field initializer
- List<ConstructorInitializer> initializers = constructor.initializers;
- if (initializers.isEmpty) {
- builder.addSimpleInsertion(parameterList.end, ' : $name = $name');
- } else {
- builder.addSimpleInsertion(initializers.last.end, ', $name = $name');
- }
- });
- }
+ var parameter = identifier.parent;
+ if (parameter is! FieldFormalParameter) return;
+
+ var parameterList = parameter.parent;
+ if (parameterList is! FormalParameterList) return;
+
+ var constructor = parameterList.parent;
+ if (constructor is! ConstructorDeclaration) return;
+
+ var parameterElement = parameter.declaredElement!;
+ var name = identifier.name;
+ var type = parameterElement.type;
+
+ await builder.addDartFileEdit(file, (builder) {
+ // replace parameter
+ if (type.isDynamic) {
+ builder.addSimpleReplacement(range.node(parameter), name);
+ } else {
+ builder.addReplacement(range.node(parameter), (builder) {
+ builder.writeType(type);
+ builder.write(' ');
+ builder.write(name);
+ });
+ }
+ // add field initializer
+ List<ConstructorInitializer> initializers = constructor.initializers;
+ if (initializers.isEmpty) {
+ builder.addSimpleInsertion(parameterList.end, ' : $name = $name');
+ } else {
+ builder.addSimpleInsertion(initializers.last.end, ', $name = $name');
+ }
+ });
}
/// Return an instance of this class. Used as a tear-off in `AssistProcessor`.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware.dart
index 5307774..3b2f419 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -28,18 +26,20 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
- if (node.parent is BinaryExpression &&
- node.parent.parent is ConditionalExpression) {
- node = node.parent.parent;
+ var parent = node.parent;
+ if (parent is BinaryExpression) {
+ var grandParent = parent.parent;
+ if (grandParent is ConditionalExpression) {
+ node = grandParent;
+ }
}
if (node is! ConditionalExpression) {
return;
}
- ConditionalExpression conditional = node;
- var condition = conditional.condition.unParenthesized;
+ var condition = node.condition.unParenthesized;
SimpleIdentifier identifier;
- Expression nullExpression;
- Expression nonNullExpression;
+ Expression? nullExpression;
+ Expression? nonNullExpression;
int periodOffset;
if (condition is BinaryExpression) {
@@ -67,11 +67,11 @@
// is the save variable being compared to `null`.
//
if (condition.operator.type == TokenType.EQ_EQ) {
- nullExpression = conditional.thenExpression;
- nonNullExpression = conditional.elseExpression;
+ nullExpression = node.thenExpression;
+ nonNullExpression = node.elseExpression;
} else if (condition.operator.type == TokenType.BANG_EQ) {
- nonNullExpression = conditional.thenExpression;
- nullExpression = conditional.elseExpression;
+ nonNullExpression = node.thenExpression;
+ nullExpression = node.elseExpression;
}
if (nullExpression == null || nonNullExpression == null) {
return;
@@ -80,8 +80,8 @@
return;
}
var unwrappedExpression = nonNullExpression.unParenthesized;
- Expression target;
- Token operator;
+ Expression? target;
+ Token? operator;
if (unwrappedExpression is MethodInvocation) {
target = unwrappedExpression.target;
operator = unwrappedExpression.operator;
@@ -100,10 +100,11 @@
}
periodOffset = operator.offset;
+ final nonNullExpression_final = nonNullExpression;
await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(range.startStart(node, nonNullExpression));
+ builder.addDeletion(range.startStart(node, nonNullExpression_final));
builder.addSimpleInsertion(periodOffset, '?');
- builder.addDeletion(range.endEnd(nonNullExpression, node));
+ builder.addDeletion(range.endEnd(nonNullExpression_final, node));
});
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware_spread.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware_spread.dart
index 8f54f38..920b2f2 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware_spread.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware_spread.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';
@@ -16,7 +14,7 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var parent = coveredNode.parent;
+ var parent = coveredNode?.parent;
if (parent is SpreadElement && !parent.isNullAware) {
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleInsertion(parent.spreadOperator.end, '?');
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_package_import.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_package_import.dart
index 074e90c..a40b1b2 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_package_import.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_package_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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -27,7 +25,7 @@
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
if (node is StringLiteral) {
- node = node.parent;
+ node = node.parent!;
}
if (node is ImportDirective) {
var importDirective = node;
@@ -45,7 +43,8 @@
// Don't offer to convert a 'package:' URI to itself.
try {
- if (Uri.parse(importDirective.uriContent).scheme == 'package') {
+ var uriContent = importDirective.uriContent;
+ if (uriContent == null || Uri.parse(uriContent).scheme == 'package') {
return;
}
} on FormatException {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_relative_import.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_relative_import.dart
index a312a32..48a99d7 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_relative_import.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_relative_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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -28,16 +26,14 @@
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
if (node is StringLiteral) {
- node = node.parent;
+ node = node.parent!;
}
if (node is! ImportDirective) {
return;
}
- ImportDirective importDirective = node;
-
// Ignore if invalid URI.
- if (importDirective.uriSource == null) {
+ if (node.uriSource == null) {
return;
}
@@ -49,7 +45,11 @@
Uri importUri;
try {
- importUri = Uri.parse(importDirective.uriContent);
+ var uriContent = node.uriContent;
+ if (uriContent == null) {
+ return;
+ }
+ importUri = Uri.parse(uriContent);
} on FormatException {
return;
}
@@ -75,9 +75,10 @@
from: path.dirname(sourceUri.path),
);
+ final node_final = node;
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(
- range.node(importDirective.uri).getExpanded(-1),
+ range.node(node_final.uri).getExpanded(-1),
relativePath,
);
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_set_literal.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_set_literal.dart
index fb2b42c..8796a16 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_set_literal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_set_literal.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/assist.dart';
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/type.dart';
import 'package:analyzer/source/source_range.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
@@ -62,8 +61,8 @@
var name = creation.constructorName.name;
var constructorTypeArguments =
creation.constructorName.type.typeArguments;
- TypeArgumentList elementTypeArguments;
- SourceRange elementsRange;
+ TypeArgumentList? elementTypeArguments;
+ SourceRange? elementsRange;
if (name == null) {
// Handle an invocation of the default constructor `Set()`.
} else if (name.name == 'from' || name.name == 'of') {
@@ -111,7 +110,7 @@
/// Return the invocation of `List.toSet` that is to be converted, or `null`
/// if the cursor is not inside a invocation of `List.toSet`.
- MethodInvocation _findInvocationOfToSet() {
+ MethodInvocation? _findInvocationOfToSet() {
var invocation = node.thisOrAncestorOfType<MethodInvocation>();
if (invocation == null ||
node.offset > invocation.argumentList.offset ||
@@ -124,12 +123,23 @@
/// Return the invocation of a `Set` constructor that is to be converted, or
/// `null` if the cursor is not inside the invocation of a constructor.
- InstanceCreationExpression _findSetCreation() {
+ InstanceCreationExpression? _findSetCreation() {
var creation = node.thisOrAncestorOfType<InstanceCreationExpression>();
+ if (creation == null) {
+ return null;
+ }
+
+ if (node.offset > creation.argumentList.offset) {
+ return null;
+ }
+
+ var type = creation.staticType;
+ if (type is! InterfaceType) {
+ return null;
+ }
+
// TODO(brianwilkerson) Consider also accepting uses of LinkedHashSet.
- if (creation == null ||
- node.offset > creation.argumentList.offset ||
- creation.staticType.element != typeProvider.setElement) {
+ if (type.element != typeProvider.setElement) {
return null;
}
return creation;
@@ -139,7 +149,7 @@
/// element that would cause a set to be inferred.
bool _hasUnambiguousElement(InstanceCreationExpression creation) {
var arguments = creation.argumentList.arguments;
- if (arguments == null || arguments.isEmpty) {
+ if (arguments.isEmpty) {
return false;
}
return _listHasUnambiguousElement(arguments[0]);
@@ -147,7 +157,7 @@
/// Return `true` if the [element] is sufficient to lexically make the
/// enclosing literal a set literal rather than a map.
- bool _isUnambiguousElement(CollectionElement element) {
+ bool _isUnambiguousElement(CollectionElement? element) {
if (element is ForElement) {
return _isUnambiguousElement(element.body);
} else if (element is IfElement) {
@@ -176,7 +186,7 @@
/// Return `true` if a set would be inferred if the literal replacing the
/// instance [creation] did not have explicit type arguments.
bool _setWouldBeInferred(InstanceCreationExpression creation) {
- var parent = creation.parent;
+ var parent = creation.parent!;
if (parent is VariableDeclaration) {
var parent2 = parent.parent;
if (parent2 is VariableDeclarationList &&
@@ -185,7 +195,7 @@
}
} else if (parent.parent is InvocationExpression) {
var parameterElement = creation.staticParameterElement;
- if (parameterElement?.type?.element == typeProvider.setElement) {
+ if (parameterElement?.type.element == typeProvider.setElement) {
return true;
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_where_type.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_where_type.dart
index eb508d4..de20ada 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_where_type.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_where_type.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,33 +18,46 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var node = this.node;
- if (node is! SimpleIdentifier || node.parent is! MethodInvocation) {
+ var methodName = node;
+ if (methodName is! SimpleIdentifier) {
return;
}
- var methodName = node as SimpleIdentifier;
- var invocation = node.parent as MethodInvocation;
+
+ var invocation = methodName.parent;
+ if (invocation is! MethodInvocation) {
+ return;
+ }
+
var arguments = invocation.argumentList.arguments;
- if (arguments.length != 1 || arguments[0] is! FunctionExpression) {
+ if (arguments.length != 1) {
return;
}
- var body = (arguments[0] as FunctionExpression).body;
- Expression returnValue;
+
+ var argument = arguments[0];
+ if (argument is! FunctionExpression) {
+ return;
+ }
+
+ Expression? returnValue;
+ var body = argument.body;
if (body is ExpressionFunctionBody) {
returnValue = body.expression;
} else if (body is BlockFunctionBody) {
var statements = body.block.statements;
- if (statements.length != 1 || statements[0] is! ReturnStatement) {
+ if (statements.length != 1) {
return;
}
- returnValue = (statements[0] as ReturnStatement).expression;
- } else {
- return;
+ var returnStatement = statements[0];
+ if (returnStatement is! ReturnStatement) {
+ return;
+ }
+ returnValue = returnStatement.expression;
}
+
if (returnValue is! IsExpression) {
return;
}
- var isExpression = returnValue as IsExpression;
+ var isExpression = returnValue;
if (isExpression.notOperator != null) {
return;
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_class.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_class.dart
index 05d4f47..833a8db 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_class.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_class.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';
@@ -14,7 +12,7 @@
import 'package:analyzer_plugin/utilities/range_factory.dart';
class CreateClass extends CorrectionProducer {
- String className;
+ String className = '';
@override
List<Object> get fixArguments => [className];
@@ -25,14 +23,14 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
- Element prefixElement;
+ Element? prefixElement;
SimpleIdentifier nameNode;
- ArgumentList arguments;
+ ArgumentList? arguments;
+
if (node is Annotation) {
- var annotation = node as Annotation;
- var name = annotation.name;
- arguments = annotation.arguments;
- if (name == null || name.staticElement != null || arguments == null) {
+ var name = node.name;
+ arguments = node.arguments;
+ if (name.staticElement != null || arguments == null) {
// TODO(brianwilkerson) Consider supporting creating a class when the
// arguments are missing by also adding an empty argument list.
return;
@@ -59,9 +57,9 @@
var prefix = '';
var suffix = '';
var offset = -1;
- String filePath;
+ String? filePath;
if (prefixElement == null) {
- targetUnit = unit.declaredElement;
+ targetUnit = unit.declaredElement!;
var enclosingMember = node.thisOrAncestorMatching((node) =>
node is CompilationUnitMember && node.parent is CompilationUnit);
if (enclosingMember == null) {
@@ -76,7 +74,7 @@
var library = import.importedLibrary;
if (library != null) {
targetUnit = library.definingCompilationUnit;
- var targetSource = targetUnit.source;
+ var targetSource = targetUnit.source!;
try {
offset = targetSource.contents.data.length;
filePath = targetSource.fullName;
@@ -91,7 +89,7 @@
}
}
}
- if (offset < 0) {
+ if (filePath == null || offset < 0) {
return;
}
await builder.addDartFileEdit(filePath, (builder) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor.dart
index 6526f89..e9be86a 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor.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';
@@ -16,7 +14,8 @@
class CreateConstructor extends CorrectionProducer {
/// The name of the constructor being created.
- ConstructorName _constructorName;
+ /// TODO(migration) We set this node when we have the change.
+ late ConstructorName _constructorName;
@override
List<Object> get fixArguments => [_constructorName];
@@ -27,39 +26,43 @@
@override
Future<void> compute(ChangeBuilder builder) async {
final argumentList = node.parent is ArgumentList ? node.parent : node;
- if (argumentList is ArgumentList &&
- argumentList.parent is InstanceCreationExpression) {
- await _proposeFromInstanceCreation(builder, argumentList.parent);
+ if (argumentList is ArgumentList) {
+ var instanceCreation = argumentList.parent;
+ if (instanceCreation is InstanceCreationExpression) {
+ await _proposeFromInstanceCreation(builder, instanceCreation);
+ }
} else {
await _proposeFromConstructorName(builder);
}
}
Future<void> _proposeFromConstructorName(ChangeBuilder builder) async {
- SimpleIdentifier name;
- InstanceCreationExpression instanceCreation;
- if (node is SimpleIdentifier) {
- // name
- name = node as SimpleIdentifier;
- if (name.parent is ConstructorName) {
- _constructorName = name.parent as ConstructorName;
- if (_constructorName.name == name) {
- // Type.name
- if (_constructorName.parent is InstanceCreationExpression) {
- instanceCreation =
- _constructorName.parent as InstanceCreationExpression;
- // new Type.name()
- if (instanceCreation.constructorName != _constructorName) {
- return;
- }
+ var name = node;
+ if (name is! SimpleIdentifier) {
+ return;
+ }
+
+ InstanceCreationExpression? instanceCreation;
+ if (name.parent is ConstructorName) {
+ _constructorName = name.parent as ConstructorName;
+ if (_constructorName.name == name) {
+ // Type.name
+ if (_constructorName.parent is InstanceCreationExpression) {
+ instanceCreation =
+ _constructorName.parent as InstanceCreationExpression;
+ // new Type.name()
+ if (instanceCreation.constructorName != _constructorName) {
+ return;
}
}
}
}
+
// do we have enough information?
if (instanceCreation == null) {
return;
}
+
// prepare target interface type
var targetType = _constructorName.type.type;
if (targetType is! InterfaceType) {
@@ -67,23 +70,35 @@
}
// prepare target ClassDeclaration
- ClassElement targetElement = targetType.element;
+ var targetElement = targetType.element;
var targetResult = await sessionHelper.getElementDeclaration(targetElement);
+ if (targetResult == null) {
+ return;
+ }
var targetNode = targetResult.node;
if (targetNode is! ClassDeclaration) {
return;
}
+ var targetUnit = targetResult.resolvedUnit;
+ if (targetUnit == null) {
+ return;
+ }
+
// prepare location
- var targetLocation = CorrectionUtils(targetResult.resolvedUnit)
- .prepareNewConstructorLocation(targetNode);
+ var targetLocation =
+ CorrectionUtils(targetUnit).prepareNewConstructorLocation(targetNode);
+ if (targetLocation == null) {
+ return;
+ }
var targetFile = targetElement.source.fullName;
+ final instanceCreation_final = instanceCreation;
await builder.addDartFileEdit(targetFile, (builder) {
builder.addInsertion(targetLocation.offset, (builder) {
builder.write(targetLocation.prefix);
builder.writeConstructorDeclaration(targetElement.name,
- argumentList: instanceCreation.argumentList,
+ argumentList: instanceCreation_final.argumentList,
constructorName: name,
constructorNameGroupName: 'NAME');
builder.write(targetLocation.suffix);
@@ -112,14 +127,25 @@
// prepare target ClassDeclaration
var targetElement = constructorElement.enclosingElement;
var targetResult = await sessionHelper.getElementDeclaration(targetElement);
- var targetNode = targetResult?.node;
+ if (targetResult == null) {
+ return;
+ }
+ var targetNode = targetResult.node;
if (targetNode is! ClassDeclaration) {
return;
}
+ var targetUnit = targetResult.resolvedUnit;
+ if (targetUnit == null) {
+ return;
+ }
+
// prepare location
- var targetLocation = CorrectionUtils(targetResult.resolvedUnit)
- .prepareNewConstructorLocation(targetNode);
+ var targetLocation =
+ CorrectionUtils(targetUnit).prepareNewConstructorLocation(targetNode);
+ if (targetLocation == null) {
+ return;
+ }
var targetSource = targetElement.source;
var targetFile = targetSource.fullName;
To view, visit change 194546. To unsubscribe, or for help writing mail filters, visit settings.
go/dart-cbuild result: SUCCESS
Details: https://goto.google.com/dart-cbuild/find/bbbe5bfc87b4346eba815ba7898ec0bfb5ddf66c
To view, visit change 194540. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Konstantin Shcheglov.
Patch set 2:Code-Review +1
6 comments:
File pkg/analysis_server/lib/src/services/correction/dart/convert_to_contains.dart:
Consider adding an explicit return of `null` here.
File pkg/analysis_server/lib/src/services/correction/dart/convert_to_expression_function_body.dart:
Patch Set #2, Line 38: Expression?
Consider using the "else return" form here in order to avoid marking the variable as nullable. I think it's a clearer signal to readers that we don't intend for the variable to ever be `null`. (It also avoid an extra null check at runtime.)
```
if (...) {
expr = ...;
} else if (...) {
expr = ...;
} else {
return;
}
```
File pkg/analysis_server/lib/src/services/correction/dart/convert_to_field_parameter.dart:
Did you intend to remove this code?
File pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware.dart:
Patch Set #2, Line 76: if (nullExpression == null || nonNullExpression == null)
Consider converting this to "else return" and making the variables non-nullable.
Patch Set #2, Line 103: final nonNullExpression_final = nonNullExpression;
Would using "else return" above allow us to get rid of this otherwise pointless local variable?
File pkg/analysis_server/lib/src/services/correction/fix_internal.dart:
Patch Set #2, Line 1768: getArgs: () =>
I don't really understand the reason for this change, but rather than create a closure here, consider passing the producer in instead of the two fields and inlining the getter invocations inside `_addFixFromBuilder`.
To view, visit change 194546. To unsubscribe, or for help writing mail filters, visit settings.
Patch set 3:Commit-Queue +2
6 comments:
File pkg/analysis_server/lib/src/services/correction/dart/convert_to_contains.dart:
Consider adding an explicit return of `null` here.
Done
File pkg/analysis_server/lib/src/services/correction/dart/convert_to_expression_function_body.dart:
Patch Set #2, Line 38: Expression?
Consider using the "else return" form here in order to avoid marking the variable as nullable. […]
I agree that `else {return;}` is nice to use when we can.
Unfortunately at the line `40` we can get `null` from `onlyStatement.expression`.
File pkg/analysis_server/lib/src/services/correction/dart/convert_to_field_parameter.dart:
Did you intend to remove this code?
Done
File pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware.dart:
Patch Set #2, Line 76: if (nullExpression == null || nonNullExpression == null)
Consider converting this to "else return" and making the variables non-nullable.
Done
Patch Set #2, Line 103: final nonNullExpression_final = nonNullExpression;
Would using "else return" above allow us to get rid of this otherwise pointless local variable?
Correct.
Fixed this as well.
Thank you!
File pkg/analysis_server/lib/src/services/correction/fix_internal.dart:
Patch Set #2, Line 1768: getArgs: () =>
I don't really understand the reason for this change, but rather than create a closure here, conside […]
Ah, this is much better.
Thank you!
To view, visit change 194546. To unsubscribe, or for help writing mail filters, visit settings.
CQ is trying the patch.
Note: The patchset #3 "Fixes for review comments." sent to CQ was uploaded after this CL was CR+1-ed.
Reviewer, please verify there is nothing unexpected https://dart-review.googlesource.com/c/194546/3
Bot data: {"action": "start", "triggered_at": "2021-04-09T16:28:02.0Z", "revision": "7cccaecc91c581931b7cdf75bbf79abd71fa135a"}
commi...@chromium.org submitted this change.
Migrate several lib/src/services/correction/dart/
Change-Id: I0db19c94d26c7c604d71663e2f2c944286898c0b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194546
Commit-Queue: Konstantin Shcheglov <sche...@google.com>
Reviewed-by: Brian Wilkerson <brianwi...@google.com>
---
M pkg/analysis_server/lib/src/services/correction/assist_internal.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_contains.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_expression_function_body.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_field_parameter.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_generic_function_syntax.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_if_null.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_int_literal.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_list_literal.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_map_literal.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_multiline_string.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_named_arguments.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_normal_parameter.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware_spread.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_package_import.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_relative_import.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_set_literal.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_where_type.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_class.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_constructor.dart
M pkg/analysis_server/lib/src/services/correction/fix_internal.dart
21 files changed, 364 insertions(+), 272 deletions(-)
diff --git a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
index b5a1027..ef6a1ef 100644
--- a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
@@ -107,7 +107,7 @@
ConvertPartOfToUri.newInstance,
ConvertToDoubleQuotes.newInstance,
ConvertToFieldParameter.newInstance,
- ConvertToMutilineString.newInstance,
+ ConvertToMultilineString.newInstance,
ConvertToNormalParameter.newInstance,
ConvertToSingleQuotes.newInstance,
ConvertToExpressionFunctionBody.newInstance,
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_contains.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_contains.dart
index 7665de9..0ca4062 100644@@ -71,17 +75,20 @@
/// Return the value of the given [expression], given that [_isInteger]
/// returned `true`.
- int _integerValue(Expression expression) {
+ int? _integerValue(Expression expression) {
if (expression is IntegerLiteral) {
return expression.value;
} else if (expression is PrefixExpression &&
expression.operator.type == TokenType.MINUS) {
var operand = expression.operand;
if (operand is IntegerLiteral) {
- return -operand.value;
+ var value = operand.value;
+ if (value != null) {
+ return -value;
+ }
}
}
- throw StateError('invalid integer value');
+ return null;index ef999da..447adee 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_field_parameter.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_field_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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -19,87 +17,128 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node == null) {
- return;
- }
- // prepare ConstructorDeclaration
- var constructor = node.thisOrAncestorOfType<ConstructorDeclaration>();
- if (constructor == null) {
- return;
- }
- var parameterList = constructor.parameters;
- List<ConstructorInitializer> initializers = constructor.initializers;
// prepare parameter
- SimpleFormalParameter parameter;
- if (node.parent is SimpleFormalParameter &&
- node.parent.parent is FormalParameterList &&
- node.parent.parent.parent is ConstructorDeclaration) {
- parameter = node.parent;
+ var context = _findParameter(node);
+ if (context == null) {
+ return;
}
- await builder.addDartFileEdit(file, (builder) {
- // replace parameter
- builder.addSimpleReplacement(range.node(parameter), 'this.$fieldName');
- // remove initializer
- var initializerIndex = initializers.indexOf(parameterInitializer);
- if (initializers.length == 1) {
- builder
- .addDeletion(range.endEnd(parameterList, parameterInitializer));
- } else {
- if (initializerIndex == 0) {
- var next = initializers[initializerIndex + 1];
- builder.addDeletion(range.startStart(parameterInitializer, next));
- } else {
- var prev = initializers[initializerIndex - 1];
- builder.addDeletion(range.endEnd(prev, parameterInitializer));
- }
- }
- });
index 5307774..013e4fe 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_null_aware.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -28,15 +26,17 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
- if (node.parent is BinaryExpression &&
- node.parent.parent is ConditionalExpression) {
- node = node.parent.parent;
+ var parent = node.parent;
+ if (parent is BinaryExpression) {
+ var grandParent = parent.parent;
+ if (grandParent is ConditionalExpression) {
+ node = grandParent;
+ }
}
if (node is! ConditionalExpression) {
return;
}
- ConditionalExpression conditional = node;
- var condition = conditional.condition.unParenthesized;
+ var condition = node.condition.unParenthesized;
SimpleIdentifier identifier;
Expression nullExpression;
Expression nonNullExpression;
@@ -67,21 +67,20 @@
// is the save variable being compared to `null`.
//
if (condition.operator.type == TokenType.EQ_EQ) {
- nullExpression = conditional.thenExpression;
- nonNullExpression = conditional.elseExpression;
+ nullExpression = node.thenExpression;
+ nonNullExpression = node.elseExpression;
} else if (condition.operator.type == TokenType.BANG_EQ) {
- nonNullExpression = conditional.thenExpression;
- nullExpression = conditional.elseExpression;
- }
- if (nullExpression == null || nonNullExpression == null) {
+ nonNullExpression = node.thenExpression;
+ nullExpression = node.elseExpression;
+ } else {
return;
}
if (nullExpression.unParenthesized is! NullLiteral) {
return;
}
var unwrappedExpression = nonNullExpression.unParenthesized;
- Expression target;
- Token operator;
+ Expression? target;
+ Token? operator;
if (unwrappedExpression is MethodInvocation) {
target = unwrappedExpression.target;
operator = unwrappedExpression.operator;
diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
index e8795a1f..02560a0 100644
--- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
@@ -1732,15 +1732,15 @@
return fixes.isNotEmpty ? fixes.first : null;
}
- void _addFixFromBuilder(ChangeBuilder builder, FixKind kind,
- {List<Object> args, bool importsOnly = false}) {
+ void _addFixFromBuilder(ChangeBuilder builder, CorrectionProducer producer) {
if (builder == null) return;
var change = builder.sourceChange;
- if (change.edits.isEmpty && !importsOnly) {
+ if (change.edits.isEmpty) {
return;
}
+ var kind = producer.fixKind;
change.id = kind.id;
- change.message = formatList(kind.message, args);
+ change.message = formatList(kind.message, producer.fixArguments);
fixes.add(Fix(kind, change));
}
@@ -1764,8 +1764,7 @@
workspace: context.workspace, eol: context.utils.endOfLine);
try {
await producer.compute(builder);
- _addFixFromBuilder(builder, producer.fixKind,
- args: producer.fixArguments);
+ _addFixFromBuilder(builder, producer);
} on ConflictingEditException catch (exception, stackTrace) {
// Handle the exception by (a) not adding a fix based on the producer
// and (b) logging the exception.
To view, visit change 194546. To unsubscribe, or for help writing mail filters, visit settings.
go/dart-cbuild result: SUCCESS
Details: https://goto.google.com/dart-cbuild/find/8fb98b25c1649fbaa64ce77315d181b16bc92a7f
Attention is currently required from: Brian Wilkerson.
Konstantin Shcheglov has uploaded this change for review.
Migrate several lib/src/services/correction/dart/
R=brianwi...@google.com
Change-Id: Ie38a28ee77c2faeb50f9c46e54a64cef933e3916
---
M pkg/analysis_server/lib/src/services/correction/dart/add_missing_required_argument.dart
M pkg/analysis_server/lib/src/services/correction/dart/convert_to_on_type.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_constructor_super.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_field.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_file.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_function.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_getter.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_local_variable.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_method.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_missing_overrides.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_mixin.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_no_such_method.dart
M pkg/analysis_server/lib/src/services/correction/dart/create_setter.dart
M pkg/analysis_server/lib/src/services/correction/dart/encapsulate_field.dart
M pkg/analysis_server/lib/src/services/correction/dart/exchange_operands.dart
M pkg/analysis_server/lib/src/services/correction/dart/extend_class_for_mixin.dart
M pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_children.dart
M pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart
M pkg/analysis_server/lib/src/services/correction/dart/flutter_move_down.dart
M pkg/analysis_server/lib/src/services/correction/dart/flutter_move_up.dart
M pkg/analysis_server/lib/src/services/correction/dart/flutter_remove_widget.dart
M pkg/analysis_server/lib/src/services/correction/dart/flutter_swap_with_child.dart
M pkg/analysis_server/lib/src/services/correction/dart/flutter_swap_with_parent.dart
M pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_generic.dart
M pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_stream_builder.dart
M pkg/analysis_server/lib/src/services/correction/dart/import_add_show.dart
M pkg/analysis_server/lib/src/services/correction/dart/import_library.dart
M pkg/analysis_server/lib/src/services/correction/dart/inline_invocation.dart
M pkg/analysis_server/lib/src/services/correction/dart/inline_typedef.dart
M pkg/analysis_server/lib/src/services/correction/dart/insert_semicolon.dart
M pkg/analysis_server/lib/src/services/correction/dart/introduce_local_cast_type.dart
M pkg/analysis_server/lib/src/services/correction/dart/invert_if_statement.dart
M pkg/analysis_server/lib/src/services/correction/dart/join_if_with_inner.dart
M pkg/analysis_server/lib/src/services/correction/dart/join_if_with_outer.dart
M pkg/analysis_server/lib/src/services/correction/dart/join_variable_declaration.dart
M pkg/analysis_server/lib/src/services/correction/util.dart
38 files changed, 529 insertions(+), 404 deletions(-)
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_missing_required_argument.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_missing_required_argument.dart
index d95722a..6da748e 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_missing_required_argument.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_missing_required_argument.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/completion/dart/utilities.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
@@ -12,10 +10,11 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
+import 'package:collection/collection.dart';
class AddMissingRequiredArgument extends CorrectionProducer {
/// The name of the parameter that was missing.
- String _missingParameterName;
+ String _missingParameterName = '';
@override
List<Object> get fixArguments => [_missingParameterName];
@@ -25,9 +24,9 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- InstanceCreationExpression creation;
- Element targetElement;
- ArgumentList argumentList;
+ InstanceCreationExpression? creation;
+ Element? targetElement;
+ ArgumentList? argumentList;
if (node is SimpleIdentifier || node is ConstructorName) {
var invocation = node.parent;
@@ -35,8 +34,7 @@
targetElement = invocation.methodName.staticElement;
argumentList = invocation.argumentList;
} else {
- creation =
- invocation.thisOrAncestorOfType<InstanceCreationExpression>();
+ creation = invocation?.thisOrAncestorOfType();
if (creation != null) {
targetElement = creation.constructorName.staticElement;
argumentList = creation.argumentList;
@@ -44,7 +42,12 @@
}
}
- if (targetElement is ExecutableElement) {
+ var diagnostic = this.diagnostic;
+ if (diagnostic == null) {
+ return;
+ }
+
+ if (targetElement is ExecutableElement && argumentList != null) {
// Format: "Missing required argument 'foo'."
var messageParts = diagnostic.problemMessage.message.split("'");
if (messageParts.length < 2) {
@@ -52,9 +55,9 @@
}
_missingParameterName = messageParts[1];
- var missingParameter = targetElement.parameters.firstWhere(
- (p) => p.name == _missingParameterName,
- orElse: () => null);
+ var missingParameter = targetElement.parameters.firstWhereOrNull(
+ (p) => p.name == _missingParameterName,
+ );
if (missingParameter == null) {
return;
}
@@ -68,7 +71,7 @@
} else {
var lastArgument = arguments.last;
offset = lastArgument.end;
- hasTrailingComma = lastArgument.endToken.next.type == TokenType.COMMA;
+ hasTrailingComma = lastArgument.endToken.next!.type == TokenType.COMMA;
if (lastArgument is NamedExpression &&
flutter.isWidgetExpression(creation)) {
@@ -80,9 +83,10 @@
}
}
}
+
var defaultValue = getDefaultStringParameterValue(missingParameter,
withNullability: libraryElement.isNonNullableByDefault &&
- missingParameter.library.isNonNullableByDefault);
+ (missingParameter.library?.isNonNullableByDefault ?? false));
await builder.addDartFileEdit(file, (builder) {
builder.addInsertion(offset, (builder) {
@@ -93,14 +97,17 @@
builder.write('$_missingParameterName: ');
// Use defaultValue.cursorPosition if it's not null.
- if (defaultValue?.cursorPosition != null) {
- builder.write(
- defaultValue.text.substring(0, defaultValue.cursorPosition));
- builder.selectHere();
- builder.write(
- defaultValue.text.substring(defaultValue.cursorPosition));
+ if (defaultValue != null) {
+ var cursorPosition = defaultValue.cursorPosition;
+ if (cursorPosition != null) {
+ builder.write(defaultValue.text.substring(0, cursorPosition));
+ builder.selectHere();
+ builder.write(defaultValue.text.substring(cursorPosition));
+ } else {
+ builder.addSimpleLinkedEdit('VALUE', defaultValue.text);
+ }
} else {
- builder.addSimpleLinkedEdit('VALUE', defaultValue?.text);
+ builder.addSimpleLinkedEdit('VALUE', 'null');
}
if (flutter.isWidgetExpression(creation)) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_on_type.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_on_type.dart
index ead067d..a47567a 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_on_type.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_on_type.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,30 +21,30 @@
var exceptionParameter = node;
if (exceptionParameter is SimpleIdentifier) {
var catchClause = exceptionParameter.parent;
- if (catchClause is CatchClause &&
- catchClause.exceptionType == null &&
- catchClause.exceptionParameter == exceptionParameter) {
- var exceptionTypeName = exceptionParameter.name;
- fixArguments.add(exceptionTypeName);
- await builder.addDartFileEdit(file, (builder) {
- if (catchClause.stackTraceParameter != null) {
- builder.addSimpleReplacement(
- range.startStart(
- catchClause.catchKeyword,
- catchClause.stackTraceParameter,
- ),
- 'on $exceptionTypeName catch (_, ',
- );
- } else {
- builder.addSimpleReplacement(
- range.startEnd(
- catchClause.catchKeyword,
- catchClause.rightParenthesis,
- ),
- 'on $exceptionTypeName',
- );
- }
- });
+ if (catchClause is CatchClause) {
+ var catchKeyword = catchClause.catchKeyword;
+ var rightParenthesis = catchClause.rightParenthesis;
+ if (catchKeyword != null &&
+ catchClause.exceptionType == null &&
+ catchClause.exceptionParameter == exceptionParameter &&
+ rightParenthesis != null) {
+ var exceptionTypeName = exceptionParameter.name;
+ fixArguments.add(exceptionTypeName);
+ await builder.addDartFileEdit(file, (builder) {
+ var stackTraceParameter = catchClause.stackTraceParameter;
+ if (stackTraceParameter != null) {
+ builder.addSimpleReplacement(
+ range.startStart(catchKeyword, stackTraceParameter),
+ 'on $exceptionTypeName catch (_, ',
+ );
+ } else {
+ builder.addSimpleReplacement(
+ range.startEnd(catchKeyword, rightParenthesis),
+ 'on $exceptionTypeName',
+ );
+ }
+ });
+ }
}
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.dart
index 62a920e..782826c 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.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';
@@ -28,8 +26,12 @@
if (classDeclaration == null) {
return;
}
+
var className = classDeclaration.name.name;
- var superType = classDeclaration.declaredElement.supertype;
+ var superType = classDeclaration.declaredElement!.supertype;
+ if (superType == null) {
+ return;
+ }
// prepare names of uninitialized final fields
var fieldNames = <String>[];
@@ -45,11 +47,17 @@
}
// prepare location for a new constructor
var targetLocation = utils.prepareNewConstructorLocation(classDeclaration);
+ if (targetLocation == null) {
+ return;
+ }
if (flutter.isExactlyStatelessWidgetType(superType) ||
flutter.isExactlyStatefulWidgetType(superType)) {
// Specialize for Flutter widgets.
var keyClass = await sessionHelper.getClass(flutter.widgetsUri, 'Key');
+ if (keyClass == null) {
+ return;
+ }
await builder.addDartFileEdit(file, (builder) {
builder.addInsertion(targetLocation.offset, (builder) {
builder.write(targetLocation.prefix);
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_super.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_super.dart
index 212261b..e4acc52 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_super.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_super.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';
@@ -16,16 +14,26 @@
@override
Iterable<CorrectionProducer> get producers sync* {
var targetClassNode = node.thisOrAncestorOfType<ClassDeclaration>();
- var targetClassElement = targetClassNode.declaredElement;
+ if (targetClassNode == null) {
+ return;
+ }
+
+ var targetClassElement = targetClassNode.declaredElement!;
var superType = targetClassElement.supertype;
+ if (superType == null) {
+ return;
+ }
+
// add proposals for all super constructors
for (var constructor in superType.constructors) {
// Only propose public constructors.
if (!Identifier.isPrivateName(constructor.name)) {
var targetLocation =
utils.prepareNewConstructorLocation(targetClassNode);
- yield _CreateConstructor(
- constructor, targetLocation, targetClassElement.name);
+ if (targetLocation != null) {
+ yield _CreateConstructor(
+ constructor, targetLocation, targetClassElement.name);
+ }
}
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_field.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_field.dart
index fa00de2..ec42942 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_field.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_field.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';
@@ -14,7 +12,7 @@
class CreateField extends CorrectionProducer {
/// The name of the field to be created.
- String _fieldName;
+ String _fieldName = '';
@override
List<Object> get fixArguments => [_fieldName];
@@ -38,9 +36,15 @@
if (targetClassNode == null) {
return;
}
+
var nameNode = parameter.identifier;
_fieldName = nameNode.name;
+
var targetLocation = utils.prepareNewFieldLocation(targetClassNode);
+ if (targetLocation == null) {
+ return;
+ }
+
//
// Add proposal.
//
@@ -56,13 +60,13 @@
}
Future<void> _proposeFromIdentifier(ChangeBuilder builder) async {
- if (node is! SimpleIdentifier) {
+ var nameNode = node;
+ if (nameNode is! SimpleIdentifier) {
return;
}
- SimpleIdentifier nameNode = node;
_fieldName = nameNode.name;
// prepare target Expression
- Expression target;
+ Expression? target;
{
var nameParent = nameNode.parent;
if (nameParent is PrefixedIdentifier) {
@@ -73,7 +77,7 @@
}
// prepare target ClassElement
var staticModifier = false;
- ClassElement targetClassElement;
+ ClassElement? targetClassElement;
if (target != null) {
targetClassElement = getTargetClassElement(target);
// maybe static
@@ -102,13 +106,20 @@
if (targetDeclarationResult == null) {
return;
}
- if (targetDeclarationResult.node is! ClassOrMixinDeclaration) {
+ var targetNode = targetDeclarationResult.node;
+ if (targetNode is! ClassOrMixinDeclaration) {
return;
}
- ClassOrMixinDeclaration targetNode = targetDeclarationResult.node;
// prepare location
- var targetLocation = CorrectionUtils(targetDeclarationResult.resolvedUnit)
- .prepareNewFieldLocation(targetNode);
+ var targetUnit = targetDeclarationResult.resolvedUnit;
+ if (targetUnit == null) {
+ return;
+ }
+ var targetLocation =
+ CorrectionUtils(targetUnit).prepareNewFieldLocation(targetNode);
+ if (targetLocation == null) {
+ return;
+ }
// build field source
var targetSource = targetClassElement.source;
var targetFile = targetSource.fullName;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_file.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_file.dart
index 6dd55bf..455c0c8 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_file.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_file.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';
@@ -12,7 +10,7 @@
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
class CreateFile extends CorrectionProducer {
- String _fileName;
+ String _fileName = '';
@override
List<Object> get fixArguments => [_fileName];
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_function.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_function.dart
index 2351d57..1a8aaf4 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_function.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_function.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';
@@ -12,7 +10,7 @@
import 'package:analyzer_plugin/utilities/range_factory.dart';
class CreateFunction extends CorrectionProducer {
- String _functionName;
+ String _functionName = '';
@override
List<Object> get fixArguments => [_functionName];
@@ -37,8 +35,10 @@
// prepare environment
int insertOffset;
String sourcePrefix;
- AstNode enclosingMember =
- node.thisOrAncestorOfType<CompilationUnitMember>();
+ var enclosingMember = node.thisOrAncestorOfType<CompilationUnitMember>();
+ if (enclosingMember == null) {
+ return;
+ }
insertOffset = enclosingMember.end;
sourcePrefix = '$eol$eol';
utils.targetClassElement = null;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_getter.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_getter.dart
index 3ca2941..3c9c661 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_getter.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_getter.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';
@@ -14,7 +12,7 @@
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
class CreateGetter extends CorrectionProducer {
- String _getterName;
+ String _getterName = '';
@override
List<Object> get fixArguments => [_getterName];
@@ -24,16 +22,16 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is! SimpleIdentifier) {
+ var nameNode = node;
+ if (nameNode is! SimpleIdentifier) {
return;
}
- SimpleIdentifier nameNode = node;
_getterName = nameNode.name;
if (!nameNode.inGetterContext()) {
return;
}
// prepare target
- Expression target;
+ Expression? target;
{
var nameParent = nameNode.parent;
if (nameParent is PrefixedIdentifier) {
@@ -44,7 +42,7 @@
}
// prepare target element
var staticModifier = false;
- Element targetElement;
+ Element? targetElement;
if (target is ExtensionOverride) {
targetElement = target.staticElement;
} else if (target is Identifier &&
@@ -72,7 +70,11 @@
}
staticModifier = inStaticContext;
}
- if (targetElement.librarySource.isInSystemLibrary) {
+ if (targetElement == null) {
+ return;
+ }
+ var targetSource = targetElement.source;
+ if (targetSource == null || targetSource.isInSystemLibrary) {
return;
}
// prepare target declaration
@@ -81,16 +83,26 @@
if (targetDeclarationResult == null) {
return;
}
- if (targetDeclarationResult.node is! ClassOrMixinDeclaration &&
- targetDeclarationResult.node is! ExtensionDeclaration) {
+ var targetNode = targetDeclarationResult.node;
+ if (targetNode is CompilationUnitMember) {
+ if (targetDeclarationResult.node is! ClassOrMixinDeclaration &&
+ targetDeclarationResult.node is! ExtensionDeclaration) {
+ return;
+ }
+ } else {
return;
}
- CompilationUnitMember targetNode = targetDeclarationResult.node;
// prepare location
- var targetLocation = CorrectionUtils(targetDeclarationResult.resolvedUnit)
- .prepareNewGetterLocation(targetNode);
+ var resolvedUnit = targetDeclarationResult.resolvedUnit;
+ if (resolvedUnit == null) {
+ return;
+ }
+ var targetLocation =
+ CorrectionUtils(resolvedUnit).prepareNewGetterLocation(targetNode);
+ if (targetLocation == null) {
+ return;
+ }
// build method source
- var targetSource = targetElement.source;
var targetFile = targetSource.fullName;
await builder.addDartFileEdit(targetFile, (builder) {
builder.addInsertion(targetLocation.offset, (builder) {
@@ -100,7 +112,7 @@
builder.writeGetterDeclaration(_getterName,
isStatic: staticModifier,
nameGroupName: 'NAME',
- returnType: fieldType,
+ returnType: fieldType ?? typeProvider.dynamicType,
returnTypeGroupName: 'TYPE');
builder.write(targetLocation.suffix);
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_local_variable.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_local_variable.dart
index 42ba1d7..22b464e 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_local_variable.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_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:_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';
@@ -14,7 +12,7 @@
import 'package:analyzer_plugin/utilities/range_factory.dart';
class CreateLocalVariable extends CorrectionProducer {
- String _variableName;
+ String _variableName = '';
@override
List<Object> get fixArguments => [_variableName];
@@ -24,14 +22,14 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is! SimpleIdentifier) {
+ var nameNode = node;
+ if (nameNode is! SimpleIdentifier) {
return;
}
- SimpleIdentifier nameNode = node;
_variableName = nameNode.name;
// if variable is assigned, convert assignment into declaration
- if (node.parent is AssignmentExpression) {
- AssignmentExpression assignment = node.parent;
+ var assignment = node.parent;
+ if (assignment is AssignmentExpression) {
if (assignment.leftHandSide == node &&
assignment.operator.type == TokenType.EQ &&
assignment.parent is ExpressionStatement) {
@@ -48,7 +46,7 @@
}
var prefix = utils.getNodePrefix(target);
// compute type
- var type = inferUndefinedExpressionType(node);
+ var type = inferUndefinedExpressionType(nameNode);
if (!(type == null || type is InterfaceType || type is FunctionType)) {
return;
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_method.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_method.dart
index 66397fc..a697fff 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_method.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_method.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';
@@ -18,7 +16,7 @@
/// The kind of method to be created.
final _MethodKind _kind;
- String _memberName;
+ String _memberName = '';
CreateMethod(this._kind);
@@ -47,11 +45,11 @@
}
final classDecl = memberDecl.thisOrAncestorOfType<ClassDeclaration>();
if (classDecl != null) {
- final classElement = classDecl.declaredElement;
+ final classElement = classDecl.declaredElement!;
var missingEquals = memberDecl is FieldDeclaration ||
(memberDecl as MethodDeclaration).name.name == 'hashCode';
- ExecutableElement element;
+ ExecutableElement? element;
if (missingEquals) {
_memberName = '==';
element = classElement.lookUpInheritedMethod(
@@ -61,14 +59,21 @@
element = classElement.lookUpInheritedConcreteGetter(
_memberName, classElement.library);
}
+ if (element == null) {
+ return;
+ }
final location =
utils.prepareNewClassMemberLocation(classDecl, (_) => true);
+ if (location == null) {
+ return;
+ }
+ final element_final = element;
await builder.addDartFileEdit(file, (fileBuilder) {
fileBuilder.addInsertion(location.offset, (builder) {
builder.write(location.prefix);
- builder.writeOverride(element, invokeSuper: true);
+ builder.writeOverride(element_final, invokeSuper: true);
builder.write(location.suffix);
});
});
@@ -84,24 +89,28 @@
_memberName = (node as SimpleIdentifier).name;
var invocation = node.parent as MethodInvocation;
// prepare environment
- Element targetElement;
+ Element? targetElement;
var staticModifier = false;
- CompilationUnitMember targetNode;
+ CompilationUnitMember? targetNode;
var target = invocation.realTarget;
var utils = this.utils;
if (target is ExtensionOverride) {
targetElement = target.staticElement;
- targetNode = await getExtensionDeclaration(targetElement);
- if (targetNode == null) {
- return;
+ if (targetElement is ExtensionElement) {
+ targetNode = await getExtensionDeclaration(targetElement);
+ if (targetNode == null) {
+ return;
+ }
}
} else if (target is Identifier &&
target.staticElement is ExtensionElement) {
targetElement = target.staticElement;
- targetNode = await getExtensionDeclaration(targetElement);
- if (targetNode == null) {
- return;
+ if (targetElement is ExtensionElement) {
+ targetNode = await getExtensionDeclaration(targetElement);
+ if (targetNode == null) {
+ return;
+ }
}
staticModifier = true;
} else if (target == null) {
@@ -112,8 +121,11 @@
// doesn't make sense to create a method.
return;
}
- targetNode = enclosingMember.parent;
- staticModifier = inStaticContext;
+ var enclosingMemberParent = enclosingMember.parent;
+ if (enclosingMemberParent is CompilationUnitMember) {
+ targetNode = enclosingMemberParent;
+ staticModifier = inStaticContext;
+ }
} else {
var targetClassElement = getTargetClassElement(target);
if (targetClassElement == null) {
@@ -130,7 +142,7 @@
}
// maybe static
if (target is Identifier) {
- staticModifier = target.staticElement.kind == ElementKind.CLASS;
+ staticModifier = target.staticElement?.kind == ElementKind.CLASS;
}
// use different utils
var targetPath = targetClassElement.source.fullName;
@@ -138,8 +150,18 @@
await resolvedResult.session.getResolvedUnit(targetPath);
utils = CorrectionUtils(targetResolveResult);
}
+ if (targetElement == null || targetNode == null) {
+ return;
+ }
var targetLocation = utils.prepareNewMethodLocation(targetNode);
- var targetFile = targetElement.source.fullName;
+ if (targetLocation == null) {
+ return;
+ }
+ var targetSource = targetElement.source;
+ if (targetSource == null) {
+ return;
+ }
+ var targetFile = targetSource.fullName;
// build method source
await builder.addDartFileEdit(targetFile, (builder) {
builder.addInsertion(targetLocation.offset, (builder) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.dart
index c831928..3695dfa 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.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';
@@ -17,9 +15,9 @@
import 'package:analyzer_plugin/utilities/range_factory.dart';
class CreateMethodOrFunction extends CorrectionProducer {
- FixKind _fixKind;
+ FixKind _fixKind = DartFixKind.CREATE_METHOD;
- String _functionName;
+ String _functionName = '';
@override
List<Object> get fixArguments => [_functionName];
@@ -29,10 +27,10 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is SimpleIdentifier) {
- var nameNode = node as SimpleIdentifier;
+ var nameNode = node;
+ if (nameNode is SimpleIdentifier) {
// prepare argument expression (to get parameter)
- ClassElement targetElement;
+ ClassElement? targetElement;
Expression argument;
{
var target = getQualifiedPropertyTarget(node);
@@ -70,12 +68,11 @@
if (parameterType is! FunctionType) {
return;
}
- var functionType = parameterType as FunctionType;
// add proposal
if (targetElement != null) {
- await _createMethod(builder, targetElement, functionType);
+ await _createMethod(builder, targetElement, parameterType);
} else {
- await _createFunction(builder, functionType);
+ await _createFunction(builder, parameterType);
}
}
}
@@ -135,7 +132,7 @@
var sourcePrefix = '$eol';
var sourceSuffix = eol;
await _createExecutable(builder, functionType, name, file, insertOffset,
- false, prefix, sourcePrefix, sourceSuffix, unit.declaredElement);
+ false, prefix, sourcePrefix, sourceSuffix, unit.declaredElement!);
_fixKind = DartFixKind.CREATE_FUNCTION;
_functionName = name;
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_missing_overrides.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_missing_overrides.dart
index a1e5881..e6e0c70 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_missing_overrides.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_missing_overrides.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/utilities/strings.dart';
@@ -15,7 +13,7 @@
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
class CreateMissingOverrides extends CorrectionProducer {
- int _numElements;
+ int _numElements = 0;
@override
List<Object> get fixArguments => [_numElements];
@@ -48,6 +46,9 @@
var location =
utils.prepareNewClassMemberLocation(targetClass, (_) => true);
+ if (location == null) {
+ return;
+ }
var prefix = utils.getIndent(1);
await builder.addDartFileEdit(file, (builder) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_mixin.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_mixin.dart
index 6f783b2..14c4598 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_mixin.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_mixin.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';
@@ -14,7 +12,7 @@
import 'package:analyzer_plugin/utilities/range_factory.dart';
class CreateMixin extends CorrectionProducer {
- String _mixinName;
+ String _mixinName = '';
@override
List<Object> get fixArguments => [_mixinName];
@@ -24,13 +22,15 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- Element prefixElement;
+ Element? prefixElement;
SimpleIdentifier nameNode;
+ var node = this.node;
if (node is SimpleIdentifier) {
var parent = node.parent;
+ var grandParent = parent?.parent;
if (parent is TypeName &&
- parent.parent is ConstructorName &&
- parent.parent.parent is InstanceCreationExpression) {
+ grandParent is ConstructorName &&
+ grandParent.parent is InstanceCreationExpression) {
return;
} else {
nameNode = node;
@@ -40,13 +40,12 @@
if (node.parent is InstanceCreationExpression) {
return;
}
- PrefixedIdentifier prefixedIdentifier = node;
- prefixElement = prefixedIdentifier.prefix.staticElement;
+ prefixElement = node.prefix.staticElement;
if (prefixElement == null) {
return;
}
- nameNode = prefixedIdentifier.identifier;
- _mixinName = prefixedIdentifier.identifier.name;
+ nameNode = node.identifier;
+ _mixinName = node.identifier.name;
} else {
return;
}
@@ -58,9 +57,9 @@
var prefix = '';
var suffix = '';
var offset = -1;
- String filePath;
+ String? filePath;
if (prefixElement == null) {
- targetUnit = unit.declaredElement;
+ targetUnit = unit.declaredElement!;
var enclosingMember = node.thisOrAncestorMatching((node) =>
node is CompilationUnitMember && node.parent is CompilationUnit);
if (enclosingMember == null) {
@@ -77,8 +76,10 @@
targetUnit = library.definingCompilationUnit;
var targetSource = targetUnit.source;
try {
- offset = targetSource.contents.data.length;
- filePath = targetSource.fullName;
+ if (targetSource != null) {
+ offset = targetSource.contents.data.length;
+ filePath = targetSource.fullName;
+ }
prefix = '$eol';
suffix = '$eol';
} on FileSystemException {
@@ -90,7 +91,7 @@
}
}
}
- if (offset < 0) {
+ if (filePath == null || offset < 0) {
return;
}
await builder.addDartFileEdit(filePath, (builder) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_no_such_method.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_no_such_method.dart
index 92af6ca..aa62503 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_no_such_method.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_no_such_method.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/create_setter.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_setter.dart
index c6c7fc5..8958731 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_setter.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_setter.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';
@@ -14,7 +12,7 @@
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
class CreateSetter extends CorrectionProducer {
- String _setterName;
+ String _setterName = '';
@override
List<Object> get fixArguments => [_setterName];
@@ -24,15 +22,15 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is! SimpleIdentifier) {
+ var nameNode = node;
+ if (nameNode is! SimpleIdentifier) {
return;
}
- SimpleIdentifier nameNode = node;
if (!nameNode.inSetterContext()) {
return;
}
// prepare target
- Expression target;
+ Expression? target;
{
var nameParent = nameNode.parent;
if (nameParent is PrefixedIdentifier) {
@@ -43,7 +41,7 @@
}
// prepare target element
var staticModifier = false;
- Element targetElement;
+ Element? targetElement;
if (target is ExtensionOverride) {
targetElement = target.staticElement;
} else if (target is Identifier &&
@@ -71,7 +69,11 @@
}
staticModifier = inStaticContext;
}
- if (targetElement.librarySource.isInSystemLibrary) {
+ if (targetElement == null) {
+ return;
+ }
+ var targetSource = targetElement.source;
+ if (targetSource == null || targetSource.isInSystemLibrary) {
return;
}
// prepare target declaration
@@ -80,16 +82,26 @@
if (targetDeclarationResult == null) {
return;
}
- if (targetDeclarationResult.node is! ClassOrMixinDeclaration &&
- targetDeclarationResult.node is! ExtensionDeclaration) {
+ var targetNode = targetDeclarationResult.node;
+ if (targetNode is CompilationUnitMember) {
+ if (targetDeclarationResult.node is! ClassOrMixinDeclaration &&
+ targetDeclarationResult.node is! ExtensionDeclaration) {
+ return;
+ }
+ } else {
return;
}
- CompilationUnitMember targetNode = targetDeclarationResult.node;
// prepare location
- var targetLocation = CorrectionUtils(targetDeclarationResult.resolvedUnit)
+ var targetUnit = targetDeclarationResult.resolvedUnit;
+ if (targetUnit == null) {
+ return;
+ }
+ var targetLocation = CorrectionUtils(targetUnit)
.prepareNewGetterLocation(targetNode); // Rename to "AccessorLocation"
+ if (targetLocation == null) {
+ return;
+ }
// build method source
- var targetSource = targetElement.source;
var targetFile = targetSource.fullName;
_setterName = nameNode.name;
await builder.addDartFileEdit(targetFile, (builder) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/encapsulate_field.dart b/pkg/analysis_server/lib/src/services/correction/dart/encapsulate_field.dart
index d5aa8b9..9bbf4d8 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/encapsulate_field.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/encapsulate_field.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -43,7 +41,7 @@
}
var field = fields.first;
var nameNode = field.name;
- FieldElement fieldElement = nameNode.staticElement;
+ var fieldElement = field.declaredElement as FieldElement;
// should have a public name
var name = nameNode.name;
if (Identifier.isPrivateName(name)) {
@@ -67,10 +65,11 @@
for (var member in classDeclaration.members) {
if (member is ConstructorDeclaration) {
for (var parameter in member.parameters.parameters) {
+ var identifier = parameter.identifier;
var parameterElement = parameter.declaredElement;
- if (parameterElement is FieldFormalParameterElement &&
+ if (identifier != null &&
+ parameterElement is FieldFormalParameterElement &&
parameterElement.field == fieldElement) {
- var identifier = parameter.identifier;
builder.addSimpleReplacement(range.node(identifier), '_$name');
}
}
@@ -79,14 +78,16 @@
// Write getter and setter.
builder.addInsertion(fieldDeclaration.end, (builder) {
- String docCode;
- if (fieldDeclaration.documentationComment != null) {
- docCode = utils.getNodeText(fieldDeclaration.documentationComment);
+ String? docCode;
+ var documentationComment = fieldDeclaration.documentationComment;
+ if (documentationComment != null) {
+ docCode = utils.getNodeText(documentationComment);
}
var typeCode = '';
- if (variableList.type != null) {
- typeCode = utils.getNodeText(variableList.type) + ' ';
+ var typeAnnotation = variableList.type;
+ if (typeAnnotation != null) {
+ typeCode = utils.getNodeText(typeAnnotation) + ' ';
}
// Write getter.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/exchange_operands.dart b/pkg/analysis_server/lib/src/services/correction/dart/exchange_operands.dart
index bb6b9de..9d1f05d 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/exchange_operands.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/exchange_operands.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -44,7 +42,7 @@
// maybe replace the operator
var operator = binaryExpression.operator;
// prepare a new operator
- String newOperator;
+ String? newOperator;
var operatorType = operator.type;
if (operatorType == TokenType.LT) {
newOperator = '>';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/extend_class_for_mixin.dart b/pkg/analysis_server/lib/src/services/correction/dart/extend_class_for_mixin.dart
index e9eab92..10dbaad 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/extend_class_for_mixin.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/extend_class_for_mixin.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 ExtendClassForMixin extends CorrectionProducer {
- String _typeName;
+ String _typeName = '';
@override
List<Object> get fixArguments => [_typeName];
@@ -21,6 +19,11 @@
@override
Future<void> compute(ChangeBuilder builder) async {
+ var diagnostic = this.diagnostic;
+ if (diagnostic == null) {
+ return;
+ }
+
var declaration = node.thisOrAncestorOfType<ClassDeclaration>();
if (declaration != null && declaration.extendsClause == null) {
// TODO(brianwilkerson) Find a way to pass in the name of the class
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_children.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_children.dart
index 01cc86a..1c0f0ca 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_children.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_children.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -21,7 +19,7 @@
NamedExpression namedExp;
{
var node = this.node;
- var parent = node?.parent;
+ var parent = node.parent;
var parent2 = parent?.parent;
if (node is SimpleIdentifier &&
parent is Label &&
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart
index 48cf327..a386080 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
@@ -37,14 +35,14 @@
}
// Find the build() method.
- MethodDeclaration buildMethod;
+ MethodDeclaration? buildMethod;
for (var member in widgetClass.members) {
- if (member is MethodDeclaration &&
- member.name.name == 'build' &&
- member.parameters != null &&
- member.parameters.parameters.length == 1) {
- buildMethod = member;
- break;
+ if (member is MethodDeclaration && member.name.name == 'build') {
+ var parameters = member.parameters;
+ if (parameters != null && parameters.parameters.length == 1) {
+ buildMethod = member;
+ break;
+ }
}
}
if (buildMethod == null) {
@@ -52,8 +50,9 @@
}
// Must be a StatelessWidget subclasses.
- var widgetClassElement = widgetClass.declaredElement;
- if (!flutter.isExactlyStatelessWidgetType(widgetClassElement.supertype)) {
+ var widgetClassElement = widgetClass.declaredElement!;
+ var superType = widgetClassElement.supertype;
+ if (superType == null || !flutter.isExactlyStatelessWidgetType(superType)) {
return;
}
@@ -75,20 +74,26 @@
for (var member in widgetClass.members) {
if (member is FieldDeclaration && !member.isStatic) {
for (var fieldNode in member.fields.variables) {
- FieldElement fieldElement = fieldNode.declaredElement;
+ var fieldElement = fieldNode.declaredElement as FieldElement;
if (!fieldsAssignedInConstructors.contains(fieldElement)) {
nodesToMove.add(member);
elementsToMove.add(fieldElement);
- elementsToMove.add(fieldElement.getter);
- if (fieldElement.setter != null) {
- elementsToMove.add(fieldElement.setter);
+
+ var getter = fieldElement.getter;
+ if (getter != null) {
+ elementsToMove.add(getter);
+ }
+
+ var setter = fieldElement.setter;
+ if (setter != null) {
+ elementsToMove.add(setter);
}
}
}
}
if (member is MethodDeclaration && !member.isStatic) {
nodesToMove.add(member);
- elementsToMove.add(member.declaredElement);
+ elementsToMove.add(member.declaredElement!);
}
}
@@ -128,8 +133,9 @@
var hasBuildMethod = false;
var typeParams = '';
- if (widgetClass.typeParameters != null) {
- typeParams = utils.getNodeText(widgetClass.typeParameters);
+ var typeParameters = widgetClass.typeParameters;
+ if (typeParameters != null) {
+ typeParams = utils.getNodeText(typeParameters);
}
/// Replace code between [replaceOffset] and [replaceEnd] with
@@ -206,10 +212,10 @@
// Write just param names (and not bounds, metadata and docs).
builder.write('<${widgetClass.name}');
- if (widgetClass.typeParameters != null) {
+ if (typeParameters != null) {
builder.write('<');
var first = true;
- for (var param in widgetClass.typeParameters.typeParameters) {
+ for (var param in typeParameters.typeParameters) {
if (!first) {
builder.write(', ');
first = false;
@@ -250,7 +256,10 @@
if (node.parent is FieldFormalParameter) {
var element = node.staticElement;
if (element is FieldFormalParameterElement) {
- fieldsAssignedInConstructors.add(element.field);
+ var field = element.field;
+ if (field != null) {
+ fieldsAssignedInConstructors.add(field);
+ }
}
}
if (node.parent is ConstructorFieldInitializer) {
@@ -290,7 +299,7 @@
}
var element = node.staticElement;
if (element is ExecutableElement &&
- element?.enclosingElement == widgetClassElement &&
+ element.enclosingElement == widgetClassElement &&
!elementsToMove.contains(element)) {
var offset = node.offset - linesRange.offset;
var qualifier =
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_move_down.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_move_down.dart
index 740afed..1e2fa0f 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_move_down.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_move_down.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_move_up.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_move_up.dart
index ec29164..8513af7 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_move_up.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_move_up.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_remove_widget.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_remove_widget.dart
index 0158fbc..9818245 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_remove_widget.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_remove_widget.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -25,7 +23,7 @@
// Prepare the list of our children.
var childrenArgument = flutter.findChildrenArgument(widgetCreation);
if (childrenArgument != null) {
- var childrenExpression = childrenArgument?.expression;
+ var childrenExpression = childrenArgument.expression;
if (childrenExpression is ListLiteral &&
childrenExpression.elements.isNotEmpty) {
await _removeChildren(
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_swap_with_child.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_swap_with_child.dart
index c1c46e5..c254da0 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_swap_with_child.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_swap_with_child.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -17,7 +15,8 @@
InstanceCreationExpression parent,
InstanceCreationExpression child) async {
// The child must have its own child.
- if (flutter.findChildArgument(child) == null) {
+ var stableChild = flutter.findChildArgument(child);
+ if (stableChild == null) {
return;
}
@@ -38,11 +37,8 @@
// Write all the arguments of the parent.
// Don't write the "child".
- Expression stableChild;
for (var argument in childArgs.arguments) {
- if (flutter.isChildArgument(argument)) {
- stableChild = argument;
- } else {
+ if (argument != stableChild) {
var text = utils.getNodeText(argument);
text = replaceSourceIndent(text, childIndent, parentIndent);
builder.write(parentIndent);
@@ -100,16 +96,16 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var parent = flutter.identifyNewExpression(node);
- if (!flutter.isWidgetCreation(parent)) {
+ if (parent == null || !flutter.isWidgetCreation(parent)) {
return;
}
var childArgument = flutter.findChildArgument(parent);
- if (childArgument?.expression is! InstanceCreationExpression ||
- !flutter.isWidgetCreation(childArgument.expression)) {
+ var child = childArgument?.expression;
+ if (child is! InstanceCreationExpression ||
+ !flutter.isWidgetCreation(child)) {
return;
}
- InstanceCreationExpression child = childArgument.expression;
await swapParentAndChild(builder, parent, child);
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_swap_with_parent.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_swap_with_parent.dart
index a8b9f8b..701c73e 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_swap_with_parent.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_swap_with_parent.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/flutter_swap_with_child.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -17,7 +15,7 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var child = flutter.identifyNewExpression(node);
- if (!flutter.isWidgetCreation(child)) {
+ if (child == null || !flutter.isWidgetCreation(child)) {
return;
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_generic.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_generic.dart
index 4938af4..5fcd235 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_generic.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_generic.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_stream_builder.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_stream_builder.dart
index 50f11bb..ce2f849 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_stream_builder.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_stream_builder.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
+import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -20,7 +19,7 @@
if (widgetExpr == null) {
return;
}
- if (flutter.isExactWidgetTypeStreamBuilder(widgetExpr.staticType)) {
+ if (flutter.isExactWidgetTypeStreamBuilder(widgetExpr.typeOrThrow)) {
return;
}
var widgetSrc = utils.getNodeText(widgetExpr);
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/import_add_show.dart b/pkg/analysis_server/lib/src/services/correction/dart/import_add_show.dart
index 19ee675..4b923a3 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/import_add_show.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/import_add_show.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 'dart:collection';
import 'package:analysis_server/src/services/correction/assist.dart';
@@ -39,7 +37,7 @@
var namespace = getImportNamespace(importElement);
// prepare names of referenced elements (from this import)
var visitor = _ReferenceFinder(namespace);
- resolvedResult.unit.accept(visitor);
+ unit.accept(visitor);
var referencedNames = visitor.referencedNames;
// ignore if unused
if (referencedNames.isEmpty) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart b/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart
index 872fd34..2274c6e 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/import_library.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 'dart:collection';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
@@ -56,10 +54,9 @@
} else if (_importKind == _ImportKind.forTopLevelVariable) {
var node = this.node;
if (node is Annotation) {
- Annotation annotation = node;
- var name = annotation.name;
- if (name != null && name.staticElement == null) {
- if (annotation.arguments != null) {
+ var name = node.name;
+ if (name.staticElement == null) {
+ if (node.arguments != null) {
return;
}
node = name;
@@ -75,10 +72,9 @@
} else if (_importKind == _ImportKind.forType) {
var node = this.node;
if (node is Annotation) {
- Annotation annotation = node;
- var name = annotation.name;
- if (name != null && name.staticElement == null) {
- if (annotation.arguments == null) {
+ var name = node.name;
+ if (name.staticElement == null) {
+ if (node.arguments == null) {
return;
}
node = name;
@@ -116,11 +112,8 @@
/// Return the relative uri from the passed [library] to the given [path].
/// If the [path] is not in the LibraryElement, `null` is returned.
- String _getRelativeURIFromLibrary(LibraryElement library, String path) {
- var librarySource = library?.librarySource;
- if (librarySource == null) {
- return null;
- }
+ String? _getRelativeURIFromLibrary(LibraryElement library, String path) {
+ var librarySource = library.librarySource;
var pathCtx = resourceProvider.pathContext;
var libraryDirectory = pathCtx.dirname(librarySource.fullName);
var sourceDirectory = pathCtx.dirname(path);
@@ -133,7 +126,7 @@
}
Iterable<CorrectionProducer> _importLibrary(FixKind fixKind, Uri library,
- [String relativeURI]) sync* {
+ [String? relativeURI]) sync* {
yield _ImportAbsoluteLibrary(fixKind, library);
if (relativeURI != null && relativeURI.isNotEmpty) {
yield _ImportRelativeLibrary(fixKind, relativeURI);
@@ -154,12 +147,15 @@
for (var imp in libraryElement.imports) {
// prepare element
var libraryElement = imp.importedLibrary;
+ if (libraryElement == null) {
+ continue;
+ }
var element = getExportedElement(libraryElement, name);
if (element == null) {
continue;
}
if (element is PropertyAccessorElement) {
- element = (element as PropertyAccessorElement).variable;
+ element = element.variable;
}
if (!elementKinds.contains(element.kind)) {
continue;
@@ -167,7 +163,7 @@
// may be apply prefix
var prefix = imp.prefix;
if (prefix != null) {
- yield _ImportLibraryPrefix(imp);
+ yield _ImportLibraryPrefix(libraryElement, prefix);
continue;
}
// may be update "show" directive
@@ -265,7 +261,7 @@
final Uri _library;
- String _uriText;
+ String _uriText = '';
_ImportAbsoluteLibrary(this._fixKind, this._library);
@@ -294,13 +290,14 @@
/// A correction processor that can make one of the possible change computed by
/// the [ImportLibrary] producer.
class _ImportLibraryPrefix extends CorrectionProducer {
- final ImportElement _importElement;
+ final LibraryElement _importedLibrary;
+ final PrefixElement _importPrefix;
- String _libraryName;
+ String _libraryName = '';
- String _prefixName;
+ String _prefixName = '';
- _ImportLibraryPrefix(this._importElement);
+ _ImportLibraryPrefix(this._importedLibrary, this._importPrefix);
@override
List<Object> get fixArguments => [_libraryName, _prefixName];
@@ -310,10 +307,8 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var libraryElement = _importElement.importedLibrary;
- var prefix = _importElement.prefix;
- _libraryName = libraryElement.displayName;
- _prefixName = prefix.displayName;
+ _libraryName = _importedLibrary.displayName;
+ _prefixName = _importPrefix.displayName;
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(range.startLength(node, 0), '$_prefixName.');
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/inline_invocation.dart b/pkg/analysis_server/lib/src/services/correction/dart/inline_invocation.dart
index eb35756..2846cbf 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/inline_invocation.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/inline_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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -32,18 +30,26 @@
@override
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
- if (node is! SimpleIdentifier || node.parent is! MethodInvocation) {
+ if (node is! SimpleIdentifier || node.name != 'add') {
return;
}
- SimpleIdentifier name = node;
- MethodInvocation invocation = node.parent;
- if (name != invocation.methodName ||
- name.name != 'add' ||
+
+ var invocation = node.parent;
+ if (invocation is! MethodInvocation) {
+ return;
+ }
+
+ if (node != invocation.methodName ||
!invocation.isCascaded ||
invocation.argumentList.arguments.length != 1) {
return;
}
- var cascade = invocation.thisOrAncestorOfType<CascadeExpression>();
+
+ var cascade = invocation.parent;
+ if (cascade is! CascadeExpression) {
+ return;
+ }
+
var sections = cascade.cascadeSections;
var target = cascade.target;
if (target is! ListLiteral || sections[0] != invocation) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/inline_typedef.dart b/pkg/analysis_server/lib/src/services/correction/dart/inline_typedef.dart
index 6790500..6272326 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/inline_typedef.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/inline_typedef.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';
@@ -14,7 +12,7 @@
import 'package:analyzer_plugin/utilities/range_factory.dart';
class InlineTypedef extends CorrectionProducer {
- String _name;
+ String _name = '';
@override
List<Object> get fixArguments => [_name];
@@ -27,13 +25,17 @@
@override
Future<void> compute(ChangeBuilder builder) async {
+ var parent = node.parent;
+ if (parent == null) {
+ return;
+ }
+
//
// Extract the information needed to build the edit.
//
- TypeAnnotation returnType;
- TypeParameterList typeParameters;
+ TypeAnnotation? returnType;
+ TypeParameterList? typeParameters;
List<FormalParameter> parameters;
- var parent = node.parent;
if (parent is FunctionTypeAlias) {
returnType = parent.returnType;
_name = parent.name.name;
@@ -44,6 +46,9 @@
return;
}
var functionType = parent.functionType;
+ if (functionType == null) {
+ return;
+ }
returnType = functionType.returnType;
_name = parent.name.name;
typeParameters = functionType.typeParameters;
@@ -53,8 +58,9 @@
}
// TODO(brianwilkerson) Handle parts.
var finder = _ReferenceFinder(_name);
- resolvedResult.unit.accept(finder);
- if (finder.count != 1) {
+ unit.accept(finder);
+ var reference = finder.reference;
+ if (reference == null || finder.count != 1) {
return;
}
//
@@ -62,7 +68,7 @@
//
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(utils.getLinesRange(range.node(parent)));
- builder.addReplacement(range.node(finder.reference), (builder) {
+ builder.addReplacement(range.node(reference), (builder) {
if (returnType != null) {
builder.write(utils.getNodeText(returnType));
builder.write(' ');
@@ -71,7 +77,7 @@
if (typeParameters != null) {
builder.write(utils.getNodeText(typeParameters));
}
- String groupEnd;
+ String? groupEnd;
builder.write('(');
for (var i = 0; i < parameters.length; i++) {
var parameter = parameters[i];
@@ -90,7 +96,7 @@
builder.write('[');
}
}
- parameter = (parameter as DefaultFormalParameter).parameter;
+ parameter = parameter.parameter;
}
if (parameter is FunctionTypedFormalParameter) {
builder.write(utils.getNodeText(parameter));
@@ -109,14 +115,18 @@
if (keyword != null && keyword.type != Keyword.VAR) {
builder.write(keyword.lexeme);
}
- if (parameter.type == null) {
+ var typeAnnotation = parameter.type;
+ if (typeAnnotation == null) {
builder.write('dynamic');
} else {
- builder.write(utils.getNodeText(parameter.type));
+ builder.write(utils.getNodeText(typeAnnotation));
}
if (parameter.isNamed) {
- builder.write(' ');
- builder.write(parameter.identifier.name);
+ var identifier = parameter.identifier;
+ if (identifier != null) {
+ builder.write(' ');
+ builder.write(identifier.name);
+ }
}
}
}
@@ -135,7 +145,7 @@
class _ReferenceFinder extends RecursiveAstVisitor {
final String typeName;
- TypeName reference;
+ TypeName? reference;
int count = 0;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/insert_semicolon.dart b/pkg/analysis_server/lib/src/services/correction/dart/insert_semicolon.dart
index 8c837c1..60ad47b 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/insert_semicolon.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/insert_semicolon.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';
@@ -16,6 +14,11 @@
@override
Future<void> compute(ChangeBuilder builder) async {
+ var diagnostic = this.diagnostic;
+ if (diagnostic == null) {
+ return;
+ }
+
var message = diagnostic.problemMessage;
if (message.message.contains("';'")) {
if (_isAwaitNode()) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/introduce_local_cast_type.dart b/pkg/analysis_server/lib/src/services/correction/dart/introduce_local_cast_type.dart
index c696b43..e3c3b29 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/introduce_local_cast_type.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/introduce_local_cast_type.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/name_suggestion.dart';
@@ -19,42 +17,25 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var node = this.node;
- if (node is IfStatement) {
- node = (node as IfStatement).condition;
- } else if (node is WhileStatement) {
- node = (node as WhileStatement).condition;
- }
- // prepare IsExpression
- if (node is! IsExpression) {
+ var isExpression = _getCondition(node);
+ if (isExpression is! IsExpression) {
return;
}
- IsExpression isExpression = node;
var castType = isExpression.type.type;
var castTypeCode = utils.getNodeText(isExpression.type);
// prepare environment
- var indent = utils.getIndent(1);
- String prefix;
- Block targetBlock;
- {
- var statement = node.thisOrAncestorOfType<Statement>();
- if (statement is IfStatement && statement.thenStatement is Block) {
- targetBlock = statement.thenStatement;
- } else if (statement is WhileStatement && statement.body is Block) {
- targetBlock = statement.body;
- } else {
- return;
- }
- prefix = utils.getNodePrefix(statement);
+ var enclosingStatement = _enclosingStatement(isExpression);
+ if (enclosingStatement == null) {
+ return;
}
// prepare location
int offset;
String statementPrefix;
if (isExpression.notOperator == null) {
- offset = targetBlock.leftBracket.end;
- statementPrefix = indent;
+ offset = enclosingStatement.block.leftBracket.end;
+ statementPrefix = utils.getIndent(1);
} else {
- offset = targetBlock.rightBracket.end;
+ offset = enclosingStatement.block.rightBracket.end;
statementPrefix = '';
}
// prepare excluded names
@@ -69,7 +50,7 @@
if (suggestions.isNotEmpty) {
await builder.addDartFileEdit(file, (builder) {
builder.addInsertion(offset, (builder) {
- builder.write(eol + prefix + statementPrefix);
+ builder.write(eol + enclosingStatement.prefix + statementPrefix);
builder.write(castTypeCode);
builder.write(' ');
builder.addSimpleLinkedEdit('NAME', suggestions[0],
@@ -84,6 +65,54 @@
}
}
+ _EnclosingStatement? _enclosingStatement(IsExpression condition) {
+ var statement = condition.thisOrAncestorOfType<Statement>();
+ if (statement is IfStatement) {
+ var thenStatement = statement.thenStatement;
+ if (thenStatement is Block) {
+ return _EnclosingStatement(
+ utils.getNodePrefix(statement),
+ thenStatement,
+ );
+ }
+ } else if (statement is WhileStatement) {
+ var body = statement.body;
+ if (body is Block) {
+ return _EnclosingStatement(
+ utils.getNodePrefix(statement),
+ body,
+ );
+ }
+ }
+ return null;
+ }
+
/// Return an instance of this class. Used as a tear-off in `AssistProcessor`.
static IntroduceLocalCastType newInstance() => IntroduceLocalCastType();
+
+ static Expression? _getCondition(AstNode node) {
+ if (node is IfStatement) {
+ return node.condition;
+ } else if (node is WhileStatement) {
+ return node.condition;
+ }
+
+ if (node is Expression) {
+ var parent = node.parent;
+ if (parent is IfStatement && parent.condition == node) {
+ return node;
+ } else if (parent is WhileStatement && parent.condition == node) {
+ return node;
+ }
+ }
+
+ return null;
+ }
+}
+
+class _EnclosingStatement {
+ final String prefix;
+ final Block block;
+
+ _EnclosingStatement(this.prefix, this.block);
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/invert_if_statement.dart b/pkg/analysis_server/lib/src/services/correction/dart/invert_if_statement.dart
index 2fa703ac..20e22c9 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/invert_if_statement.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/invert_if_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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -17,15 +15,15 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is! IfStatement) {
+ var ifStatement = node;
+ if (ifStatement is! IfStatement) {
return;
}
- var ifStatement = node as IfStatement;
var condition = ifStatement.condition;
// should have both "then" and "else"
var thenStatement = ifStatement.thenStatement;
var elseStatement = ifStatement.elseStatement;
- if (thenStatement == null || elseStatement == null) {
+ if (elseStatement == null) {
return;
}
// prepare source
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_inner.dart b/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_inner.dart
index 00337c6..2c267d2 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_inner.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_inner.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/util.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';
@@ -19,25 +18,20 @@
@override
Future<void> compute(ChangeBuilder builder) async {
// climb up condition to the (supposedly) "if" statement
- var node = this.node;
- while (node is Expression) {
- node = node.parent;
- }
+ var targetIfStatement = _enclosingIfStatement(node);
// prepare target "if" statement
- if (node is! IfStatement) {
+ if (targetIfStatement == null) {
return;
}
- var targetIfStatement = node as IfStatement;
if (targetIfStatement.elseStatement != null) {
return;
}
// prepare inner "if" statement
var targetThenStatement = targetIfStatement.thenStatement;
- var innerStatement = getSingleStatement(targetThenStatement);
- if (innerStatement is! IfStatement) {
+ var innerIfStatement = getSingleStatement(targetThenStatement);
+ if (innerIfStatement is! IfStatement) {
return;
}
- var innerIfStatement = innerStatement as IfStatement;
if (innerIfStatement.elseStatement != null) {
return;
}
@@ -70,4 +64,14 @@
/// Return an instance of this class. Used as a tear-off in `AssistProcessor`.
static JoinIfWithInner newInstance() => JoinIfWithInner();
+
+ static IfStatement? _enclosingIfStatement(AstNode start) {
+ for (var node in start.withParents) {
+ if (node is IfStatement) {
+ return node;
+ } else if (node is! Expression) {
+ return null;
+ }
+ }
+ }
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_outer.dart b/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_outer.dart
index 22761fe..5dd2cbd 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_outer.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_outer.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/util.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';
@@ -19,22 +18,18 @@
@override
Future<void> compute(ChangeBuilder builder) async {
// climb up condition to the (supposedly) "if" statement
- var node = this.node;
- while (node is Expression) {
- node = node.parent;
- }
+ var targetIfStatement = _enclosingIfStatement(node);
// prepare target "if" statement
- if (node is! IfStatement) {
+ if (targetIfStatement == null) {
return;
}
- var targetIfStatement = node as IfStatement;
if (targetIfStatement.elseStatement != null) {
return;
}
// prepare outer "if" statement
var parent = targetIfStatement.parent;
if (parent is Block) {
- if ((parent as Block).statements.length != 1) {
+ if (parent.statements.length != 1) {
return;
}
parent = parent.parent;
@@ -42,7 +37,7 @@
if (parent is! IfStatement) {
return;
}
- var outerIfStatement = parent as IfStatement;
+ var outerIfStatement = parent;
if (outerIfStatement.elseStatement != null) {
return;
}
@@ -75,4 +70,14 @@
/// Return an instance of this class. Used as a tear-off in `AssistProcessor`.
static JoinIfWithOuter newInstance() => JoinIfWithOuter();
+
+ static IfStatement? _enclosingIfStatement(AstNode start) {
+ for (var node in start.withParents) {
+ if (node is IfStatement) {
+ return node;
+ } else if (node is! Expression) {
+ return null;
+ }
+ }
+ }
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/join_variable_declaration.dart b/pkg/analysis_server/lib/src/services/correction/dart/join_variable_declaration.dart
index c7713a2..015acb8 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/join_variable_declaration.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/join_variable_declaration.dart
@@ -2,13 +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:_fe_analyzer_shared/src/scanner/token.dart';
import 'package:analysis_server/src/services/correction/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
-import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -19,12 +16,13 @@
@override
Future<void> compute(ChangeBuilder builder) async {
+ var node = this.node;
if (node is SimpleIdentifier) {
var parent = node.parent;
if (parent is AssignmentExpression &&
parent.leftHandSide == node &&
parent.parent is ExpressionStatement) {
- await _joinOnAssignment(builder, parent);
+ await _joinOnAssignment(builder, node, parent);
return;
}
}
@@ -36,58 +34,57 @@
/// Join the declaration when the variable is on the left-hand side of an
/// assignment.
- Future<void> _joinOnAssignment(
- ChangeBuilder builder, AssignmentExpression assignExpression) async {
+ Future<void> _joinOnAssignment(ChangeBuilder builder, SimpleIdentifier left,
+ AssignmentExpression assignment) async {
// Check that assignment is not a compound assignment.
- if (assignExpression.operator.type != TokenType.EQ) {
+ if (assignment.operator.type != TokenType.EQ) {
return;
}
- // prepare "declaration" statement
- var element = (node as SimpleIdentifier).staticElement;
- if (element == null) {
+
+ // The assignment must be a separate statement.
+ var assignmentStatement = assignment.parent;
+ if (assignmentStatement is! ExpressionStatement) {
return;
}
- var declOffset = element.nameOffset;
- var unit = resolvedResult.unit;
- var declNode = NodeLocator(declOffset).searchWithin(unit);
- if (declNode != null &&
- declNode.parent is VariableDeclaration &&
- (declNode.parent as VariableDeclaration).name == declNode &&
- declNode.parent.parent is VariableDeclarationList &&
- declNode.parent.parent.parent is VariableDeclarationStatement) {
- } else {
+
+ // ...in a Block.
+ var block = assignmentStatement.parent;
+ if (block is! Block) {
return;
}
- var decl = declNode.parent as VariableDeclaration;
- var declStatement = decl.parent.parent as VariableDeclarationStatement;
- // may be has initializer
- if (decl.initializer != null) {
+
+ // Prepare the index in the enclosing Block.
+ var statements = block.statements;
+ var assignmentStatementIndex = statements.indexOf(assignmentStatement);
+ if (assignmentStatementIndex < 1) {
return;
}
- // check that "declaration" statement declared only one variable
- if (declStatement.variables.variables.length != 1) {
+
+ // The immediately previous statement must be a declaration.
+ var declarationStatement = statements[assignmentStatementIndex - 1];
+ if (declarationStatement is! VariableDeclarationStatement) {
return;
}
- // check that the "declaration" and "assignment" statements are
- // parts of the same Block
- var assignStatement = node.parent.parent as ExpressionStatement;
- if (assignStatement.parent is Block &&
- assignStatement.parent == declStatement.parent) {
- } else {
+
+ // Only one variable must be declared.
+ var declaredVariables = declarationStatement.variables.variables;
+ if (declaredVariables.length != 1) {
return;
}
- var block = assignStatement.parent as Block;
- // check that "declaration" and "assignment" statements are adjacent
- List<Statement> statements = block.statements;
- if (statements.indexOf(assignStatement) ==
- statements.indexOf(declStatement) + 1) {
- } else {
+
+ // The declared variable must be the one that is assigned.
+ // There must be no initializer.
+ var declaredVariable = declaredVariables.single;
+ if (declaredVariable.declaredElement != left.staticElement ||
+ declaredVariable.initializer != null) {
return;
}
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(
- range.endStart(declNode, assignExpression.operator), ' ');
+ range.endStart(declaredVariable, assignment.operator),
+ ' ',
+ );
});
}
@@ -95,50 +92,65 @@
/// assignment.
Future<void> _joinOnDeclaration(
ChangeBuilder builder, VariableDeclarationList declList) async {
- // prepare enclosing VariableDeclarationList
- var decl = declList.variables[0];
- // already initialized
- if (decl.initializer != null) {
+ // Only one variable must be declared.
+ var declaredVariables = declList.variables;
+ if (declaredVariables.length != 1) {
return;
}
- // prepare VariableDeclarationStatement in Block
- if (declList.parent is VariableDeclarationStatement &&
- declList.parent.parent is Block) {
- } else {
+
+ // The declared variable must not be initialized.
+ var declaredVariable = declaredVariables.single;
+ if (declaredVariable.initializer != null) {
return;
}
- var declStatement = declList.parent as VariableDeclarationStatement;
- var block = declStatement.parent as Block;
- List<Statement> statements = block.statements;
- // prepare assignment
- // declaration should not be last Statement
- var declIndex = statements.indexOf(declStatement);
- if (declIndex < statements.length - 1) {
- } else {
+
+ // The declaration must be a separate statement.
+ var declarationStatement = declList.parent;
+ if (declarationStatement is! VariableDeclarationStatement) {
return;
}
- // next Statement should be assignment
- var assignStatement = statements[declIndex + 1];
- if (assignStatement is ExpressionStatement) {
- } else {
+
+ // ...in a Block.
+ var block = declarationStatement.parent;
+ if (block is! Block) {
return;
}
- var expressionStatement = assignStatement as ExpressionStatement;
- // expression should be assignment
- if (expressionStatement.expression is AssignmentExpression) {
- } else {
+
+ // The declaration statement must not be the last in the block.
+ var statements = block.statements;
+ var declarationStatementIndex = statements.indexOf(declarationStatement);
+ if (declarationStatementIndex < 0 ||
+ declarationStatementIndex >= statements.length - 1) {
return;
}
- var assignExpression =
- expressionStatement.expression as AssignmentExpression;
- // check that pure assignment
- if (assignExpression.operator.type != TokenType.EQ) {
+
+ // The immediately following statement must be an assignment statement.
+ var assignmentStatement = statements[declarationStatementIndex + 1];
+ if (assignmentStatement is! ExpressionStatement) {
+ return;
+ }
+
+ // Really an assignment.
+ var assignment = assignmentStatement.expression;
+ if (assignment is! AssignmentExpression) {
+ return;
+ }
+
+ // The assignment should write into the declared variable.
+ if (assignment.writeElement != declaredVariable.declaredElement) {
+ return;
+ }
+
+ // The assignment must be pure.
+ if (assignment.operator.type != TokenType.EQ) {
return;
}
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(
- range.endStart(decl.name, assignExpression.operator), ' ');
+ range.endStart(declaredVariable.name, assignment.operator),
+ ' ',
+ );
});
}
diff --git a/pkg/analysis_server/lib/src/services/correction/util.dart b/pkg/analysis_server/lib/src/services/correction/util.dart
index 7480d17..9187da0 100644
--- a/pkg/analysis_server/lib/src/services/correction/util.dart
+++ b/pkg/analysis_server/lib/src/services/correction/util.dart
@@ -482,7 +482,7 @@
/// If the given [expression] is the `expression` property of a
/// [NamedExpression] then returns this [NamedExpression], otherwise returns
/// [expression].
-Expression? stepUpNamedExpression(Expression? expression) {
+Expression stepUpNamedExpression(Expression expression) {
if (expression != null) {
var parent = expression.parent;
if (parent is NamedExpression && parent.expression == expression) {
To view, visit change 194880. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brian Wilkerson.
go/dart-cbuild result: SUCCESS
Details: https://goto.google.com/dart-cbuild/find/0fe9f4d94bedbef68cca0be72472296bf43d45b5
3 comments:
File pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.dart:
Patch Set #2, Line 31: declaredElement!.supertype
Given that we compare the results to `null` on the next line, consider using `?.` rather than a null check.
File pkg/analysis_server/lib/src/services/correction/dart/join_if_with_inner.dart:
Patch Set #2, Line 68: _enclosingIfStatement
Consider making this either (a) a utility method in a superclass or (b) an extension method on `AstNode` so that it can be shared rather than duplicated.
File pkg/analysis_server/lib/src/services/correction/util.dart:
Patch Set #2, Line 487: parent.expression == expression
`NamedExpression` only has one child that can be an `Expression`, so this part of the condition is unnecessary. No problem if you don't want to change it, but I noticed it.
To view, visit change 194880. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Konstantin Shcheglov.
go/dart-cbuild result: SUCCESS
Details: https://goto.google.com/dart-cbuild/find/feb176137758ccad9246d7cd5f6ed7625efb048f
Patch set 3:Commit-Queue +2
3 comments:
File pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.dart:
Patch Set #2, Line 31: declaredElement!.supertype
Given that we compare the results to `null` on the next line, consider using `?. […]
Done
File pkg/analysis_server/lib/src/services/correction/dart/join_if_with_inner.dart:
Patch Set #2, Line 68: _enclosingIfStatement
Consider making this either (a) a utility method in a superclass or (b) an extension method on `AstN […]
Done
File pkg/analysis_server/lib/src/services/correction/util.dart:
Patch Set #2, Line 487: parent.expression == expression
`NamedExpression` only has one child that can be an `Expression`, so this part of the condition is u […]
Right, `Label` is not an expression.
Done, thank you.
To view, visit change 194880. To unsubscribe, or for help writing mail filters, visit settings.
CQ is trying the patch.
Note: The patchset #3 "Fixes for review comments." sent to CQ was uploaded after this CL was CR+1-ed.
Reviewer, please verify there is nothing unexpected https://dart-review.googlesource.com/c/194880/3
Bot data: {"action": "start", "triggered_at": "2021-04-10T00:19:30.0Z", "revision": "982b77ffa47b938c234ab6e5c597d2921b085ca4"}
commi...@chromium.org submitted this change.
Migrate several lib/src/services/correction/dart/
R=brianwi...@google.com
Change-Id: Ie38a28ee77c2faeb50f9c46e54a64cef933e3916
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194880
Commit-Queue: Konstantin Shcheglov <sche...@google.com>
Reviewed-by: Brian Wilkerson <brianwi...@google.com>
M pkg/analysis_server/lib/src/utilities/extensions/ast.dart
39 files changed, 525 insertions(+), 415 deletions(-)
index 62a920e..f8788d4 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.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';
@@ -28,8 +26,12 @@
if (classDeclaration == null) {
return;
}
+
var className = classDeclaration.name.name;
- var superType = classDeclaration.declaredElement.supertype;
+ var superType = classDeclaration.declaredElement?.supertype;index eb35756..c2e0711 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/inline_invocation.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/inline_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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -32,35 +30,42 @@// TODO(brianwilkerson) Consider extending this to handle set literals.
return;
}
- ListLiteral list = target;
var argument = invocation.argumentList.arguments[0];
var elementText = utils.getNodeText(argument);
await builder.addDartFileEdit(file, (builder) {
- if (list.elements.isNotEmpty) {
+ if (target.elements.isNotEmpty) {
// ['a']..add(e);
- builder.addSimpleInsertion(list.elements.last.end, ', $elementText');
+ builder.addSimpleInsertion(target.elements.last.end, ', $elementText');
} else {
// []..add(e);
- builder.addSimpleInsertion(list.leftBracket.end, elementText);
+ builder.addSimpleInsertion(target.leftBracket.end, elementText);
}
builder.addDeletion(range.node(invocation));
});
index 00337c6..9b123a07 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_inner.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_inner.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/util.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';
@@ -19,25 +18,20 @@
@override
Future<void> compute(ChangeBuilder builder) async {
// climb up condition to the (supposedly) "if" statement
- var node = this.node;
- while (node is Expression) {
- node = node.parent;
- }
+ var targetIfStatement = node.enclosingIfStatement;
// prepare target "if" statement
- if (node is! IfStatement) {
+ if (targetIfStatement == null) {
return;
}
- var targetIfStatement = node as IfStatement;
if (targetIfStatement.elseStatement != null) {
return;
}
// prepare inner "if" statement
var targetThenStatement = targetIfStatement.thenStatement;
- var innerStatement = getSingleStatement(targetThenStatement);
- if (innerStatement is! IfStatement) {
+ var innerIfStatement = getSingleStatement(targetThenStatement);
+ if (innerIfStatement is! IfStatement) {
return;
}
- var innerIfStatement = innerStatement as IfStatement;
if (innerIfStatement.elseStatement != null) {
return;
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_outer.dart b/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_outer.dart
index 22761fe..5d17b46 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_outer.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/join_if_with_outer.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/util.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';
@@ -19,22 +18,18 @@
@override
Future<void> compute(ChangeBuilder builder) async {
// climb up condition to the (supposedly) "if" statement
- var node = this.node;
- while (node is Expression) {
- node = node.parent;
- }
+ var targetIfStatement = node.enclosingIfStatement;
// prepare target "if" statement
- if (node is! IfStatement) {
+ if (targetIfStatement == null) {
return;
}
- var targetIfStatement = node as IfStatement;
if (targetIfStatement.elseStatement != null) {
return;
}
// prepare outer "if" statement
var parent = targetIfStatement.parent;
if (parent is Block) {
- if ((parent as Block).statements.length != 1) {
+ if (parent.statements.length != 1) {
return;
}
parent = parent.parent;
@@ -42,7 +37,7 @@
if (parent is! IfStatement) {
return;
}
- var outerIfStatement = parent as IfStatement;
+ var outerIfStatement = parent;
if (outerIfStatement.elseStatement != null) {
return;
}
index 7480d17..c61a3fd 100644
--- a/pkg/analysis_server/lib/src/services/correction/util.dart
+++ b/pkg/analysis_server/lib/src/services/correction/util.dart
@@ -482,14 +482,9 @@
/// If the given [expression] is the `expression` property of a
/// [NamedExpression] then returns this [NamedExpression], otherwise returns
/// [expression].
-Expression? stepUpNamedExpression(Expression? expression) {
- if (expression != null) {
- var parent = expression.parent;
- if (parent is NamedExpression && parent.expression == expression) {
- return parent;
- }
- }
- return expression;
+Expression stepUpNamedExpression(Expression expression) {
+ var parent = expression.parent;
+ return parent is NamedExpression ? parent : expression;
}
/// Return `true` if the given [lists] are identical at the given [position].
diff --git a/pkg/analysis_server/lib/src/utilities/extensions/ast.dart b/pkg/analysis_server/lib/src/utilities/extensions/ast.dart
index 3448861..92403c0 100644
--- a/pkg/analysis_server/lib/src/utilities/extensions/ast.dart
+++ b/pkg/analysis_server/lib/src/utilities/extensions/ast.dart
@@ -62,6 +62,17 @@
bool get inWhileLoop => thisOrAncestorOfType<WhileStatement>() != null;
+ /// Return the [IfStatement] associated with `this`.
+ IfStatement? get enclosingIfStatement {
+ for (var node in withParents) {
+ if (node is IfStatement) {
+ return node;
+ } else if (node is! Expression) {
+ return null;
+ }
+ }
+ }
+
/// Return this node and all its parents.
Iterable<AstNode> get withParents sync* {
var current = this;
To view, visit change 194880. To unsubscribe, or for help writing mail filters, visit settings.
go/dart-cbuild result: SUCCESS
Details: https://goto.google.com/dart-cbuild/find/7367c980a7225d223154f6d3f323ed0727acfa7b
Attention is currently required from: Brian Wilkerson.
To view, visit change 194962. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brian Wilkerson.
Konstantin Shcheglov would like Brian Wilkerson to review this change.
Migrate several lib/src/services/correction/dart/
Change-Id: I49f43e801f13dbbfe67be79fea19073990acb658
---
M pkg/analysis_server/lib/src/services/correction/dart/make_class_abstract.dart
M pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart
M pkg/analysis_server/lib/src/services/correction/dart/make_final.dart
M pkg/analysis_server/lib/src/services/correction/dart/make_return_type_nullable.dart
M pkg/analysis_server/lib/src/services/correction/dart/make_variable_not_final.dart
M pkg/analysis_server/lib/src/services/correction/dart/make_variable_nullable.dart
M pkg/analysis_server/lib/src/services/correction/dart/move_type_arguments_to_class.dart
M pkg/analysis_server/lib/src/services/correction/dart/organize_imports.dart
M pkg/analysis_server/lib/src/services/correction/dart/qualify_reference.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_argument.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_await.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_comparison.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_const.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_dead_code.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_dead_if_null.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_duplicate_case.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_empty_catch.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_empty_constructor_body.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_empty_else.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_empty_statement.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_if_null_operator.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_initializer.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_interpolation_braces.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_method_declaration.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_name_from_combinator.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_non_null_assertion.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_operator.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_parameters_in_getter_declaration.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_parentheses_in_getter_invocation.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_question_mark.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_this_expression.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_type_annotation.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_type_arguments.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_cast.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_new.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_parentheses.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_unnecessary_string_interpolation.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_unused_catch_clause.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_unused_catch_stack.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_unused_import.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_unused_label.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart
M pkg/analysis_server/lib/src/services/correction/dart/remove_unused_parameter.dart
45 files changed, 591 insertions(+), 506 deletions(-)
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..4a87296 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 to 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..97e5d0f 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..85c70ba 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`.
To view, visit change 194962. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brian Wilkerson.
go/dart-cbuild result: SUCCESS
Details: https://goto.google.com/dart-cbuild/find/0385372a5971f4a232f5f9eef6c7acb948716294
Attention is currently required from: Konstantin Shcheglov.
2 comments:
File pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart:
"to" --> "no"
File pkg/analysis_server/lib/src/services/correction/dart/remove_dead_code.dart:
In a previous CL I decided that I like the style in which any local variable that is just a copy of a field and has the same name as the field should be `final` to prevent anyone from writing to the local variable when they think they're writing to the field. I think it will help prevent errors while allowing us to not need to have multiple names for the same value. I don't know what you thought of it.
If you like that style I'm happy to make a clean-up pass later.
To view, visit change 194962. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Konstantin Shcheglov.
Patch set 1:Code-Review +1
To view, visit change 194962. To unsubscribe, or for help writing mail filters, visit settings.
2 comments:
File pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart:
"to" --> "no"
Done
File pkg/analysis_server/lib/src/services/correction/dart/remove_dead_code.dart:
In a previous CL I decided that I like the style in which any local variable that is just a copy of […]
Such style seems reasonable to me.
I will try to follow this in new code.
I will not make any changes in this place, and let you mass-replace it later.
To view, visit change 194962. To unsubscribe, or for help writing mail filters, visit settings.
Patch set 2:Commit-Queue +2
CQ is trying the patch.
Note: The patchset #2 "tweak" sent to CQ was uploaded after this CL was CR+1-ed.
Reviewer, please verify there is nothing unexpected https://dart-review.googlesource.com/c/194962/2
Bot data: {"action": "start", "triggered_at": "2021-04-10T22:02:18.0Z", "revision": "f4d7690cfdfec39d19591f6f2f3e7072f0055a4d"}
commi...@chromium.org submitted this change.
Migrate several lib/src/services/correction/dart/
Change-Id: I49f43e801f13dbbfe67be79fea19073990acb658
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194962
Commit-Queue: Konstantin Shcheglov <sche...@google.com>
Reviewed-by: Brian Wilkerson <brianwi...@google.com>
index ab137eb..a2c636a 100644+ // The variable must be not synthetic, and have no setter yet.To view, visit change 194962. To unsubscribe, or for help writing mail filters, visit settings.
go/dart-cbuild result: SUCCESS
Details: https://goto.google.com/dart-cbuild/find/e1539dc3d4d0d1fdc347542bddb55c520c6c7081
Attention is currently required from: Brian Wilkerson.
Konstantin Shcheglov has uploaded this change for review.
Migrate several lib/src/services/correction/dart/
R=brianwi...@google.com
Change-Id: I7957e3fa724e618e6cf8c6a94344b0b4057aa080
---
M pkg/analysis_server/lib/src/services/correction/dart/rename_to_camel_case.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_boolean_with_bool.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_cascade_with_dot.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_colon_with_equals.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_conditional_with_if_else.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_const.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_var.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_if_else_with_conditional.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_new_with_const.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_null_with_closure.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_return_type_future.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_var_with_dynamic.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_with_brackets.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_with_conditional_assignment.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_with_eight_digit_hex.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_with_extension_name.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_with_filled.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_with_identifier.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_with_interpolation.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_with_is_empty.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_with_not_null_aware.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_with_null_aware.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_with_tear_off.dart
M pkg/analysis_server/lib/src/services/correction/dart/replace_with_var.dart
M pkg/analysis_server/lib/src/services/correction/dart/shadow_field.dart
M pkg/analysis_server/lib/src/services/correction/dart/sort_child_property_last.dart
M pkg/analysis_server/lib/src/services/correction/dart/split_and_condition.dart
M pkg/analysis_server/lib/src/services/correction/dart/split_variable_declaration.dart
M pkg/analysis_server/lib/src/services/correction/dart/update_sdk_constraints.dart
M pkg/analysis_server/lib/src/services/correction/dart/use_const.dart
M pkg/analysis_server/lib/src/services/correction/dart/use_curly_braces.dart
M pkg/analysis_server/lib/src/services/correction/dart/use_effective_integer_division.dart
M pkg/analysis_server/lib/src/services/correction/dart/use_eq_eq_null.dart
M pkg/analysis_server/lib/src/services/correction/dart/use_is_not_empty.dart
M pkg/analysis_server/lib/src/services/correction/dart/use_not_eq_null.dart
M pkg/analysis_server/lib/src/services/correction/dart/use_rethrow.dart
M pkg/analysis_server/lib/src/services/correction/dart/wrap_in_text.dart
M pkg/analysis_server/lib/src/services/correction/util.dart
38 files changed, 536 insertions(+), 458 deletions(-)
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/rename_to_camel_case.dart b/pkg/analysis_server/lib/src/services/correction/dart/rename_to_camel_case.dart
index 7e61a0e..752dd51 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/rename_to_camel_case.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/rename_to_camel_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:analysis_server/src/services/correction/util.dart';
@@ -16,7 +14,7 @@
class RenameToCamelCase extends CorrectionProducer {
/// The camel-case version of the name.
- String _newName;
+ String _newName = '';
@override
List<Object> get fixArguments => [_newName];
@@ -29,10 +27,10 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is! SimpleIdentifier) {
+ var identifier = node;
+ if (identifier is! SimpleIdentifier) {
return;
}
- SimpleIdentifier identifier = node;
// Prepare the new name.
var words = identifier.name.split('_');
@@ -42,17 +40,21 @@
_newName = words.first + words.skip(1).map((w) => capitalize(w)).join();
// Find references to the identifier.
- List<SimpleIdentifier> references;
+ List<SimpleIdentifier>? references;
var element = identifier.staticElement;
if (element is LocalVariableElement) {
- AstNode root = node.thisOrAncestorOfType<Block>();
- references = findLocalElementReferences(root, element);
+ var root = node.thisOrAncestorOfType<Block>();
+ if (root != null) {
+ references = findLocalElementReferences(root, element);
+ }
} else if (element is ParameterElement) {
if (!element.isNamed) {
var root = node.thisOrAncestorMatching((node) =>
node.parent is ClassOrMixinDeclaration ||
node.parent is CompilationUnit);
- references = findLocalElementReferences(root, element);
+ if (root != null) {
+ references = findLocalElementReferences(root, element);
+ }
}
}
if (references == null) {
@@ -60,8 +62,9 @@
}
// Compute the change.
+ var references_final = references;
await builder.addDartFileEdit(file, (builder) {
- for (var reference in references) {
+ for (var reference in references_final) {
builder.addSimpleReplacement(range.node(reference), _newName);
}
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_boolean_with_bool.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_boolean_with_bool.dart
index 5039e9b7..c35c13f 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_boolean_with_bool.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_boolean_with_bool.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:analyzer/error/error.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';
@@ -19,8 +18,13 @@
@override
Future<void> compute(ChangeBuilder builder) async {
+ final analysisError = diagnostic;
+ if (analysisError is! AnalysisError) {
+ return;
+ }
+
await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleReplacement(range.error(diagnostic), 'bool');
+ builder.addSimpleReplacement(range.error(analysisError), 'bool');
});
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_cascade_with_dot.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_cascade_with_dot.dart
index 00d41c3..3a7b269 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_cascade_with_dot.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_cascade_with_dot.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,29 +29,35 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var node = this.node;
- if (node is CascadeExpression) {
- var sections = node.cascadeSections;
- if (sections.length == 1) {
- await _replaceFor(builder, sections[0]);
- }
+ final cascadeExpression = node;
+ if (cascadeExpression is! CascadeExpression) {
+ return;
+ }
+
+ var sections = cascadeExpression.cascadeSections;
+ if (sections.length == 1) {
+ await _replaceFor(builder, sections[0]);
}
}
- Future<void> _replaceFor(ChangeBuilder builder, Expression section) async {
+ Future<void> _replaceFor(ChangeBuilder builder, Expression? section) async {
if (section is AssignmentExpression) {
return _replaceFor(builder, section.leftHandSide);
}
if (section is IndexExpression) {
- if (section.period != null) {
- return _replaceToken(builder, section.period, _indexReplacement);
+ var period = section.period;
+ if (period != null) {
+ return _replaceToken(builder, period, _indexReplacement);
}
return _replaceFor(builder, section.target);
}
if (section is MethodInvocation) {
- return _replaceToken(builder, section.operator, _propertyReplacement);
+ var operator = section.operator;
+ if (operator != null) {
+ return _replaceToken(builder, operator, _propertyReplacement);
+ }
}
if (section is PropertyAccess) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_colon_with_equals.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_colon_with_equals.dart
index 4f9b235..f6adef4 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_colon_with_equals.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_colon_with_equals.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,12 +18,19 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is DefaultFormalParameter) {
- await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleReplacement(
- range.token((node as DefaultFormalParameter).separator), ' =');
- });
+ final node = this.node;
+ if (node is! DefaultFormalParameter) {
+ return;
}
+
+ var separator = node.separator;
+ if (separator == null) {
+ return;
+ }
+
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addSimpleReplacement(range.token(separator), ' =');
+ });
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_conditional_with_if_else.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_conditional_with_if_else.dart
index adb16a8..7dee07a 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_conditional_with_if_else.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_conditional_with_if_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:_fe_analyzer_shared/src/scanner/token.dart';
import 'package:analysis_server/src/services/correction/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
@@ -18,58 +16,98 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- ConditionalExpression conditional;
// may be on Statement with Conditional
var statement = node.thisOrAncestorOfType<Statement>();
if (statement == null) {
return;
}
- // variable declaration
- var inVariable = false;
- if (statement is VariableDeclarationStatement) {
- var variableStatement = statement;
- for (var variable in variableStatement.variables.variables) {
- if (variable.initializer is ConditionalExpression) {
- conditional = variable.initializer as ConditionalExpression;
- inVariable = true;
- break;
- }
- }
- }
- // assignment
- var inAssignment = false;
- if (statement is ExpressionStatement) {
- var exprStmt = statement;
- if (exprStmt.expression is AssignmentExpression) {
- var assignment = exprStmt.expression as AssignmentExpression;
- if (assignment.operator.type == TokenType.EQ &&
- assignment.rightHandSide is ConditionalExpression) {
- conditional = assignment.rightHandSide as ConditionalExpression;
- inAssignment = true;
- }
- }
- }
- // return
- var inReturn = false;
- if (statement is ReturnStatement) {
- var returnStatement = statement;
- if (returnStatement.expression is ConditionalExpression) {
- conditional = returnStatement.expression as ConditionalExpression;
- inReturn = true;
- }
- }
- // prepare environment
- var indent = utils.getIndent(1);
- var prefix = utils.getNodePrefix(statement);
- if (inVariable || inAssignment || inReturn) {
+ // Type v = conditional;
+ if (statement is VariableDeclarationStatement) {
+ return _variableDeclarationStatement(builder, statement);
+ }
+
+ // v = conditional;
+ if (statement is ExpressionStatement) {
+ var expression = statement.expression;
+ if (expression is AssignmentExpression) {
+ return _assignmentExpression(builder, statement, expression);
+ }
+ }
+
+ // return conditional;
+ if (statement is ReturnStatement) {
+ return _returnStatement(builder, statement);
+ }
+ }
+
+ Future<void> _assignmentExpression(
+ ChangeBuilder builder,
+ ExpressionStatement statement,
+ AssignmentExpression assignment,
+ ) async {
+ var conditional = assignment.rightHandSide;
+ if (assignment.operator.type == TokenType.EQ &&
+ conditional is ConditionalExpression) {
+ var indent = utils.getIndent(1);
+ var prefix = utils.getNodePrefix(statement);
+
await builder.addDartFileEdit(file, (builder) {
- // Type v = Conditional;
- if (inVariable) {
+ var leftSide = assignment.leftHandSide;
+ var conditionSrc = utils.getNodeText(conditional.condition);
+ var thenSrc = utils.getNodeText(conditional.thenExpression);
+ var elseSrc = utils.getNodeText(conditional.elseExpression);
+ var name = utils.getNodeText(leftSide);
+ var src = '';
+ src += 'if ($conditionSrc) {' + eol;
+ src += prefix + indent + '$name = $thenSrc;' + eol;
+ src += prefix + '} else {' + eol;
+ src += prefix + indent + '$name = $elseSrc;' + eol;
+ src += prefix + '}';
+ builder.addSimpleReplacement(range.node(statement), src);
+ });
+ }
+ }
+
+ Future<void> _returnStatement(
+ ChangeBuilder builder,
+ ReturnStatement statement,
+ ) async {
+ var conditional = statement.expression;
+ if (conditional is ConditionalExpression) {
+ var indent = utils.getIndent(1);
+ var prefix = utils.getNodePrefix(statement);
+
+ await builder.addDartFileEdit(file, (builder) {
+ var conditionSrc = utils.getNodeText(conditional.condition);
+ var thenSrc = utils.getNodeText(conditional.thenExpression);
+ var elseSrc = utils.getNodeText(conditional.elseExpression);
+ var src = '';
+ src += 'if ($conditionSrc) {' + eol;
+ src += prefix + indent + 'return $thenSrc;' + eol;
+ src += prefix + '} else {' + eol;
+ src += prefix + indent + 'return $elseSrc;' + eol;
+ src += prefix + '}';
+ builder.addSimpleReplacement(range.node(statement), src);
+ });
+ }
+ }
+
+ Future<void> _variableDeclarationStatement(
+ ChangeBuilder builder,
+ VariableDeclarationStatement statement,
+ ) async {
+ for (var variable in statement.variables.variables) {
+ var conditional = variable.initializer;
+ if (conditional is ConditionalExpression) {
+ var indent = utils.getIndent(1);
+ var prefix = utils.getNodePrefix(statement);
+
+ await builder.addDartFileEdit(file, (builder) {
var variable = conditional.parent as VariableDeclaration;
var variableList = variable.parent as VariableDeclarationList;
if (variableList.type == null) {
- var type = variable.declaredElement.type;
+ var type = variable.declaredElement!.type;
var keyword = variableList.keyword;
if (keyword != null && keyword.keyword == Keyword.VAR) {
builder.addReplacement(range.token(keyword), (builder) {
@@ -94,37 +132,8 @@
src += prefix + indent + '$name = $elseSrc;' + eol;
src += prefix + '}';
builder.addSimpleReplacement(range.endLength(statement, 0), src);
- }
- // v = Conditional;
- if (inAssignment) {
- var assignment = conditional.parent as AssignmentExpression;
- var leftSide = assignment.leftHandSide;
- var conditionSrc = utils.getNodeText(conditional.condition);
- var thenSrc = utils.getNodeText(conditional.thenExpression);
- var elseSrc = utils.getNodeText(conditional.elseExpression);
- var name = utils.getNodeText(leftSide);
- var src = '';
- src += 'if ($conditionSrc) {' + eol;
- src += prefix + indent + '$name = $thenSrc;' + eol;
- src += prefix + '} else {' + eol;
- src += prefix + indent + '$name = $elseSrc;' + eol;
- src += prefix + '}';
- builder.addSimpleReplacement(range.node(statement), src);
- }
- // return Conditional;
- if (inReturn) {
- var conditionSrc = utils.getNodeText(conditional.condition);
- var thenSrc = utils.getNodeText(conditional.thenExpression);
- var elseSrc = utils.getNodeText(conditional.elseExpression);
- var src = '';
- src += 'if ($conditionSrc) {' + eol;
- src += prefix + indent + 'return $thenSrc;' + eol;
- src += prefix + '} else {' + eol;
- src += prefix + indent + 'return $elseSrc;' + eol;
- src += prefix + '}';
- builder.addSimpleReplacement(range.node(statement), src);
- }
- });
+ });
+ }
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_const.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_const.dart
index 3a77351..76b2a8e 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_const.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_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';
@@ -20,11 +18,14 @@
@override
Future<void> compute(ChangeBuilder builder) async {
+ final node = this.node;
if (node is VariableDeclarationList) {
- await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleReplacement(
- range.token((node as VariableDeclarationList).keyword), 'const');
- });
+ var keyword = node.keyword;
+ if (keyword != null) {
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addSimpleReplacement(range.token(keyword), 'const');
+ });
+ }
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_var.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_var.dart
index 58a4a76..9371a8d 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_var.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_var.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,11 +18,12 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var target = node;
- if (target is VariableDeclarationList) {
- if (target.type == null) {
+ final node = this.node;
+ if (node is VariableDeclarationList) {
+ var keyword = node.keyword;
+ if (keyword != null && node.type == null) {
await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleReplacement(range.token(target.keyword), 'var');
+ builder.addSimpleReplacement(range.token(keyword), 'var');
});
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_if_else_with_conditional.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_if_else_with_conditional.dart
index 9224921..85e9599 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_if_else_with_conditional.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_if_else_with_conditional.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_new_with_const.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_new_with_const.dart
index 9a9cd77..e8db9a3 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_new_with_const.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_new_with_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';
@@ -20,7 +18,7 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var node = this.node;
+ AstNode? node = this.node;
if (node is ConstructorName) {
node = node.parent;
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_null_with_closure.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_null_with_closure.dart
index 39aabc2..9da2766 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_null_with_closure.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_null_with_closure.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,13 +20,14 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- AstNode nodeToFix;
+ AstNode? nodeToFix;
var parameters = const <ParameterElement>[];
+
+ final coveredNode = this.coveredNode;
if (coveredNode is NamedExpression) {
- NamedExpression namedExpression = coveredNode;
- var expression = namedExpression.expression;
+ var expression = coveredNode.expression;
if (expression is NullLiteral) {
- var element = namedExpression.element;
+ var element = coveredNode.element;
if (element is ParameterElement) {
var type = element.type;
if (type is FunctionType) {
@@ -41,14 +40,17 @@
nodeToFix = coveredNode;
}
- if (nodeToFix != null) {
- await builder.addDartFileEdit(file, (builder) {
- builder.addReplacement(range.node(nodeToFix), (builder) {
- builder.writeParameters(parameters);
- builder.write(' => null');
- });
- });
+ if (nodeToFix == null) {
+ return;
}
+
+ final nodeToFix_final = nodeToFix;
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addReplacement(range.node(nodeToFix_final), (builder) {
+ builder.writeParameters(parameters);
+ builder.write(' => null');
+ });
+ });
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_return_type_future.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_return_type_future.dart
index 3d0000a..c19cf4a 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_return_type_future.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_return_type_future.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,10 @@
Future<void> compute(ChangeBuilder builder) async {
// prepare the existing type
var typeName = node.thisOrAncestorOfType<TypeAnnotation>();
- var typeProvider = this.typeProvider;
+ if (typeName == null) {
+ return;
+ }
+
await builder.addDartFileEdit(file, (builder) {
builder.replaceTypeWithFuture(typeName, typeProvider);
});
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_var_with_dynamic.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_var_with_dynamic.dart
index 3da975f..1991391 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_var_with_dynamic.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_var_with_dynamic.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:analyzer/error/error.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';
@@ -16,9 +15,12 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleReplacement(range.error(diagnostic), 'dynamic');
- });
+ final diagnostic = this.diagnostic;
+ if (diagnostic is AnalysisError) {
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addSimpleReplacement(range.error(diagnostic), 'dynamic');
+ });
+ }
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_brackets.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_brackets.dart
index 83f4caf..eb4fc02 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_brackets.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_brackets.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/replace_with_conditional_assignment.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_conditional_assignment.dart
index e7dfbd2..9d69d50 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_conditional_assignment.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_conditional_assignment.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';
@@ -21,20 +19,14 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- IfStatement ifStatement =
+ var node = this.node;
+ var ifStatement =
node is IfStatement ? node : node.thisOrAncestorOfType<IfStatement>();
if (ifStatement == null) {
return;
}
- var thenStatement = ifStatement.thenStatement;
- Statement uniqueStatement(Statement statement) {
- if (statement is Block) {
- return uniqueStatement(statement.statements.first);
- }
- return statement;
- }
- thenStatement = uniqueStatement(thenStatement);
+ var thenStatement = _uniqueStatement(ifStatement.thenStatement);
if (thenStatement is ExpressionStatement) {
final expression = thenStatement.expression.unParenthesized;
if (expression is AssignmentExpression) {
@@ -53,4 +45,11 @@
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static ReplaceWithConditionalAssignment newInstance() =>
ReplaceWithConditionalAssignment();
+
+ static Statement _uniqueStatement(Statement statement) {
+ if (statement is Block) {
+ return _uniqueStatement(statement.statements.first);
+ }
+ return statement;
+ }
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_eight_digit_hex.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_eight_digit_hex.dart
index d0c7229..ff9e2eb 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_eight_digit_hex.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_eight_digit_hex.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 @@
class ReplaceWithEightDigitHex extends CorrectionProducer {
/// The replacement text, used as an argument to the fix message.
- String _replacement;
+ String _replacement = '';
@override
List<Object> get fixArguments => [_replacement];
@@ -33,6 +31,9 @@
return;
}
var value = (node as IntegerLiteral).value;
+ if (value == null) {
+ return;
+ }
_replacement = '0x' + value.toRadixString(16).padLeft(8, '0');
//
// Build the edit.
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_extension_name.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_extension_name.dart
index ed7fa48..6bac2bd 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_extension_name.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_extension_name.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';
@@ -12,7 +10,7 @@
import 'package:analyzer_plugin/utilities/range_factory.dart';
class ReplaceWithExtensionName extends CorrectionProducer {
- String _extensionName;
+ String _extensionName = '';
@override
List<Object> get fixArguments => [_extensionName];
@@ -35,7 +33,7 @@
}
}
- AstNode _getTarget(AstNode invocation) {
+ AstNode? _getTarget(AstNode? invocation) {
if (invocation is MethodInvocation && node == invocation.methodName) {
return invocation.target;
} else if (invocation is PropertyAccess &&
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_filled.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_filled.dart
index fc40853..860e620 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_filled.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_filled.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/replace_with_identifier.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_identifier.dart
index 33d0b66..9b30817 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_identifier.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_identifier.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/replace_with_interpolation.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_interpolation.dart
index 77aa027..639f53f 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_interpolation.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_interpolation.dart
@@ -2,16 +2,14 @@
// 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/ast/token.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';
-import 'package:meta/meta.dart';
class ReplaceWithInterpolation extends CorrectionProducer {
@override
@@ -22,9 +20,9 @@
//
// Validate the fix.
//
- BinaryExpression binary;
- var candidate = node;
- while (_isStringConcatenation(candidate)) {
+ BinaryExpression? binary;
+ AstNode? candidate = node;
+ while (candidate is BinaryExpression && _isStringConcatenation(candidate)) {
binary = candidate;
candidate = candidate.parent;
}
@@ -43,8 +41,9 @@
//
// Build the edit.
//
+ final binary_final = binary;
await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleReplacement(range.node(binary), interpolation);
+ builder.addSimpleReplacement(range.node(binary_final), interpolation);
});
}
@@ -82,9 +81,11 @@
return leftStyle;
}
return leftStyle == rightStyle ? leftStyle : _StringStyle.invalid;
- } else if (expression is MethodInvocation &&
- expression.methodName.name == 'toString') {
- return _extractComponentsInto(expression.target, components);
+ } else if (expression is MethodInvocation) {
+ var target = expression.target;
+ if (target != null && expression.methodName.name == 'toString') {
+ return _extractComponentsInto(target, components);
+ }
} else if (expression is ParenthesizedExpression) {
return _extractComponentsInto(expression.expression, components);
}
@@ -95,8 +96,8 @@
bool _isStringConcatenation(AstNode node) =>
node is BinaryExpression &&
node.operator.type == TokenType.PLUS &&
- node.leftOperand.staticType.isDartCoreString &&
- node.rightOperand.staticType.isDartCoreString;
+ node.leftOperand.typeOrThrow.isDartCoreString &&
+ node.rightOperand.typeOrThrow.isDartCoreString;
String _mergeComponents(_StringStyle style, List<AstNode> components) {
var quotes = style.quotes;
@@ -161,10 +162,11 @@
final int state;
- factory _StringStyle(
- {@required bool multiline,
- @required bool raw,
- @required bool singleQuoted}) {
+ factory _StringStyle({
+ required bool multiline,
+ required bool raw,
+ required bool singleQuoted,
+ }) {
return _StringStyle._((multiline ? multilineBit : 0) +
(raw ? rawBit : 0) +
(singleQuoted ? singleQuotedBit : 0));
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_is_empty.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_is_empty.dart
index 9c02fda..96b25d6 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_is_empty.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_is_empty.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';
@@ -14,106 +12,28 @@
class ReplaceWithIsEmpty extends CorrectionProducer {
@override
- FixKind fixKind;
+ FixKind fixKind = DartFixKind.REPLACE_WITH_IS_EMPTY;
@override
- FixKind multiFixKind;
+ FixKind multiFixKind = DartFixKind.REPLACE_WITH_IS_EMPTY_MULTI;
@override
Future<void> compute(ChangeBuilder builder) async {
- /// Return the value of an integer literal or prefix expression with a
- /// minus and then an integer literal. For anything else, returns `null`.
- int getIntValue(Expression expressions) {
- // Copied from package:linter/src/rules/prefer_is_empty.dart.
- if (expressions is IntegerLiteral) {
- return expressions.value;
- } else if (expressions is PrefixExpression) {
- var operand = expressions.operand;
- if (expressions.operator.type == TokenType.MINUS &&
- operand is IntegerLiteral) {
- return -operand.value;
- }
- }
- return null;
- }
-
- /// Return the expression producing the object on which `length` is being
- /// invoked, or `null` if there is no such expression.
- Expression getLengthTarget(Expression expression) {
- if (expression is PropertyAccess &&
- expression.propertyName.name == 'length') {
- return expression.target;
- } else if (expression is PrefixedIdentifier &&
- expression.identifier.name == 'length') {
- return expression.prefix;
- }
- return null;
- }
-
var binary = node.thisOrAncestorOfType<BinaryExpression>();
- var operator = binary.operator.type;
- String getter;
- Expression lengthTarget;
- var rightValue = getIntValue(binary.rightOperand);
- if (rightValue != null) {
- lengthTarget = getLengthTarget(binary.leftOperand);
- if (rightValue == 0) {
- if (operator == TokenType.EQ_EQ || operator == TokenType.LT_EQ) {
- getter = 'isEmpty';
- fixKind = DartFixKind.REPLACE_WITH_IS_EMPTY;
- multiFixKind = DartFixKind.REPLACE_WITH_IS_EMPTY_MULTI;
- } else if (operator == TokenType.GT || operator == TokenType.BANG_EQ) {
- getter = 'isNotEmpty';
- fixKind = DartFixKind.REPLACE_WITH_IS_NOT_EMPTY;
- multiFixKind = DartFixKind.REPLACE_WITH_IS_NOT_EMPTY_MULTI;
- }
- } else if (rightValue == 1) {
- // 'length >= 1' is same as 'isNotEmpty',
- // and 'length < 1' is same as 'isEmpty'
- if (operator == TokenType.GT_EQ) {
- getter = 'isNotEmpty';
- fixKind = DartFixKind.REPLACE_WITH_IS_NOT_EMPTY;
- multiFixKind = DartFixKind.REPLACE_WITH_IS_NOT_EMPTY_MULTI;
- } else if (operator == TokenType.LT) {
- getter = 'isEmpty';
- fixKind = DartFixKind.REPLACE_WITH_IS_EMPTY;
- multiFixKind = DartFixKind.REPLACE_WITH_IS_EMPTY_MULTI;
- }
- }
- } else {
- var leftValue = getIntValue(binary.leftOperand);
- if (leftValue != null) {
- lengthTarget = getLengthTarget(binary.rightOperand);
- if (leftValue == 0) {
- if (operator == TokenType.EQ_EQ || operator == TokenType.GT_EQ) {
- getter = 'isEmpty';
- fixKind = DartFixKind.REPLACE_WITH_IS_EMPTY;
- multiFixKind = DartFixKind.REPLACE_WITH_IS_EMPTY_MULTI;
- } else if (operator == TokenType.LT ||
- operator == TokenType.BANG_EQ) {
- getter = 'isNotEmpty';
- fixKind = DartFixKind.REPLACE_WITH_IS_NOT_EMPTY;
- multiFixKind = DartFixKind.REPLACE_WITH_IS_NOT_EMPTY_MULTI;
- }
- } else if (leftValue == 1) {
- // '1 <= length' is same as 'isNotEmpty',
- // and '1 > length' is same as 'isEmpty'
- if (operator == TokenType.LT_EQ) {
- getter = 'isNotEmpty';
- fixKind = DartFixKind.REPLACE_WITH_IS_NOT_EMPTY;
- multiFixKind = DartFixKind.REPLACE_WITH_IS_NOT_EMPTY_MULTI;
- } else if (operator == TokenType.GT) {
- getter = 'isEmpty';
- fixKind = DartFixKind.REPLACE_WITH_IS_EMPTY;
- multiFixKind = DartFixKind.REPLACE_WITH_IS_EMPTY_MULTI;
- }
- }
- }
- }
- if (lengthTarget == null || getter == null || fixKind == null) {
+ if (binary == null) {
return;
}
- var target = utils.getNodeText(lengthTarget);
+
+ var replacement = _analyzeBinaryExpression(binary);
+ if (replacement == null) {
+ return;
+ }
+
+ fixKind = replacement.fixKind;
+ multiFixKind = replacement.multiFixKind;
+
+ var target = utils.getNodeText(replacement.lengthTarget);
+ var getter = replacement.getter;
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(range.node(binary), '$target.$getter');
});
@@ -121,4 +41,117 @@
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static ReplaceWithIsEmpty newInstance() => ReplaceWithIsEmpty();
+
+ static _Replacement? _analyzeBinaryExpression(BinaryExpression binary) {
+ var operator = binary.operator.type;
+ var rightValue = _getIntValue(binary.rightOperand);
+ if (rightValue != null) {
+ var lengthTarget = _getLengthTarget(binary.leftOperand);
+ if (lengthTarget == null) {
+ return null;
+ }
+ if (rightValue == 0) {
+ if (operator == TokenType.EQ_EQ || operator == TokenType.LT_EQ) {
+ return _Replacement.isEmpty(lengthTarget);
+ } else if (operator == TokenType.GT || operator == TokenType.BANG_EQ) {
+ return _Replacement.isNotEmpty(lengthTarget);
+ }
+ } else if (rightValue == 1) {
+ // 'length >= 1' is same as 'isNotEmpty',
+ // and 'length < 1' is same as 'isEmpty'
+ if (operator == TokenType.GT_EQ) {
+ return _Replacement.isNotEmpty(lengthTarget);
+ } else if (operator == TokenType.LT) {
+ return _Replacement.isEmpty(lengthTarget);
+ }
+ }
+ } else {
+ var leftValue = _getIntValue(binary.leftOperand);
+ if (leftValue != null) {
+ var lengthTarget = _getLengthTarget(binary.rightOperand);
+ if (lengthTarget == null) {
+ return null;
+ }
+ if (leftValue == 0) {
+ if (operator == TokenType.EQ_EQ || operator == TokenType.GT_EQ) {
+ return _Replacement.isEmpty(lengthTarget);
+ } else if (operator == TokenType.LT ||
+ operator == TokenType.BANG_EQ) {
+ return _Replacement.isNotEmpty(lengthTarget);
+ }
+ } else if (leftValue == 1) {
+ // '1 <= length' is same as 'isNotEmpty',
+ // and '1 > length' is same as 'isEmpty'
+ if (operator == TokenType.LT_EQ) {
+ return _Replacement.isNotEmpty(lengthTarget);
+ } else if (operator == TokenType.GT) {
+ return _Replacement.isEmpty(lengthTarget);
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ /// Return the value of an integer literal or prefix expression with a
+ /// minus and then an integer literal. For anything else, returns `null`.
+ static int? _getIntValue(Expression expressions) {
+ // Copied from package:linter/src/rules/prefer_is_empty.dart.
+ if (expressions is IntegerLiteral) {
+ return expressions.value;
+ } else if (expressions is PrefixExpression) {
+ var operand = expressions.operand;
+ if (expressions.operator.type == TokenType.MINUS &&
+ operand is IntegerLiteral) {
+ var value = operand.value;
+ if (value != null) {
+ return -value;
+ }
+ }
+ }
+ return null;
+ }
+
+ /// Return the expression producing the object on which `length` is being
+ /// invoked, or `null` if there is no such expression.
+ static Expression? _getLengthTarget(Expression expression) {
+ if (expression is PropertyAccess &&
+ expression.propertyName.name == 'length') {
+ return expression.target;
+ } else if (expression is PrefixedIdentifier &&
+ expression.identifier.name == 'length') {
+ return expression.prefix;
+ }
+ return null;
+ }
+}
+
+class _Replacement {
+ final FixKind fixKind;
+ final FixKind multiFixKind;
+ final String getter;
+ final Expression lengthTarget;
+
+ _Replacement.isEmpty(Expression lengthTarget)
+ : this._(
+ fixKind: DartFixKind.REPLACE_WITH_IS_EMPTY,
+ multiFixKind: DartFixKind.REPLACE_WITH_IS_EMPTY_MULTI,
+ getter: 'isEmpty',
+ lengthTarget: lengthTarget,
+ );
+
+ _Replacement.isNotEmpty(Expression lengthTarget)
+ : this._(
+ fixKind: DartFixKind.REPLACE_WITH_IS_NOT_EMPTY,
+ multiFixKind: DartFixKind.REPLACE_WITH_IS_NOT_EMPTY_MULTI,
+ getter: 'isNotEmpty',
+ lengthTarget: lengthTarget,
+ );
+
+ _Replacement._({
+ required this.fixKind,
+ required this.multiFixKind,
+ required this.getter,
+ required this.lengthTarget,
+ });
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_not_null_aware.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_not_null_aware.dart
index 4cc14c0..a46a17e 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_not_null_aware.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_not_null_aware.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';
@@ -14,7 +12,7 @@
class ReplaceWithNotNullAware extends CorrectionProducer {
/// The operator that will replace the existing operator.
- String _newOperator;
+ String _newOperator = '';
@override
List<Object> get fixArguments => [_newOperator];
@@ -26,11 +24,13 @@
Future<void> compute(ChangeBuilder builder) async {
var node = coveredNode;
if (node is MethodInvocation) {
- _newOperator =
- node.operator.type == TokenType.QUESTION_PERIOD ? '.' : '..';
- await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleReplacement(range.token(node.operator), _newOperator);
- });
+ var operator = node.operator;
+ if (operator != null) {
+ _newOperator = operator.type == TokenType.QUESTION_PERIOD ? '.' : '..';
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addSimpleReplacement(range.token(operator), _newOperator);
+ });
+ }
} else if (node is PropertyAccess) {
_newOperator =
node.operator.type == TokenType.QUESTION_PERIOD ? '.' : '..';
@@ -38,15 +38,17 @@
builder.addSimpleReplacement(range.token(node.operator), _newOperator);
});
} else if (node is IndexExpression) {
- if (node.period != null) {
+ var period = node.period;
+ var question = node.question;
+ if (period != null) {
_newOperator = '..';
await builder.addDartFileEdit(file, (builder) {
- builder.addSimpleReplacement(range.token(node.period), '..');
+ builder.addSimpleReplacement(range.token(period), '..');
});
- } else if (node.question != null) {
+ } else if (question != null) {
_newOperator = '[';
await builder.addDartFileEdit(file, (builder) {
- builder.addDeletion(range.token(node.question));
+ builder.addDeletion(range.token(question));
});
}
} else if (node is SpreadElement) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_null_aware.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_null_aware.dart
index 0f36a39..c6b7dd1 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_null_aware.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_null_aware.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';
@@ -19,18 +17,22 @@
Future<void> compute(ChangeBuilder builder) async {
var node = coveredNode;
if (node is Expression) {
+ final node_final = node;
await builder.addDartFileEdit(file, (builder) {
- var parent = node.parent;
+ var parent = node_final.parent;
while (parent != null) {
if (parent is MethodInvocation && parent.target == node) {
- builder.addSimpleReplacement(range.token(parent.operator), '?.');
+ var operator = parent.operator;
+ if (operator != null) {
+ builder.addSimpleReplacement(range.token(operator), '?.');
+ }
} else if (parent is PropertyAccess && parent.target == node) {
builder.addSimpleReplacement(range.token(parent.operator), '?.');
} else {
break;
}
node = parent;
- parent = node.parent;
+ parent = node?.parent;
}
});
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_tear_off.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_tear_off.dart
index 44df3ff..14d77c2 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_tear_off.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_tear_off.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,16 +22,22 @@
if (ancestor == null) {
return;
}
- Future<void> addFixOfExpression(InvocationExpression expression) async {
- await builder.addDartFileEdit(file, (builder) {
- builder.addReplacement(range.node(ancestor), (builder) {
- if (expression is MethodInvocation && expression.target != null) {
- builder.write(utils.getNodeText(expression.target));
- builder.write('.');
- }
- builder.write(utils.getNodeText(expression.function));
+
+ Future<void> addFixOfExpression(Expression? expression) async {
+ if (expression is InvocationExpression) {
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addReplacement(range.node(ancestor), (builder) {
+ if (expression is MethodInvocation) {
+ var target = expression.target;
+ if (target != null) {
+ builder.write(utils.getNodeText(target));
+ builder.write('.');
+ }
+ }
+ builder.write(utils.getNodeText(expression.function));
+ });
});
- });
+ }
}
final body = ancestor.body;
@@ -47,7 +51,7 @@
await addFixOfExpression(expression.unParenthesized);
} else if (statement is ReturnStatement) {
final expression = statement.expression;
- await addFixOfExpression(expression.unParenthesized);
+ await addFixOfExpression(expression?.unParenthesized);
}
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_var.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_var.dart
index dcdee93..1c6da82 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_var.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_var.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/assist.dart';
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/src/dart/ast/extensions.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -44,25 +43,28 @@
return;
}
var initializer = variables[0].initializer;
- String typeArgumentsText;
- int typeArgumentsOffset;
- if (type is NamedType && type.typeArguments != null) {
- if (initializer is CascadeExpression) {
- initializer = (initializer as CascadeExpression).target;
- }
- if (initializer is TypedLiteral) {
- if (initializer.typeArguments == null) {
- typeArgumentsText = utils.getNodeText(type.typeArguments);
- if (initializer is ListLiteral) {
- typeArgumentsOffset = initializer.leftBracket.offset;
- } else if (initializer is SetOrMapLiteral) {
- typeArgumentsOffset = initializer.leftBracket.offset;
- }
+ String? typeArgumentsText;
+ int? typeArgumentsOffset;
+ if (type is NamedType) {
+ var typeArguments = type.typeArguments;
+ if (typeArguments != null) {
+ if (initializer is CascadeExpression) {
+ initializer = initializer.target;
}
- } else if (initializer is InstanceCreationExpression) {
- if (initializer.constructorName.type.typeArguments == null) {
- typeArgumentsText = utils.getNodeText(type.typeArguments);
- typeArgumentsOffset = initializer.constructorName.type.end;
+ if (initializer is TypedLiteral) {
+ if (initializer.typeArguments == null) {
+ typeArgumentsText = utils.getNodeText(typeArguments);
+ if (initializer is ListLiteral) {
+ typeArgumentsOffset = initializer.leftBracket.offset;
+ } else if (initializer is SetOrMapLiteral) {
+ typeArgumentsOffset = initializer.leftBracket.offset;
+ }
+ }
+ } else if (initializer is InstanceCreationExpression) {
+ if (initializer.constructorName.type.typeArguments == null) {
+ typeArgumentsText = utils.getNodeText(typeArguments);
+ typeArgumentsOffset = initializer.constructorName.type.end;
+ }
}
}
}
@@ -80,19 +82,22 @@
} else {
builder.addSimpleReplacement(range.node(type), 'var');
}
- if (typeArgumentsText != null) {
+ if (typeArgumentsText != null && typeArgumentsOffset != null) {
builder.addSimpleInsertion(typeArgumentsOffset, typeArgumentsText);
}
});
} else if (parent is DeclaredIdentifier &&
grandparent is ForEachPartsWithDeclaration) {
- String typeArgumentsText;
- int typeArgumentsOffset;
- if (type is NamedType && type.typeArguments != null) {
- var iterable = grandparent.iterable;
- if (iterable is TypedLiteral && iterable.typeArguments == null) {
- typeArgumentsText = utils.getNodeText(type.typeArguments);
- typeArgumentsOffset = iterable.offset;
+ String? typeArgumentsText;
+ int? typeArgumentsOffset;
+ if (type is NamedType) {
+ var typeArguments = type.typeArguments;
+ if (typeArguments != null) {
+ var iterable = grandparent.iterable;
+ if (iterable is TypedLiteral && iterable.typeArguments == null) {
+ typeArgumentsText = utils.getNodeText(typeArguments);
+ typeArgumentsOffset = iterable.offset;
+ }
}
}
await builder.addDartFileEdit(file, (builder) {
@@ -101,7 +106,7 @@
} else {
builder.addSimpleReplacement(range.node(type), 'var');
}
- if (typeArgumentsText != null) {
+ if (typeArgumentsText != null && typeArgumentsOffset != null) {
builder.addSimpleInsertion(typeArgumentsOffset, typeArgumentsText);
}
});
@@ -110,7 +115,7 @@
/// Return `true` if the type in the [node] can be replaced with `var`.
bool _canConvertVariableDeclarationList(VariableDeclarationList node) {
- final staticType = node?.type?.type;
+ final staticType = node.type?.type;
if (staticType == null || staticType.isDynamic) {
return false;
}
@@ -137,10 +142,10 @@
if (staticType == null || staticType.isDynamic) {
return false;
}
- final iterableType = parent.iterable.staticType;
+ final iterableType = parent.iterable.typeOrThrow;
var instantiatedType =
iterableType.asInstanceOf(typeProvider.iterableElement);
- if (instantiatedType?.typeArguments?.first == staticType) {
+ if (instantiatedType?.typeArguments.first == staticType) {
return true;
}
return false;
@@ -152,7 +157,7 @@
/// Using the [node] as a starting point, return the type annotation that is
/// to be replaced, or `null` if there is no type annotation.
- TypeAnnotation _findType(AstNode node) {
+ TypeAnnotation? _findType(AstNode node) {
if (node is VariableDeclarationList) {
return node.type;
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/shadow_field.dart b/pkg/analysis_server/lib/src/services/correction/dart/shadow_field.dart
index 8f4de40..c46f159 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/shadow_field.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/shadow_field.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
@@ -20,34 +18,45 @@
@override
Future<void> compute(ChangeBuilder builder) async {
+ final node = this.node;
if (node is! SimpleIdentifier) {
return;
}
- var element = (node as SimpleIdentifier).writeOrReadElement;
- if (element is! PropertyAccessorElement) {
+
+ var accessor = node.writeOrReadElement;
+ if (accessor is! PropertyAccessorElement) {
return;
}
- var accessor = element as PropertyAccessorElement;
+
if (!accessor.isGetter || accessor.enclosingElement is! ClassElement) {
// TODO(brianwilkerson) Should we also require that the getter be synthetic?
return;
}
+
var statement = _getStatement();
if (statement == null) {
return;
}
- if (statement.parent is! Block) {
+
+ var enclosingBlock = statement.parent;
+ if (enclosingBlock is! Block) {
// TODO(brianwilkerson) Support adding a block between the statement and
// its parent (where the parent will be something like a while or if
// statement). Also support the case where the parent is a case clause.
return;
}
- var enclosingBlock = statement.parent as Block;
- var finder = _ReferenceFinder(accessor.correspondingSetter);
+
+ var correspondingSetter = accessor.correspondingSetter;
+ if (correspondingSetter == null) {
+ return;
+ }
+
+ var finder = _ReferenceFinder(correspondingSetter);
enclosingBlock.accept(finder);
if (finder.hasSetterReference) {
return;
}
+
var fieldName = accessor.name;
var offset = statement.offset;
var prefix = utils.getLinePrefix(offset);
@@ -74,13 +83,13 @@
/// Return the statement immediately enclosing the [node] that would promote
/// the type of the field if it were replaced by a local variable.
- Statement _getStatement() {
+ Statement? _getStatement() {
var parent = node.parent;
- Statement enclosingIf(Expression expression) {
+ Statement? enclosingIf(Expression expression) {
var parent = expression.parent;
while (parent is BinaryExpression) {
- var opType = (parent as BinaryExpression).operator.type;
+ var opType = parent.operator.type;
if (opType != TokenType.AMPERSAND_AMPERSAND) {
break;
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/sort_child_property_last.dart b/pkg/analysis_server/lib/src/services/correction/dart/sort_child_property_last.dart
index 6f0d392..e2cc444 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/sort_child_property_last.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/sort_child_property_last.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -32,13 +30,12 @@
return;
}
- var parent = childProp.parent?.parent;
- if (parent is! InstanceCreationExpression ||
- !flutter.isWidgetCreation(parent)) {
+ var creationExpression = childProp.parent?.parent;
+ if (creationExpression is! InstanceCreationExpression ||
+ !flutter.isWidgetCreation(creationExpression)) {
return;
}
- InstanceCreationExpression creationExpression = parent;
var args = creationExpression.argumentList;
var last = args.arguments.last;
@@ -48,10 +45,10 @@
}
await builder.addDartFileEdit(file, (fileEditBuilder) {
- var hasTrailingComma = last.endToken.next.type == TokenType.COMMA;
+ var hasTrailingComma = last.endToken.next!.type == TokenType.COMMA;
- var childStart = childProp.beginToken.previous.end;
- var childEnd = childProp.endToken.next.end;
+ var childStart = childProp.beginToken.previous!.end;
+ var childEnd = childProp.endToken.next!.end;
var childRange = range.startOffsetEndOffset(childStart, childEnd);
var deletionRange = childRange;
@@ -69,7 +66,7 @@
var insertionPoint = last.end;
if (hasTrailingComma) {
- insertionPoint = last.endToken.next.end;
+ insertionPoint = last.endToken.next!.end;
} else if (childStart == childProp.offset) {
childText = ', $childText';
} else {
@@ -85,7 +82,7 @@
/// Using the [node] as the starting point, find the named expression that is
/// for either the `child` or `children` parameter.
- NamedExpression _findNamedExpression(AstNode node) {
+ NamedExpression? _findNamedExpression(AstNode node) {
if (node is NamedExpression) {
var name = node.name.label.name;
if (name == 'child' || name == 'children') {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/split_and_condition.dart b/pkg/analysis_server/lib/src/services/correction/dart/split_and_condition.dart
index f358b4e..028d245 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/split_and_condition.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/split_and_condition.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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/util.dart';
@@ -20,10 +18,10 @@
@override
Future<void> compute(ChangeBuilder builder) async {
// check that user invokes quick assist on binary expression
- if (node is! BinaryExpression) {
+ var binaryExpression = node;
+ if (binaryExpression is! BinaryExpression) {
return;
}
- var binaryExpression = node as BinaryExpression;
// prepare operator position
if (!isOperatorSelected(binaryExpression)) {
return;
@@ -33,11 +31,10 @@
return;
}
// prepare "if"
- var statement = node.thisOrAncestorOfType<Statement>();
- if (statement is! IfStatement) {
+ var ifStatement = node.thisOrAncestorOfType<Statement>();
+ if (ifStatement is! IfStatement) {
return;
}
- var ifStatement = statement as IfStatement;
// no support "else"
if (ifStatement.elseStatement != null) {
return;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/split_variable_declaration.dart b/pkg/analysis_server/lib/src/services/correction/dart/split_variable_declaration.dart
index c5b109e..83073d8 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/split_variable_declaration.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/split_variable_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:_fe_analyzer_shared/src/scanner/token.dart';
import 'package:analysis_server/src/services/correction/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
@@ -18,16 +16,20 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var variableList = node?.thisOrAncestorOfType<VariableDeclarationList>();
-
- // Must be a local variable declaration.
- if (variableList?.parent is! VariableDeclarationStatement) {
+ var variableList = node.thisOrAncestorOfType<VariableDeclarationList>();
+ if (variableList == null) {
return;
}
- VariableDeclarationStatement statement = variableList.parent;
+
+ // Must be a local variable declaration.
+ var statement = variableList.parent;
+ if (statement is! VariableDeclarationStatement) {
+ return;
+ }
// Cannot be `const` or `final`.
- var keywordKind = variableList.keyword?.keyword;
+ var keyword = variableList.keyword;
+ var keywordKind = keyword?.keyword;
if (keywordKind == Keyword.CONST || keywordKind == Keyword.FINAL) {
return;
}
@@ -50,9 +52,9 @@
await builder.addDartFileEdit(file, (builder) {
if (variableList.type == null) {
- final type = variable.declaredElement.type;
- if (!type.isDynamic) {
- builder.addReplacement(range.token(variableList.keyword), (builder) {
+ final type = variable.declaredElement!.type;
+ if (!type.isDynamic && keyword != null) {
+ builder.addReplacement(range.token(keyword), (builder) {
builder.writeType(type);
});
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/update_sdk_constraints.dart b/pkg/analysis_server/lib/src/services/correction/dart/update_sdk_constraints.dart
index dddf351..4cab9d2 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/update_sdk_constraints.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/update_sdk_constraints.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/file_system/file_system.dart';
@@ -25,32 +23,25 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- var context = resourceProvider.pathContext;
- File pubspecFile;
- var folder = resourceProvider.getFolder(context.dirname(file));
- while (folder != null) {
- pubspecFile = folder.getChildAssumingFile('pubspec.yaml');
- if (pubspecFile.exists) {
- break;
- }
- pubspecFile = null;
- folder = folder.parent2;
- }
+ var pubspecFile = _findPubspecFile();
if (pubspecFile == null) {
return;
}
+
var extractor = SdkConstraintExtractor(pubspecFile);
var text = extractor.constraintText();
var offset = extractor.constraintOffset();
if (text == null || offset < 0) {
return;
}
+
var length = text.length;
- String newText;
var spaceOffset = text.indexOf(' ');
if (spaceOffset >= 0) {
length = spaceOffset;
}
+
+ String? newText;
if (text == 'any') {
newText = '^$_minimumVersion';
} else if (text.startsWith('^')) {
@@ -63,11 +54,23 @@
if (newText == null) {
return;
}
+
+ final newText_final = newText;
await builder.addGenericFileEdit(pubspecFile.path, (builder) {
- builder.addSimpleReplacement(SourceRange(offset, length), newText);
+ builder.addSimpleReplacement(SourceRange(offset, length), newText_final);
});
}
+ File? _findPubspecFile() {
+ var file = resourceProvider.getFile(this.file);
+ for (var folder in file.parent2.withAncestors) {
+ var pubspecFile = folder.getChildAssumingFile('pubspec.yaml');
+ if (pubspecFile.exists) {
+ return pubspecFile;
+ }
+ }
+ }
+
/// Return an instance of this class that will update the SDK constraints to
/// '2.1.0'. Used as a tear-off in `FixProcessor`.
static UpdateSdkConstraints version_2_1_0() => UpdateSdkConstraints('2.1.0');
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/use_const.dart b/pkg/analysis_server/lib/src/services/correction/dart/use_const.dart
index 1c9d823..110432e 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/use_const.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/use_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';
@@ -20,12 +18,12 @@
if (coveredNode is InstanceCreationExpression) {
var instanceCreation = coveredNode as InstanceCreationExpression;
await builder.addDartFileEdit(file, (builder) {
- if (instanceCreation.keyword == null) {
+ var keyword = instanceCreation.keyword;
+ if (keyword == null) {
builder.addSimpleInsertion(
instanceCreation.constructorName.offset, 'const');
} else {
- builder.addSimpleReplacement(
- range.token(instanceCreation.keyword), 'const');
+ builder.addSimpleReplacement(range.token(keyword), 'const');
}
});
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/use_curly_braces.dart b/pkg/analysis_server/lib/src/services/correction/dart/use_curly_braces.dart
index f76e561..b414e48 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/use_curly_braces.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/use_curly_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/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -37,9 +35,12 @@
} else if (parent is ForStatement) {
return _forStatement(builder, parent);
} else if (statement is IfStatement) {
- if (statement.elseKeyword != null &&
- range.token(statement.elseKeyword).contains(selectionOffset)) {
- return _ifStatement(builder, statement, statement.elseStatement);
+ var elseKeyword = statement.elseKeyword;
+ var elseStatement = statement.elseStatement;
+ if (elseKeyword != null &&
+ elseStatement != null &&
+ range.token(elseKeyword).contains(selectionOffset)) {
+ return _ifStatement(builder, statement, elseStatement);
} else {
return _ifStatement(builder, statement, null);
}
@@ -88,21 +89,22 @@
}
Future<void> _ifStatement(
- ChangeBuilder builder, IfStatement node, Statement thenOrElse) async {
+ ChangeBuilder builder, IfStatement node, Statement? thenOrElse) async {
var prefix = utils.getLinePrefix(node.offset);
var indent = prefix + utils.getIndent(1);
await builder.addDartFileEdit(file, (builder) {
var thenStatement = node.thenStatement;
+ var elseKeyword = node.elseKeyword;
if (thenStatement is! Block &&
(thenOrElse == null || thenOrElse == thenStatement)) {
builder.addSimpleReplacement(
range.endStart(node.rightParenthesis, thenStatement),
' {$eol$indent',
);
- if (node.elseKeyword != null) {
+ if (elseKeyword != null) {
builder.addSimpleReplacement(
- range.endStart(thenStatement, node.elseKeyword),
+ range.endStart(thenStatement, elseKeyword),
'$eol$prefix} ',
);
} else {
@@ -111,11 +113,12 @@
}
var elseStatement = node.elseStatement;
- if (elseStatement != null &&
+ if (elseKeyword != null &&
+ elseStatement != null &&
elseStatement is! Block &&
(thenOrElse == null || thenOrElse == elseStatement)) {
builder.addSimpleReplacement(
- range.endStart(node.elseKeyword, elseStatement),
+ range.endStart(elseKeyword, elseStatement),
' {$eol$indent',
);
builder.addSimpleInsertion(elseStatement.end, '$eol$prefix}');
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/use_effective_integer_division.dart b/pkg/analysis_server/lib/src/services/correction/dart/use_effective_integer_division.dart
index babab16..5a0839d 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/use_effective_integer_division.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/use_effective_integer_division.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/utilities/extensions/ast.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';
@@ -17,21 +16,24 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- for (var n = node; n != null; n = n.parent) {
- if (n is MethodInvocation &&
- n.offset == errorOffset &&
- n.length == errorLength) {
- var target = (n as MethodInvocation).target.unParenthesized;
- await builder.addDartFileEdit(file, (builder) {
- // replace "/" with "~/"
- var binary = target as BinaryExpression;
- builder.addSimpleReplacement(range.token(binary.operator), '~/');
- // remove everything before and after
- builder.addDeletion(range.startStart(n, binary.leftOperand));
- builder.addDeletion(range.endEnd(binary.rightOperand, n));
- });
- // done
- break;
+ for (var n in node.withParents) {
+ if (n is MethodInvocation) {
+ if (n.offset == errorOffset && n.length == errorLength) {
+ var target = n.target;
+ if (target != null) {
+ target = target.unParenthesized;
+ await builder.addDartFileEdit(file, (builder) {
+ // replace "/" with "~/"
+ var binary = target as BinaryExpression;
+ builder.addSimpleReplacement(range.token(binary.operator), '~/');
+ // remove everything before and after
+ builder.addDeletion(range.startStart(n, binary.leftOperand));
+ builder.addDeletion(range.endEnd(binary.rightOperand, n));
+ });
+ }
+ // done
+ break;
+ }
}
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/use_eq_eq_null.dart b/pkg/analysis_server/lib/src/services/correction/dart/use_eq_eq_null.dart
index ce4e1f4..86ad614 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/use_eq_eq_null.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/use_eq_eq_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';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/use_is_not_empty.dart b/pkg/analysis_server/lib/src/services/correction/dart/use_is_not_empty.dart
index ca10cbf..8b06bc9 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/use_is_not_empty.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/use_is_not_empty.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';
@@ -21,10 +19,10 @@
@override
Future<void> compute(ChangeBuilder builder) async {
- if (node is! PrefixExpression) {
+ var prefixExpression = node;
+ if (prefixExpression is! PrefixExpression) {
return;
}
- PrefixExpression prefixExpression = node;
var negation = prefixExpression.operator;
if (negation.type != TokenType.BANG) {
return;
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/use_not_eq_null.dart b/pkg/analysis_server/lib/src/services/correction/dart/use_not_eq_null.dart
index 120e87d..e53bc02 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/use_not_eq_null.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/use_not_eq_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';
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/use_rethrow.dart b/pkg/analysis_server/lib/src/services/correction/dart/use_rethrow.dart
index dc55220..9ebe4dc 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/use_rethrow.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/use_rethrow.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,6 +18,7 @@
@override
Future<void> compute(ChangeBuilder builder) async {
+ final coveredNode = this.coveredNode;
if (coveredNode is ThrowExpression) {
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(range.node(coveredNode), 'rethrow');
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/wrap_in_text.dart b/pkg/analysis_server/lib/src/services/correction/dart/wrap_in_text.dart
index bfd97f5..49fb849 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/wrap_in_text.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/wrap_in_text.dart
@@ -2,20 +2,16 @@
// 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';
class WrapInText extends CorrectionProducer {
- ParameterElement _parameterElement;
- Expression _stringExpression;
-
@override
FixKind get fixKind => DartFixKind.WRAP_IN_TEXT;
@@ -24,43 +20,55 @@
//
// Extract the information needed to build the edit.
//
- _extractContextInformation(node);
- if (_parameterElement == null || _stringExpression == null) {
+ var context = _extractContextInformation(node);
+ if (context == null) {
return;
}
- if (!flutter.isWidgetType(_parameterElement.type)) {
+ if (!flutter.isWidgetType(context.parameterElement.type)) {
return;
}
//
// Extract the information needed to build the edit.
//
- var stringExpressionCode = utils.getNodeText(_stringExpression);
+ var stringExpressionCode = utils.getNodeText(context.stringExpression);
//
// Build the edit.
//
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(
- range.node(_stringExpression),
+ range.node(context.stringExpression),
'Text($stringExpressionCode)',
);
});
}
- /// Set the `String` typed named expression to [_stringExpression], and the
- /// corresponding parameter to [_parameterElement]. Leave the fields `null`
- /// if not a named argument, or not a `String` typed expression.
- void _extractContextInformation(AstNode node) {
+ /// Return an instance of this class. Used as a tear-off in `FixProcessor`.
+ static WrapInText newInstance() => WrapInText();
+
+ static _Context? _extractContextInformation(AstNode node) {
if (node is NamedExpression) {
var expression = node.expression;
- if (expression.staticType.isDartCoreString) {
- _parameterElement = node.name.label.staticElement;
- _stringExpression = expression;
+ if (expression.typeOrThrow.isDartCoreString) {
+ var parameterElement = node.name.label.staticElement;
+ if (parameterElement is ParameterElement) {
+ return _Context(
+ stringExpression: expression,
+ parameterElement: parameterElement,
+ );
+ }
}
}
}
+}
- /// Return an instance of this class. Used as a tear-off in `FixProcessor`.
- static WrapInText newInstance() => WrapInText();
+class _Context {
+ final Expression stringExpression;
+ final ParameterElement parameterElement;
+
+ _Context({
+ required this.stringExpression,
+ required this.parameterElement,
+ });
}
diff --git a/pkg/analysis_server/lib/src/services/correction/util.dart b/pkg/analysis_server/lib/src/services/correction/util.dart
index c61a3fd..2f4bec9 100644
--- a/pkg/analysis_server/lib/src/services/correction/util.dart
+++ b/pkg/analysis_server/lib/src/services/correction/util.dart
@@ -429,7 +429,7 @@
/// Returns the given [statement] if not a block, or the first child statement
/// if a block, or `null` if more than one child.
-Statement? getSingleStatement(Statement statement) {
+Statement? getSingleStatement(Statement? statement) {
if (statement is Block) {
List<Statement> blockStatements = statement.statements;
if (blockStatements.length != 1) {
To view, visit change 195041. To unsubscribe, or for help writing mail filters, visit settings.
go/dart-cbuild result: SUCCESS
Details: https://goto.google.com/dart-cbuild/find/7536cfaa5516e5d7bdc9bf9aa9520938a31cf1db
Patch set 1:Commit-Queue +2
commi...@chromium.org submitted this change.
Migrate several lib/src/services/correction/dart/
R=brianwi...@google.com
Change-Id: I7957e3fa724e618e6cf8c6a94344b0b4057aa080
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/195041
Reviewed-by: Brian Wilkerson <brianwi...@google.com>
Commit-Queue: Konstantin Shcheglov <sche...@google.com>
To view, visit change 195041. To unsubscribe, or for help writing mail filters, visit settings.
go/dart-cbuild result: SUCCESS
Details: https://goto.google.com/dart-cbuild/find/81be9be4a6bc4d326002d90cb005ba3d71cdfd42