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

Commit

Permalink
fix: correctly handle method invocations on getters (#642)
Browse files Browse the repository at this point in the history
* fix: correctly handle method invocations on getters

* chore: update changelog

* fix: restore removed code for property access method of calls
  • Loading branch information
incendial authored Jan 12, 2022
1 parent 860ff4e commit 62e5602
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## Unreleased

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

## 4.9.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class UnusedL10nVisitor extends RecursiveAstVisitor<void> {
if (_matchIdentifier(classTarget)) {
_addMemberInvocation(classTarget as SimpleIdentifier, name);
}
} else if (_matchStaticGetter(target)) {
_addMemberInvocation((target as PrefixedIdentifier).prefix, name);
}
}

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

_addMemberInvocationOnAccessor(classTarget, name);
} else if (_matchMethodOf(target)) {
final classTarget = (target as MethodInvocation).target;

if (_matchIdentifier(classTarget)) {
_addMemberInvocation(classTarget as SimpleIdentifier, name);
}
} else if (_matchStaticGetter(target)) {
_addMemberInvocation((target as PrefixedIdentifier).prefix, name);
}
Expand Down
6 changes: 6 additions & 0 deletions test/resources/unused_l10n_analyzer/test_i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class TestI18n {

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

String get anotherRegularGetter => 'another regular getter';

final String regularField = 'regular field';

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

String regularMethod(String value) => value;

String anotherRegularMethod(String value) => value;

String get regularGetter => 'regular getter';

String get anotherRegularGetter => 'another regular getter';

final String regularField = 'regular field'; // LINT

// ignore: prefer_constructors_over_static_methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ void main() {

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

TestI18n.of('').anotherRegularGetter;

final wrapper = L10nWrapper();

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

void method() {
S.of('').regularMethod('');
S.of('').anotherRegularGetter;
S.current.regularGetter;
S.current.anotherRegularMethod('');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ void main() {
final report = result.single;
expect(report.className, 'TestI18n');

expect(report.issues.length, 3);

final firstIssue = report.issues.first;
expect(firstIssue.memberName, 'getter');
expect(firstIssue.location.line, 4);
Expand Down Expand Up @@ -69,27 +71,29 @@ void main() {
final report = result.single;
expect(report.className, 'S');

expect(report.issues.length, 4);

final firstIssue = report.issues.first;
expect(firstIssue.memberName, 'field');
expect(firstIssue.location.line, 23);
expect(firstIssue.location.line, 25);
expect(firstIssue.location.column, 3);

final secondIssue = report.issues.elementAt(1);
expect(secondIssue.memberName, 'regularField');
expect(secondIssue.location.line, 36);
expect(secondIssue.location.line, 42);
expect(secondIssue.location.column, 3);

final thirdIssue = report.issues.elementAt(2);
expect(thirdIssue.memberName, 'method(String value)');
expect(thirdIssue.location.line, 27);
expect(thirdIssue.location.line, 29);
expect(thirdIssue.location.column, 3);

final forthIssue = report.issues.elementAt(3);
expect(
forthIssue.memberName,
'secondMethod(String value, num number)',
);
expect(forthIssue.location.line, 29);
expect(forthIssue.location.line, 31);
expect(forthIssue.location.column, 3);
});

Expand Down

0 comments on commit 62e5602

Please sign in to comment.