From 12f21ee11d7017b566e9297a3afb91a4b099f166 Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Thu, 2 Nov 2017 21:31:41 +0100 Subject: [PATCH] sort_constructors_first applies for all members --- lib/src/rules/sort_constructors_first.dart | 17 +++++++++-------- test/rules/sort_constructors_first.dart | 10 ++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/src/rules/sort_constructors_first.dart b/lib/src/rules/sort_constructors_first.dart index 6a8dc1233..62c2f7438 100644 --- a/lib/src/rules/sort_constructors_first.dart +++ b/lib/src/rules/sort_constructors_first.dart @@ -6,16 +6,17 @@ 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 { - const Animation(); + const Animation(this.value); + double value; void addListener(VoidCallback listener); } ``` @@ -23,6 +24,7 @@ abstract class Animation { **BAD:** ``` abstract class Visitor { + double value; visitSomething(Something s); Visitor(); } @@ -52,15 +54,14 @@ class Visitor extends SimpleAstVisitor { List 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; } } } diff --git a/test/rules/sort_constructors_first.dart b/test/rules/sort_constructors_first.dart index 4382f5d5a..3a3bfa2fb 100644 --- a/test/rules/sort_constructors_first.dart +++ b/test/rules/sort_constructors_first.dart @@ -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 +} \ No newline at end of file