Repository: felangel/bloc (dart)

Issue:
fix: Ignoring a lint warning only works with the ignore aligned at the start of the line
**Description**
I am using `@visibleForTesting` for a couple of public methods in my bloc. And therefore would like to ignore the lint warning.
However, when using `// ignore: avoid_public_bloc_methods`, it only works when this line is aligned at the start.
The dart formatter always aligns this ignore with two spaces at the start. So it always shows the warning.
Other lints from Dart itself work as expected.

Not aligned at the start:
![Image](https://github.com/user-attachments/assets/48365ab6-2be3-45c1-a60d-ebe4cec16718)

Aligned at the start:
![Image](https://github.com/user-attachments/assets/9db4d454-aa5e-4b7e-90a3-d0f47e9f3f9c)

**Expected Behavior**
I expect the lint ignore to work with leading spaces as well.

Pull request description:
<!--
  Thanks for contributing!

  Provide a description of your changes below and a general summary in the title

  Please look at the following checklist to ensure that your PR can be accepted quickly:
-->

## Status

**READY**

## Breaking Changes

NO

## Description

<!--- Describe your changes in detail -->
- fix(bloc_lint): ignore whitespace in ignore comments (closes #4645)

## Type of Change

<!--- Put an `x` in all the boxes that apply: -->

- [ ] ✨ New feature (non-breaking change which adds functionality)
- [X] 🛠️ Bug fix (non-breaking change which fixes an issue)
- [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change)
- [ ] 🧹 Code refactor
- [ ] ✅ Build configuration change
- [ ] 📝 Documentation
- [ ] 🗑️ Chore

Patch:
diff --git a/packages/bloc_lint/lib/src/text_document.dart b/packages/bloc_lint/lib/src/text_document.dart
index 5d672fff6db..ca6449bbef5 100644
--- a/packages/bloc_lint/lib/src/text_document.dart
+++ b/packages/bloc_lint/lib/src/text_document.dart
@@ -19,12 +19,14 @@ class TextDocument {
       _content = content;
 
   static final _ignoreForFileRegExp = RegExp(
-    r'^//\s*ignore_for_file:(.*?)$',
+    r'^\s*//\s*ignore_for_file:(.*?)$',
     dotAll: true,
     multiLine: true,
   );
 
-  static final _ignoreForLineRegExp = RegExp(r'^//\s*ignore:(.*)$');
+  static final _ignoreForLineRegExp = RegExp(r'^\s*//\s*ignore:(.*)$');
+
+  static final _ignoreAfterLineRegExp = RegExp(r'\s*//\s*ignore:(.*)$');
 
   final Uri _uri;
   final String _content;
@@ -62,7 +64,7 @@ class TextDocument {
         end: Position(character: _content.length, line: range.end.line),
       ),
     );
-    final index = afterText.indexOf('// ignore:');
+    final index = afterText.indexOf(_ignoreAfterLineRegExp);
     if (index == -1) return const <String>{};
     final line = afterText.substring(index);
     return _lineIgnores(line);


Test patch:
diff --git a/packages/bloc_lint/test/src/linter_test.dart b/packages/bloc_lint/test/src/linter_test.dart
index a46378def40..6a37005b8f4 100644
--- a/packages/bloc_lint/test/src/linter_test.dart
+++ b/packages/bloc_lint/test/src/linter_test.dart
@@ -127,6 +127,54 @@ bloc:
 import 'package:bloc/bloc.dart';
 import 'package:flutter/material.dart';
 
+enum CounterEvent { increment, decrement }
+class CounterBloc extends Bloc<CounterEvent, int> {
+  CounterBloc() : super(0);
+}
+''',
+      );
+
+      lintTest(
+        'does not report when rule is ignored for file (w/leading space)',
+        rule: AvoidFlutterImports.new,
+        path: 'counter_bloc.dart',
+        content: '''
+ // ignore_for_file: ${AvoidFlutterImports.rule}
+import 'package:bloc/bloc.dart';
+import 'package:flutter/material.dart';
+
+enum CounterEvent { increment, decrement }
+class CounterBloc extends Bloc<CounterEvent, int> {
+  CounterBloc() : super(0);
+}
+''',
+      );
+
+      lintTest(
+        'does not report when rule is ignored for file (w/in-between space)',
+        rule: AvoidFlutterImports.new,
+        path: 'counter_bloc.dart',
+        content: '''
+//   ignore_for_file: ${AvoidFlutterImports.rule}
+import 'package:bloc/bloc.dart';
+import 'package:flutter/material.dart';
+
+enum CounterEvent { increment, decrement }
+class CounterBloc extends Bloc<CounterEvent, int> {
+  CounterBloc() : super(0);
+}
+''',
+      );
+
+      lintTest(
+        'does not report when rule is ignored for file (w/trailing space)',
+        rule: AvoidFlutterImports.new,
+        path: 'counter_bloc.dart',
+        content: '''
+//   ignore_for_file: ${AvoidFlutterImports.rule}  
+import 'package:bloc/bloc.dart';
+import 'package:flutter/material.dart';
+
 enum CounterEvent { increment, decrement }
 class CounterBloc extends Bloc<CounterEvent, int> {
   CounterBloc() : super(0);
@@ -208,6 +256,51 @@ class CounterBloc extends Bloc<CounterEvent, int> {
 import 'package:bloc/bloc.dart';
 import 'package:flutter/material.dart'; // ignore: ${AvoidFlutterImports.rule}
 
+enum CounterEvent { increment, decrement }
+class CounterBloc extends Bloc<CounterEvent, int> {
+  CounterBloc() : super(0);
+}
+''',
+    );
+
+    lintTest(
+      'does not report when rule is ignored for line (after w/leading space)',
+      rule: AvoidFlutterImports.new,
+      path: 'counter_bloc.dart',
+      content: '''
+import 'package:bloc/bloc.dart';
+import 'package:flutter/material.dart'; //   ignore: ${AvoidFlutterImports.rule}
+
+enum CounterEvent { increment, decrement }
+class CounterBloc extends Bloc<CounterEvent, int> {
+  CounterBloc() : super(0);
+}
+''',
+    );
+
+    lintTest(
+      'does not report when rule is ignored for line (after w/in-between space)',
+      rule: AvoidFlutterImports.new,
+      path: 'counter_bloc.dart',
+      content: '''
+import 'package:bloc/bloc.dart';
+import 'package:flutter/material.dart'; // ignore:   ${AvoidFlutterImports.rule}
+
+enum CounterEvent { increment, decrement }
+class CounterBloc extends Bloc<CounterEvent, int> {
+  CounterBloc() : super(0);
+}
+''',
+    );
+
+    lintTest(
+      'does not report when rule is ignored for line (after w/trailing space)',
+      rule: AvoidFlutterImports.new,
+      path: 'counter_bloc.dart',
+      content: '''
+import 'package:bloc/bloc.dart';
+import 'package:flutter/material.dart'; // ignore: ${AvoidFlutterImports.rule}  
+
 enum CounterEvent { increment, decrement }
 class CounterBloc extends Bloc<CounterEvent, int> {
   CounterBloc() : super(0);