From 709cbd404476529fc8d6c526a666f193857d8c80 Mon Sep 17 00:00:00 2001 From: Yurii Prykhodko Date: Fri, 22 Sep 2023 14:00:40 +0300 Subject: [PATCH] Typos, naming --- .../models/declaration_token_info.dart | 11 +++---- .../prefer_match_file_name_visitor.dart | 29 ++++++++++++++----- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/lints/prefer_match_file_name/models/declaration_token_info.dart b/lib/lints/prefer_match_file_name/models/declaration_token_info.dart index 54a0e72a..bfceb8e0 100644 --- a/lib/lints/prefer_match_file_name/models/declaration_token_info.dart +++ b/lib/lints/prefer_match_file_name/models/declaration_token_info.dart @@ -2,13 +2,10 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/token.dart'; /// Data class represents declaration token and declaration parent node -class DeclarationTokeInfo { +typedef DeclarationTokenInfo = ({ /// Declaration token - final Token token; + Token token, /// Declaration parent node - final AstNode parent; - - /// Creates instance of [DeclarationTokeInfo] - const DeclarationTokeInfo(this.token, this.parent); -} + AstNode parent, +}); diff --git a/lib/lints/prefer_match_file_name/prefer_match_file_name_visitor.dart b/lib/lints/prefer_match_file_name/prefer_match_file_name_visitor.dart index ecc22d33..b306f175 100644 --- a/lib/lints/prefer_match_file_name/prefer_match_file_name_visitor.dart +++ b/lib/lints/prefer_match_file_name/prefer_match_file_name_visitor.dart @@ -5,17 +5,23 @@ import 'package:solid_lints/lints/prefer_match_file_name/models/declaration_toke /// The AST visitor that will collect all Class, Enum, Extension and Mixin /// declarations class PreferMatchFileNameVisitor extends RecursiveAstVisitor { - final _declarations = []; + final _declarations = []; /// List of all declarations - Iterable get declarations => - _declarations..sort(_compareByPrivateType); + Iterable get declarations => _declarations + ..sort( + // partition into public and private + // put public ones first + // each partition sorted by declaration order + // TODO: unit-test to check correctness + (a, b) => _publicDeclarationsFirst(a, b) ?? _byDeclarationOrder(a, b), + ); @override void visitClassDeclaration(ClassDeclaration node) { super.visitClassDeclaration(node); - _declarations.add(DeclarationTokeInfo(node.name, node)); + _declarations.add((token: node.name, parent: node)); } @override @@ -24,7 +30,7 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor { final name = node.name; if (name != null) { - _declarations.add(DeclarationTokeInfo(name, node)); + _declarations.add((token: name, parent: node)); } } @@ -32,17 +38,20 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor { void visitMixinDeclaration(MixinDeclaration node) { super.visitMixinDeclaration(node); - _declarations.add(DeclarationTokeInfo(node.name, node)); + _declarations.add((token: node.name, parent: node)); } @override void visitEnumDeclaration(EnumDeclaration node) { super.visitEnumDeclaration(node); - _declarations.add(DeclarationTokeInfo(node.name, node)); + _declarations.add((token: node.name, parent: node)); } - int _compareByPrivateType(DeclarationTokeInfo a, DeclarationTokeInfo b) { + int? _publicDeclarationsFirst( + DeclarationTokenInfo a, + DeclarationTokenInfo b, + ) { final isAPrivate = Identifier.isPrivateName(a.token.lexeme); final isBPrivate = Identifier.isPrivateName(b.token.lexeme); if (!isAPrivate && isBPrivate) { @@ -50,7 +59,11 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor { } else if (isAPrivate && !isBPrivate) { return 1; } + // no reorder needed; + return null; + } + int _byDeclarationOrder(DeclarationTokenInfo a, DeclarationTokenInfo b) { return a.token.offset.compareTo(b.token.offset); } }