Skip to content

Commit a2f7425

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[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 <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 50623f0 commit a2f7425

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

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

Lines changed: 15 additions & 6 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 '../util/ascii_utils.dart';
@@ -48,22 +50,29 @@ class NoLeadingUnderscoresForLibraryPrefixes extends LintRule {
4850
@override
4951
void registerNodeProcessors(
5052
NodeLintRegistry registry, LinterContext context) {
51-
var visitor = _Visitor(this);
53+
var visitor = _Visitor(this, context.libraryElement);
5254
registry.addImportDirective(this, visitor);
5355
}
5456
}
5557

5658
class _Visitor extends SimpleAstVisitor<void> {
59+
/// Whether the `wildcard_variables` feature is enabled.
60+
final bool _wildCardVariablesEnabled;
61+
5762
final LintRule rule;
5863

59-
_Visitor(this.rule);
64+
_Visitor(this.rule, LibraryElement? library)
65+
: _wildCardVariablesEnabled =
66+
library?.featureSet.isEnabled(Feature.wildcard_variables) ?? false;
6067

6168
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;
6574

66-
if (id.name.hasLeadingUnderscore) {
75+
if (name.hasLeadingUnderscore) {
6776
rule.reportLint(id, arguments: [id.name]);
6877
}
6978
}

pkg/linter/test/rules/no_leading_underscores_for_library_prefixes_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ import 'dart:async' as _async;
3333
test_snakeCase() async {
3434
await assertNoDiagnostics(r'''
3535
import 'dart:async' as dart_async;
36+
''');
37+
}
38+
39+
test_underscores() async {
40+
await assertDiagnostics(r'''
41+
import 'dart:async' as __;
42+
''', [
43+
lint(23, 2),
44+
]);
45+
}
46+
47+
test_wildcard() async {
48+
await assertNoDiagnostics(r'''
49+
import 'dart:async' as _;
3650
''');
3751
}
3852
}

0 commit comments

Comments
 (0)