addHookWidgetBody method
void
addHookWidgetBody(
- void listener(
- FunctionBody node,
- AstNode diagnosticNode
- bool isExactly = false,
Implementation
void addHookWidgetBody(
void Function(FunctionBody node, AstNode diagnosticNode) listener, {
bool isExactly = false,
}) {
addInstanceCreationExpression((node) {
if (maybeHookBuilderBody(node) case final body?) {
listener(body, node.constructorName);
}
});
addClassDeclaration((node) {
final element = node.declaredElement;
if (element == null) {
return;
}
const checker = TypeChecker.any([
TypeChecker.fromName(
'HookWidget',
packageName: 'flutter_hooks',
),
TypeChecker.fromName(
'HookConsumerWidget',
packageName: 'hooks_riverpod',
),
]);
final AstNode diagnosticNode;
if (isExactly) {
final superclass = node.extendsClause?.superclass;
final superclassElement = superclass?.element;
if (superclass == null || superclassElement == null) {
return;
}
final isDirectHookWidget = checker.isExactly(superclassElement);
if (!isDirectHookWidget) {
return;
}
diagnosticNode = superclass;
} else {
final isHookWidget = checker.isSuperOf(element);
if (!isHookWidget) {
return;
}
diagnosticNode = node;
}
final buildMethod = getBuildMethod(node);
if (buildMethod == null) {
return;
}
listener(buildMethod.body, diagnosticNode);
});
}