This repository was archived by the owner on Jul 16, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +44
-1
lines changed
lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_returning_widgets
test/src/analyzers/lint_analyzer/rules/rules_list/avoid_returning_widgets/examples Expand file tree Collapse file tree 4 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 33## Unreleased
44
55* chore: activate new lint rules.
6+ * feat: improve ` avoid-returning-widgets ` builder functions handling.
67* fix: correctly handle const maps in ` no-magic-number ` .
78
89## 4.11.0-dev.1
Original file line number Diff line number Diff line change 33import 'package:analyzer/dart/ast/ast.dart' ;
44import 'package:analyzer/dart/ast/token.dart' ;
55import 'package:analyzer/dart/ast/visitor.dart' ;
6+ import 'package:analyzer/dart/element/type.dart' ;
67
78import '../../../../../utils/flutter_types_utils.dart' ;
89import '../../../../../utils/node_utils.dart' ;
Original file line number Diff line number Diff line change @@ -72,10 +72,25 @@ class _InvocationsVisitor extends RecursiveAstVisitor<void> {
7272 @override
7373 void visitMethodInvocation (MethodInvocation node) {
7474 if (_declarationNames.contains (node.methodName.name) &&
75- node.realTarget == null ) {
75+ node.realTarget == null &&
76+ ! _isInsideBuilder (node)) {
7677 _invocations.add (node);
7778 }
7879 }
80+
81+ bool _isInsideBuilder (MethodInvocation node) {
82+ final expression = node.thisOrAncestorOfType <NamedExpression >();
83+
84+ if (expression is NamedExpression ) {
85+ final type = expression.staticType;
86+ if (type is FunctionType ) {
87+ return type.returnType is InterfaceType &&
88+ hasWidgetType (type.returnType);
89+ }
90+ }
91+
92+ return false ;
93+ }
7994}
8095
8196class _DeclarationsVisitor extends RecursiveAstVisitor <void > {
Original file line number Diff line number Diff line change @@ -93,3 +93,29 @@ class Builder extends Widget {
9393}
9494
9595class StatelessWidget extends Widget {}
96+
97+ class MyOtherWidget extends StatelessWidget {
98+ Widget _buildMyWidget (BuildContext context) {
99+ return Container ();
100+ }
101+
102+ @override
103+ Widget build (BuildContext context) {
104+ return Builder (builder: (context) {
105+ return _buildMyWidget (context);
106+ });
107+ }
108+ }
109+
110+ class MyAnotherWidget extends StatelessWidget {
111+ Widget _buildMyWidget (BuildContext context) {
112+ return Container ();
113+ }
114+
115+ @override
116+ Widget build (BuildContext context) {
117+ return Builder (
118+ builder: (context) => _buildMyWidget (context),
119+ );
120+ }
121+ }
You can’t perform that action at this time.
0 commit comments