Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 62e5602

Browse files
authored
fix: correctly handle method invocations on getters (#642)
* fix: correctly handle method invocations on getters * chore: update changelog * fix: restore removed code for property access method of calls
1 parent 860ff4e commit 62e5602

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
## Unreleased
44

55
* feat: support excludes for a separate anti-pattern.
6-
* fix: prefer-extracting-callbacks in nested widgets
6+
* fix: prefer-extracting-callbacks in nested widgets.
7+
* fix: correctly handle method invocations on getters and method of for `check-unused-l10n` command.
78

89
## 4.9.0
910

lib/src/analyzers/unused_l10n_analyzer/unused_l10n_visitor.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class UnusedL10nVisitor extends RecursiveAstVisitor<void> {
3737
if (_matchIdentifier(classTarget)) {
3838
_addMemberInvocation(classTarget as SimpleIdentifier, name);
3939
}
40+
} else if (_matchStaticGetter(target)) {
41+
_addMemberInvocation((target as PrefixedIdentifier).prefix, name);
4042
}
4143
}
4244

@@ -76,6 +78,12 @@ class UnusedL10nVisitor extends RecursiveAstVisitor<void> {
7678
final classTarget = (target as PrefixedIdentifier).identifier;
7779

7880
_addMemberInvocationOnAccessor(classTarget, name);
81+
} else if (_matchMethodOf(target)) {
82+
final classTarget = (target as MethodInvocation).target;
83+
84+
if (_matchIdentifier(classTarget)) {
85+
_addMemberInvocation(classTarget as SimpleIdentifier, name);
86+
}
7987
} else if (_matchStaticGetter(target)) {
8088
_addMemberInvocation((target as PrefixedIdentifier).prefix, name);
8189
}

test/resources/unused_l10n_analyzer/test_i18n.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class TestI18n {
1212

1313
String get regularGetter => 'regular getter'; // LINT
1414

15+
String get anotherRegularGetter => 'another regular getter';
16+
1517
final String regularField = 'regular field';
1618

1719
TestI18n.of(String value) {
@@ -31,8 +33,12 @@ class S {
3133

3234
String regularMethod(String value) => value;
3335

36+
String anotherRegularMethod(String value) => value;
37+
3438
String get regularGetter => 'regular getter';
3539

40+
String get anotherRegularGetter => 'another regular getter';
41+
3642
final String regularField = 'regular field'; // LINT
3743

3844
// ignore: prefer_constructors_over_static_methods

test/resources/unused_l10n_analyzer/unused_l10n_analyzer_example.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ void main() {
99

1010
TestI18n.of('').regularField.trim();
1111

12+
TestI18n.of('').anotherRegularGetter;
13+
1214
final wrapper = L10nWrapper();
1315

1416
wrapper.l10n.regularField.trim();
@@ -23,6 +25,8 @@ class SomeClass {
2325

2426
void method() {
2527
S.of('').regularMethod('');
28+
S.of('').anotherRegularGetter;
2629
S.current.regularGetter;
30+
S.current.anotherRegularMethod('');
2731
}
2832
}

test/src/analyzers/unused_l10n_analyzer/unused_l10n_analyzer_test.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ void main() {
3535
final report = result.single;
3636
expect(report.className, 'TestI18n');
3737

38+
expect(report.issues.length, 3);
39+
3840
final firstIssue = report.issues.first;
3941
expect(firstIssue.memberName, 'getter');
4042
expect(firstIssue.location.line, 4);
@@ -69,27 +71,29 @@ void main() {
6971
final report = result.single;
7072
expect(report.className, 'S');
7173

74+
expect(report.issues.length, 4);
75+
7276
final firstIssue = report.issues.first;
7377
expect(firstIssue.memberName, 'field');
74-
expect(firstIssue.location.line, 23);
78+
expect(firstIssue.location.line, 25);
7579
expect(firstIssue.location.column, 3);
7680

7781
final secondIssue = report.issues.elementAt(1);
7882
expect(secondIssue.memberName, 'regularField');
79-
expect(secondIssue.location.line, 36);
83+
expect(secondIssue.location.line, 42);
8084
expect(secondIssue.location.column, 3);
8185

8286
final thirdIssue = report.issues.elementAt(2);
8387
expect(thirdIssue.memberName, 'method(String value)');
84-
expect(thirdIssue.location.line, 27);
88+
expect(thirdIssue.location.line, 29);
8589
expect(thirdIssue.location.column, 3);
8690

8791
final forthIssue = report.issues.elementAt(3);
8892
expect(
8993
forthIssue.memberName,
9094
'secondMethod(String value, num number)',
9195
);
92-
expect(forthIssue.location.line, 29);
96+
expect(forthIssue.location.line, 31);
9397
expect(forthIssue.location.column, 3);
9498
});
9599

0 commit comments

Comments
 (0)