[wildcards] support for `no_leading_underscores_for_library_prefixes`
Fixes: https://github.com/dart-lang/linter/issues/5039
Change-Id: I71c3af855c8744877711921ab0e7b27293a53c01
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/378570
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
diff --git a/pkg/linter/lib/src/rules/no_leading_underscores_for_library_prefixes.dart b/pkg/linter/lib/src/rules/no_leading_underscores_for_library_prefixes.dart
index 51cfafe..dda7ced 100644
--- a/pkg/linter/lib/src/rules/no_leading_underscores_for_library_prefixes.dart
+++ b/pkg/linter/lib/src/rules/no_leading_underscores_for_library_prefixes.dart
@@ -2,8 +2,10 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
+import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
+import 'package:analyzer/dart/element/element.dart';
import '../analyzer.dart';
import '../util/ascii_utils.dart';
@@ -48,22 +50,29 @@
@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
- var visitor = _Visitor(this);
+ var visitor = _Visitor(this, context.libraryElement);
registry.addImportDirective(this, visitor);
}
}
class _Visitor extends SimpleAstVisitor<void> {
+ /// Whether the `wildcard_variables` feature is enabled.
+ final bool _wildCardVariablesEnabled;
+
final LintRule rule;
- _Visitor(this.rule);
+ _Visitor(this.rule, LibraryElement? library)
+ : _wildCardVariablesEnabled =
+ library?.featureSet.isEnabled(Feature.wildcard_variables) ?? false;
void checkIdentifier(SimpleIdentifier? id) {
- if (id == null) {
- return;
- }
+ if (id == null) return;
- if (id.name.hasLeadingUnderscore) {
+ var name = id.name;
+
+ if (_wildCardVariablesEnabled && name == '_') return;
+
+ if (name.hasLeadingUnderscore) {
rule.reportLint(id, arguments: [id.name]);
}
}
diff --git a/pkg/linter/test/rules/no_leading_underscores_for_library_prefixes_test.dart b/pkg/linter/test/rules/no_leading_underscores_for_library_prefixes_test.dart
index 2ab006f..4215fca 100644
--- a/pkg/linter/test/rules/no_leading_underscores_for_library_prefixes_test.dart
+++ b/pkg/linter/test/rules/no_leading_underscores_for_library_prefixes_test.dart
@@ -35,4 +35,18 @@
import 'dart:async' as dart_async;
''');
}
+
+ test_underscores() async {
+ await assertDiagnostics(r'''
+import 'dart:async' as __;
+''', [
+ lint(23, 2),
+ ]);
+ }
+
+ test_wildcard() async {
+ await assertNoDiagnostics(r'''
+import 'dart:async' as _;
+''');
+ }
}