Skip to content

Commit ff56afc

Browse files
committed
now the logic of ignoring entities can be reused
1 parent f572aff commit ff56afc

File tree

3 files changed

+78
-30
lines changed

3 files changed

+78
-30
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'package:analyzer/dart/ast/ast.dart';
2+
3+
/// A model representing "exclude_entity" parameters for linting, defining
4+
/// identifiers (classes, mixins, enums, extensions) to be ignored during
5+
/// analysis.
6+
class ExcludedEntitiesListParameters {
7+
/// The parameter model
8+
final List<String> excludedEntityNames;
9+
10+
/// A common parameter key for analysis_options.yaml
11+
static const String excludeEntityKey = 'exclude_entity';
12+
13+
/// Constructor for [ExcludedEntitiesListParameters] class
14+
ExcludedEntitiesListParameters({
15+
required this.excludedEntityNames,
16+
});
17+
18+
/// Method for creating from json data
19+
factory ExcludedEntitiesListParameters.fromJson(Map<String, dynamic> json) {
20+
final raw = json['exclude_entity'];
21+
if (raw is List) {
22+
return ExcludedEntitiesListParameters(
23+
excludedEntityNames: List<String>.from(raw),
24+
);
25+
}
26+
return ExcludedEntitiesListParameters(
27+
excludedEntityNames: [],
28+
);
29+
}
30+
31+
/// Returns whether the target node should be ignored during analysis.
32+
bool shouldIgnoreEntity(Declaration node) {
33+
if (excludedEntityNames.isEmpty) return false;
34+
35+
if (node is ClassDeclaration && excludedEntityNames.contains('class')) {
36+
return true;
37+
} else if (node is MixinDeclaration &&
38+
excludedEntityNames.contains('mixin')) {
39+
return true;
40+
} else if (node is EnumDeclaration &&
41+
excludedEntityNames.contains('enum')) {
42+
return true;
43+
} else if (node is ExtensionDeclaration &&
44+
excludedEntityNames.contains('extension')) {
45+
return true;
46+
}
47+
48+
return false;
49+
}
50+
}
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/// A data model class that represents the "prefer factmatch file name" input
1+
import 'package:solid_lints/src/common/parameters/excluded_entities_list_parameter.dart';
2+
3+
/// A data model class that represents the "prefer match file name" input
24
/// parameters
35
class PreferMatchFileNameParameters {
4-
/// A list of methods that should be excluded from the lint.
5-
final Iterable<String> excludeEntity;
6-
7-
static const bool _defaultIgnoreExtensionsValue = false;
6+
/// A list of entities that should be excluded from the lint.
7+
final ExcludedEntitiesListParameters excludeEntity;
88

99
/// Constructor for [PreferMatchFileNameParameters] model
1010
const PreferMatchFileNameParameters({
@@ -14,9 +14,6 @@ class PreferMatchFileNameParameters {
1414
/// Method for creating from json data
1515
factory PreferMatchFileNameParameters.fromJson(Map<String, Object?> json) =>
1616
PreferMatchFileNameParameters(
17-
excludeEntity: (json['exclude_entity'] as Iterable?)
18-
?.map((e) => e.toString())
19-
.toList() ??
20-
[],
17+
excludeEntity: ExcludedEntitiesListParameters.fromJson(json),
2118
);
2219
}

lib/src/lints/prefer_match_file_name/visitors/prefer_match_file_name_visitor.dart

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:analyzer/dart/ast/ast.dart';
22
import 'package:analyzer/dart/ast/visitor.dart';
3+
import 'package:solid_lints/src/common/parameters/excluded_entities_list_parameter.dart';
34
import 'package:solid_lints/src/common/parameters/excluded_identifiers_list_parameter.dart';
45
import 'package:solid_lints/src/lints/prefer_match_file_name/models/declaration_token_info.dart';
56

@@ -10,7 +11,7 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> {
1011

1112
/// Iterable that contains the name of entity (or entities) that should
1213
/// be ignored
13-
final Iterable<String> excludedEntities;
14+
final ExcludedEntitiesListParameters excludedEntities;
1415

1516
/// Constructor of [PreferMatchFileNameVisitor] class
1617
PreferMatchFileNameVisitor({
@@ -26,28 +27,28 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> {
2627
(a, b) => _publicDeclarationsFirst(a, b) ?? _byDeclarationOrder(a, b),
2728
);
2829

29-
bool _shouldIgnore(Declaration node) {
30-
if (excludedEntities.isEmpty) return false;
31-
32-
if (node is ClassDeclaration && excludedEntities.contains('class')) {
33-
return true;
34-
} else if (node is MixinDeclaration && excludedEntities.contains('mixin')) {
35-
return true;
36-
} else if (node is EnumDeclaration && excludedEntities.contains('enum')) {
37-
return true;
38-
} else if (node is ExtensionDeclaration &&
39-
excludedEntities.contains('extension')) {
40-
return true;
41-
}
42-
43-
return false;
44-
}
30+
// bool _shouldIgnore(Declaration node) {
31+
// if (excludedEntities.isEmpty) return false;
32+
//
33+
// if (node is ClassDeclaration && excludedEntities.contains('class')) {
34+
// return true;
35+
// } else if (node is MixinDeclaration && excludedEntities.contains('mixin')) {
36+
// return true;
37+
// } else if (node is EnumDeclaration && excludedEntities.contains('enum')) {
38+
// return true;
39+
// } else if (node is ExtensionDeclaration &&
40+
// excludedEntities.contains('extension')) {
41+
// return true;
42+
// }
43+
//
44+
// return false;
45+
// }
4546

4647
@override
4748
void visitClassDeclaration(ClassDeclaration node) {
4849
super.visitClassDeclaration(node);
4950

50-
if (_shouldIgnore(node)) return;
51+
if (excludedEntities.shouldIgnoreEntity(node)) return;
5152

5253
_declarations.add((token: node.name, parent: node));
5354
}
@@ -56,7 +57,7 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> {
5657
void visitExtensionDeclaration(ExtensionDeclaration node) {
5758
super.visitExtensionDeclaration(node);
5859

59-
if (_shouldIgnore(node)) return;
60+
if (excludedEntities.shouldIgnoreEntity(node)) return;
6061

6162
final name = node.name;
6263
if (name != null) {
@@ -68,7 +69,7 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> {
6869
void visitMixinDeclaration(MixinDeclaration node) {
6970
super.visitMixinDeclaration(node);
7071

71-
if (_shouldIgnore(node)) return;
72+
if (excludedEntities.shouldIgnoreEntity(node)) return;
7273

7374
_declarations.add((token: node.name, parent: node));
7475
}
@@ -77,7 +78,7 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> {
7778
void visitEnumDeclaration(EnumDeclaration node) {
7879
super.visitEnumDeclaration(node);
7980

80-
if (_shouldIgnore(node)) return;
81+
if (excludedEntities.shouldIgnoreEntity(node)) return;
8182

8283
_declarations.add((token: node.name, parent: node));
8384
}

0 commit comments

Comments
 (0)