11import 'package:analyzer/dart/ast/ast.dart' ;
22import 'package:analyzer/dart/ast/visitor.dart' ;
3+ import 'package:solid_lints/src/common/parameters/excluded_identifiers_list_parameter.dart' ;
34import 'package:solid_lints/src/lints/prefer_match_file_name/models/declaration_token_info.dart' ;
45
56/// The AST visitor that will collect all Class, Enum, Extension and Mixin
@@ -10,69 +11,97 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> {
1011 /// Variable for making sure if extensions should be ignored
1112 final bool ignoreExtensions;
1213
14+ /// Iterable that contains the name of entity (or entities) that should
15+ /// be ignored
16+ final Iterable <String > excludedEntities;
17+
1318 /// Constructor of [PreferMatchFileNameVisitor] class
1419 PreferMatchFileNameVisitor ({
1520 required this .ignoreExtensions,
21+ required this .excludedEntities,
1622 });
1723
1824 /// List of all declarations
19- Iterable <DeclarationTokenInfo > get declarations => _declarations
20- ..sort (
21- // partition into public and private
22- // put public ones first
23- // each partition sorted by declaration order
24- (a, b) => _publicDeclarationsFirst (a, b) ?? _byDeclarationOrder (a, b),
25- );
26-
27- @override
28- void visitClassDeclaration (ClassDeclaration node) {
29- super .visitClassDeclaration (node);
30-
31- _declarations.add ((token: node.name, parent: node));
32- }
25+ Iterable <DeclarationTokenInfo > get declarations =>
26+ _declarations
27+ ..sort (
28+ // partition into public and private
29+ // put public ones first
30+ // each partition sorted by declaration order
31+ (a, b) =>
32+ _publicDeclarationsFirst (a, b) ?? _byDeclarationOrder (a, b),
33+ );
3334
34- @override
35- void visitExtensionDeclaration (ExtensionDeclaration node) {
36- super .visitExtensionDeclaration (node);
35+ bool _shouldIgnore (Declaration node) {
36+ if (excludedEntities.isEmpty) return false ;
3737
38- if (! ignoreExtensions) {
39- final name = node.name;
40- if (name != null ) {
41- _declarations.add ((token: name, parent: node));
42- }
38+ if (node is ClassDeclaration && excludedEntities.contains ('class' )) {
39+ return true ;
40+ } else if (node is MixinDeclaration && excludedEntities.contains ('mixin' )) {
41+ return true ;
42+ } else if (node is EnumDeclaration && excludedEntities.contains ('enum' )) {
43+ return true ;
44+ } else if (node is ExtensionDeclaration &&
45+ excludedEntities.contains ('extension' )) {
46+ return true ;
4347 }
48+
49+ return false ;
4450 }
4551
46- @override
47- void visitMixinDeclaration (MixinDeclaration node) {
48- super .visitMixinDeclaration (node);
4952
50- _declarations.add ((token: node.name, parent: node));
51- }
53+ @override
54+ void visitClassDeclaration (ClassDeclaration node) {
55+ super .visitClassDeclaration (node);
5256
53- @override
54- void visitEnumDeclaration (EnumDeclaration node) {
55- super .visitEnumDeclaration (node);
57+ if (_shouldIgnore (node)) return ;
5658
57- _declarations.add ((token: node.name, parent: node));
58- }
59+ _declarations.add ((token: node.name, parent: node));
60+ }
5961
60- int ? _publicDeclarationsFirst (
61- DeclarationTokenInfo a,
62- DeclarationTokenInfo b,
63- ) {
64- final isAPrivate = Identifier .isPrivateName (a.token.lexeme);
65- final isBPrivate = Identifier .isPrivateName (b.token.lexeme);
66- if (! isAPrivate && isBPrivate) {
67- return - 1 ;
68- } else if (isAPrivate && ! isBPrivate) {
69- return 1 ;
70- }
71- // no reorder needed;
72- return null ;
62+ @override
63+ void visitExtensionDeclaration (ExtensionDeclaration node) {
64+ super .visitExtensionDeclaration (node);
65+
66+ if (_shouldIgnore (node)) return ;
67+
68+ final name = node.name;
69+ if (name != null ) {
70+ _declarations.add ((token: name, parent: node));
7371 }
72+ }
7473
75- int _byDeclarationOrder (DeclarationTokenInfo a, DeclarationTokenInfo b) {
76- return a.token.offset.compareTo (b.token.offset);
74+ @override
75+ void visitMixinDeclaration (MixinDeclaration node) {
76+ super .visitMixinDeclaration (node);
77+
78+ if (_shouldIgnore (node)) return ;
79+
80+ _declarations.add ((token: node.name, parent: node));
81+ }
82+
83+ @override
84+ void visitEnumDeclaration (EnumDeclaration node) {
85+ super .visitEnumDeclaration (node);
86+
87+ if (_shouldIgnore (node)) return ;
88+
89+ _declarations.add ((token: node.name, parent: node));
90+ }
91+
92+ int ? _publicDeclarationsFirst (DeclarationTokenInfo a,
93+ DeclarationTokenInfo b,) {
94+ final isAPrivate = Identifier .isPrivateName (a.token.lexeme);
95+ final isBPrivate = Identifier .isPrivateName (b.token.lexeme);
96+ if (! isAPrivate && isBPrivate) {
97+ return - 1 ;
98+ } else if (isAPrivate && ! isBPrivate) {
99+ return 1 ;
77100 }
101+ // no reorder needed;
102+ return null ;
78103}
104+
105+ int _byDeclarationOrder (DeclarationTokenInfo a, DeclarationTokenInfo b) {
106+ return a.token.offset.compareTo (b.token.offset);
107+ }}
0 commit comments