This repository was archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 271
feat: add avoid-shrink-wrap-in-lists #918
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...analyzer/rules/rules_list/avoid_shrink_wrap_in_lists/avoid_shrink_wrap_in_lists_rule.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// ignore_for_file: public_member_api_docs | ||
|
||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
import 'package:collection/collection.dart'; | ||
|
||
import '../../../../../utils/flutter_types_utils.dart'; | ||
import '../../../../../utils/node_utils.dart'; | ||
import '../../../lint_utils.dart'; | ||
import '../../../models/internal_resolved_unit_result.dart'; | ||
import '../../../models/issue.dart'; | ||
import '../../../models/severity.dart'; | ||
import '../../models/flutter_rule.dart'; | ||
import '../../rule_utils.dart'; | ||
|
||
part 'visitor.dart'; | ||
|
||
class AvoidShrinkWrapInListsRule extends FlutterRule { | ||
static const String ruleId = 'avoid-shrink-wrap-in-lists'; | ||
|
||
static const _warning = | ||
'Avoid using ListView with shrinkWrap, since it might degrade the performance. Consider using slivers instead.'; | ||
|
||
AvoidShrinkWrapInListsRule([Map<String, Object> config = const {}]) | ||
: super( | ||
id: ruleId, | ||
severity: readSeverity(config, Severity.performance), | ||
excludes: readExcludes(config), | ||
); | ||
|
||
@override | ||
Iterable<Issue> check(InternalResolvedUnitResult source) { | ||
final visitor = _Visitor(); | ||
|
||
source.unit.visitChildren(visitor); | ||
|
||
return visitor.expressions | ||
.map((expression) => createIssue( | ||
rule: this, | ||
location: nodeLocation( | ||
node: expression, | ||
source: source, | ||
), | ||
message: _warning, | ||
)) | ||
.toList(growable: false); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_shrink_wrap_in_lists/visitor.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
part of 'avoid_shrink_wrap_in_lists_rule.dart'; | ||
|
||
class _Visitor extends RecursiveAstVisitor<void> { | ||
final _expressions = <Expression>[]; | ||
|
||
Iterable<Expression> get expressions => _expressions; | ||
|
||
@override | ||
void visitInstanceCreationExpression(InstanceCreationExpression node) { | ||
super.visitInstanceCreationExpression(node); | ||
|
||
if (isListViewWidget(node.staticType) && | ||
_hasShrinkWrap(node) && | ||
_hasParentList(node)) { | ||
_expressions.add(node); | ||
} | ||
} | ||
|
||
bool _hasShrinkWrap(InstanceCreationExpression node) => | ||
node.argumentList.arguments.firstWhereOrNull( | ||
(arg) => arg is NamedExpression && arg.name.label.name == 'shrinkWrap', | ||
) != | ||
null; | ||
|
||
bool _hasParentList(InstanceCreationExpression node) => | ||
node.thisOrAncestorMatching((parent) => | ||
parent != node && | ||
parent is InstanceCreationExpression && | ||
(isListViewWidget(parent.staticType) || | ||
isColumnWidget(parent.staticType) || | ||
isRowWidget(parent.staticType))) != | ||
null; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...zer/rules/rules_list/avoid_shrink_wrap_in_lists/avoid_shrink_wrap_in_lists_rule_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/models/severity.dart'; | ||
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/rules/rules_list/avoid_shrink_wrap_in_lists/avoid_shrink_wrap_in_lists_rule.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../../../../helpers/rule_test_helper.dart'; | ||
|
||
const _examplePath = 'avoid_shrink_wrap_in_lists/examples/example.dart'; | ||
|
||
void main() { | ||
group('AvoidShrinkWrapInListsRule', () { | ||
test('initialization', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_examplePath); | ||
final issues = AvoidShrinkWrapInListsRule().check(unit); | ||
|
||
RuleTestHelper.verifyInitialization( | ||
issues: issues, | ||
ruleId: 'avoid-shrink-wrap-in-lists', | ||
severity: Severity.performance, | ||
); | ||
}); | ||
|
||
test('reports about found issues', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_examplePath); | ||
final issues = AvoidShrinkWrapInListsRule().check(unit); | ||
|
||
RuleTestHelper.verifyIssues( | ||
issues: issues, | ||
startLines: [10, 17, 24], | ||
startColumns: [30, 27, 32], | ||
locationTexts: [ | ||
'ListView(shrinkWrap: true, children: [])', | ||
'ListView(shrinkWrap: true, children: [])', | ||
'ListView(shrinkWrap: true, children: [])', | ||
], | ||
messages: [ | ||
'Avoid using ListView with shrinkWrap, since it might degrade the performance. Consider using slivers instead.', | ||
'Avoid using ListView with shrinkWrap, since it might degrade the performance. Consider using slivers instead.', | ||
'Avoid using ListView with shrinkWrap, since it might degrade the performance. Consider using slivers instead.', | ||
], | ||
); | ||
}); | ||
}); | ||
} |
47 changes: 47 additions & 0 deletions
47
...analyzers/lint_analyzer/rules/rules_list/avoid_shrink_wrap_in_lists/examples/example.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class CoolWidget { | ||
Widget build() { | ||
return ListView(child: Widget()); | ||
} | ||
} | ||
|
||
class WidgetWithColumn { | ||
Widget build() { | ||
// LINT | ||
return Column(children: [ListView(shrinkWrap: true, children: [])]); | ||
} | ||
} | ||
|
||
class WidgetWithRow { | ||
Widget build() { | ||
// LINT | ||
return Row(children: [ListView(shrinkWrap: true, children: [])]); | ||
} | ||
} | ||
|
||
class WidgetWithList { | ||
Widget build() { | ||
// LINT | ||
return ListView(children: [ListView(shrinkWrap: true, children: [])]); | ||
} | ||
} | ||
|
||
class ListView extends Widget { | ||
final bool shrinkWrap; | ||
final List<Widget> children; | ||
|
||
const ListView({required this.shrinkWrap, required this.children}); | ||
} | ||
|
||
class Column extends Widget { | ||
final List<Widget> children; | ||
|
||
const Column({required this.children}); | ||
} | ||
|
||
class Row extends Widget { | ||
final List<Widget> children; | ||
|
||
const Row({required this.children}); | ||
} | ||
|
||
class Widget {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Avoid shrink wrap in lists | ||
|
||
## Rule id {#rule-id} | ||
|
||
avoid-shrink-wrap-in-lists | ||
|
||
## Severity {#severity} | ||
|
||
Performance | ||
|
||
## Description {#description} | ||
|
||
Warns when a `ListView` widget with `shrinkWrap` parameter is wrapped in a `Column`, `Row` or another `ListView` widget. | ||
|
||
According to the Flutter documentation, using `shrinkWrap` in lists is expensive performance-wise and should be avoided, since using slivers is significantly cheaper and achieves the same or even better results. | ||
|
||
Additional resources: | ||
|
||
- <https://github.com/dart-lang/linter/issues/3496> | ||
|
||
### Example {#example} | ||
|
||
Bad: | ||
|
||
```dart | ||
Column( | ||
children: [ | ||
Expanded( | ||
// LINT | ||
child: ListView( | ||
shrinkWrap: true, | ||
physics: const NeverScrollableScrollPhysics(), | ||
children: [], | ||
), | ||
), | ||
], | ||
), | ||
``` | ||
|
||
Good: | ||
|
||
```dart | ||
CustomScrollView( | ||
slivers: [ | ||
SliverList( | ||
delegate: SliverChildBuilderDelegate( | ||
(context, index) => Container(), | ||
childCount: someObject.length, | ||
), | ||
), | ||
], | ||
), | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey @incendial , awesome work! I took the liberty of reviewing your PR and I think there's one way to make it even better: when we have a list inside another like we're testing, we usually have to wrap them in an Expanded Widget (check the bad case scenario). Because of that, I believe it would be better if we test for that case as well in this rule.
WDYT? Does that make sense to you? Let me know if you need me to make the adjustments for you, I'd be glad to help!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gabrielaraujoz hi! Thanks for taking a look. Do you mean that we should first check if the ListView is in the Expanded and then if it's in the Column/Row/ListView?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey! If I'd do it, I'd check for both scenarios: if it's parent is an Expanded that has a Column/Row parent and the cases you're currently testing as well.
When working with Column and Row, if you have a widget with unbounded height/width you need to wrap it in a Flexible or Expanded, else you'll have the unbounded height/width exception. I don't believe this is necessary for ListViews tbh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gabrielaraujoz but it looks like wrapping in Expanded is a subset of all cases when a ListView is in the Column/Row, isn't it? Meaning, that the current check already covers it