This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure constructors are sorted first (#186).
BUG= R=scheglov@google.com Review URL: https://codereview.chromium.org//1706933002 .
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
library linter.src.rules.sort_constructors_first; | ||
|
||
import 'package:analyzer/src/generated/ast.dart'; | ||
import 'package:linter/src/linter.dart'; | ||
|
||
const desc = r'Sort constructor declarations before method declarations.'; | ||
|
||
const details = r''' | ||
**DO** sort constructor declarations before method declarations. | ||
**GOOD:** | ||
``` | ||
abstract class Animation<T> { | ||
const Animation(); | ||
void addListener(VoidCallback listener); | ||
} | ||
``` | ||
**BAD:** | ||
``` | ||
abstract class Visitor { | ||
visitSomething(Something s); | ||
Visitor(); | ||
} | ||
``` | ||
'''; | ||
|
||
class SortConstructorsFirst extends LintRule { | ||
SortConstructorsFirst() | ||
: super( | ||
name: 'sort_constructors_first', | ||
description: desc, | ||
details: details, | ||
group: Group.style); | ||
|
||
@override | ||
AstVisitor getVisitor() => new Visitor(this); | ||
} | ||
|
||
class Visitor extends SimpleAstVisitor { | ||
final LintRule rule; | ||
Visitor(this.rule); | ||
|
||
@override | ||
visitClassDeclaration(ClassDeclaration decl) { | ||
// Sort members by offset. | ||
List<ClassMember> members = decl.members.toList(); | ||
members.sort((ClassMember m1, ClassMember m2) => m1.offset - m2.offset); | ||
|
||
bool seenMethod = false; | ||
for (ClassMember member in members) { | ||
if (member is ConstructorDeclaration) { | ||
if (seenMethod) { | ||
rule.reportLint(member.returnType); | ||
} | ||
} | ||
if (member is MethodDeclaration) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Hixie
|
||
seenMethod = true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
// test w/ `dart test/util/solo_test.dart sort_constructors_first` | ||
|
||
abstract class A { | ||
const A(); | ||
void f(); | ||
} | ||
|
||
abstract class B { | ||
void f(); | ||
const B(); //LINT | ||
} | ||
|
||
abstract class C { | ||
void f(); | ||
C(); //LINT | ||
C.named(); //LINT | ||
} |
It's actually properties and fields that are our biggest offenders.