-
Notifications
You must be signed in to change notification settings - Fork 24
Add new rule avoid_similar_names #321
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
9
commits into
solid-software:analysis_server_migration
Choose a base branch
from
solid-illiaaihistov:95-add-avoid_similar_names
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
9 commits
Select commit
Hold shift + click to select a range
a2cb420
feat: add avoid_similar_names lint rule
a5c0076
refactor: optimize variable collection and improve tokenization logic…
19674b7
fix: improve acronym tokenization in avoid_similar_names lint to supp…
c9e7bb3
refactor: support linting of declared identifiers and extract variabl…
6c569e8
feat: update avoid_similar_names lint to ignore nullability differenc…
8a1407d
refactor: switch to SimpleAstVisitor and remove redundant super calls…
19e9eee
feat: add support for detecting similar names in pattern variable dec…
03fe8ef
fix: clear reported nodes in avoid similar names visitor to prevent s…
01d9bfd
feat: improve name similarity detection by refining NameTokenizer and…
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
66 changes: 66 additions & 0 deletions
66
lib/src/lints/avoid_similar_names/avoid_similar_names_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,66 @@ | ||
| 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/avoid_similar_names/visitors/avoid_similar_names_visitor.dart'; | ||
| import 'package:solid_lints/src/models/solid_lint_rule.dart'; | ||
|
|
||
| /// Avoid similar names | ||
| /// | ||
| /// Warns about variables or parameters that have confusingly similar names | ||
| /// within the same function scope (e.g., using numeric suffixes or | ||
| /// single-letter modifiers like `someClass1` and `someClass2`). | ||
| /// | ||
| /// This encourages using descriptive, distinct names to improve code | ||
| /// readability and prevent logical errors caused by mixing up variables. | ||
| /// | ||
| /// ### Example | ||
| /// | ||
| /// #### BAD: | ||
| /// | ||
| /// ```dart | ||
| /// void test(SomeClass someClass1, SomeClass someClass2) { // LINT | ||
| /// final tempA = 'a'; // LINT | ||
| /// final tempB = 'b'; // LINT | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// #### GOOD: | ||
| /// | ||
| /// ```dart | ||
| /// void test(SomeClass first, SomeClass second) { | ||
| /// final that = 'a'; | ||
| /// final other = 'b'; | ||
| /// } | ||
| /// ``` | ||
| class AvoidSimilarNamesRule extends SolidLintRule<void> { | ||
| /// The name of this lint rule. | ||
| static const lintName = 'avoid_similar_names'; | ||
|
|
||
| static const _code = LintCode( | ||
| lintName, | ||
| 'Avoid using similar names.', | ||
| correctionMessage: 'Use more descriptive names.', | ||
| ); | ||
|
|
||
| /// Creates an instance of [AvoidSimilarNamesRule]. | ||
| AvoidSimilarNamesRule() | ||
| : super( | ||
| name: lintName, | ||
| description: 'Warns about variables or parameters with similar names.', | ||
| ); | ||
|
|
||
| @override | ||
| LintCode get diagnosticCode => _code; | ||
|
|
||
| @override | ||
| void registerNodeProcessors( | ||
| RuleVisitorRegistry registry, | ||
| RuleContext context, | ||
| ) { | ||
| final visitor = AvoidSimilarNamesVisitor(this); | ||
|
|
||
| registry.addMethodDeclaration(this, visitor); | ||
| registry.addConstructorDeclaration(this, visitor); | ||
| registry.addFunctionDeclaration(this, visitor); | ||
| } | ||
|
solid-illiaaihistov marked this conversation as resolved.
|
||
| } | ||
49 changes: 49 additions & 0 deletions
49
lib/src/lints/avoid_similar_names/models/scope_variable.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,49 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/token.dart'; | ||
| import 'package:analyzer/dart/element/type.dart'; | ||
| import 'package:solid_lints/src/lints/avoid_similar_names/utils/name_tokenizer.dart'; | ||
|
|
||
| /// Represents a variable or parameter collected from a scope. | ||
| class ScopeVariable { | ||
| /// The resolved type of the variable, if available. | ||
| final DartType? type; | ||
|
|
||
| /// The AST node representing this variable declaration. | ||
| final AstNode node; | ||
|
|
||
| /// The token representing the name. | ||
| final Token nameToken; | ||
|
|
||
| /// The individual word/digit tokens of the name. | ||
| final List<String> tokens; | ||
|
|
||
| /// The minimum length for a variable name to be considered descriptive enough | ||
| /// to be analyzed for similarity. | ||
| static const minDescriptiveNameLength = 3; | ||
|
|
||
| /// Creates a new [ScopeVariable] if the [nameToken] is descriptive enough | ||
| /// to be analyzed for similarity. Returns `null` if the cleaned name | ||
| /// is too short. | ||
| static ScopeVariable? createOrNull({ | ||
| required Token nameToken, | ||
| required DartType? type, | ||
| required AstNode node, | ||
| }) { | ||
| final cleaned = NameTokenizer.cleanName(nameToken.lexeme); | ||
| if (cleaned.length < minDescriptiveNameLength) return null; | ||
|
|
||
| return ScopeVariable._( | ||
| nameToken: nameToken, | ||
| type: type, | ||
| node: node, | ||
| tokens: NameTokenizer.tokenize(cleaned), | ||
| ); | ||
| } | ||
|
|
||
| ScopeVariable._({ | ||
| required this.nameToken, | ||
| required this.type, | ||
| required this.node, | ||
| required this.tokens, | ||
| }); | ||
| } |
70 changes: 70 additions & 0 deletions
70
lib/src/lints/avoid_similar_names/utils/name_tokenizer.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,70 @@ | ||
| /// Utility class for tokenizing identifiers and | ||
| /// comparing name similarity. | ||
| abstract final class NameTokenizer { | ||
| /// Regex Fragment | Meaning | ||
| /// ================================================================= | ||
| /// [A-Z]?[a-z]+ | Match words (e.g., Class, user): | ||
| /// [A-Z]? | ... optional leading uppercase, | ||
| /// [a-z]+ | ... followed by lowercase letters. | ||
| /// | | OR | ||
| /// [A-Z]+(?=[A-Z][a-z]|\d|\b)| Match acronyms (uppercase letters). | ||
| /// | | OR | ||
| /// \d+ | Match digits (e.g., 1, 10). | ||
| static final _tokenPattern = RegExp( | ||
| r'[A-Z]?[a-z]+|[A-Z]+(?=[A-Z][a-z]|\d|\b)|\d+', | ||
| ); | ||
|
|
||
| /// Regex Fragment | Meaning | ||
| /// ================================================================= | ||
| /// ^ | Match start of string. | ||
| /// _+ | Match one or more leading underscores. | ||
| static final _leadingUnderscoresPattern = RegExp('^_+'); | ||
|
|
||
| static const _allowedTokens = {'x', 'y', 'z', 'w', 'i', 'j', 'k'}; | ||
|
|
||
| /// Splits a camelCase or snake_case identifier | ||
| /// into lowercase tokens. | ||
| /// | ||
| /// E.g., `someClass1` returns `['some', 'class', '1']`. | ||
| static List<String> tokenize(String name) => [ | ||
| for (final match in _tokenPattern.allMatches(name)) | ||
| if (match.group(0) case final group?) group.toLowerCase(), | ||
| ]; | ||
|
|
||
| /// Strips leading underscores from a name. | ||
| /// | ||
| /// E.g., `_someName` returns `someName`. | ||
| static String cleanName(String name) => | ||
| name.replaceFirst(_leadingUnderscoresPattern, ''); | ||
|
|
||
| /// Returns `true` if the string consists only | ||
| /// of digit characters. | ||
| static bool isDigit(String s) => int.tryParse(s) != null; | ||
|
|
||
| /// Returns `true` if the token is a common | ||
| /// loop variable or coordinate name. | ||
| static bool isAllowedToken(String s) => _allowedTokens.contains(s); | ||
|
|
||
| /// Returns `true` if the token is considered non-descriptive | ||
| /// (either a digit or a disallowed single letter). | ||
| static bool isNonDescriptiveToken(String s) => | ||
| isDigit(s) || (s.length == 1 && !isAllowedToken(s)); | ||
|
|
||
| /// Returns `true` if [longer] is a superset of | ||
| /// [shorter] with exactly one extra non-descriptive token. | ||
| static bool isSubsetWithNonDescriptiveToken( | ||
| List<String> longer, | ||
| List<String> shorter, | ||
| ) { | ||
| if (longer.length != shorter.length + 1) return false; | ||
| var i = 0; | ||
| while (i < shorter.length && longer[i] == shorter[i]) { | ||
| i++; | ||
| } | ||
| if (!isNonDescriptiveToken(longer[i])) return false; | ||
| while (i < shorter.length && longer[i + 1] == shorter[i]) { | ||
| i++; | ||
| } | ||
| return i == shorter.length; | ||
| } | ||
|
solid-illiaaihistov marked this conversation as resolved.
|
||
| } | ||
147 changes: 147 additions & 0 deletions
147
lib/src/lints/avoid_similar_names/visitors/avoid_similar_names_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,147 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:solid_lints/src/lints/avoid_similar_names/avoid_similar_names_rule.dart'; | ||
| import 'package:solid_lints/src/lints/avoid_similar_names/models/scope_variable.dart'; | ||
| import 'package:solid_lints/src/lints/avoid_similar_names/utils/name_tokenizer.dart'; | ||
| import 'package:solid_lints/src/lints/avoid_similar_names/visitors/local_variables_visitor.dart'; | ||
| import 'package:solid_lints/src/utils/types_utils.dart'; | ||
|
|
||
| /// A visitor that checks for variables with | ||
| /// confusingly similar names. | ||
| class AvoidSimilarNamesVisitor extends SimpleAstVisitor<void> { | ||
| final AvoidSimilarNamesRule _rule; | ||
| final _reportedNodes = <AstNode>{}; | ||
| final _collector = LocalVariablesVisitor(); | ||
|
|
||
| /// Creates a new instance of | ||
| /// [AvoidSimilarNamesVisitor]. | ||
| AvoidSimilarNamesVisitor(this._rule); | ||
|
|
||
| @override | ||
| void visitMethodDeclaration( | ||
| MethodDeclaration node, | ||
| ) { | ||
| _checkScope(node.parameters, node.body); | ||
| } | ||
|
|
||
| @override | ||
| void visitConstructorDeclaration( | ||
| ConstructorDeclaration node, | ||
| ) { | ||
| _checkScope(node.parameters, node.body); | ||
| } | ||
|
|
||
| @override | ||
| void visitFunctionDeclaration( | ||
| FunctionDeclaration node, | ||
| ) { | ||
| _checkScope( | ||
| node.functionExpression.parameters, | ||
| node.functionExpression.body, | ||
| ); | ||
| } | ||
|
solid-illiaaihistov marked this conversation as resolved.
solid-illiaaihistov marked this conversation as resolved.
|
||
|
|
||
| void _checkScope( | ||
| FormalParameterList? parameters, | ||
| FunctionBody body, | ||
| ) { | ||
| _reportedNodes.clear(); | ||
| _collector.variables.clear(); | ||
|
solid-illiaaihistov marked this conversation as resolved.
|
||
| body.accept(_collector); | ||
|
|
||
| final variables = [ | ||
| ..._extractParameters(parameters), | ||
| ..._collector.variables, | ||
| ]; | ||
|
|
||
| _compareVariables(variables); | ||
| _reportedNodes.clear(); | ||
| } | ||
|
|
||
| Iterable<ScopeVariable> _extractParameters( | ||
| FormalParameterList? parameters, | ||
| ) => [ | ||
| for (final parameter in parameters?.parameters ?? const <FormalParameter>[]) | ||
| if (parameter.name case final nameToken?) | ||
| if (ScopeVariable.createOrNull( | ||
| nameToken: nameToken, | ||
| type: parameter.declaredFragment?.element.type, | ||
| node: parameter, | ||
| ) | ||
| case final variable?) | ||
| variable, | ||
| ]; | ||
|
|
||
| void _compareVariables( | ||
| List<ScopeVariable> variables, | ||
| ) { | ||
| for (var i = 0; i < variables.length; i++) { | ||
| for (var j = i + 1; j < variables.length; j++) { | ||
| _comparePair(variables[i], variables[j]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void _comparePair( | ||
| ScopeVariable a, | ||
| ScopeVariable b, | ||
| ) { | ||
| if (a.type?.isDifferentIgnoringNullability(b.type) ?? false) { | ||
| return; | ||
| } | ||
|
|
||
| switch ((a.tokens.length - b.tokens.length).abs()) { | ||
| case 0: | ||
| _checkSameLengthTokens(a, b); | ||
| case 1: | ||
| _checkDifferentLengthTokens(a, b); | ||
| } | ||
| } | ||
|
|
||
| void _checkSameLengthTokens( | ||
| ScopeVariable a, | ||
| ScopeVariable b, | ||
| ) { | ||
| var diffCount = 0; | ||
| var diffIndex = -1; | ||
| for (var k = 0; k < a.tokens.length; k++) { | ||
| if (a.tokens[k] != b.tokens[k]) { | ||
| diffCount++; | ||
| if (diffCount > 1) return; | ||
| diffIndex = k; | ||
| } | ||
| } | ||
|
|
||
| if (diffCount != 1) return; | ||
|
|
||
| if (NameTokenizer.isNonDescriptiveToken(a.tokens[diffIndex]) && | ||
| NameTokenizer.isNonDescriptiveToken(b.tokens[diffIndex])) { | ||
| _report(a, b); | ||
| } | ||
| } | ||
|
|
||
| void _checkDifferentLengthTokens( | ||
| ScopeVariable a, | ||
| ScopeVariable b, | ||
| ) { | ||
| final (longer, shorter) = a.tokens.length > b.tokens.length | ||
| ? (a.tokens, b.tokens) | ||
| : (b.tokens, a.tokens); | ||
|
|
||
| if (NameTokenizer.isSubsetWithNonDescriptiveToken( | ||
| longer, | ||
| shorter, | ||
| )) { | ||
| _report(a, b); | ||
| } | ||
| } | ||
|
|
||
| void _report(ScopeVariable a, ScopeVariable b) { | ||
| if (_reportedNodes.add(a.node)) { | ||
| _rule.reportAtToken(a.nameToken); | ||
| } | ||
| if (_reportedNodes.add(b.node)) { | ||
| _rule.reportAtToken(b.nameToken); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.