|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a
|
3 | 3 | // BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
| 5 | +import 'package:analyzer/dart/analysis/features.dart'; |
5 | 6 | import 'package:analyzer/dart/ast/ast.dart';
|
6 | 7 | import 'package:analyzer/dart/ast/visitor.dart';
|
| 8 | +import 'package:analyzer/dart/element/element.dart'; |
7 | 9 |
|
8 | 10 | import '../analyzer.dart';
|
9 | 11 | import '../util/ascii_utils.dart';
|
@@ -48,22 +50,29 @@ class NoLeadingUnderscoresForLibraryPrefixes extends LintRule {
|
48 | 50 | @override
|
49 | 51 | void registerNodeProcessors(
|
50 | 52 | NodeLintRegistry registry, LinterContext context) {
|
51 |
| - var visitor = _Visitor(this); |
| 53 | + var visitor = _Visitor(this, context.libraryElement); |
52 | 54 | registry.addImportDirective(this, visitor);
|
53 | 55 | }
|
54 | 56 | }
|
55 | 57 |
|
56 | 58 | class _Visitor extends SimpleAstVisitor<void> {
|
| 59 | + /// Whether the `wildcard_variables` feature is enabled. |
| 60 | + final bool _wildCardVariablesEnabled; |
| 61 | + |
57 | 62 | final LintRule rule;
|
58 | 63 |
|
59 |
| - _Visitor(this.rule); |
| 64 | + _Visitor(this.rule, LibraryElement? library) |
| 65 | + : _wildCardVariablesEnabled = |
| 66 | + library?.featureSet.isEnabled(Feature.wildcard_variables) ?? false; |
60 | 67 |
|
61 | 68 | void checkIdentifier(SimpleIdentifier? id) {
|
62 |
| - if (id == null) { |
63 |
| - return; |
64 |
| - } |
| 69 | + if (id == null) return; |
| 70 | + |
| 71 | + var name = id.name; |
| 72 | + |
| 73 | + if (_wildCardVariablesEnabled && name == '_') return; |
65 | 74 |
|
66 |
| - if (id.name.hasLeadingUnderscore) { |
| 75 | + if (name.hasLeadingUnderscore) { |
67 | 76 | rule.reportLint(id, arguments: [id.name]);
|
68 | 77 | }
|
69 | 78 | }
|
|
0 commit comments