-
Notifications
You must be signed in to change notification settings - Fork 24
refactor: migrate prefer_match_file_name #305
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
4
commits into
solid-software:analysis_server_migration
Choose a base branch
from
solid-illiaaihistov:258-migrate-prefer_match_file_name
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
4 commits
Select commit
Hold shift + click to select a range
46749c7
refactor: migrate prefer_match_file_name
4d40144
refactor: improve prefer_match_file_name lint logic with switch expre…
f930667
test: add test cases for prefer_match_file_name rule
605be49
refactor: move normalization logic to visitor and add multi-sort util…
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
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
122 changes: 68 additions & 54 deletions
122
lib/src/lints/prefer_match_file_name/visitors/prefer_match_file_name_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 |
|---|---|---|
| @@ -1,83 +1,97 @@ | ||
| import 'package:analyzer/analysis_rule/rule_context.dart'; | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:analyzer/error/error.dart'; | ||
| import 'package:collection/collection.dart'; | ||
| import 'package:path/path.dart' as p; | ||
| import 'package:solid_lints/src/common/parameters/excluded_entities_list_parameter.dart'; | ||
| import 'package:solid_lints/src/lints/prefer_match_file_name/models/declaration_token_info.dart'; | ||
| import 'package:solid_lints/src/utils/iterable_utils.dart'; | ||
| import 'package:solid_lints/src/utils/node_utils.dart'; | ||
|
|
||
| /// The AST visitor that will collect all Class, Enum, Extension and Mixin | ||
| /// declarations | ||
| class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> { | ||
| final _declarations = <DeclarationTokenInfo>[]; | ||
| /// The AST visitor that will collect all Class, Enum, Extension, Mixin and | ||
| /// Extension Type declarations | ||
| class PreferMatchFileNameVisitor extends SimpleAstVisitor<void> { | ||
| static final _onlySymbolsRegex = RegExp('[^a-zA-Z0-9]'); | ||
|
|
||
| /// The diagnostic code to report | ||
| final DiagnosticCode diagnosticCode; | ||
|
|
||
| /// The rule context | ||
| final RuleContext context; | ||
|
|
||
| /// Iterable that contains the name of entity (or entities) that should | ||
| /// be ignored | ||
| final ExcludedEntitiesListParameter excludedEntities; | ||
|
|
||
| /// Constructor of [PreferMatchFileNameVisitor] class | ||
| PreferMatchFileNameVisitor({ | ||
| required this.diagnosticCode, | ||
| required this.context, | ||
| required this.excludedEntities, | ||
| }); | ||
|
|
||
| /// List of all declarations | ||
| Iterable<DeclarationTokenInfo> get declarations => _declarations.where( | ||
| (declaration) { | ||
| if (declaration.parent is Declaration) { | ||
| return !excludedEntities | ||
| .shouldIgnoreEntity(declaration.parent as Declaration); | ||
| } | ||
| return true; | ||
| }, | ||
| ).toList() | ||
| ..sort( | ||
| (a, b) => _publicDeclarationsFirst(a, b) ?? _byDeclarationOrder(a, b), | ||
| ); | ||
|
|
||
| @override | ||
| void visitClassDeclaration(ClassDeclaration node) { | ||
| super.visitClassDeclaration(node); | ||
| void visitCompilationUnit(CompilationUnit node) { | ||
| final declarations = node.declarations | ||
| .whereNot(excludedEntities.shouldIgnoreEntity) | ||
| .map<DeclarationTokenInfo?>( | ||
| (d) { | ||
| final token = switch (d) { | ||
| ClassDeclaration() => d.namePart.typeName, | ||
| ExtensionDeclaration() => d.name, | ||
| MixinDeclaration() => d.name, | ||
| EnumDeclaration() => d.namePart.typeName, | ||
| ExtensionTypeDeclaration() => d.primaryConstructor.typeName, | ||
| _ => null, | ||
| }; | ||
|
|
||
| _declarations.add((token: node.name, parent: node)); | ||
| } | ||
| return token == null ? null : (token: token, parent: d); | ||
| }, | ||
| ) | ||
| .nonNulls | ||
| .multiSortedBy( | ||
| [ | ||
| (t) => Identifier.isPrivateName(t.token.lexeme) ? 1 : 0, | ||
| (t) => t.token.offset, | ||
| ], | ||
| ); | ||
|
|
||
| @override | ||
| void visitExtensionDeclaration(ExtensionDeclaration node) { | ||
| super.visitExtensionDeclaration(node); | ||
| if (declarations.isEmpty) return; | ||
|
|
||
| final name = node.name; | ||
| if (name != null) { | ||
| _declarations.add((token: name, parent: node)); | ||
| final firstDeclaration = declarations.first; | ||
| final fullName = context.currentUnit?.file.path; | ||
|
|
||
| if (fullName != null && | ||
| _doNormalizedNamesMatch( | ||
| fullName, | ||
| firstDeclaration.token.lexeme, | ||
| )) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| void visitMixinDeclaration(MixinDeclaration node) { | ||
| super.visitMixinDeclaration(node); | ||
| final nodeType = humanReadableNodeType( | ||
| firstDeclaration.parent, | ||
| ).toLowerCase(); | ||
|
|
||
| _declarations.add((token: node.name, parent: node)); | ||
| final reporter = context.currentUnit?.diagnosticReporter; | ||
| reporter?.atToken( | ||
| firstDeclaration.token, | ||
| diagnosticCode, | ||
| arguments: [nodeType], | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| void visitEnumDeclaration(EnumDeclaration node) { | ||
| super.visitEnumDeclaration(node); | ||
| bool _doNormalizedNamesMatch(String path, String identifierName) { | ||
| final fileName = _normalizePath(path); | ||
| final dartIdentifier = _normalizeDartIdentifierName(identifierName); | ||
|
|
||
| _declarations.add((token: node.name, parent: node)); | ||
| return fileName == dartIdentifier; | ||
| } | ||
|
|
||
| int? _publicDeclarationsFirst( | ||
| DeclarationTokenInfo a, | ||
| DeclarationTokenInfo b, | ||
| ) { | ||
| final isAPrivate = Identifier.isPrivateName(a.token.lexeme); | ||
| final isBPrivate = Identifier.isPrivateName(b.token.lexeme); | ||
| if (!isAPrivate && isBPrivate) { | ||
| return -1; | ||
| } else if (isAPrivate && !isBPrivate) { | ||
| return 1; | ||
| } | ||
| // no reorder needed; | ||
| return null; | ||
| } | ||
| String _normalizePath(String s) => | ||
| _normalizeDartIdentifierName(p.basename(s).split('.').first); | ||
|
|
||
| int _byDeclarationOrder(DeclarationTokenInfo a, DeclarationTokenInfo b) { | ||
| return a.token.offset.compareTo(b.token.offset); | ||
| } | ||
| String _normalizeDartIdentifierName(String s) => | ||
| s.replaceAll(_onlySymbolsRegex, '').toLowerCase(); | ||
| } | ||
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,15 @@ | ||
| import 'package:collection/collection.dart'; | ||
|
|
||
| /// Extension on [Iterable] to sort by multiple keys. | ||
| extension MultiSortedByIterable<T> on Iterable<T> { | ||
| /// Sorts this iterable by multiple keys sequentially. | ||
| /// | ||
| /// The first key selector that produces a non-zero comparison is used. | ||
| List<T> multiSortedBy(List<Comparable<dynamic> Function(T)> keys) => sorted( | ||
| (a, b) => | ||
| keys | ||
| .map((key) => key(a).compareTo(key(b))) | ||
| .firstWhereOrNull((comparison) => comparison != 0) ?? | ||
| 0, | ||
| ); | ||
| } |
This file was deleted.
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.
To add to utils:
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.
Done!