|
| 1 | +// import 'package:analyzer/dart/ast/ast.dart'; |
| 2 | +// import 'package:analyzer/dart/element/nullability_suffix.dart'; |
| 3 | +// import 'package:analyzer/dart/element/type.dart'; |
| 4 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 5 | +import 'package:custom_lint_core/src/node_lint_visitor.dart'; |
| 6 | +import 'package:analyzer/dart/element/element.dart'; |
| 7 | +import 'package:analyzer/error/error.dart' hide LintCode; |
| 8 | +import 'package:analyzer/error/listener.dart'; |
| 9 | +import 'package:custom_lint_builder/custom_lint_builder.dart'; |
| 10 | + |
| 11 | +final tableChecker = TypeChecker.fromName('Table', packageName: 'drift'); |
| 12 | +final databaseConnectionUserChecker = |
| 13 | + TypeChecker.fromName('DatabaseConnectionUser', packageName: 'drift'); |
| 14 | +final columnBuilderChecker = |
| 15 | + TypeChecker.fromName('ColumnBuilder', packageName: 'drift'); |
| 16 | + |
| 17 | +bool inTransactionBlock(AstNode node) { |
| 18 | + return node.thisOrAncestorMatching( |
| 19 | + (method) { |
| 20 | + if (method is! MethodInvocation) return false; |
| 21 | + |
| 22 | + final methodElement = method.methodName.staticElement; |
| 23 | + if (methodElement is! MethodElement || |
| 24 | + methodElement.name != 'transaction') return false; |
| 25 | + |
| 26 | + final enclosingElement = methodElement.enclosingElement; |
| 27 | + if (enclosingElement is! ClassElement || |
| 28 | + !databaseConnectionUserChecker.isExactly(enclosingElement)) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + return true; |
| 32 | + }, |
| 33 | + ) != |
| 34 | + null; |
| 35 | +} |
| 36 | + |
| 37 | +class UnawaitedFuturesInTransaction extends DartLintRule { |
| 38 | + UnawaitedFuturesInTransaction() : super(code: _code); |
| 39 | + |
| 40 | + static const _code = LintCode( |
| 41 | + name: 'unawaited_futures_in_transaction', |
| 42 | + problemMessage: |
| 43 | + 'All futures in a transaction should be awaited to ensure that all operations are completed before the transaction is closed.', |
| 44 | + errorSeverity: ErrorSeverity.ERROR, |
| 45 | + ); |
| 46 | + @override |
| 47 | + void run(CustomLintResolver resolver, ErrorReporter reporter, |
| 48 | + CustomLintContext context) { |
| 49 | + context.registry.addVariableDeclarationStatement((node) { |
| 50 | + // If this variable declaration is not inside a transaction block, return |
| 51 | + if (inTransactionBlock(node)) { |
| 52 | + for (var variable in node.variables.variables) { |
| 53 | + final type = variable.declaredElement?.type; |
| 54 | + if (type == null || !type.isDartAsyncFuture) continue; |
| 55 | + reporter.atNode(variable, _code); |
| 56 | + } |
| 57 | + } |
| 58 | + }); |
| 59 | + context.registry.addExpressionStatement((statement) { |
| 60 | + // If this variable declaration is not inside a transaction block, return |
| 61 | + if (inTransactionBlock(statement)) { |
| 62 | + final expression = statement.expression; |
| 63 | + if (expression is! MethodInvocation) return; |
| 64 | + final element = expression.methodName.staticElement; |
| 65 | + if (element is! MethodElement) return; |
| 66 | + if (element.returnType.isDartAsyncFuture) { |
| 67 | + reporter.atNode(expression, _code); |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + // context.registry.addInstanceCreationExpression((statement) { |
| 72 | + // if (inTransactionBlock(statement)) { |
| 73 | + // if (statement |
| 74 | + // .constructorName.staticElement?.returnType.isDartAsyncFuture ?? |
| 75 | + // false) { |
| 76 | + // reporter.atNode(statement, _code); |
| 77 | + // } |
| 78 | + // } |
| 79 | + // If this variable declaration is not inside a transaction block, return |
| 80 | + // if (inTransactionBlock(statement)) { |
| 81 | + // final expression = statement.expression; |
| 82 | + // if (expression is! MethodInvocation) return; |
| 83 | + // final element = expression.methodName.staticElement; |
| 84 | + // if (element is! MethodElement) return; |
| 85 | + // if (element.returnType.isDartAsyncFuture) { |
| 86 | + // reporter.atNode(expression, _code); |
| 87 | + // } |
| 88 | + // } |
| 89 | + // }); |
| 90 | + // context.registry.addArgumentList( |
| 91 | + // (node) { |
| 92 | + // final method = node.parent; |
| 93 | + // if (method is! MethodInvocation) return; |
| 94 | + |
| 95 | + // final methodElement = method.methodName.staticElement; |
| 96 | + // if (methodElement is! MethodElement) return; |
| 97 | + |
| 98 | + // // Verify that the method is a transaction method |
| 99 | + // if (methodElement.name != 'transaction') return; |
| 100 | + |
| 101 | + // // Verify that the method is called on a DatabaseConnectionUser |
| 102 | + // final enclosingElement = methodElement.enclosingElement; |
| 103 | + // if (enclosingElement is! ClassElement) return; |
| 104 | + |
| 105 | + // // Get the 1st argument of the transaction method |
| 106 | + // final classback = node.arguments.firstOrNull; |
| 107 | + // if (classback is! FunctionExpression) return; |
| 108 | + |
| 109 | + // // Get the body of the function |
| 110 | + // final body = classback.body; |
| 111 | + // if (body is! BlockFunctionBody) return; |
| 112 | + // for (var statement in body.block.statements) { |
| 113 | + // if (statement is ExpressionStatement) { |
| 114 | + // final expression = statement.expression; |
| 115 | + // if (expression is! MethodInvocation) continue; |
| 116 | + // // If the return type of the method is a Future, then the method should have been awaited |
| 117 | + // final element = expression.methodName.staticElement; |
| 118 | + // if (element is! MethodElement) continue; |
| 119 | + // if (element.returnType.isDartAsyncFuture) { |
| 120 | + // reporter.atNode(expression, _code); |
| 121 | + // } |
| 122 | + // } else if (statement is VariableDeclarationStatement) { |
| 123 | + // for (var variable in statement.variables.variables) { |
| 124 | + // final type = variable.declaredElement?.type; |
| 125 | + // if (type == null || !type.isDartAsyncFuture) continue; |
| 126 | + // reporter.atNode(variable, _code); |
| 127 | + // } |
| 128 | + // } |
| 129 | + // } |
| 130 | + // }, |
| 131 | + // ); |
| 132 | + } |
| 133 | +} |
0 commit comments