Skip to content

Commit

Permalink
Typos, naming
Browse files Browse the repository at this point in the history
  • Loading branch information
solid-yuriiprykhodko committed Sep 22, 2023
1 parent fc23c9c commit 709cbd4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
final _declarations = <DeclarationTokeInfo>[];
final _declarations = <DeclarationTokenInfo>[];

/// List of all declarations
Iterable<DeclarationTokeInfo> get declarations =>
_declarations..sort(_compareByPrivateType);
Iterable<DeclarationTokenInfo> 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
Expand All @@ -24,33 +30,40 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> {

final name = node.name;
if (name != null) {
_declarations.add(DeclarationTokeInfo(name, node));
_declarations.add((token: name, parent: node));
}
}

@override
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) {
return -1;
} 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);
}
}

0 comments on commit 709cbd4

Please sign in to comment.