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

fix: correctly handle prefixed enums and static instance fields for prefer-moving-to-variable #1123

Merged
merged 4 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* fix: correctly handle prefixed enums and static instance fields for [`prefer-moving-to-variable`](https://dartcodemetrics.dev/docs/rules/common/prefer-moving-to-variable).
* feat: add static code diagnostic [`prefer-provide-intl-description`](https://dartcodemetrics.dev/docs/rules/intl/prefer-provide-intl-description).
* feat: exclude `.freezed.dart` files by default.
* fix: handle try and switch statements for [`use-setstate-synchronously`](https://dartcodemetrics.dev/docs/rules/flutter/use-setstate-synchronously)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';

import '../../../../../utils/node_utils.dart';
import '../../../lint_utils.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,18 @@ class _BlockVisitor extends RecursiveAstVisitor<void> {

@override
void visitPropertyAccess(PropertyAccess node) {
if (node.target == null) {
final target = node.target;
if (target == null) {
return;
}

if (target is PrefixedIdentifier) {
final element = target.identifier.staticElement;
if (element is EnumElement || element is ClassElement) {
return;
}
}

final hasDuplicates = _checkForDuplicates(node, node.target);
if (!hasDuplicates) {
super.visitPropertyAccess(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,18 @@ class Theme {
}

String getValue() => 'hello';

enum SomeValue {
firstValue,
secondValue,
entry1,
entry2,
}

class SomeClass {
static final value = '10';

final field = 11;
}

final instance = SomeClass();
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ class GetIt {

T call<T extends Object>() => get<T>;
}

enum AnotherEnum {
firstValue,
anotherValue,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'example.dart' as prefix;
import 'generics_example.dart';

void main() {
AnotherEnum.anotherValue;
AnotherEnum.anotherValue;
AnotherEnum.firstValue;

prefix.SomeValue.firstValue;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try referencing prefix.SomeValue.firstValue
repeatedly
I believe that would match the case where I was seeing false positives.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's it! Thank you!

Copy link
Member Author

@incendial incendial Jan 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, actually, if I duplicate the line 5 AnotherEnum.anotherValue; the rule will also trigger. So it's not connected to prefixes, maybe you remember other cases?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The strange thing is removing the prefixes did seem to fix the issue in DevTools. That said, I wouldn't want the rule to trigger for duplicated AnotherEnum.anotherValue case either. Specifying the enum value name twice seems more idiomatic than defining an unneeded local variable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. And maybe the same should work for the class level static variables. You probably wouldn't want to assign this type of calls to a variable.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree that I wouldn't want this firing for class to class level static variables either.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for misleading, it was a problem exactly with the prefixes 🤦
Now fixed, thank you!

prefix.SomeValue.firstValue;
prefix.SomeValue.secondValue;

prefix.SomeClass.value;
prefix.SomeClass.value;
prefix.instance.field;

print(prefix.SomeValue.entry1);
print(prefix.SomeValue.entry2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const _cascadeExamplePath =
'prefer_moving_to_variable/examples/cascade_example.dart';
const _genericsExamplePath =
'prefer_moving_to_variable/examples/generics_example.dart';
const _prefixExamplePath =
'prefer_moving_to_variable/examples/prefix_example.dart';

void main() {
group('PreferMovingToVariableRule', () {
Expand Down Expand Up @@ -222,6 +224,13 @@ void main() {
RuleTestHelper.verifyNoIssues(issues);
});

test('reports no issues for prefix imports', () async {
final unit = await RuleTestHelper.resolveFromFile(_prefixExamplePath);
final issues = PreferMovingToVariableRule().check(unit);

RuleTestHelper.verifyNoIssues(issues);
});

test('reports issues with custom config', () async {
final unit = await RuleTestHelper.resolveFromFile(_examplePath);
final config = {
Expand Down