-
Notifications
You must be signed in to change notification settings - Fork 24
Add new rule (migrated): use_descriptive_names_for_type_parameters #319
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
Merged
solid-illiaaihistov
merged 12 commits into
solid-software:analysis_server_migration
from
solid-illiaaihistov:issue-94-use_descriptive_names_for_type_parameters
Jul 17, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
36d9ca1
feat: add new rule use_descriptive_names_for_type_parameters
xJac0b 2a0d5ca
refactor code
xJac0b cea1860
use cascade notattion
xJac0b 00b89e7
Add test case with nested functions
xJac0b 1d8c57d
Add parameter for minimum number of type parameters
xJac0b cac35b6
docs: add missing period to fromJson documentation comment
feadc70
refactor: migrate use_descriptive_names_for_type_parameters rule
4c11e62
feat: extend use_descriptive_names_for_type_parameters to check class…
671fdc7
feat: add use_descriptive_names_for_type_parameters rule and bump ver…
3d1f022
chore: remove use_descriptive_names_for_type_parameters test files
7bb3856
refactor: simplify visitor implementation using pattern matching and …
1acb435
Merge remote-tracking branch 'upstream/analysis_server_migration' int…
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
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
28 changes: 28 additions & 0 deletions
28
...ames_for_type_parameters/models/use_descriptive_names_for_type_parameters_parameters.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,28 @@ | ||
| /// A data model class that represents the | ||
| /// "use descriptive names for type parameters" input parameters. | ||
| class UseDescriptiveNamesForTypeParametersParameters { | ||
| static const _defaultMinTypeParameters = 3; | ||
|
|
||
| /// Minimum number of type parameters required before the rule is enforced. | ||
| final int minTypeParameters; | ||
|
|
||
| /// Constructor for [UseDescriptiveNamesForTypeParametersParameters] model | ||
| const UseDescriptiveNamesForTypeParametersParameters({ | ||
| required this.minTypeParameters, | ||
| }); | ||
|
|
||
| /// Empty [UseDescriptiveNamesForTypeParametersParameters] model. | ||
| factory UseDescriptiveNamesForTypeParametersParameters.empty() { | ||
| return const UseDescriptiveNamesForTypeParametersParameters( | ||
| minTypeParameters: _defaultMinTypeParameters, | ||
| ); | ||
| } | ||
|
|
||
| /// Method for creating from json data. | ||
| factory UseDescriptiveNamesForTypeParametersParameters.fromJson( | ||
| Map<String, Object?> json, | ||
| ) => UseDescriptiveNamesForTypeParametersParameters( | ||
| minTypeParameters: | ||
| json['min_type_parameters'] as int? ?? _defaultMinTypeParameters, | ||
| ); | ||
| } |
90 changes: 90 additions & 0 deletions
90
...descriptive_names_for_type_parameters/use_descriptive_names_for_type_parameters_rule.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,90 @@ | ||
| import 'package:analyzer/analysis_rule/rule_context.dart'; | ||
| import 'package:analyzer/analysis_rule/rule_visitor_registry.dart'; | ||
| import 'package:analyzer/error/error.dart'; | ||
| import 'package:solid_lints/src/lints/use_descriptive_names_for_type_parameters/models/use_descriptive_names_for_type_parameters_parameters.dart'; | ||
| import 'package:solid_lints/src/lints/use_descriptive_names_for_type_parameters/visitors/use_descriptive_names_for_type_parameters_visitor.dart'; | ||
| import 'package:solid_lints/src/models/solid_lint_rule.dart'; | ||
|
|
||
| /// A `use_descriptive_names_for_type_parameters` rule which | ||
| /// warns about single-letter type parameter names when there are | ||
| /// three or more type parameters. | ||
| /// | ||
| /// ### Example: | ||
| /// | ||
| /// Assuming config: | ||
| /// | ||
| /// ```yaml | ||
| /// plugins: | ||
| /// solid_lints: | ||
| /// diagnostics: | ||
| /// use_descriptive_names_for_type_parameters: | ||
| /// min_type_parameters: 3 | ||
| /// ``` | ||
| /// | ||
| /// #### BAD: | ||
| /// ```dart | ||
| /// class MyClass<T, U, V> {} // LINT | ||
| /// ``` | ||
| /// | ||
| /// #### GOOD: | ||
| /// ```dart | ||
| /// class MyClass<TSource, TResult, TError> {} // OK | ||
| /// class MyClass2<T, U> {} // OK | ||
| /// ``` | ||
| class UseDescriptiveNamesForTypeParametersRule | ||
| extends SolidLintRule<UseDescriptiveNamesForTypeParametersParameters> { | ||
| /// The lint rule name. | ||
| static const lintName = 'use_descriptive_names_for_type_parameters'; | ||
|
|
||
| static const _code = LintCode( | ||
| lintName, | ||
| 'Type parameters should have descriptive names instead ' | ||
| 'of single letters when there are {0} or ' | ||
| 'more type parameters.', | ||
| ); | ||
|
|
||
| @override | ||
| DiagnosticCode get diagnosticCode => _code; | ||
|
|
||
| /// Creates a new instance of [UseDescriptiveNamesForTypeParametersRule]. | ||
| UseDescriptiveNamesForTypeParametersRule({ | ||
| required super.analysisOptionsLoader, | ||
| }) : super.withParameters( | ||
| name: lintName, | ||
| description: | ||
| 'Warns about single-letter type parameter names when there are ' | ||
| 'three or more type parameters.', | ||
| parametersParser: | ||
| UseDescriptiveNamesForTypeParametersParameters.fromJson, | ||
| ); | ||
|
|
||
| @override | ||
| void registerNodeProcessors( | ||
| RuleVisitorRegistry registry, | ||
| RuleContext context, | ||
| ) { | ||
| super.registerNodeProcessors(registry, context); | ||
|
|
||
| final parameters = | ||
| getParametersForContext(context) ?? | ||
| UseDescriptiveNamesForTypeParametersParameters.empty(); | ||
|
|
||
| final visitor = UseDescriptiveNamesForTypeParametersVisitor( | ||
| this, | ||
| parameters, | ||
| ); | ||
|
|
||
| registry | ||
| ..addClassDeclaration(this, visitor) | ||
| ..addClassTypeAlias(this, visitor) | ||
| ..addEnumDeclaration(this, visitor) | ||
| ..addFunctionExpression(this, visitor) | ||
| ..addMethodDeclaration(this, visitor) | ||
| ..addGenericTypeAlias(this, visitor) | ||
| ..addFunctionTypeAlias(this, visitor) | ||
| ..addGenericFunctionType(this, visitor) | ||
| ..addExtensionDeclaration(this, visitor) | ||
| ..addMixinDeclaration(this, visitor) | ||
| ..addExtensionTypeDeclaration(this, visitor); | ||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
...names_for_type_parameters/visitors/use_descriptive_names_for_type_parameters_visitor.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,73 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:solid_lints/src/lints/use_descriptive_names_for_type_parameters/models/use_descriptive_names_for_type_parameters_parameters.dart'; | ||
| import 'package:solid_lints/src/lints/use_descriptive_names_for_type_parameters/use_descriptive_names_for_type_parameters_rule.dart'; | ||
|
|
||
| /// A visitor that checks descriptive names for type parameters on declarations. | ||
| class UseDescriptiveNamesForTypeParametersVisitor | ||
| extends SimpleAstVisitor<void> { | ||
| final UseDescriptiveNamesForTypeParametersRule _rule; | ||
| final UseDescriptiveNamesForTypeParametersParameters _parameters; | ||
|
|
||
| int get _minParameters => _parameters.minTypeParameters; | ||
|
|
||
| /// Creates a new instance of [UseDescriptiveNamesForTypeParametersVisitor]. | ||
| UseDescriptiveNamesForTypeParametersVisitor(this._rule, this._parameters); | ||
|
|
||
| void _visit(TypeParameterList? types) { | ||
| if (types case TypeParameterList( | ||
| typeParameters: final ps, | ||
| ) when ps.length >= _minParameters) { | ||
| ps.where(_hasInvalidShortName).forEach(_report); | ||
| } | ||
| } | ||
|
|
||
| bool _hasInvalidShortName(TypeParameter p) => | ||
| p.name.length == 1 && p.name.lexeme != '_'; | ||
|
|
||
| void _report(TypeParameter p) => | ||
| _rule.reportAtNode(p, arguments: ['$_minParameters']); | ||
|
|
||
| @override | ||
| void visitClassDeclaration(ClassDeclaration node) => | ||
| _visit(node.namePart.typeParameters); | ||
|
|
||
| @override | ||
| void visitClassTypeAlias(ClassTypeAlias node) => _visit(node.typeParameters); | ||
|
|
||
| @override | ||
| void visitEnumDeclaration(EnumDeclaration node) => | ||
| _visit(node.namePart.typeParameters); | ||
|
|
||
| @override | ||
| void visitFunctionExpression(FunctionExpression node) => | ||
| _visit(node.typeParameters); | ||
|
|
||
| @override | ||
| void visitMethodDeclaration(MethodDeclaration node) => | ||
| _visit(node.typeParameters); | ||
|
|
||
| @override | ||
| void visitGenericTypeAlias(GenericTypeAlias node) => | ||
| _visit(node.typeParameters); | ||
|
|
||
| @override | ||
| void visitFunctionTypeAlias(FunctionTypeAlias node) => | ||
| _visit(node.typeParameters); | ||
|
|
||
| @override | ||
| void visitGenericFunctionType(GenericFunctionType node) => | ||
| _visit(node.typeParameters); | ||
|
|
||
| @override | ||
| void visitExtensionDeclaration(ExtensionDeclaration node) => | ||
| _visit(node.typeParameters); | ||
|
|
||
| @override | ||
| void visitMixinDeclaration(MixinDeclaration node) => | ||
| _visit(node.typeParameters); | ||
|
|
||
| @override | ||
| void visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) => | ||
| _visit(node.primaryConstructor.typeParameters); | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Could be a bit shorter: