This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lint): implement event_base_class_suffix rule
Implements: #7
- Loading branch information
Showing
5 changed files
with
111 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
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,29 @@ | ||
event_base_class_suffix | ||
=== | ||
severity: WARNING | ||
|
||
The base event class should always be suffixed by `Event`. | ||
|
||
## Example: | ||
|
||
❌ **BAD**: | ||
|
||
```dart | ||
@immutable | ||
sealed class CounterData {} | ||
final class CounterStarted extends CounterData {} | ||
``` | ||
|
||
✅ **GOOD**: | ||
|
||
```dart | ||
@immutable | ||
sealed class CounterEvent {} | ||
final class CounterStarted extends CounterEvent {} | ||
``` | ||
|
||
## Additional Resources | ||
|
||
- [Bloc Library Documentation: Naming Conventions / Event Conventions](https://bloclibrary.dev/naming-conventions/#event-conventions) |
23 changes: 23 additions & 0 deletions
23
example/lib/lint_rules/event_base_class_suffix_lint_rule_test_bloc.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,23 @@ | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
class EventBaseClassSuffixLintRuleTestBloc extends Bloc< | ||
// expect_lint: event_base_class_suffix | ||
EventBaseClassSuffixLintRuleTestError, | ||
EventBaseClassSuffixLintRuleTestState> { | ||
EventBaseClassSuffixLintRuleTestBloc() | ||
: super(EventBaseClassSuffixLintRuleTestInitial()) { | ||
on<EventBaseClassSuffixLintRuleTestError>((event, emit) { | ||
// TODO: implement event handler | ||
}); | ||
} | ||
} | ||
|
||
@immutable | ||
sealed class EventBaseClassSuffixLintRuleTestError {} | ||
|
||
@immutable | ||
sealed class EventBaseClassSuffixLintRuleTestState {} | ||
|
||
final class EventBaseClassSuffixLintRuleTestInitial | ||
extends EventBaseClassSuffixLintRuleTestState {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/element/type.dart'; | ||
import 'package:analyzer/error/error.dart'; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:bloc_lint/bloc_lint_constants.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
class EventBaseClassSuffix extends DartLintRule { | ||
const EventBaseClassSuffix() | ||
: super( | ||
code: const LintCode( | ||
name: 'event_base_class_suffix', | ||
problemMessage: | ||
'''The base event class should always be suffixed by 'Event'.''', | ||
errorSeverity: ErrorSeverity.WARNING, | ||
), | ||
); | ||
|
||
static const _ignoredType = [ | ||
'int', | ||
'double', | ||
'num', | ||
'String', | ||
'bool', | ||
'List', | ||
'Set', | ||
'Map', | ||
]; | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ErrorReporter reporter, | ||
CustomLintContext context, | ||
) { | ||
context.registry.addExtendsClause((node) { | ||
final superClass = node.superclass; | ||
TypeAnnotation? stateNode; | ||
|
||
if (superClass.element?.name == BlocLintConstants.blocClass) { | ||
stateNode = superClass.typeArguments?.arguments[0]; | ||
} | ||
if (stateNode == null) { | ||
return; | ||
} | ||
final stateType = stateNode.type as InterfaceType?; | ||
if (stateType == null || _ignoredType.contains(stateType.element.name)) { | ||
return; | ||
} | ||
|
||
if (stateType.element.name.endsWith('Event') == false) { | ||
reporter.atNode(stateNode, code); | ||
} | ||
}); | ||
} | ||
} |