|
| 1 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 2 | +import 'package:analyzer/error/listener.dart'; |
| 3 | +import 'package:collection/collection.dart'; |
| 4 | +import 'package:custom_lint_builder/custom_lint_builder.dart'; |
| 5 | + |
| 6 | +/// A `prefer_to_include_sliver_in_name` rule that ensures widgets returning |
| 7 | +/// a Sliver-type widget include "Sliver" in their class names. |
| 8 | +/// |
| 9 | +/// This naming convention improves code readability and consistency |
| 10 | +/// by clearly indicating the widget's functionality |
| 11 | +/// and return type through its name. |
| 12 | +/// |
| 13 | +/// The rule also applies if "Sliver" is present in the named constructor, |
| 14 | +/// allowing flexibility in how the convention is followed. |
| 15 | +/// |
| 16 | +/// ### Example |
| 17 | +/// |
| 18 | +/// #### BAD: |
| 19 | +/// |
| 20 | +/// ```dart |
| 21 | +/// class MyCustomList extends StatelessWidget { |
| 22 | +/// @override |
| 23 | +/// Widget build(BuildContext context) { |
| 24 | +/// return SliverList(...); // LINT |
| 25 | +/// } |
| 26 | +/// } |
| 27 | +/// ``` |
| 28 | +/// |
| 29 | +/// #### GOOD: |
| 30 | +/// |
| 31 | +/// ```dart |
| 32 | +/// class SliverMyCustomList extends StatelessWidget { |
| 33 | +/// @override |
| 34 | +/// Widget build(BuildContext context) { |
| 35 | +/// return SliverList(...); |
| 36 | +/// } |
| 37 | +/// } |
| 38 | +/// ``` |
| 39 | +class PreferToIncludeSliverInName extends DartLintRule { |
| 40 | + /// Creates a new instance of [PreferToIncludeSliverInName]. |
| 41 | + const PreferToIncludeSliverInName() : super(code: _code); |
| 42 | + |
| 43 | + static const _code = LintCode( |
| 44 | + name: 'prefer_to_include_sliver_in_name', |
| 45 | + problemMessage: 'Widgets returning Sliver should include "Sliver" ' |
| 46 | + 'in the class name or named constructor.', |
| 47 | + correctionMessage: |
| 48 | + 'Consider adding "Sliver" to the class name or a named constructor.', |
| 49 | + ); |
| 50 | + |
| 51 | + @override |
| 52 | + void run( |
| 53 | + CustomLintResolver resolver, |
| 54 | + ErrorReporter reporter, |
| 55 | + CustomLintContext context, |
| 56 | + ) { |
| 57 | + context.registry.addClassDeclaration((node) { |
| 58 | + final methodBody = node.members |
| 59 | + .whereType<MethodDeclaration>() |
| 60 | + .firstWhereOrNull((method) => method.name.lexeme == 'build') |
| 61 | + ?.body; |
| 62 | + |
| 63 | + if (methodBody is! BlockFunctionBody) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + final returnStatements = |
| 68 | + methodBody.block.statements.whereType<ReturnStatement>(); |
| 69 | + final returnsSliverWidget = returnStatements.any( |
| 70 | + (returnStatement) { |
| 71 | + final returnType = returnStatement.expression?.staticType; |
| 72 | + final typeName = returnType?.getDisplayString(); |
| 73 | + return typeName?.startsWith('Sliver') ?? false; |
| 74 | + }, |
| 75 | + ); |
| 76 | + |
| 77 | + if (!returnsSliverWidget) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + final className = node.name.lexeme; |
| 82 | + |
| 83 | + if (className.contains('Sliver')) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + final constructorNames = node.members |
| 88 | + .whereType<ConstructorDeclaration>() |
| 89 | + .map((constructor) => constructor.name?.lexeme) |
| 90 | + .nonNulls; |
| 91 | + |
| 92 | + final hasSliverInConstructor = constructorNames.any( |
| 93 | + (constructorName) => constructorName.toLowerCase().contains('sliver'), |
| 94 | + ); |
| 95 | + |
| 96 | + if (hasSliverInConstructor) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + reporter.atNode(node, _code); |
| 101 | + }); |
| 102 | + } |
| 103 | +} |
0 commit comments