|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:analysis_server/src/services/correction/assist.dart'; |
| 6 | +import 'package:analysis_server/src/services/correction/fix.dart'; |
| 7 | +import 'package:analysis_server_plugin/edit/dart/correction_producer.dart'; |
| 8 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 9 | +import 'package:analyzer/dart/ast/token.dart'; |
| 10 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 11 | +import 'package:analyzer/dart/element/type.dart'; |
| 12 | +import 'package:analyzer/dart/element/type_provider.dart'; |
| 13 | +import 'package:analyzer/dart/element/type_system.dart'; |
| 14 | +import 'package:analyzer_plugin/utilities/assist/assist.dart'; |
| 15 | +import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; |
| 16 | +import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; |
| 17 | +import 'package:analyzer_plugin/utilities/range_factory.dart'; |
| 18 | + |
| 19 | +class RemoveAsync extends ResolvedCorrectionProducer { |
| 20 | + final _Type _type; |
| 21 | + |
| 22 | + RemoveAsync({required super.context}) : _type = _Type.other; |
| 23 | + |
| 24 | + RemoveAsync.unnecessary({required super.context}) : _type = _Type.unnecessary; |
| 25 | + |
| 26 | + @override |
| 27 | + CorrectionApplicability get applicability => |
| 28 | + // Not predictably the correct action. |
| 29 | + CorrectionApplicability.singleLocation; |
| 30 | + |
| 31 | + @override |
| 32 | + AssistKind get assistKind => DartAssistKind.REMOVE_ASYNC; |
| 33 | + |
| 34 | + @override |
| 35 | + FixKind get fixKind => DartFixKind.REMOVE_ASYNC; |
| 36 | + |
| 37 | + @override |
| 38 | + Future<void> compute(ChangeBuilder builder) async { |
| 39 | + bool updateReturnType = true; |
| 40 | + AstNode? node = this.node; |
| 41 | + FunctionBody body; |
| 42 | + DartType? returnType; |
| 43 | + if (_type == _Type.unnecessary) { |
| 44 | + if (node.thisOrAncestorOfType<FunctionBody>() case var ancestorBody?) { |
| 45 | + body = ancestorBody; |
| 46 | + } else { |
| 47 | + return; |
| 48 | + } |
| 49 | + } else { |
| 50 | + if (node is Block) { |
| 51 | + node = node.parent; |
| 52 | + } |
| 53 | + if (node is BlockFunctionBody) { |
| 54 | + node = node.parent; |
| 55 | + } else if (node is ExpressionFunctionBody) { |
| 56 | + node = node.parent; |
| 57 | + } |
| 58 | + if (node case FormalParameterList(:var parent?)) { |
| 59 | + node = parent; |
| 60 | + } |
| 61 | + if (node case NamedType(:var parent?)) { |
| 62 | + node = parent; |
| 63 | + } |
| 64 | + if (node case FunctionExpression(:FunctionDeclaration parent)) { |
| 65 | + node = parent; |
| 66 | + } |
| 67 | + switch (node) { |
| 68 | + case FunctionExpression(): |
| 69 | + body = node.body; |
| 70 | + case FunctionDeclaration( |
| 71 | + :var functionExpression, |
| 72 | + :var declaredFragment, |
| 73 | + ): |
| 74 | + body = functionExpression.body; |
| 75 | + if (declaredFragment?.element case var declaredElement?) { |
| 76 | + returnType = declaredElement.returnType; |
| 77 | + } else if (declaredFragment case var declaredFragment?) { |
| 78 | + returnType = declaredFragment.element.returnType; |
| 79 | + } |
| 80 | + case MethodDeclaration(): |
| 81 | + body = node.body; |
| 82 | + returnType = node.declaredFragment!.element.returnType; |
| 83 | + default: |
| 84 | + return; |
| 85 | + } |
| 86 | + } |
| 87 | + if (body.keyword?.lexeme != Keyword.ASYNC.lexeme || body.star != null) { |
| 88 | + return; |
| 89 | + } |
| 90 | + if (returnType is InterfaceType && |
| 91 | + (returnType.isDartAsyncFuture || returnType.isDartAsyncFutureOr)) { |
| 92 | + var newReturn = returnType.typeArguments.first; |
| 93 | + var visitor = _VisitorTester(typeSystem, typeProvider, newReturn); |
| 94 | + if (!visitor.returnsWillBeAssignable(body)) { |
| 95 | + if (visitor.returnsAreAssignable(body)) { |
| 96 | + updateReturnType = false; |
| 97 | + } else { |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + if (visitor.foundAwait) { |
| 102 | + return; |
| 103 | + } |
| 104 | + } else if (returnType != null && |
| 105 | + !_VisitorTester( |
| 106 | + typeSystem, |
| 107 | + typeProvider, |
| 108 | + returnType, |
| 109 | + ).returnsAreAssignable(body)) { |
| 110 | + return; |
| 111 | + } else { |
| 112 | + updateReturnType = false; |
| 113 | + } |
| 114 | + if (updateReturnType) { |
| 115 | + return await builder.addDartFileEdit(file, (builder) { |
| 116 | + builder.convertFunctionFromAsyncToSync( |
| 117 | + body: body, |
| 118 | + typeSystem: typeSystem, |
| 119 | + typeProvider: typeProvider, |
| 120 | + ); |
| 121 | + }); |
| 122 | + } else { |
| 123 | + await builder.addDartFileEdit(file, (builder) { |
| 124 | + var keyword = body.keyword!; |
| 125 | + builder.addDeletion( |
| 126 | + range.startOffsetEndOffset( |
| 127 | + keyword.offset, |
| 128 | + keyword.end + (keyword.next!.offset == (keyword.end) ? 0 : 1), |
| 129 | + ), |
| 130 | + ); |
| 131 | + }); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +enum _Type { unnecessary, other } |
| 137 | + |
| 138 | +/// An AST visitor used to test if all return statements in a function body |
| 139 | +/// are assignable to a given type. |
| 140 | +class _VisitorTester extends RecursiveAstVisitor<void> { |
| 141 | + /// A flag indicating whether a return statement was visited. |
| 142 | + bool _foundOneReturn = false; |
| 143 | + |
| 144 | + /// A flag indicating whether an await expression was found. |
| 145 | + bool _foundAwait = false; |
| 146 | + |
| 147 | + /// A flag indicating whether the return type is assignable considering |
| 148 | + /// [_isAssignable]. |
| 149 | + bool _returnsAreAssignable = true; |
| 150 | + |
| 151 | + final TypeProvider typeProvider; |
| 152 | + |
| 153 | + /// The type system used to check assignability. |
| 154 | + final TypeSystem typeSystem; |
| 155 | + |
| 156 | + /// The type that the return statements should be assignable to. |
| 157 | + final DartType argumentType; |
| 158 | + |
| 159 | + bool _processingFuture = false; |
| 160 | + |
| 161 | + /// Initialize a newly created visitor. |
| 162 | + _VisitorTester(this.typeSystem, this.typeProvider, this.argumentType); |
| 163 | + |
| 164 | + /// A flag indicating whether an await expression was found. |
| 165 | + bool get foundAwait => _foundAwait; |
| 166 | + |
| 167 | + /// Returns `true` if all return statements in the given [node] are |
| 168 | + /// assignable to the [argumentType] type. |
| 169 | + bool returnsAreAssignable(AstNode node) { |
| 170 | + _foundAwait = false; |
| 171 | + _returnsAreAssignable = true; |
| 172 | + _foundOneReturn = false; |
| 173 | + _processingFuture = true; |
| 174 | + node.accept(this); |
| 175 | + return _returnsAreAssignable && _foundOneReturn || !_foundOneReturn; |
| 176 | + } |
| 177 | + |
| 178 | + /// Returns `true` if all return statements in the given [node] are |
| 179 | + /// assignable to the [argumentType] type. |
| 180 | + bool returnsWillBeAssignable(AstNode node) { |
| 181 | + _foundAwait = false; |
| 182 | + _returnsAreAssignable = true; |
| 183 | + _foundOneReturn = false; |
| 184 | + node.accept(this); |
| 185 | + return _returnsAreAssignable && _foundOneReturn || !_foundOneReturn; |
| 186 | + } |
| 187 | + |
| 188 | + @override |
| 189 | + void visitAwaitExpression(AwaitExpression node) { |
| 190 | + _foundAwait = true; |
| 191 | + // No need to continue processing if we found an await expression. |
| 192 | + } |
| 193 | + |
| 194 | + @override |
| 195 | + void visitExpressionFunctionBody(ExpressionFunctionBody node) { |
| 196 | + _foundOneReturn = true; |
| 197 | + if (node.expression.staticType case var type?) { |
| 198 | + _returnsAreAssignable &= _isAssignable(type); |
| 199 | + } else { |
| 200 | + _returnsAreAssignable = false; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + @override |
| 205 | + void visitFunctionExpression(FunctionExpression node) { |
| 206 | + // Return statements within closures aren't counted. |
| 207 | + } |
| 208 | + |
| 209 | + @override |
| 210 | + void visitReturnStatement(ReturnStatement node) { |
| 211 | + _foundOneReturn = true; |
| 212 | + if (node.expression?.staticType case var type?) { |
| 213 | + _returnsAreAssignable &= _isAssignable(type); |
| 214 | + } else { |
| 215 | + _returnsAreAssignable = false; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + /// Tests whether a type is assignable to the [argumentType] type. |
| 220 | + bool _isAssignable(DartType type) { |
| 221 | + if (_processingFuture) { |
| 222 | + return typeSystem.isAssignableTo( |
| 223 | + type, |
| 224 | + typeProvider.futureOrType(argumentType), |
| 225 | + ); |
| 226 | + } |
| 227 | + return typeSystem.isAssignableTo(type, argumentType); |
| 228 | + } |
| 229 | +} |
0 commit comments