From 7c084a6b675306f9e7bd98de7db6a6be62b878f3 Mon Sep 17 00:00:00 2001 From: Dmitry Zhifarsky Date: Mon, 26 Dec 2022 15:17:04 +0400 Subject: [PATCH 1/3] test: add test cases --- .../prefer_moving_to_variable/examples/example.dart | 7 +++++++ .../examples/generics_example.dart | 5 +++++ .../examples/prefix_example.dart | 13 +++++++++++++ .../prefer_moving_to_variable_rule_test.dart | 9 +++++++++ 4 files changed, 34 insertions(+) create mode 100644 test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/prefix_example.dart diff --git a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/example.dart b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/example.dart index 67a23f9ca2..94e34e570d 100644 --- a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/example.dart +++ b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/example.dart @@ -56,3 +56,10 @@ class Theme { } String getValue() => 'hello'; + +enum SomeValue { + firstValue, + secondValue, + entry1, + entry2, +} diff --git a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/generics_example.dart b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/generics_example.dart index 9ef263036e..4167ceefc9 100644 --- a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/generics_example.dart +++ b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/generics_example.dart @@ -12,3 +12,8 @@ class GetIt { T call() => get; } + +enum AnotherEnum { + firstValue, + anotherValue, +} diff --git a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/prefix_example.dart b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/prefix_example.dart new file mode 100644 index 0000000000..ccd1c7350b --- /dev/null +++ b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/prefix_example.dart @@ -0,0 +1,13 @@ +import 'example.dart' as prefix; +import 'generics_example.dart'; + +void main() { + AnotherEnum.anotherValue; + AnotherEnum.firstValue; + + prefix.SomeValue.firstValue; + prefix.SomeValue.secondValue; + + print(prefix.SomeValue.entry1); + print(prefix.SomeValue.entry2); +} diff --git a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/prefer_moving_to_variable_rule_test.dart b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/prefer_moving_to_variable_rule_test.dart index c9482c3fd9..6430b6d86f 100644 --- a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/prefer_moving_to_variable_rule_test.dart +++ b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/prefer_moving_to_variable_rule_test.dart @@ -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', () { @@ -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 = { From 492b6f819daeb1a69d157a53fb953b7ce5b0041d Mon Sep 17 00:00:00 2001 From: Dmitry Zhifarsky Date: Tue, 10 Jan 2023 19:34:12 +0400 Subject: [PATCH 2/3] fix: correctly handle prefixed enums and static instance fields for prefer-moving-to-variable --- CHANGELOG.md | 4 ++++ .../prefer_moving_to_variable_rule.dart | 1 + .../rules_list/prefer_moving_to_variable/visitor.dart | 10 +++++++++- .../prefer_moving_to_variable/examples/example.dart | 8 ++++++++ .../examples/prefix_example.dart | 10 ++++++++-- 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c495cdc75f..7494d40cd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 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). + ## 5.3.0 * feat: add static code diagnostic [`list-all-equatable-fields`](https://dartcodemetrics.dev/docs/rules/common/list-all-equatable-fields). diff --git a/lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/prefer_moving_to_variable_rule.dart b/lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/prefer_moving_to_variable_rule.dart index 3d39744aa6..197fca9b0f 100644 --- a/lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/prefer_moving_to_variable_rule.dart +++ b/lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/prefer_moving_to_variable_rule.dart @@ -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'; diff --git a/lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/visitor.dart b/lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/visitor.dart index 44c8055c64..21340f8bf9 100644 --- a/lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/visitor.dart +++ b/lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/visitor.dart @@ -40,10 +40,18 @@ class _BlockVisitor extends RecursiveAstVisitor { @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); diff --git a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/example.dart b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/example.dart index 94e34e570d..154b3b2bb7 100644 --- a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/example.dart +++ b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/example.dart @@ -63,3 +63,11 @@ enum SomeValue { entry1, entry2, } + +class SomeClass { + static final value = '10'; + + final field = 11; +} + +final instance = SomeClass(); diff --git a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/prefix_example.dart b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/prefix_example.dart index ccd1c7350b..f7e00b357b 100644 --- a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/prefix_example.dart +++ b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/prefix_example.dart @@ -2,11 +2,17 @@ import 'example.dart' as prefix; import 'generics_example.dart'; void main() { + AnotherEnum.anotherValue; AnotherEnum.anotherValue; AnotherEnum.firstValue; - prefix.SomeValue.firstValue; - prefix.SomeValue.secondValue; + // prefix.SomeValue.firstValue; + // prefix.SomeValue.firstValue; + // prefix.SomeValue.secondValue; + + prefix.SomeClass.value; + prefix.SomeClass.value; + prefix.instance.field; print(prefix.SomeValue.entry1); print(prefix.SomeValue.entry2); From d9fbf65dba71e253a554caf2e390b7ddcd2a8894 Mon Sep 17 00:00:00 2001 From: Dmitry Zhifarsky Date: Wed, 11 Jan 2023 12:47:25 +0400 Subject: [PATCH 3/3] test: restore test example --- .../prefer_moving_to_variable/examples/prefix_example.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/prefix_example.dart b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/prefix_example.dart index f7e00b357b..3e917ef24b 100644 --- a/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/prefix_example.dart +++ b/test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable/examples/prefix_example.dart @@ -6,9 +6,9 @@ void main() { AnotherEnum.anotherValue; AnotherEnum.firstValue; - // prefix.SomeValue.firstValue; - // prefix.SomeValue.firstValue; - // prefix.SomeValue.secondValue; + prefix.SomeValue.firstValue; + prefix.SomeValue.firstValue; + prefix.SomeValue.secondValue; prefix.SomeClass.value; prefix.SomeClass.value;