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

Commit

Permalink
fix: avoid-global-state to support static fields (#644)
Browse files Browse the repository at this point in the history
* fix: `avoid-global-state` to support static fields

* fix: static field with final and const modification

* fix: test example
  • Loading branch information
Konoshenko Vlad authored Jan 13, 2022
1 parent 62e5602 commit 4a81d13
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ class _Visitor extends RecursiveAstVisitor<void> {
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
],
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}

0 comments on commit 4a81d13

Please sign in to comment.