-
Notifications
You must be signed in to change notification settings - Fork 24
refactor: migrate no_magic_number rule #300
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
6
commits into
solid-software:analysis_server_migration
Choose a base branch
from
solid-illiaaihistov:255-migrate-no_magic_number
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
6 commits
Select commit
Hold shift + click to select a range
b40bee6
refactor: migrate no_magic_number rule
95ac8e1
fix: ignore magic numbers in annotations and update boolean configura…
e31a5d9
fix: allow negative numbers in list index expressions for no_magic_nu…
cc0e0cf
feat: ignore magic numbers within record literals and named record fi…
586fcae
refactor: migrate no_magic_number test cases to unit tests and remove…
78b275c
refactor: move AST inspection logic to AstNode extension and simplify…
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
98 changes: 98 additions & 0 deletions
98
lib/src/lints/no_magic_number/visitors/no_magic_number_rule_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,98 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:analyzer/dart/element/type.dart'; | ||
| import 'package:collection/collection.dart'; | ||
| import 'package:solid_lints/src/lints/no_magic_number/models/no_magic_number_parameters.dart'; | ||
| import 'package:solid_lints/src/models/solid_lint_rule.dart'; | ||
| import 'package:solid_lints/src/utils/node_utils.dart'; | ||
|
|
||
| /// The AST visitor that checks if double and integer literals are magic numbers. | ||
| class NoMagicNumberRuleVisitor extends SimpleAstVisitor<void> { | ||
| final SolidLintRule _rule; | ||
| final NoMagicNumberParameters _parameters; | ||
|
|
||
| /// Creates a new instance of [NoMagicNumberRuleVisitor]. | ||
| NoMagicNumberRuleVisitor(this._rule, this._parameters); | ||
|
|
||
| @override | ||
| void visitDoubleLiteral(DoubleLiteral node) { | ||
| _checkLiteral(node); | ||
| } | ||
|
|
||
| @override | ||
| void visitIntegerLiteral(IntegerLiteral node) { | ||
| _checkLiteral(node); | ||
| } | ||
|
|
||
| void _checkLiteral(Literal node) { | ||
| if (_isNotMagicNumber(node)) return; | ||
| if (_isInsideVariable(node)) return; | ||
| if (_isInsideCollectionLiteral(node)) return; | ||
| if (_isWidgetParameter(node)) return; | ||
|
|
||
| if (node.isInsideConstConstructor) return; | ||
| if (node.isInDateTime) return; | ||
| if (node.isInsideIndexExpression) return; | ||
| if (node.isInsideEnumConstantArguments) return; | ||
| if (node.isDefaultValue) return; | ||
| if (node.isInConstructorInitializer) return; | ||
|
|
||
| _rule.reportAtNode(node); | ||
| } | ||
|
|
||
| bool _isNotMagicNumber(Literal l) => | ||
| (l is DoubleLiteral && _parameters.allowedNumbers.contains(l.value)) || | ||
| (l is IntegerLiteral && _parameters.allowedNumbers.contains(l.value)); | ||
|
|
||
| /// Returns the effective parent of [l], skipping over a unary | ||
| /// [PrefixExpression] (e.g. the `-` in `-42`). | ||
| AstNode? _effectiveParent(Literal l) => | ||
| l.parent is PrefixExpression ? l.parent?.parent : l.parent; | ||
|
|
||
| /// Returns `true` if the literal is the direct initializer of a variable | ||
| /// declaration. | ||
| /// | ||
| /// Allows `var x = 42` (literal gets a name) but reports `var r = x + 42` | ||
| /// (42 is a magic number inside an expression). | ||
| bool _isInsideVariable(Literal l) => | ||
| _effectiveParent(l) is VariableDeclaration; | ||
|
|
||
| /// Returns `true` if the literal is inside a collection literal. | ||
| /// | ||
| /// Covers list elements (`[42]`, `[-42]`), set elements (`{42}`, `{-42}`), | ||
| /// and map keys/values (`{42: 'a'}`, `{-42: 'a'}`, `{'a': -42}`). | ||
| /// | ||
| /// Unary prefix expressions (e.g. `-42`) are skipped transparently so that | ||
| /// the effective parent—the collection literal—is checked instead of the | ||
| /// intermediate [PrefixExpression] node. | ||
| bool _isInsideCollectionLiteral(Literal l) { | ||
| final p = _effectiveParent(l); | ||
| return p is TypedLiteral || | ||
| p is MapLiteralEntry || | ||
| p is RecordLiteral || | ||
| (p is NamedExpression && p.parent is RecordLiteral); | ||
| } | ||
|
|
||
| bool _isWidgetParameter(Literal literal) { | ||
| if (!_parameters.allowedInWidgetParams) return false; | ||
|
|
||
| final widgetCreationExpression = literal.thisOrAncestorMatching( | ||
| _isWidgetCreationExpression, | ||
| ); | ||
|
|
||
| return widgetCreationExpression != null; | ||
| } | ||
|
|
||
| bool _isWidgetCreationExpression(AstNode node) { | ||
| if (node is! InstanceCreationExpression) return false; | ||
|
|
||
| final staticType = node.staticType; | ||
| if (staticType is! InterfaceType) return false; | ||
|
|
||
| final widgetSupertype = staticType.allSupertypes.firstWhereOrNull( | ||
| (supertype) => supertype.getDisplayString() == 'Widget', | ||
| ); | ||
|
|
||
| return widgetSupertype != null; | ||
| } | ||
| } | ||
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.