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

Commit 4a81d13

Browse files
author
Konoshenko Vlad
authored
fix: avoid-global-state to support static fields (#644)
* fix: `avoid-global-state` to support static fields * fix: static field with final and const modification * fix: test example
1 parent 62e5602 commit 4a81d13

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
* fix: `avoid-global-state` to support static fields.
56
* feat: support excludes for a separate anti-pattern.
67
* fix: prefer-extracting-callbacks in nested widgets.
78
* fix: correctly handle method invocations on getters and method of for `check-unused-l10n` command.

lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/visitor.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ class _Visitor extends RecursiveAstVisitor<void> {
99
void visitVariableDeclaration(VariableDeclaration node) {
1010
super.visitVariableDeclaration(node);
1111

12-
if (!node.isFinal &&
13-
!node.isConst &&
14-
node.declaredElement?.enclosingElement is CompilationUnitElement) {
15-
_declarations.add(node);
12+
if (node.declaredElement?.enclosingElement is CompilationUnitElement) {
13+
if (!node.isFinal && !node.isConst) {
14+
_declarations.add(node);
15+
}
16+
} else {
17+
if ((node.declaredElement?.isStatic ?? false) &&
18+
!node.isFinal &&
19+
!node.isConst) {
20+
_declarations.add(node);
21+
}
1622
}
1723
}
1824
}

test/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/avoid_global_state_rule_test.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@ void main() {
2525

2626
RuleTestHelper.verifyIssues(
2727
issues: issues,
28-
startLines: [1, 2],
29-
startColumns: [5, 5],
28+
startLines: [1, 2, 10, 11],
29+
startColumns: [5, 5, 15, 18],
3030
locationTexts: [
3131
'answer = 42',
3232
'evenNumbers = [1, 2, 3].where((element) => element.isEven)',
33+
'a',
34+
'c',
3335
],
3436
messages: [
3537
'Avoid using global variable without const or final keywords.',
3638
'Avoid using global variable without const or final keywords.',
39+
'Avoid using global variable without const or final keywords.',
40+
'Avoid using global variable without const or final keywords.',
3741
],
3842
);
3943
});

test/src/analyzers/lint_analyzer/rules/rules_list/avoid_global_state/examples/example.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ var answer = 42; // LINT
22
var evenNumbers = [1, 2, 3].where((element) => element.isEven); // LINT
33
const a = 1;
44
final b = 1;
5+
const noted2 = '';
56

67
void function() {}
78

89
class Foo {
9-
static int? a;
10+
static int? a; // LINT
11+
static dynamic c; // LINT
1012
final int? b;
11-
dynamic c;
13+
dynamic c2;
1214
const d = 1;
15+
static const noted = '';
1316
void function() {}
1417
}

0 commit comments

Comments
 (0)