Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
flag cases with only methods (#2754)
Browse files Browse the repository at this point in the history
  • Loading branch information
pq authored Jul 7, 2021
1 parent 020fc4d commit b48290e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
10 changes: 7 additions & 3 deletions lib/src/rules/avoid_classes_with_only_static_members.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ class _Visitor extends SimpleAstVisitor<void> {
@override
void visitClassDeclaration(ClassDeclaration node) {
var declaredElement = node.declaredElement;
if (declaredElement != null &&
node.members.isNotEmpty &&
if (declaredElement == null) {
return;
}

if (node.members.isNotEmpty &&
node.members.every(_isStaticMember) &&
declaredElement.fields.any(_isNonConst)) {
(declaredElement.methods.isNotEmpty ||
declaredElement.fields.any(_isNonConst))) {
rule.reportLint(node);
}
}
Expand Down
28 changes: 21 additions & 7 deletions test_data/rules/avoid_classes_with_only_static_members.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,50 @@

// test w/ `dart test -N avoid_classes_with_only_static_members`

class Bad { // LINT
class Bad // LINT
{
static int a;

static foo() {}
}

class Bad2 extends Good1 { // LINT
class Bad2 extends Good1 // LINT
{
static int staticInt;

static foo() {}
}

class Bad3 {} // OK
class Bad3 // OK
{}

class Good1 { // OK
class Good1 // OK
{
int a = 0;
}

class Good2 { // OK
class Good2 // OK
{
void foo() {}
}

class Good3 { // OK
class Good3 // OK
{
Good3();
}

class Color { // OK
class Color // OK
{
static const red = '#f00';
static const green = '#0f0';
static const blue = '#00f';
static const black = '#000';
static const white = '#fff';
}

class DateUtils // LINT
{
static DateTime mostRecent(List<DateTime> dates) {
return dates.reduce((a, b) => a.isAfter(b) ? a : b);
}
}

0 comments on commit b48290e

Please sign in to comment.