-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ignored_entities_model; use it for avoid_unused_parameters & a…
…void_returning_widgets
- Loading branch information
Showing
10 changed files
with
159 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
lib/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_exclude.dart
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
lib/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_parameters.dart
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
lib/src/models/ignored_entities_model/ignored_entities_model.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:solid_lints/src/models/ignored_entities_model/ignored_entity.dart'; | ||
|
||
/// Manages a list of entities (functions/methods/classes) that should be | ||
/// excluded from a lint rule. | ||
/// | ||
/// Example config: | ||
/// ```yaml | ||
/// custom_lint: | ||
/// rules: | ||
/// - <rule_name>: | ||
/// exclude: | ||
/// # excludes a matching method in a matching class | ||
/// - method_name: excludeMethod | ||
/// class_name: ExcludeClass | ||
/// # excludes a matching method anywhere | ||
/// - method_name: excludeFunction | ||
/// # excludes all methods within a matching class | ||
/// - class_name: ExcludeEntireClass | ||
/// ``` | ||
class IgnoredEntitiesModel { | ||
IgnoredEntitiesModel._({required this.entities}); | ||
|
||
/// | ||
factory IgnoredEntitiesModel.fromJson(Map<dynamic, dynamic> json) { | ||
final entities = <IgnoredEntity>[]; | ||
final excludeList = json['exclude'] as Iterable? ?? []; | ||
for (final item in excludeList) { | ||
if (item is Map) { | ||
entities.add(IgnoredEntity.fromJson(item)); | ||
} | ||
} | ||
return IgnoredEntitiesModel._(entities: entities); | ||
} | ||
|
||
/// The entities to be ignored | ||
final List<IgnoredEntity> entities; | ||
|
||
/// Checks if the entire class should be ignored. | ||
/// Doesn't match if the config specifies a specific function within the class | ||
bool matchClass(ClassDeclaration node) { | ||
final className = node.name.toString(); | ||
|
||
return entities.any((element) { | ||
return element.functionName == null && element.className == className; | ||
}); | ||
} | ||
|
||
/// Checks if the given method/function should be ignored. | ||
bool matchMethod(Declaration node) { | ||
final methodName = node.declaredElement?.name; | ||
|
||
return entities.any((entity) { | ||
if (entity.functionName != methodName) { | ||
return false; | ||
} | ||
|
||
if (entity.className == null) { | ||
return true; | ||
} | ||
|
||
final matchingClass = node.thisOrAncestorMatching((node) { | ||
if (node case final ClassDeclaration classNode) { | ||
return classNode.name.toString() == entity.className; | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
return matchingClass != null; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/// An entity (method/function/class) to be excluded from lint | ||
class IgnoredEntity { | ||
IgnoredEntity._({ | ||
this.className, | ||
this.functionName, | ||
}); | ||
|
||
/// | ||
factory IgnoredEntity.fromJson(Map<dynamic, dynamic> json) { | ||
return IgnoredEntity._( | ||
className: json['class_name'] as String?, | ||
functionName: json['method_name'] as String?, | ||
); | ||
} | ||
|
||
/// Class name | ||
final String? className; | ||
/// Function name | ||
final String? functionName; | ||
|
||
@override | ||
String toString() { | ||
return "$className: $functionName"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ analyzer: | |
- custom_lint | ||
|
||
custom_lint: | ||
debug: true, | ||
rules: | ||
- cyclomatic_complexity: | ||
max_complexity: 4 | ||
|
12 changes: 12 additions & 0 deletions
12
lint_test/avoid_unused_parameters_test/analysis_options.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
analyzer: | ||
plugins: | ||
- ../custom_lint | ||
|
||
custom_lint: | ||
rules: | ||
- avoid_unused_parameters: | ||
exclude: | ||
- method_name: excludeMethod | ||
class_name: ExcludeClass | ||
- method_name: excludeFunction | ||
- class_name: ExcludeEntireClass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters