| // 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:_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/search/hierarchy.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'; |
| import 'package:analyzer_plugin/utilities/range_factory.dart'; |
| |
| class ConvertIntoIsNotEmpty extends CorrectionProducer { |
| @override |
| AssistKind get assistKind => DartAssistKind.CONVERT_INTO_IS_NOT_EMPTY; |
| |
| @override |
| Future<void> compute(ChangeBuilder builder) async { |
| // prepare "expr.isEmpty" |
| AstNode isEmptyAccess; |
| SimpleIdentifier isEmptyIdentifier; |
| if (node is SimpleIdentifier) { |
| var identifier = node as SimpleIdentifier; |
| var parent = identifier.parent; |
| // normal case (but rare) |
| if (parent is PropertyAccess) { |
| isEmptyIdentifier = parent.propertyName; |
| isEmptyAccess = parent; |
| } |
| // usual case |
| if (parent is PrefixedIdentifier) { |
| isEmptyIdentifier = parent.identifier; |
| isEmptyAccess = parent; |
| } |
| } |
| if (isEmptyIdentifier == null) { |
| return; |
| } |
| // should be "isEmpty" |
| var propertyElement = isEmptyIdentifier.staticElement; |
| if (propertyElement == null || 'isEmpty' != propertyElement.name) { |
| return; |
| } |
| // should have "isNotEmpty" |
| var propertyTarget = propertyElement.enclosingElement; |
| if (propertyTarget == null || |
| getChildren(propertyTarget, 'isNotEmpty').isEmpty) { |
| return; |
| } |
| // should be in PrefixExpression |
| if (isEmptyAccess.parent is! PrefixExpression) { |
| return; |
| } |
| var prefixExpression = isEmptyAccess.parent as PrefixExpression; |
| // should be ! |
| if (prefixExpression.operator.type != TokenType.BANG) { |
| return; |
| } |
| |
| await builder.addDartFileEdit(file, (builder) { |
| builder.addDeletion( |
| range.startStart(prefixExpression, prefixExpression.operand)); |
| builder.addSimpleReplacement(range.node(isEmptyIdentifier), 'isNotEmpty'); |
| }); |
| } |
| |
| /// Return an instance of this class. Used as a tear-off in `AssistProcessor`. |
| static ConvertIntoIsNotEmpty newInstance() => ConvertIntoIsNotEmpty(); |
| } |