blob: 4c04dd26b92298e41eed5049186af297b431d757 [file] [log] [blame]
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// 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.
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_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
class AddNullCheck extends CorrectionProducer {
@override
FixKind get fixKind => DartFixKind.ADD_NULL_CHECK;
@override
Future<void> compute(ChangeBuilder builder) async {
if (!unit.featureSet.isEnabled(Feature.non_nullable)) {
// Don't suggest a feature that isn't supported.
return;
}
Expression target;
if (coveredNode is Expression) {
target = coveredNode;
} else {
return;
}
var fromType = target.staticType;
if (fromType == typeProvider.nullType) {
// Adding a null check after an explicit `null` is pointless.
return;
}
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;
} else if (parent is ArgumentList) {
toType = target.staticParameterElement.type;
} else if (parent is IndexExpression) {
toType = parent.realTarget.staticType;
} else if ((parent is PrefixedIdentifier && target == parent.prefix) ||
(parent is PropertyAccess && target == parent.target) ||
(parent is MethodInvocation && target == parent.target) ||
(parent is FunctionExpressionInvocation && target == parent.function)) {
// No need to set the `toType` because there isn't any need for a type
// check.
} else {
return;
}
if (toType != null &&
!typeSystem.isAssignableTo(
toType, typeSystem.promoteToNonNull(fromType))) {
// The reason that `fromType` can't be assigned to `toType` is more than
// just because it's nullable, in which case a null check won't fix the
// problem.
return;
}
var needsParentheses = target.precedence < Precedence.postfix;
await builder.addDartFileEdit(file, (builder) {
if (needsParentheses) {
builder.addSimpleInsertion(target.offset, '(');
}
builder.addInsertion(target.end, (builder) {
if (needsParentheses) {
builder.write(')');
}
builder.write('!');
});
});
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static AddNullCheck newInstance() => AddNullCheck();
}