This repository was archived by the owner on Jul 16, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 7 files changed +59
-1
lines changed
lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable
test/src/analyzers/lint_analyzer/rules/rules_list/prefer_moving_to_variable Expand file tree Collapse file tree 7 files changed +59
-1
lines changed Original file line number Diff line number Diff line change 22
33## Unreleased
44
5+ * fix: correctly handle prefixed enums and static instance fields for [ ` prefer-moving-to-variable ` ] ( https://dartcodemetrics.dev/docs/rules/common/prefer-moving-to-variable ) .
56* feat: add static code diagnostic [ ` prefer-provide-intl-description ` ] ( https://dartcodemetrics.dev/docs/rules/intl/prefer-provide-intl-description ) .
67* feat: exclude ` .freezed.dart ` files by default.
78* fix: handle try and switch statements for [ ` use-setstate-synchronously ` ] ( https://dartcodemetrics.dev/docs/rules/flutter/use-setstate-synchronously )
Original file line number Diff line number Diff line change 22
33import 'package:analyzer/dart/ast/ast.dart' ;
44import 'package:analyzer/dart/ast/visitor.dart' ;
5+ import 'package:analyzer/dart/element/element.dart' ;
56
67import '../../../../../utils/node_utils.dart' ;
78import '../../../lint_utils.dart' ;
Original file line number Diff line number Diff line change @@ -40,10 +40,18 @@ class _BlockVisitor extends RecursiveAstVisitor<void> {
4040
4141 @override
4242 void visitPropertyAccess (PropertyAccess node) {
43- if (node.target == null ) {
43+ final target = node.target;
44+ if (target == null ) {
4445 return ;
4546 }
4647
48+ if (target is PrefixedIdentifier ) {
49+ final element = target.identifier.staticElement;
50+ if (element is EnumElement || element is ClassElement ) {
51+ return ;
52+ }
53+ }
54+
4755 final hasDuplicates = _checkForDuplicates (node, node.target);
4856 if (! hasDuplicates) {
4957 super .visitPropertyAccess (node);
Original file line number Diff line number Diff line change @@ -56,3 +56,18 @@ class Theme {
5656}
5757
5858String getValue () => 'hello' ;
59+
60+ enum SomeValue {
61+ firstValue,
62+ secondValue,
63+ entry1,
64+ entry2,
65+ }
66+
67+ class SomeClass {
68+ static final value = '10' ;
69+
70+ final field = 11 ;
71+ }
72+
73+ final instance = SomeClass ();
Original file line number Diff line number Diff line change @@ -12,3 +12,8 @@ class GetIt {
1212
1313 T call <T extends Object >() => get< T > ;
1414}
15+
16+ enum AnotherEnum {
17+ firstValue,
18+ anotherValue,
19+ }
Original file line number Diff line number Diff line change 1+ import 'example.dart' as prefix;
2+ import 'generics_example.dart' ;
3+
4+ void main () {
5+ AnotherEnum .anotherValue;
6+ AnotherEnum .anotherValue;
7+ AnotherEnum .firstValue;
8+
9+ prefix.SomeValue .firstValue;
10+ prefix.SomeValue .firstValue;
11+ prefix.SomeValue .secondValue;
12+
13+ prefix.SomeClass .value;
14+ prefix.SomeClass .value;
15+ prefix.instance.field;
16+
17+ print (prefix.SomeValue .entry1);
18+ print (prefix.SomeValue .entry2);
19+ }
Original file line number Diff line number Diff line change @@ -15,6 +15,8 @@ const _cascadeExamplePath =
1515 'prefer_moving_to_variable/examples/cascade_example.dart' ;
1616const _genericsExamplePath =
1717 'prefer_moving_to_variable/examples/generics_example.dart' ;
18+ const _prefixExamplePath =
19+ 'prefer_moving_to_variable/examples/prefix_example.dart' ;
1820
1921void main () {
2022 group ('PreferMovingToVariableRule' , () {
@@ -222,6 +224,13 @@ void main() {
222224 RuleTestHelper .verifyNoIssues (issues);
223225 });
224226
227+ test ('reports no issues for prefix imports' , () async {
228+ final unit = await RuleTestHelper .resolveFromFile (_prefixExamplePath);
229+ final issues = PreferMovingToVariableRule ().check (unit);
230+
231+ RuleTestHelper .verifyNoIssues (issues);
232+ });
233+
225234 test ('reports issues with custom config' , () async {
226235 final unit = await RuleTestHelper .resolveFromFile (_examplePath);
227236 final config = {
You can’t perform that action at this time.
0 commit comments