diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a493fd9ba..a29c89334a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Unreleased + +* feat: support excludes for a separate anti-pattern. +* chore: restrict `analyzer` version to `>=2.4.0 <3.2.0`. + ## 4.9.1 * fix: `avoid-global-state` to support static fields. 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 7eadec3dd9..b4e1055b4e 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 @@ -13,12 +13,10 @@ class _Visitor extends RecursiveAstVisitor { if (!node.isFinal && !node.isConst) { _declarations.add(node); } - } else { - if ((node.declaredElement?.isStatic ?? false) && - !node.isFinal && - !node.isConst) { - _declarations.add(node); - } + } else if ((node.declaredElement?.isStatic ?? false) && + !node.isFinal && + !node.isConst) { + _declarations.add(node); } } } diff --git a/pubspec.yaml b/pubspec.yaml index 66c66b8b03..bd3d10e244 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,7 +10,7 @@ environment: sdk: ">=2.14.0 <3.0.0" dependencies: - analyzer: ">=2.4.0 <3.1.0" + analyzer: ">=2.4.0 <3.2.0" analyzer_plugin: ">=0.8.0 <0.10.0" ansicolor: ^2.0.1 args: ^2.0.0 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 5c73533ca1..ac7073d67f 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,19 +25,27 @@ void main() { RuleTestHelper.verifyIssues( issues: issues, - startLines: [1, 2, 10, 11], - startColumns: [5, 5, 15, 18], + startLines: [1, 2, 10, 11, 21, 22, 32, 33], + startColumns: [5, 5, 15, 18, 15, 18, 15, 18], locationTexts: [ 'answer = 42', 'evenNumbers = [1, 2, 3].where((element) => element.isEven)', 'a', 'c', + 'a', + 'c', + '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.', + '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 6b8493611a..1b5d2e9b3b 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 @@ -13,5 +13,25 @@ class Foo { dynamic c2; const d = 1; static const noted = ''; + static final finalField = 1; + void function() {} +} + +class Mixin { + static int? a; // LINT + static dynamic c; // LINT + final int? b; + dynamic c2; + const d = 1; + static const noted = ''; + static final finalField = 1; + void function() {} +} + +extension Extension on String { + static int? a; // LINT + static dynamic c; // LINT + static const noted = ''; + static final finalField = 1; void function() {} }