Skip to content

Commit 4a7db83

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[wildcards] allow overridden wildcards in avoid_renaming_method_parameters
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]>
1 parent caa36cf commit 4a7db83

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

Diff for: pkg/linter/lib/src/rules/avoid_renaming_method_parameters.dart

+13-11
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class _Visitor extends SimpleAstVisitor<void> {
7878
: _wildCardVariablesEnabled =
7979
library?.featureSet.isEnabled(Feature.wildcard_variables) ?? false;
8080

81+
bool isWildcardIdentifier(String lexeme) =>
82+
_wildCardVariablesEnabled && lexeme == '_';
83+
8184
@override
8285
void visitMethodDeclaration(MethodDeclaration node) {
8386
if (node.isStatic) return;
@@ -125,19 +128,18 @@ class _Visitor extends SimpleAstVisitor<void> {
125128
for (var i = 0; i < count; i++) {
126129
if (parentParameters.length <= i) break;
127130

128-
var paramIdentifier = parameters[i].name;
129-
if (paramIdentifier == null) {
130-
continue;
131-
}
131+
var parentParameterName = parentParameters[i].name;
132+
if (isWildcardIdentifier(parentParameterName)) continue;
132133

133-
var paramLexeme = paramIdentifier.lexeme;
134-
if (_wildCardVariablesEnabled && paramLexeme == '_') {
135-
continue; // wildcard identifier
136-
}
134+
var parameterName = parameters[i].name;
135+
if (parameterName == null) continue;
136+
137+
var paramLexeme = parameterName.lexeme;
138+
if (isWildcardIdentifier(paramLexeme)) continue;
137139

138-
if (paramLexeme != parentParameters[i].name) {
139-
rule.reportLintForToken(paramIdentifier,
140-
arguments: [paramIdentifier.lexeme, parentParameters[i].name]);
140+
if (paramLexeme != parentParameterName) {
141+
rule.reportLintForToken(parameterName,
142+
arguments: [paramLexeme, parentParameterName]);
141143
}
142144
}
143145
}

Diff for: pkg/linter/test/rules/avoid_renaming_method_parameters_test.dart

+22
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,28 @@ class B extends A {
244244
]);
245245
}
246246

247+
test_wildcardInBase() async {
248+
await assertNoDiagnostics(r'''
249+
class A {
250+
void m(int _, int b, int c) {}
251+
}
252+
class B extends A {
253+
void m(a, b, c) {}
254+
}
255+
''');
256+
}
257+
258+
test_wildcardInBaseAndSub() async {
259+
await assertNoDiagnostics(r'''
260+
class A {
261+
void m(int _, int b, int c) {}
262+
}
263+
class B extends A {
264+
void m(a, b, _) {}
265+
}
266+
''');
267+
}
268+
247269
test_zeroParameters() async {
248270
await assertNoDiagnostics(r'''
249271
class A {

0 commit comments

Comments
 (0)