Skip to content

Commit

Permalink
[wildcards] allow overridden wildcards in `avoid_renaming_method_para…
Browse files Browse the repository at this point in the history
…meters`

Allow overriding methods to add variable names where they are declared as wildcards in their base implementation.

Follow-up from https://github.com/dart-lang/linter/issues/5025.

Change-Id: I5c9ad1dc5a946e5062cb51f8069ca3af1bd81a00
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/383708
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Sep 5, 2024
1 parent caa36cf commit 4a7db83
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
24 changes: 13 additions & 11 deletions pkg/linter/lib/src/rules/avoid_renaming_method_parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class _Visitor extends SimpleAstVisitor<void> {
: _wildCardVariablesEnabled =
library?.featureSet.isEnabled(Feature.wildcard_variables) ?? false;

bool isWildcardIdentifier(String lexeme) =>
_wildCardVariablesEnabled && lexeme == '_';

@override
void visitMethodDeclaration(MethodDeclaration node) {
if (node.isStatic) return;
Expand Down Expand Up @@ -125,19 +128,18 @@ class _Visitor extends SimpleAstVisitor<void> {
for (var i = 0; i < count; i++) {
if (parentParameters.length <= i) break;

var paramIdentifier = parameters[i].name;
if (paramIdentifier == null) {
continue;
}
var parentParameterName = parentParameters[i].name;
if (isWildcardIdentifier(parentParameterName)) continue;

var paramLexeme = paramIdentifier.lexeme;
if (_wildCardVariablesEnabled && paramLexeme == '_') {
continue; // wildcard identifier
}
var parameterName = parameters[i].name;
if (parameterName == null) continue;

var paramLexeme = parameterName.lexeme;
if (isWildcardIdentifier(paramLexeme)) continue;

if (paramLexeme != parentParameters[i].name) {
rule.reportLintForToken(paramIdentifier,
arguments: [paramIdentifier.lexeme, parentParameters[i].name]);
if (paramLexeme != parentParameterName) {
rule.reportLintForToken(parameterName,
arguments: [paramLexeme, parentParameterName]);
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/linter/test/rules/avoid_renaming_method_parameters_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,28 @@ class B extends A {
]);
}

test_wildcardInBase() async {
await assertNoDiagnostics(r'''
class A {
void m(int _, int b, int c) {}
}
class B extends A {
void m(a, b, c) {}
}
''');
}

test_wildcardInBaseAndSub() async {
await assertNoDiagnostics(r'''
class A {
void m(int _, int b, int c) {}
}
class B extends A {
void m(a, b, _) {}
}
''');
}

test_zeroParameters() async {
await assertNoDiagnostics(r'''
class A {
Expand Down

0 comments on commit 4a7db83

Please sign in to comment.