From 7d9182cc539cc0187e1e3f2c258a212bb13322c7 Mon Sep 17 00:00:00 2001 From: pq Date: Thu, 24 Feb 2022 14:56:34 -0800 Subject: [PATCH] enhanced enum support for `sort_unnamed_constructors_first` --- .../sort_unnamed_constructors_first.dart | 16 +++++-- test/rules/all.dart | 3 ++ .../sort_unnamed_constructors_first.dart | 46 +++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 test/rules/sort_unnamed_constructors_first.dart diff --git a/lib/src/rules/sort_unnamed_constructors_first.dart b/lib/src/rules/sort_unnamed_constructors_first.dart index f100b7cde..d3a414f5d 100644 --- a/lib/src/rules/sort_unnamed_constructors_first.dart +++ b/lib/src/rules/sort_unnamed_constructors_first.dart @@ -46,6 +46,7 @@ class SortUnnamedConstructorsFirst extends LintRule { NodeLintRegistry registry, LinterContext context) { var visitor = _Visitor(this); registry.addClassDeclaration(this, visitor); + registry.addEnumDeclaration(this, visitor); } } @@ -54,10 +55,9 @@ class _Visitor extends SimpleAstVisitor { _Visitor(this.rule); - @override - void visitClassDeclaration(ClassDeclaration node) { + void check(NodeList classMembers) { // Sort members by offset. - var members = node.members.toList() + var members = classMembers.toList() ..sort((ClassMember m1, ClassMember m2) => m1.offset - m2.offset); var seenConstructor = false; @@ -73,4 +73,14 @@ class _Visitor extends SimpleAstVisitor { } } } + + @override + void visitClassDeclaration(ClassDeclaration node) { + check(node.members); + } + + @override + void visitEnumDeclaration(EnumDeclaration node) { + check(node.members); + } } diff --git a/test/rules/all.dart b/test/rules/all.dart index a5a2d0d12..1ba797771 100644 --- a/test/rules/all.dart +++ b/test/rules/all.dart @@ -56,6 +56,8 @@ import 'prefer_generic_function_type_aliases_test.dart' as prefer_generic_function_type_aliases; import 'prefer_spread_collections_test.dart' as prefer_spread_collections; import 'public_member_api_docs_test.dart' as public_member_api_docs; +import 'sort_unnamed_constructors_first.dart' + as sort_unnamed_constructors_first; import 'super_goes_last_test.dart' as super_goes_last; import 'tighten_type_of_initializing_formals_test.dart' as tighten_type_of_initializing_formals; @@ -104,6 +106,7 @@ void main() { prefer_generic_function_type_aliases.main(); prefer_spread_collections.main(); public_member_api_docs.main(); + sort_unnamed_constructors_first.main(); super_goes_last.main(); tighten_type_of_initializing_formals.main(); type_init_formals.main(); diff --git a/test/rules/sort_unnamed_constructors_first.dart b/test/rules/sort_unnamed_constructors_first.dart new file mode 100644 index 000000000..74eaddb08 --- /dev/null +++ b/test/rules/sort_unnamed_constructors_first.dart @@ -0,0 +1,46 @@ +// Copyright (c) 2022, 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. + +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import '../rule_test_support.dart'; + +main() { + defineReflectiveSuite(() { + defineReflectiveTests(SortUnnamedConstructorsFirstTest); + }); +} + +@reflectiveTest +class SortUnnamedConstructorsFirstTest extends LintRuleTest { + @override + List get experiments => [ + EnableString.enhanced_enums, + ]; + + @override + String get lintRule => 'sort_unnamed_constructors_first'; + + test_ok() async { + await assertNoDiagnostics(r''' +enum A { + a,b,c.aa(); + const A(); + const A.aa(); +} +'''); + } + + test_unsorted() async { + await assertDiagnostics(r''' +enum A { + a,b,c.aa(); + const A.aa(); + const A(); +} +''', [ + lint('sort_unnamed_constructors_first', 47, 1), + ]); + } +}