-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
prefer_sliver_prefix
rule (#32)
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
76 changes: 76 additions & 0 deletions
76
packages/altive_lints/lib/src/lints/prefer_sliver_prefix.dart
This file contains 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,76 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:collection/collection.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
/// A `prefer_sliver_prefix` rule that ensures widgets returning | ||
/// a Sliver-type widget have a "Sliver" prefix in their class names. | ||
/// | ||
/// This naming convention improves code readability and | ||
/// consistency by clearly indicating the widget's functionality and | ||
/// return type through its name. | ||
/// | ||
/// ### Example | ||
/// | ||
/// #### BAD: | ||
/// | ||
/// ```dart | ||
/// class MyCustomList extends StatelessWidget { | ||
/// @override | ||
/// Widget build(BuildContext context) { | ||
/// return SliverList(...); // LINT | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// #### GOOD: | ||
/// | ||
/// ```dart | ||
/// class SliverMyCustomList extends StatelessWidget { | ||
/// @override | ||
/// Widget build(BuildContext context) { | ||
/// return SliverList(...); | ||
/// } | ||
/// } | ||
/// ``` | ||
class PreferSliverPrefix extends DartLintRule { | ||
const PreferSliverPrefix() : super(code: _code); | ||
|
||
static const _code = LintCode( | ||
name: 'prefer_sliver_prefix', | ||
problemMessage: 'Widgets returning Sliver should have a "Sliver" prefix.', | ||
correctionMessage: 'Consider adding "Sliver" prefix to the widget name.', | ||
); | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ErrorReporter reporter, | ||
CustomLintContext context, | ||
) { | ||
context.registry.addClassDeclaration((node) { | ||
final className = node.name.lexeme; | ||
final methodBody = node.members | ||
.whereType<MethodDeclaration>() | ||
.firstWhereOrNull((method) => method.name.lexeme == 'build') | ||
?.body; | ||
|
||
if (methodBody is BlockFunctionBody) { | ||
final returnStatements = | ||
methodBody.block.statements.whereType<ReturnStatement>(); | ||
final returnsSliverWidget = returnStatements.any( | ||
(returnStatement) { | ||
final returnType = returnStatement.expression?.staticType; | ||
final typeName = | ||
returnType?.getDisplayString(withNullability: false); | ||
return typeName?.startsWith('Sliver') ?? false; | ||
}, | ||
); | ||
|
||
if (returnsSliverWidget && !className.startsWith('Sliver')) { | ||
reporter.reportErrorForNode(_code, node); | ||
} | ||
} | ||
}); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/altive_lints/lint_test/lints/prefer_sliver_prefix.dart
This file contains 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,16 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
// expect_lint: prefer_sliver_prefix | ||
class MyWidget extends StatelessWidget { | ||
const MyWidget({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return SliverList.builder( | ||
itemCount: 10, | ||
itemBuilder: (context, index) { | ||
return const Placeholder(); | ||
}, | ||
); | ||
} | ||
} |