@@ -7,9 +7,21 @@ import 'package:analyzer/error/listener.dart';
77import 'package:analyzer/source/line_info.dart' ;
88import 'package:analyzer/src/error/codes.dart' ;
99import 'package:analyzer/src/ignore_comments/ignore_info.dart' ;
10+ import 'package:analyzer/src/lint/registry.dart' ;
11+ import 'package:analyzer/src/lint/state.dart' ;
1012
1113/// Used to validate the ignore comments in a single file.
1214class IgnoreValidator {
15+ /// A list of known error codes used to ensure we don't over-report
16+ /// `unnecessary_ignore` s on error codes that may be contributed by a plugin.
17+ static final Set <String > _validErrorCodeNames =
18+ errorCodeValues.map ((e) => e.name.toLowerCase ()).toSet ();
19+
20+ /// The error code used to report `unnecessary_ignore` s.
21+ /// This code is set when the `UnnecessaryIgnore` lint rule is instantiated and
22+ /// registered by the linter.
23+ static late ErrorCode unnecessaryIgnoreLintCode;
24+
1325 /// The error reporter to which errors are to be reported.
1426 final ErrorReporter _errorReporter;
1527
@@ -28,11 +40,14 @@ class IgnoreValidator {
2840 /// use because we have no visibility of them here.
2941 final Set <String > _unignorableNames;
3042
43+ /// Whether to validate unnecessary ignores (enabled by the `unnecessary_ignore` lint).
44+ final bool _validateUnnecessaryIgnores;
45+
3146 /// Initialize a newly created validator to report any issues with ignore
3247 /// comments in the file being analyzed. The diagnostics will be reported to
3348 /// the [_errorReporter] .
3449 IgnoreValidator (this ._errorReporter, this ._reportedErrors, this ._ignoreInfo,
35- this ._lineInfo, this ._unignorableNames);
50+ this ._lineInfo, this ._unignorableNames, this ._validateUnnecessaryIgnores );
3651
3752 /// Report any issues with ignore comments in the file being analyzed.
3853 void reportErrors () {
@@ -124,7 +139,7 @@ class IgnoreValidator {
124139 // errorCode: WarningCode.UNIGNORABLE_IGNORE,
125140 // offset: unignorableName.offset,
126141 // length: name.length,
127- // data : [name]);
142+ // arguments : [name]);
128143 // list.remove(unignorableName);
129144 // }
130145 // }
@@ -153,46 +168,49 @@ class IgnoreValidator {
153168 /// Report the [ignoredNames] as being unnecessary.
154169 void _reportUnnecessaryOrRemovedOrDeprecatedIgnores (
155170 List <IgnoredElement > ignoredNames) {
156- // TODO(pq): find the right way to roll-out enablement and uncomment
157- // https://github.com/dart-lang/sdk/issues/51214
158- // for (var ignoredName in ignoredNames) {
159- // if (ignoredName is IgnoredDiagnosticName) {
160- // var name = ignoredName.name;
161- // var rule = Registry.ruleRegistry.getRule(name);
162- // if (rule != null) {
163- // var state = rule.state;
164- // var since = state.since.toString();
165- // if (state is DeprecatedState) {
166- // // `todo`(pq): implement
167- // } else if (state is RemovedState) {
168- // var replacedBy = state.replacedBy;
169- // if (replacedBy != null) {
170- // _errorReporter.atOffset(
171- // errorCode: WarningCode.REPLACED_LINT_USE,
172- // offset: ignoredName.offset,
173- // length: name.length,
174- // data: [name, since, replacedBy]);
175- // continue;
176- // } else {
177- // _errorReporter.atOffset(
178- // errorCode: WarningCode.REMOVED_LINT_USE,
179- // offset: ignoredName.offset,
180- // length: name.length,
181- // data: [name, since]);
182- // continue;
183- // }
184- // }
185- // }
186-
187- // // TODO(brianwilkerson): Uncomment the code below after the unnecessary
188- // // ignores in the Flutter code base have been cleaned up.
189- // _errorReporter.atOffset(
190- // errorCode: WarningCode.UNNECESSARY_IGNORE,
191- // offset: ignoredName.offset,
192- // length: name.length,
193- // data: [name]);
194- // }
195- // }
171+ if (! _validateUnnecessaryIgnores) return ;
172+
173+ for (var ignoredName in ignoredNames) {
174+ if (ignoredName is IgnoredDiagnosticName ) {
175+ var name = ignoredName.name;
176+ var rule = Registry .ruleRegistry.getRule (name);
177+ if (rule == null ) {
178+ // If a code is not a lint or a recognized error,
179+ // don't report. (It could come from a plugin.)
180+ // TODO(pq): consider another diagnostic that reports undefined codes
181+ if (! _validErrorCodeNames.contains (name.toLowerCase ())) continue ;
182+ } else {
183+ var state = rule.state;
184+ var since = state.since.toString ();
185+ if (state is DeprecatedState ) {
186+ // `todo`(pq): implement
187+ } else if (state is RemovedState ) {
188+ var replacedBy = state.replacedBy;
189+ if (replacedBy != null ) {
190+ _errorReporter.atOffset (
191+ errorCode: WarningCode .REPLACED_LINT_USE ,
192+ offset: ignoredName.offset,
193+ length: name.length,
194+ arguments: [name, since, replacedBy]);
195+ continue ;
196+ } else {
197+ _errorReporter.atOffset (
198+ errorCode: WarningCode .REMOVED_LINT_USE ,
199+ offset: ignoredName.offset,
200+ length: name.length,
201+ arguments: [name, since]);
202+ continue ;
203+ }
204+ }
205+ }
206+
207+ _errorReporter.atOffset (
208+ errorCode: unnecessaryIgnoreLintCode,
209+ offset: ignoredName.offset,
210+ length: name.length,
211+ arguments: [name]);
212+ }
213+ }
196214 }
197215}
198216
0 commit comments