Repository: felangel/bloc

Issue:
fix(bloc_lint): only `*_bloc.dart` and `*_cubit.dart` are included in analysis
**Description**
The `bloc_lint` rules should apply to all bloc and cubit instances not just instances that are declared in files ending in `_bloc.dart` or `_cubit.dart`.



Discussion: Was curious about this choice as I'm currently working on implementing a lint rule. We could also counter that `Cubit`s and `Bloc`s should _always_ be declared in `_cubit.dart` and `_bloc.dart` files respectively.
> We could also counter that Cubits and Blocs should always be declared in _cubit.dart and _bloc.dart files respectively.

I think a lint rule to enforce the file suffixes would be a nice addition.

Fix patch:
diff --git a/packages/bloc_lint/lib/src/rules/avoid_public_bloc_methods.dart b/packages/bloc_lint/lib/src/rules/avoid_public_bloc_methods.dart
index 9b00242c1a7..395847a4ab3 100644
--- a/packages/bloc_lint/lib/src/rules/avoid_public_bloc_methods.dart
+++ b/packages/bloc_lint/lib/src/rules/avoid_public_bloc_methods.dart
@@ -12,10 +12,7 @@ class AvoidPublicBlocMethods extends LintRule {
   static const rule = 'avoid_public_bloc_methods';
 
   @override
-  Listener? create(LintContext context) {
-    if (!context.document.type.isBloc) return null;
-    return _Listener(context);
-  }
+  Listener? create(LintContext context) => _Listener(context);
 }
 
 class _Listener extends Listener {
diff --git a/packages/bloc_lint/lib/src/rules/avoid_public_fields.dart b/packages/bloc_lint/lib/src/rules/avoid_public_fields.dart
index add03e675a3..0d97a0f6524 100644
--- a/packages/bloc_lint/lib/src/rules/avoid_public_fields.dart
+++ b/packages/bloc_lint/lib/src/rules/avoid_public_fields.dart
@@ -12,10 +12,7 @@ class AvoidPublicFields extends LintRule {
   static const rule = 'avoid_public_fields';
 
   @override
-  Listener? create(LintContext context) {
-    if (context.document.type.isOther) return null;
-    return _Listener(context);
-  }
+  Listener? create(LintContext context) => _Listener(context);
 }
 
 class _Listener extends Listener {
diff --git a/packages/bloc_lint/lib/src/rules/prefer_void_public_cubit_methods.dart b/packages/bloc_lint/lib/src/rules/prefer_void_public_cubit_methods.dart
index edb6a57b451..6e72e559e11 100644
--- a/packages/bloc_lint/lib/src/rules/prefer_void_public_cubit_methods.dart
+++ b/packages/bloc_lint/lib/src/rules/prefer_void_public_cubit_methods.dart
@@ -12,10 +12,7 @@ class PreferVoidPublicCubitMethods extends LintRule {
   static const rule = 'prefer_void_public_cubit_methods';
 
   @override
-  Listener? create(LintContext context) {
-    if (!context.document.type.isCubit) return null;
-    return _Listener(context);
-  }
+  Listener? create(LintContext context) => _Listener(context);
 }
 
 class _Listener extends Listener {


Test patch:
diff --git a/packages/bloc_lint/test/src/rules/avoid_public_bloc_methods_test.dart b/packages/bloc_lint/test/src/rules/avoid_public_bloc_methods_test.dart
index 083e9aed344..dbf5e8fc302 100644
--- a/packages/bloc_lint/test/src/rules/avoid_public_bloc_methods_test.dart
+++ b/packages/bloc_lint/test/src/rules/avoid_public_bloc_methods_test.dart
@@ -12,6 +12,23 @@ void main() {
       content: '''
 import 'package:bloc/bloc.dart';
 
+enum CounterEvent { increment, decrement }
+class CounterBloc extends Bloc<CounterEvent, int> {
+  CounterBloc() : super(0);
+
+  void foo() { print ('foo'); }
+       ^^^
+}
+''',
+    );
+
+    lintTest(
+      'lints when file name does not follow naming conventions',
+      rule: AvoidPublicBlocMethods.new,
+      path: 'main.dart',
+      content: '''
+import 'package:bloc/bloc.dart';
+
 enum CounterEvent { increment, decrement }
 class CounterBloc extends Bloc<CounterEvent, int> {
   CounterBloc() : super(0);
diff --git a/packages/bloc_lint/test/src/rules/avoid_public_fields_test.dart b/packages/bloc_lint/test/src/rules/avoid_public_fields_test.dart
index 6a91826fb85..8ad540b8b01 100644
--- a/packages/bloc_lint/test/src/rules/avoid_public_fields_test.dart
+++ b/packages/bloc_lint/test/src/rules/avoid_public_fields_test.dart
@@ -12,6 +12,23 @@ void main() {
       content: '''
 import 'package:bloc/bloc.dart';
 
+enum CounterEvent { increment, decrement }
+class CounterBloc extends Bloc<CounterEvent, int> {
+  CounterBloc() : super(0);
+
+  var count = 0;
+  ^^^^^^^^^^^^^
+}
+''',
+    );
+
+    lintTest(
+      'lints when file name does not follow naming conventions',
+      rule: AvoidPublicFields.new,
+      path: 'main.dart',
+      content: '''
+import 'package:bloc/bloc.dart';
+
 enum CounterEvent { increment, decrement }
 class CounterBloc extends Bloc<CounterEvent, int> {
   CounterBloc() : super(0);
diff --git a/packages/bloc_lint/test/src/rules/prefer_void_public_cubit_methods_test.dart b/packages/bloc_lint/test/src/rules/prefer_void_public_cubit_methods_test.dart
index 21619db4e56..ac5e72f0751 100644
--- a/packages/bloc_lint/test/src/rules/prefer_void_public_cubit_methods_test.dart
+++ b/packages/bloc_lint/test/src/rules/prefer_void_public_cubit_methods_test.dart
@@ -149,6 +149,24 @@ class CounterCubit extends Cubit<int> {
 ''',
     );
 
+    lintTest(
+      'lints when file name does not follow naming conventions',
+      rule: PreferVoidPublicCubitMethods.new,
+      path: 'main.dart',
+      content: '''
+import 'package:bloc/bloc.dart';
+
+class CounterCubit extends Cubit<int> {
+  CounterCubit() : super(0);
+
+  void increment() => emit(state + 1);
+
+  bool get isEven => state.isEven;
+           ^^^^^^
+}
+''',
+    );
+
     lintTest(
       'lints when public getter exists on Cubit (bool)',
       rule: PreferVoidPublicCubitMethods.new,