-
Notifications
You must be signed in to change notification settings - Fork 13
Add the bloc_related_class_naming lint
#504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6629ac1
Add the `bloc_related_class_naming` lint
PiotrRogulski a70f645
Add tests for classes in parts
PiotrRogulski 4bf7dbe
Update note
PiotrRogulski b949472
Merge remote-tracking branch 'origin/master' into bloc-related-class-…
PiotrRogulski da2d097
Add a doc comment
PiotrRogulski a8d2c95
Simplify and allow configuring suffixes
PiotrRogulski a4eddec
Don't pass entire config around
PiotrRogulski 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/element/element.dart'; | ||
| import 'package:leancode_lint/src/type_checker.dart'; | ||
|
|
||
| const blocChecker = TypeChecker.fromName('Bloc', packageName: 'bloc'); | ||
|
|
||
| const cubitChecker = TypeChecker.fromName('Cubit', packageName: 'bloc'); | ||
|
|
||
| const blocPresentationMixinChecker = TypeChecker.fromName( | ||
| 'BlocPresentationMixin', | ||
| packageName: 'bloc_presentation', | ||
| ); | ||
|
|
||
| String? getBlocSubject(String className, {required BlocType blocType}) => | ||
| switch (blocType) { | ||
| .bloc when className.endsWith('Bloc') => className.substring( | ||
| 0, | ||
| className.length - 4, | ||
| ), | ||
| .cubit when className.endsWith('Cubit') => className.substring( | ||
| 0, | ||
| className.length - 5, | ||
| ), | ||
| _ => null, | ||
| }; | ||
|
|
||
| enum BlocType { bloc, cubit } | ||
|
|
||
| BlocType? determineBlocType(Element? element) { | ||
| if (element == null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (blocChecker.isAssignableFrom(element)) { | ||
| return .bloc; | ||
| } else if (cubitChecker.isAssignableFrom(element)) { | ||
| return .cubit; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| class BlocInfo { | ||
| const BlocInfo({ | ||
| required this.type, | ||
| this.stateType, | ||
| this.eventType, | ||
| this.presentationEventType, | ||
| }); | ||
|
|
||
| final BlocType type; | ||
| final TypeAnnotation? stateType; | ||
| final TypeAnnotation? eventType; | ||
| final TypeAnnotation? presentationEventType; | ||
| } | ||
|
|
||
| BlocInfo? getBlocInfo(ClassDeclaration node) { | ||
| final extendsClause = node.extendsClause; | ||
| final superclass = extendsClause?.superclass; | ||
| final superclassElement = superclass?.element; | ||
|
|
||
| final type = determineBlocType(superclassElement); | ||
| if (type == null) { | ||
| return null; | ||
| } | ||
|
|
||
| final typeArguments = superclass?.typeArguments?.arguments; | ||
|
|
||
| final (eventType, stateType) = switch (typeArguments) { | ||
| [final state] when type == .cubit => (null, state), | ||
| [final event, final state] when type == .bloc => (event, state), | ||
| _ => (null, null), | ||
| }; | ||
|
|
||
| TypeAnnotation? presentationEventType; | ||
| if (node.withClause case final withClause?) { | ||
| for (final mixin in withClause.mixinTypes) { | ||
| if (mixin.element case final mixinElement? | ||
| when blocPresentationMixinChecker.isExactly(mixinElement)) { | ||
| if (mixin.typeArguments?.arguments case [_, final presentationEvent]) { | ||
| presentationEventType = presentationEvent; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return .new( | ||
| type: type, | ||
| stateType: stateType, | ||
| eventType: eventType, | ||
| presentationEventType: presentationEventType, | ||
| ); | ||
| } |
106 changes: 106 additions & 0 deletions
106
packages/leancode_lint/lib/src/lints/bloc_related_class_naming.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,106 @@ | ||
| import 'package:analyzer/analysis_rule/analysis_rule.dart'; | ||
| import 'package:analyzer/analysis_rule/rule_context.dart'; | ||
| import 'package:analyzer/analysis_rule/rule_visitor_registry.dart'; | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:analyzer/error/error.dart'; | ||
| import 'package:leancode_lint/config.dart'; | ||
| import 'package:leancode_lint/src/bloc_utils.dart'; | ||
| import 'package:leancode_lint/src/utils.dart'; | ||
|
|
||
| /// Enforces consistent naming of state, event, and presentation event classes | ||
| /// related to a BLoC or Cubit. | ||
| /// | ||
| /// Given a BLoC or Cubit named `FooBloc` or `FooCubit`, the associated classes | ||
| /// should be named: | ||
| /// - state → `FooState` | ||
| /// - event → `FooEvent` | ||
| /// - presentation event → `FooPresentationEvent` | ||
| /// | ||
| /// The suffixes are configurable via [BlocRelatedClassNamingConfig]. | ||
| class BlocRelatedClassNaming extends AnalysisRule { | ||
| BlocRelatedClassNaming({this.config = const .new()}) | ||
| : super(name: code.lowerCaseName, description: code.problemMessage); | ||
|
|
||
| final BlocRelatedClassNamingConfig config; | ||
|
|
||
| static const code = LintCode( | ||
| 'bloc_related_class_naming', | ||
| "The name of {0}'s {1} should be {2}.", | ||
| severity: .WARNING, | ||
| ); | ||
|
|
||
| @override | ||
| LintCode get diagnosticCode => code; | ||
|
|
||
| @override | ||
| void registerNodeProcessors( | ||
| RuleVisitorRegistry registry, | ||
| RuleContext context, | ||
| ) { | ||
| registry.addClassDeclaration(this, _Visitor(this, context, config)); | ||
| } | ||
| } | ||
|
|
||
| class _Visitor extends SimpleAstVisitor<void> { | ||
| _Visitor(this.rule, this.context, this.config); | ||
|
|
||
| final AnalysisRule rule; | ||
| final RuleContext context; | ||
| final BlocRelatedClassNamingConfig config; | ||
|
|
||
| @override | ||
| void visitClassDeclaration(ClassDeclaration node) { | ||
| final blocInfo = getBlocInfo(node); | ||
| if (blocInfo == null) { | ||
| return; | ||
| } | ||
|
|
||
| final classElement = node.declaredFragment?.element; | ||
| final className = node.namePart.typeName.lexeme; | ||
| final subject = getBlocSubject(className, blocType: blocInfo.type); | ||
|
|
||
| if (subject == null) { | ||
| return; | ||
| } | ||
|
|
||
| void checkName(TypeAnnotation type, String classType, String suffix) { | ||
| final expectedName = '$subject$suffix'; | ||
|
|
||
| if (type case NamedType( | ||
| :final name, | ||
| :final element?, | ||
| :final CompilationUnit root, | ||
| ) when name.lexeme != expectedName) { | ||
| if (element.library != classElement?.library) { | ||
| return; | ||
| } | ||
|
|
||
| final declaration = root.declarations | ||
| .whereType<ClassDeclaration>() | ||
| .firstWhereOrNull((d) => d.declaredFragment?.element == element); | ||
|
|
||
| rule.reportAtToken( | ||
|
PiotrRogulski marked this conversation as resolved.
|
||
| declaration?.namePart.typeName ?? name, | ||
| arguments: [className, classType, expectedName], | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (blocInfo.stateType case final stateType?) { | ||
| checkName(stateType, 'state', config.stateSuffix); | ||
| } | ||
|
|
||
| if (blocInfo.eventType case final eventType?) { | ||
| checkName(eventType, 'event', config.eventSuffix); | ||
| } | ||
|
|
||
| if (blocInfo.presentationEventType case final presentationEventType?) { | ||
| checkName( | ||
| presentationEventType, | ||
| 'presentation event', | ||
| config.presentationEventSuffix, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.