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

fix avoid_classes_with_only_static_members to flag classes with only methods #2754

Merged
merged 1 commit into from
Jul 7, 2021
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
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);
}
}