Skip to content

Commit

Permalink
[wildcards] no_wildcard_variable_uses feature awareness
Browse files Browse the repository at this point in the history
Fixes: dart-lang/linter#5072

Change-Id: I926ab733df2a8caaa3574fd4284618683780b37e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381920
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Aug 22, 2024
1 parent 48f66ec commit 5be7c56
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/linter/lib/src/rules/no_wildcard_variable_uses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/element/extensions.dart'; //ignore: implementation_imports

import '../analyzer.dart';
import '../linter_lint_codes.dart';
Expand Down Expand Up @@ -62,6 +63,10 @@ class NoWildcardVariableUses extends LintRule {
@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
if (context.libraryElement.hasWildcardVariablesFeatureEnabled) {
return;
}

var visitor = _Visitor(this);
registry.addSimpleIdentifier(this, visitor);
}
Expand Down
35 changes: 34 additions & 1 deletion pkg/linter/test/rules/no_wildcard_variable_uses_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class NoWildcardVariableUsesTest extends LintRuleTest {

test_constructor() async {
await assertNoDiagnostics(r'''
// @dart = 3.4
// (pre wildcard-variables)
class C {
C._();
m() {
Expand All @@ -31,6 +34,9 @@ class C {

test_declaredIdentifier() async {
await assertNoDiagnostics(r'''
// @dart = 3.4
// (pre wildcard-variables)
f() {
for (var _ in [1, 2, 3]) ;
}
Expand All @@ -39,6 +45,9 @@ f() {

test_field() async {
await assertNoDiagnostics(r'''
// @dart = 3.4
// (pre wildcard-variables)
class C {
int _ = 0;
m() {
Expand All @@ -50,6 +59,9 @@ class C {

test_getter() async {
await assertNoDiagnostics(r'''
// @dart = 3.4
// (pre wildcard-variables)
class C {
int get _ => 0;
m() {
Expand All @@ -72,8 +84,20 @@ f() {
]);
}

test_localVar_wildcardsEnabled() async {
// Ensure no lint.
await assertNoDiagnostics(r'''
f() {
var _ = 1;
}
''');
}

test_method() async {
await assertNoDiagnostics(r'''
// @dart = 3.4
// (pre wildcard-variables)
class C {
String _() => '';
m() {
Expand All @@ -86,16 +110,22 @@ class C {

test_param() async {
await assertDiagnostics(r'''
// @dart = 3.4
// (pre wildcard-variables)
f(int __) {
print(__);
}
''', [
lint(20, 2),
lint(64, 2),
]);
}

test_topLevelFunction() async {
await assertNoDiagnostics(r'''
// @dart = 3.4
// (pre wildcard-variables)
String _() => '';
f() {
Expand All @@ -107,6 +137,9 @@ f() {

test_topLevelGetter() async {
await assertNoDiagnostics(r'''
// @dart = 3.4
// (pre wildcard-variables)
int get _ => 0;
f() {
Expand Down

0 comments on commit 5be7c56

Please sign in to comment.