fix: avoid returning widgets - #371
Conversation
… handling across lint rules
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (4)
Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthroughThe change adds a shared ignored-type parameter model, integrates it into three lint rules, and updates avoid-returning-widgets to inspect declared and single-expression return types. Documentation and tests cover supported configuration formats and widget-return filtering. ChangesIgnored type configuration
Estimated code review effort: 3 (Moderate) | ~25 minutes Merge Risk: ⚪ Minimal · up to This change only adjusts which widget-return diagnostics a static analysis rule reports and does not affect runtime behavior, permissions, data, or deployment; no actionable merge-blocking risk remains beyond normal checks and review. Sequence Diagram(s)sequenceDiagram
participant Declaration
participant AvoidReturningWidgetsVisitor
participant IgnoredTypesListParameter
participant isWidgetOrSubclass
Declaration->>AvoidReturningWidgetsVisitor: Visit declaration
AvoidReturningWidgetsVisitor->>isWidgetOrSubclass: Check return type
isWidgetOrSubclass-->>AvoidReturningWidgetsVisitor: Return widget classification
AvoidReturningWidgetsVisitor->>IgnoredTypesListParameter: Check declared and expression types
IgnoredTypesListParameter-->>AvoidReturningWidgetsVisitor: Return ignore decision
AvoidReturningWidgetsVisitor-->>Declaration: Emit or suppress diagnostic
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Full details: Docstring CoverageExplanation No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0 files. (4 skipped: 4 unsupported.) ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| bool isWidgetType(DartType type) => | ||
| isWidgetOrSubclass(type) && | ||
| !(_isMultiProvider(type) || _isSubclassOfInheritedProvider(type)); | ||
| bool isWidgetType(DartType type) => isWidgetOrSubclass(type); |
There was a problem hiding this comment.
| bool isWidgetType(DartType type) => isWidgetOrSubclass(type); |
I'm not sure if we need both? the only difference is the name and parameter type, but I don't feel like it's reflected in the name change or needed?
and if we use the null accepting one, we can do
if (!isWidgetOrSubclass(returnType)) return;instead of
if (returnType == null) return;
if (!isWidgetType(returnType)) return;| ignored_types: | ||
| - MultiProvider | ||
| - InheritedProvider | ||
| - InheritedTheme |
There was a problem hiding this comment.
I'm not sure if we want to make these default
…onfiguration options
| @override | ||
| bool operator ==(Object other) => | ||
| identical(this, other) || | ||
| other is IgnoredTypesListParameter && | ||
| const SetEquality<String>().equals( | ||
| other.ignoredTypes, | ||
| ignoredTypes, | ||
| ); | ||
|
|
||
| @override | ||
| int get hashCode => const SetEquality<String>().hash(ignoredTypes); |
There was a problem hiding this comment.
we can use Equatable for this
| @override | |
| bool operator ==(Object other) => | |
| identical(this, other) || | |
| other is IgnoredTypesListParameter && | |
| const SetEquality<String>().equals( | |
| other.ignoredTypes, | |
| ignoredTypes, | |
| ); | |
| @override | |
| int get hashCode => const SetEquality<String>().hash(ignoredTypes); |
| _parameters.ignoredTypes.shouldIgnore(returnType) || | ||
| _parameters.ignoredTypes.shouldIgnore( | ||
| node.singleReturnExpression?.staticType, | ||
| ) || | ||
| _parameters.exclude.shouldIgnore(node); |
There was a problem hiding this comment.
| _parameters.ignoredTypes.shouldIgnore(returnType) || | |
| _parameters.ignoredTypes.shouldIgnore( | |
| node.singleReturnExpression?.staticType, | |
| ) || | |
| _parameters.exclude.shouldIgnore(node); | |
| _parameters.shouldIgnore(node) |
I'd rather extract this to AvoidReturningWidgetsParameters
ignoredTypes.shouldIgnoreAny([
node.returnType,
node.singleReturnExpression?.staticType,
]) ||
exclude.shouldIgnore(node);
}
extension on Declaration {
DartType? get returnType => switch (this) {
MethodDeclaration(:final declaredFragment?) =>
declaredFragment.element.returnType,
FunctionDeclaration(:final declaredFragment?) =>
declaredFragment.element.returnType,
_ => null,
};
}and
bool shouldIgnoreAny(List<DartType?> types) => types.any(shouldIgnore);Also we probably can join all of the if (...) return;s into a single one
There was a problem hiding this comment.
Done! Thank you!
| bool _isOverridden(Declaration node) { | ||
| if (node is MethodDeclaration && | ||
| node.metadata.any((m) => m.name.name == 'override')) { | ||
| if (node is MethodDeclaration && isOverride(node.metadata)) { |
There was a problem hiding this comment.
this can be moved into the switch - MethodDeclaration() when isOverride(...) => true
…fy equality checks
…meter model and node utility extension
Summary by CodeRabbit
ignored_typesconfiguration support for suppressing selected lint diagnostics.