-
Notifications
You must be signed in to change notification settings - Fork 24
Add new rule: use_descriptive_names_for_type_parameters #215
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
xJac0b
wants to merge
5
commits into
master
Choose a base branch
from
issue-94-use_descriptive_names_for_type_parameters
base: master
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 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
babf88e
feat: add new rule use_descriptive_names_for_type_parameters
xJac0b 3769d30
refactor code
xJac0b 130ca22
use cascade notattion
xJac0b 7c54c79
Add test case with nested functions
xJac0b f7e4b80
Add parameter for minimum number of type parameters
xJac0b 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
84 changes: 84 additions & 0 deletions
84
...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,84 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/error/listener.dart'; | ||
| import 'package:custom_lint_builder/custom_lint_builder.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/rule_config.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. | ||
| class UseDescriptiveNamesForTypeParametersRule extends SolidLintRule { | ||
| /// The lint rule name. | ||
| static const lintName = 'use_descriptive_names_for_type_parameters'; | ||
|
|
||
| UseDescriptiveNamesForTypeParametersRule._(super.config); | ||
|
|
||
| /// Creates a new instance of [UseDescriptiveNamesForTypeParametersRule] | ||
| /// based on the lint configuration. | ||
| factory UseDescriptiveNamesForTypeParametersRule.createRule( | ||
| CustomLintConfigs configs, | ||
| ) { | ||
| final rule = RuleConfig( | ||
| configs: configs, | ||
| name: lintName, | ||
| problemMessage: (_) => | ||
| 'Type parameters should have descriptive names instead ' | ||
| 'of single letters when there are three or more type parameters.', | ||
| ); | ||
|
|
||
| return UseDescriptiveNamesForTypeParametersRule._(rule); | ||
| } | ||
|
|
||
| @override | ||
| void run( | ||
| CustomLintResolver resolver, | ||
| ErrorReporter reporter, | ||
| CustomLintContext context, | ||
| ) { | ||
| context.registry.addClassDeclaration((node) { | ||
| final visitor = UseDescriptiveNamesForTypeParametersVisitor(); | ||
| visitor.visitClassDeclaration(node); | ||
| _reportViolations(reporter, visitor.singleLetterTypeParameters); | ||
| }); | ||
|
|
||
| context.registry.addFunctionDeclaration((node) { | ||
| final visitor = UseDescriptiveNamesForTypeParametersVisitor(); | ||
| visitor.visitFunctionDeclaration(node); | ||
| _reportViolations(reporter, visitor.singleLetterTypeParameters); | ||
| }); | ||
|
|
||
| context.registry.addMethodDeclaration((node) { | ||
| final visitor = UseDescriptiveNamesForTypeParametersVisitor(); | ||
| visitor.visitMethodDeclaration(node); | ||
| _reportViolations(reporter, visitor.singleLetterTypeParameters); | ||
| }); | ||
|
|
||
| context.registry.addGenericTypeAlias((node) { | ||
| final visitor = UseDescriptiveNamesForTypeParametersVisitor(); | ||
| visitor.visitGenericTypeAlias(node); | ||
| _reportViolations(reporter, visitor.singleLetterTypeParameters); | ||
| }); | ||
|
|
||
| context.registry.addExtensionDeclaration((node) { | ||
| final visitor = UseDescriptiveNamesForTypeParametersVisitor(); | ||
| visitor.visitExtensionDeclaration(node); | ||
| _reportViolations(reporter, visitor.singleLetterTypeParameters); | ||
| }); | ||
|
|
||
| context.registry.addMixinDeclaration((node) { | ||
| final visitor = UseDescriptiveNamesForTypeParametersVisitor(); | ||
| visitor.visitMixinDeclaration(node); | ||
| _reportViolations(reporter, visitor.singleLetterTypeParameters); | ||
| }); | ||
| } | ||
|
|
||
| void _reportViolations( | ||
| ErrorReporter reporter, | ||
| List<TypeParameter> singleLetterTypeParameters, | ||
| ) { | ||
| for (final param in singleLetterTypeParameters) { | ||
| reporter.atNode(param, code); | ||
| } | ||
| } | ||
xJac0b marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
60 changes: 60 additions & 0 deletions
60
...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,60 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
|
|
||
| /// AST Visitor which finds type parameters with single-letter names | ||
| /// in declarations with three or more type parameters. | ||
| class UseDescriptiveNamesForTypeParametersVisitor | ||
| extends RecursiveAstVisitor<void> { | ||
| /// List of type parameters with single-letter names. | ||
| final singleLetterTypeParameters = <TypeParameter>[]; | ||
|
|
||
| @override | ||
| void visitClassDeclaration(ClassDeclaration node) { | ||
| super.visitClassDeclaration(node); | ||
| _checkTypeParameters(node.typeParameters); | ||
| } | ||
|
|
||
| @override | ||
| void visitFunctionDeclaration(FunctionDeclaration node) { | ||
| super.visitFunctionDeclaration(node); | ||
| final function = node.functionExpression; | ||
| _checkTypeParameters(function.typeParameters); | ||
| } | ||
|
|
||
| @override | ||
| void visitMethodDeclaration(MethodDeclaration node) { | ||
| super.visitMethodDeclaration(node); | ||
| _checkTypeParameters(node.typeParameters); | ||
| } | ||
|
|
||
| @override | ||
| void visitGenericTypeAlias(GenericTypeAlias node) { | ||
| super.visitGenericTypeAlias(node); | ||
| _checkTypeParameters(node.typeParameters); | ||
| } | ||
|
|
||
| @override | ||
| void visitExtensionDeclaration(ExtensionDeclaration node) { | ||
| super.visitExtensionDeclaration(node); | ||
| _checkTypeParameters(node.typeParameters); | ||
| } | ||
|
|
||
| @override | ||
| void visitMixinDeclaration(MixinDeclaration node) { | ||
| super.visitMixinDeclaration(node); | ||
| _checkTypeParameters(node.typeParameters); | ||
| } | ||
|
|
||
| void _checkTypeParameters(TypeParameterList? typeParameters) { | ||
| if (typeParameters == null || typeParameters.typeParameters.length < 3) { | ||
| return; | ||
| } | ||
|
|
||
| for (final param in typeParameters.typeParameters) { | ||
| final name = param.name.lexeme; | ||
| if (name.length == 1) { | ||
| singleLetterTypeParameters.add(param); | ||
| } | ||
| } | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
lint_test/use_descriptive_names_for_type_parameters_test/analysis_options.yaml
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,3 @@ | ||
| custom_lint: | ||
| rules: | ||
| - use_descriptive_names_for_type_parameters |
45 changes: 45 additions & 0 deletions
45
...iptive_names_for_type_parameters_test/use_descriptive_names_for_type_parameters_test.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,45 @@ | ||
| /// Check the `use_descriptive_names_for_type_parameters` rule | ||
|
|
||
| // expect_lint: use_descriptive_names_for_type_parameters | ||
| class SomeClass<T, U, K> {} | ||
|
|
||
| class ValidClass<Type, Data, Context> {} | ||
|
|
||
| class TwoParams<T, U> {} | ||
|
|
||
| // expect_lint: use_descriptive_names_for_type_parameters | ||
| class AnotherClass<A, B, C, D> {} | ||
|
|
||
| // expect_lint: use_descriptive_names_for_type_parameters | ||
| void functionWithTypes<T, U, K>(T t, U u, K k) {} | ||
xJac0b marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| void validFunction<Type, Data, Context>(Type t, Data d, Context c) {} | ||
|
|
||
| void twoTypeParams<T, U>(T t, U u) {} | ||
|
|
||
| // expect_lint: use_descriptive_names_for_type_parameters | ||
| class ComplexClass<X, Y, Z, W> { | ||
| // expect_lint: use_descriptive_names_for_type_parameters | ||
| void method<T, U, K>() {} | ||
| } | ||
|
|
||
| class ValidComplexClass<Type, Data, Context, State> { | ||
| void validMethod<Key, Value, Result>() {} | ||
| } | ||
|
|
||
| // expect_lint: use_descriptive_names_for_type_parameters | ||
| typedef SomeAlias<T, U, K> = Map<T, Map<U, K>>; | ||
|
|
||
| typedef ValidAlias<Type, Data, Context> = Map<Type, Map<Data, Context>>; | ||
|
|
||
| typedef TwoParamAlias<T, U> = Map<T, U>; | ||
|
|
||
| // expect_lint: use_descriptive_names_for_type_parameters | ||
| extension Ext<T, U, K> on List<T> {} | ||
|
|
||
| extension ValidExt<Type, Data, Context> on List<Type> {} | ||
|
|
||
| // expect_lint: use_descriptive_names_for_type_parameters | ||
| mixin Mixin<T, U, K> {} | ||
|
|
||
| mixin ValidMixin<Type, Data, Context> {} | ||
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.