-
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: Improve prefer_sliver_prefix to prefer_to_include_sliver_in_name (
#58) * feat: Improve prefer_sliver_prefix to prefer_to_include_sliver_in_name * chore: use early return
- Loading branch information
Showing
6 changed files
with
173 additions
and
97 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
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: 0 additions & 76 deletions
76
packages/altive_lints/lib/src/lints/prefer_sliver_prefix.dart
This file was deleted.
Oops, something went wrong.
103 changes: 103 additions & 0 deletions
103
packages/altive_lints/lib/src/lints/prefer_to_include_sliver_in_name.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,103 @@ | ||
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_to_include_sliver_in_name` rule that ensures widgets returning | ||
/// a Sliver-type widget include "Sliver" 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. | ||
/// | ||
/// The rule also applies if "Sliver" is present in the named constructor, | ||
/// allowing flexibility in how the convention is followed. | ||
/// | ||
/// ### 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 PreferToIncludeSliverInName extends DartLintRule { | ||
/// Creates a new instance of [PreferToIncludeSliverInName]. | ||
const PreferToIncludeSliverInName() : super(code: _code); | ||
|
||
static const _code = LintCode( | ||
name: 'prefer_to_include_sliver_in_name', | ||
problemMessage: 'Widgets returning Sliver should include "Sliver" ' | ||
'in the class name or named constructor.', | ||
correctionMessage: | ||
'Consider adding "Sliver" to the class name or a named constructor.', | ||
); | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ErrorReporter reporter, | ||
CustomLintContext context, | ||
) { | ||
context.registry.addClassDeclaration((node) { | ||
final methodBody = node.members | ||
.whereType<MethodDeclaration>() | ||
.firstWhereOrNull((method) => method.name.lexeme == 'build') | ||
?.body; | ||
|
||
if (methodBody is! BlockFunctionBody) { | ||
return; | ||
} | ||
|
||
final returnStatements = | ||
methodBody.block.statements.whereType<ReturnStatement>(); | ||
final returnsSliverWidget = returnStatements.any( | ||
(returnStatement) { | ||
final returnType = returnStatement.expression?.staticType; | ||
final typeName = returnType?.getDisplayString(); | ||
return typeName?.startsWith('Sliver') ?? false; | ||
}, | ||
); | ||
|
||
if (!returnsSliverWidget) { | ||
return; | ||
} | ||
|
||
final className = node.name.lexeme; | ||
|
||
if (className.contains('Sliver')) { | ||
return; | ||
} | ||
|
||
final constructorNames = node.members | ||
.whereType<ConstructorDeclaration>() | ||
.map((constructor) => constructor.name?.lexeme) | ||
.nonNulls; | ||
|
||
final hasSliverInConstructor = constructorNames.any( | ||
(constructorName) => constructorName.toLowerCase().contains('sliver'), | ||
); | ||
|
||
if (hasSliverInConstructor) { | ||
return; | ||
} | ||
|
||
reporter.atNode(node, _code); | ||
}); | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
packages/altive_lints/lint_test/lints/prefer_sliver_prefix.dart
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
packages/altive_lints/lint_test/lints/prefer_to_include_sliver_in_name.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,65 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
// expect_lint: prefer_to_include_sliver_in_name | ||
class MyWidget extends StatelessWidget { | ||
const MyWidget({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return SliverList.builder( | ||
itemCount: 10, | ||
itemBuilder: (context, index) { | ||
return const Placeholder(); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
// expect_lint: prefer_to_include_sliver_in_name | ||
class MyWidget2 extends StatelessWidget { | ||
const MyWidget2({super.key, this.maxCount = 10}); | ||
|
||
const MyWidget2.small({super.key, this.maxCount = 5}); | ||
|
||
final int maxCount; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return SliverList.builder( | ||
itemCount: maxCount, | ||
itemBuilder: (context, index) { | ||
return const Placeholder(); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
class MySliverWidget extends StatelessWidget { | ||
const MySliverWidget({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return SliverList.builder( | ||
itemCount: 10, | ||
itemBuilder: (context, index) { | ||
return const Placeholder(); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
class MyWidget3 extends StatelessWidget { | ||
const MyWidget3({super.key}); | ||
|
||
const MyWidget3.sliver({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return SliverList.builder( | ||
itemCount: 10, | ||
itemBuilder: (context, index) { | ||
return const Placeholder(); | ||
}, | ||
); | ||
} | ||
} |