Skip to content

Commit 701eae7

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[wildcards] support for library_prefixes
Fixes: https://github.com/dart-lang/linter/issues/5021 Change-Id: I43273a6bef6987298691daaf4b320a4d59e535be Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/375782 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 37f7206 commit 701eae7

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

pkg/linter/lib/src/rules/library_prefixes.dart

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/dart/analysis/features.dart';
56
import 'package:analyzer/dart/ast/ast.dart';
67
import 'package:analyzer/dart/ast/visitor.dart';
8+
import 'package:analyzer/dart/element/element.dart';
79

810
import '../analyzer.dart';
911
import '../utils.dart';
@@ -54,21 +56,32 @@ class LibraryPrefixes extends LintRule {
5456
@override
5557
void registerNodeProcessors(
5658
NodeLintRegistry registry, LinterContext context) {
57-
var visitor = _Visitor(this);
59+
var visitor = _Visitor(this, context.libraryElement);
5860
registry.addImportDirective(this, visitor);
5961
}
6062
}
6163

6264
class _Visitor extends SimpleAstVisitor<void> {
65+
/// Whether the `wildcard_variables` feature is enabled.
66+
final bool _wildCardVariablesEnabled;
67+
6368
final LintRule rule;
6469

65-
_Visitor(this.rule);
70+
_Visitor(this.rule, LibraryElement? library)
71+
: _wildCardVariablesEnabled =
72+
library?.featureSet.isEnabled(Feature.wildcard_variables) ?? false;
6673

6774
@override
6875
void visitImportDirective(ImportDirective node) {
6976
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]);
7285
}
7386
}
7487
}

pkg/linter/test/rules/library_prefixes_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,21 @@ import 'dart:async' as _1;
5555
lint(23, 2),
5656
]);
5757
}
58+
59+
test_wildcard() async {
60+
await assertNoDiagnostics(r'''
61+
import 'dart:async' as _;
62+
''');
63+
}
64+
65+
test_wildcard_preWildCards() async {
66+
await assertDiagnostics(r'''
67+
// @dart = 3.4
68+
// (pre wildcard-variables)
69+
70+
import 'dart:async' as _;
71+
''', [
72+
lint(67, 1),
73+
]);
74+
}
5875
}

0 commit comments

Comments
 (0)