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

fix: avoid-global-state to support static fields #644

Merged
merged 3 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {}
}