-
Notifications
You must be signed in to change notification settings - Fork 24
feat: add exclude_annotation support to avoid_unused_parameters #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
solid-illiaaihistov
wants to merge
7
commits into
solid-software:analysis_server_migration
Choose a base branch
from
solid-illiaaihistov:147-avoid-unused-parameters-exclude-annotations
base: analysis_server_migration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
95bd88e
feat: add exclude_annotation support to avoid_unused_parameters lint …
aaf7c99
feat: allow configuration parameters to accept single strings in addi…
1a0dc92
feat: support checking inherited annotations for all declaration type…
a16fe8e
refactor: simplify excluded annotations check by iterating through al…
62599c3
feat: allow map values in exclude parameters, generalize Iterable par…
89ae715
test: simplify freezed class definitions in avoid_unused_parameters t…
07f3df7
fix: allow excluded annotations to be matched by full qualified name
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
58 changes: 58 additions & 0 deletions
58
lib/src/common/parameters/excluded_annotations_list_parameter.dart
This file contains hidden or 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,58 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
|
|
||
| /// A parameter model representing excluded annotations for linting. | ||
| /// It defines class-level annotations that indicate when class members | ||
| /// should be ignored during analysis. | ||
| class ExcludedAnnotationsListParameter { | ||
| /// The set of excluded annotation names. | ||
| final Set<String> excludedAnnotations; | ||
|
|
||
| /// A common parameter key for analysis_options.yaml | ||
| static const String excludeAnnotationKey = 'exclude_annotation'; | ||
|
|
||
| /// Constructor for [ExcludedAnnotationsListParameter] class | ||
| ExcludedAnnotationsListParameter({ | ||
| required this.excludedAnnotations, | ||
| }); | ||
|
|
||
| /// Method for creating from json data | ||
| factory ExcludedAnnotationsListParameter.fromJson(Map<String, dynamic> json) { | ||
| final raw = json[excludeAnnotationKey]; | ||
| if (raw is Iterable) { | ||
| return ExcludedAnnotationsListParameter( | ||
| excludedAnnotations: Set<String>.from(raw.whereType<String>()), | ||
| ); | ||
| } else if (raw is String) { | ||
| return ExcludedAnnotationsListParameter( | ||
| excludedAnnotations: {raw}, | ||
| ); | ||
| } | ||
|
|
||
| return ExcludedAnnotationsListParameter( | ||
| excludedAnnotations: {}, | ||
| ); | ||
| } | ||
|
|
||
| /// Returns whether the target node should be ignored during analysis because | ||
| /// its enclosing declaration is annotated with one of the excluded | ||
| /// annotations. | ||
| bool shouldIgnore(Declaration node) { | ||
| if (excludedAnnotations.isEmpty) return false; | ||
|
|
||
| AstNode? current = node; | ||
| while (current != null) { | ||
| if (current is Declaration) { | ||
| final hasAnnotation = current.metadata.any((annotation) { | ||
| final name = annotation.name.name; | ||
| final simpleName = name.split('.').last; | ||
| return excludedAnnotations.contains(name) || | ||
| excludedAnnotations.contains(simpleName); | ||
| }); | ||
|
solid-illiaaihistov marked this conversation as resolved.
|
||
| if (hasAnnotation) return true; | ||
| } | ||
| current = current.parent; | ||
| } | ||
|
solid-illiaaihistov marked this conversation as resolved.
|
||
|
|
||
| return false; | ||
| } | ||
|
solid-illiaaihistov marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,87 @@ | ||
| import 'package:solid_lints/src/common/parameters/excluded_annotations_list_parameter.dart'; | ||
| import 'package:solid_lints/src/common/parameters/excluded_entities_list_parameter.dart'; | ||
| import 'package:solid_lints/src/common/parameters/excluded_identifiers_list_parameter.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() { | ||
| group('ExcludedAnnotationsListParameter', () { | ||
| test('parses list of strings', () { | ||
| final param = ExcludedAnnotationsListParameter.fromJson({ | ||
| 'exclude_annotation': ['MyAnnotation1', 'MyAnnotation2'], | ||
| }); | ||
| expect(param.excludedAnnotations, containsAll(['MyAnnotation1', 'MyAnnotation2'])); | ||
| }); | ||
|
|
||
| test('parses single string', () { | ||
| final param = ExcludedAnnotationsListParameter.fromJson({ | ||
| 'exclude_annotation': 'MyAnnotation', | ||
| }); | ||
| expect(param.excludedAnnotations, contains('MyAnnotation')); | ||
| expect(param.excludedAnnotations.length, 1); | ||
| }); | ||
|
|
||
| test('parses empty or invalid input', () { | ||
| final param = ExcludedAnnotationsListParameter.fromJson({}); | ||
| expect(param.excludedAnnotations, isEmpty); | ||
| }); | ||
| }); | ||
|
|
||
| group('ExcludedEntitiesListParameter', () { | ||
| test('parses list of strings', () { | ||
| final param = ExcludedEntitiesListParameter.fromJson({ | ||
| 'exclude_entity': ['mixin', 'enum'], | ||
| }); | ||
| expect(param.excludedEntityNames, containsAll(['mixin', 'enum'])); | ||
| }); | ||
|
|
||
| test('parses single string', () { | ||
| final param = ExcludedEntitiesListParameter.fromJson({ | ||
| 'exclude_entity': 'mixin', | ||
| }); | ||
| expect(param.excludedEntityNames, contains('mixin')); | ||
| expect(param.excludedEntityNames.length, 1); | ||
| }); | ||
|
|
||
| test('parses empty or invalid input', () { | ||
| final param = ExcludedEntitiesListParameter.fromJson({}); | ||
| expect(param.excludedEntityNames, isEmpty); | ||
| }); | ||
| }); | ||
|
|
||
| group('ExcludedIdentifiersListParameter', () { | ||
| test('parses list of strings and maps', () { | ||
| final param = ExcludedIdentifiersListParameter.defaultFromJson({ | ||
| 'exclude': [ | ||
| 'my_function', | ||
| {'class_name': 'MyClass', 'method_name': 'my_method'}, | ||
| ], | ||
| }); | ||
| expect(param.exclude.length, 2); | ||
| expect(param.exclude[0].declarationName, 'my_function'); | ||
| expect(param.exclude[1].className, 'MyClass'); | ||
| expect(param.exclude[1].methodName, 'my_method'); | ||
| }); | ||
|
|
||
| test('parses single string', () { | ||
| final param = ExcludedIdentifiersListParameter.defaultFromJson({ | ||
| 'exclude': 'my_function', | ||
| }); | ||
| expect(param.exclude.length, 1); | ||
| expect(param.exclude[0].declarationName, 'my_function'); | ||
| }); | ||
|
|
||
| test('parses single map', () { | ||
| final param = ExcludedIdentifiersListParameter.defaultFromJson({ | ||
| 'exclude': {'class_name': 'MyClass', 'method_name': 'my_method'}, | ||
| }); | ||
| expect(param.exclude.length, 1); | ||
| expect(param.exclude[0].className, 'MyClass'); | ||
| expect(param.exclude[0].methodName, 'my_method'); | ||
| }); | ||
|
|
||
| test('parses empty or invalid input', () { | ||
| final param = ExcludedIdentifiersListParameter.defaultFromJson({}); | ||
| expect(param.exclude, isEmpty); | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.