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

sort_constructors_first applies for all members #834

Merged
merged 1 commit into from
Oct 2, 2018
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
17 changes: 9 additions & 8 deletions lib/src/rules/sort_constructors_first.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:linter/src/analyzer.dart';

const _desc = r'Sort constructor declarations before method declarations.';
const _desc = r'Sort constructor declarations before other members.';

const _details = r'''

**DO** sort constructor declarations before method declarations.
**DO** sort constructor declarations before other members.

**GOOD:**
```
abstract class Animation<T> {
const Animation();
const Animation(this.value);
double value;
void addListener(VoidCallback listener);
}
```

**BAD:**
```
abstract class Visitor {
double value;
visitSomething(Something s);
Visitor();
}
Expand Down Expand Up @@ -52,15 +54,14 @@ class Visitor extends SimpleAstVisitor {
List<ClassMember> members = decl.members.toList()
..sort((ClassMember m1, ClassMember m2) => m1.offset - m2.offset);

bool seenMethod = false;
bool other = false;
for (ClassMember member in members) {
if (member is ConstructorDeclaration) {
if (seenMethod) {
if (other) {
rule.reportLint(member.returnType);
}
}
if (member is MethodDeclaration) {
seenMethod = true;
} else {
other = true;
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/rules/sort_constructors_first.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ abstract class C {
C(); //LINT
C.named(); //LINT
}

abstract class D {
final a;
D(); //LINT
}

abstract class E {
static final a;
E(); //LINT
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing trailing newline