diff --git a/CHANGELOG.md b/CHANGELOG.md index 61f63e7221..286a3c1f8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* fix: `avoid-global-state` to support static fields. * feat: support excludes for a separate anti-pattern. * fix: prefer-extracting-callbacks in nested widgets. * fix: correctly handle method invocations on getters and method of for `check-unused-l10n` command. diff --git a/lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/visitor.dart b/lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/visitor.dart index ff7d0fe167..7eadec3dd9 100644 --- a/lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/visitor.dart +++ b/lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/visitor.dart @@ -9,10 +9,16 @@ class _Visitor extends RecursiveAstVisitor { void visitVariableDeclaration(VariableDeclaration node) { super.visitVariableDeclaration(node); - if (!node.isFinal && - !node.isConst && - node.declaredElement?.enclosingElement is CompilationUnitElement) { - _declarations.add(node); + if (node.declaredElement?.enclosingElement is CompilationUnitElement) { + if (!node.isFinal && !node.isConst) { + _declarations.add(node); + } + } else { + if ((node.declaredElement?.isStatic ?? false) && + !node.isFinal && + !node.isConst) { + _declarations.add(node); + } } } } diff --git a/test/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/avoid_global_state_rule_test.dart b/test/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/avoid_global_state_rule_test.dart index 1d9d936f94..5c73533ca1 100644 --- a/test/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/avoid_global_state_rule_test.dart +++ b/test/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/avoid_global_state_rule_test.dart @@ -25,15 +25,19 @@ void main() { RuleTestHelper.verifyIssues( issues: issues, - startLines: [1, 2], - startColumns: [5, 5], + startLines: [1, 2, 10, 11], + startColumns: [5, 5, 15, 18], locationTexts: [ 'answer = 42', 'evenNumbers = [1, 2, 3].where((element) => element.isEven)', + 'a', + 'c', ], messages: [ 'Avoid using global variable without const or final keywords.', 'Avoid using global variable without const or final keywords.', + 'Avoid using global variable without const or final keywords.', + 'Avoid using global variable without const or final keywords.', ], ); }); diff --git a/test/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/examples/example.dart b/test/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/examples/example.dart index ff5194b478..6b8493611a 100644 --- a/test/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/examples/example.dart +++ b/test/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/examples/example.dart @@ -2,13 +2,16 @@ var answer = 42; // LINT var evenNumbers = [1, 2, 3].where((element) => element.isEven); // LINT const a = 1; final b = 1; +const noted2 = ''; void function() {} class Foo { - static int? a; + static int? a; // LINT + static dynamic c; // LINT final int? b; - dynamic c; + dynamic c2; const d = 1; + static const noted = ''; void function() {} }