|
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 '../utils.dart';
|
@@ -54,21 +56,32 @@ class LibraryPrefixes extends LintRule {
|
54 | 56 | @override
|
55 | 57 | void registerNodeProcessors(
|
56 | 58 | NodeLintRegistry registry, LinterContext context) {
|
57 |
| - var visitor = _Visitor(this); |
| 59 | + var visitor = _Visitor(this, context.libraryElement); |
58 | 60 | registry.addImportDirective(this, visitor);
|
59 | 61 | }
|
60 | 62 | }
|
61 | 63 |
|
62 | 64 | class _Visitor extends SimpleAstVisitor<void> {
|
| 65 | + /// Whether the `wildcard_variables` feature is enabled. |
| 66 | + final bool _wildCardVariablesEnabled; |
| 67 | + |
63 | 68 | final LintRule rule;
|
64 | 69 |
|
65 |
| - _Visitor(this.rule); |
| 70 | + _Visitor(this.rule, LibraryElement? library) |
| 71 | + : _wildCardVariablesEnabled = |
| 72 | + library?.featureSet.isEnabled(Feature.wildcard_variables) ?? false; |
66 | 73 |
|
67 | 74 | @override
|
68 | 75 | void visitImportDirective(ImportDirective node) {
|
69 | 76 | var prefix = node.prefix;
|
70 |
| - if (prefix != null && !isValidLibraryPrefix(prefix.toString())) { |
71 |
| - rule.reportLint(prefix, arguments: [prefix.toString()]); |
| 77 | + if (prefix == null) return; |
| 78 | + |
| 79 | + var prefixString = prefix.toString(); |
| 80 | + // With wildcards, `_` is allowed. |
| 81 | + if (_wildCardVariablesEnabled && prefixString == '_') return; |
| 82 | + |
| 83 | + if (!isValidLibraryPrefix(prefixString)) { |
| 84 | + rule.reportLint(prefix, arguments: [prefixString]); |
72 | 85 | }
|
73 | 86 | }
|
74 | 87 | }
|
0 commit comments