refactor: migrate no_magic_number rule#300
Conversation
There was a problem hiding this comment.
Code Review
This pull request migrates the no_magic_number lint rule to the new custom_lint API structure, introducing a new NoMagicNumberRuleVisitor to replace the old recursive visitor, adding safe configuration parsing, and introducing comprehensive unit tests. The review feedback suggests two valuable improvements: first, to make configuration parsing more robust against invalid types by replacing as bool? with a safe == true comparison; second, to prevent false positives by ignoring magic numbers located inside annotations.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request migrates the no_magic_number lint rule to the new analyzer rule system, refactoring the logic into NoMagicNumberRuleVisitor and adding comprehensive tests. The reviewer identified a bug in _isNotInsideIndexExpression where negative numbers inside index expressions (e.g., list[-42]) would be incorrectly flagged as magic numbers because l.parent is used instead of _effectiveParent(l). Suggestions were provided to use _effectiveParent(l) and to add a corresponding test case.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors the no_magic_number lint rule to use the new analyzer APIs, delegating AST traversal to a new NoMagicNumberRuleVisitor and adding comprehensive unit tests. Feedback on the changes suggests extending the collection literal check in the visitor to also ignore RecordLiteral nodes, preventing false positives when using Dart 3 records.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request registers and refactors the no_magic_number lint rule to use the new rule-visitor architecture. Specifically, it introduces NoMagicNumberRuleVisitor to handle AST traversal, refactors NoMagicNumberRule to delegate to this visitor, updates NoMagicNumberParameters with safe JSON parsing and an empty factory, and adds a comprehensive test suite covering various magic number scenarios. There are no review comments, and I have no additional feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
… legacy integration tests
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors the no_magic_number lint rule to align with the updated custom lint framework, introducing NoMagicNumberRuleVisitor to handle AST node processing and adding comprehensive unit tests. The feedback suggests improving the _effectiveParent helper method in the visitor to recursively skip parenthesized expressions and unary prefix expressions, preventing potential false positives when magic numbers are wrapped in parentheses.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
solid-danylosafonov
left a comment
There was a problem hiding this comment.
I think we can refactor the Visitor a bit
| if (!_isMagicNumber(node)) return; | ||
| if (!_isNotInsideVariable(node)) return; | ||
| if (!_isNotInsideCollectionLiteral(node)) return; | ||
| if (!_isNotInsideConstConstructor(node)) return; | ||
| if (!_isNotInDateTime(node)) return; | ||
| if (!_isNotInsideIndexExpression(node)) return; | ||
| if (!_isNotInsideEnumConstantArguments(node)) return; | ||
| if (!_isNotDefaultValue(node)) return; | ||
| if (!_isNotInConstructorInitializer(node)) return; | ||
| if (!_isNotWidgetParameter(node)) return; |
There was a problem hiding this comment.
why don't we invert them?
| if (!_isMagicNumber(node)) return; | |
| if (!_isNotInsideVariable(node)) return; | |
| if (!_isNotInsideCollectionLiteral(node)) return; | |
| if (!_isNotInsideConstConstructor(node)) return; | |
| if (!_isNotInDateTime(node)) return; | |
| if (!_isNotInsideIndexExpression(node)) return; | |
| if (!_isNotInsideEnumConstantArguments(node)) return; | |
| if (!_isNotDefaultValue(node)) return; | |
| if (!_isNotInConstructorInitializer(node)) return; | |
| if (!_isNotWidgetParameter(node)) return; | |
| if (_isNotMagicNumber(node)) return; | |
| if (_isInsideVariable(node)) return; | |
| if (_isInsideCollectionLiteral(node)) return; | |
| if (_isInsideConstConstructor(node)) return; | |
| if (_isInDateTime(node)) return; | |
| if (_isInsideIndexExpression(node)) return; | |
| if (_isInsideEnumConstantArguments(node)) return; | |
| if (_isDefaultValue(node)) return; | |
| if (_isInConstructorInitializer(node)) return; | |
| if (_isWidgetParameter(node)) return; |
| bool _isNotDefaultValue(Literal literal) { | ||
| return literal.thisOrAncestorOfType<DefaultFormalParameter>() == null; | ||
| } | ||
|
|
||
| bool _isNotInConstructorInitializer(Literal literal) { | ||
| return literal.thisOrAncestorOfType<ConstructorInitializer>() == null; | ||
| } |
There was a problem hiding this comment.
I think we can extract the methods like those (that aren't dependent on _rule or _parameters) to an extension and put it somewhere in utils folder
There was a problem hiding this comment.
I've moved the methods that look reusable into an extension.
… magic number validation checks
Closes #255