Skip to content

refactor: migrate no_magic_number rule#300

Open
solid-illiaaihistov wants to merge 6 commits into
solid-software:analysis_server_migrationfrom
solid-illiaaihistov:255-migrate-no_magic_number
Open

refactor: migrate no_magic_number rule#300
solid-illiaaihistov wants to merge 6 commits into
solid-software:analysis_server_migrationfrom
solid-illiaaihistov:255-migrate-no_magic_number

Conversation

@solid-illiaaihistov

Copy link
Copy Markdown
Collaborator

Closes #255

@solid-illiaaihistov solid-illiaaihistov linked an issue Jun 24, 2026 that may be closed by this pull request

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread lib/src/lints/no_magic_number/models/no_magic_number_parameters.dart Outdated
Comment thread lib/src/lints/no_magic_number/visitors/no_magic_number_rule_visitor.dart Outdated
@solid-illiaaihistov

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread lib/src/lints/no_magic_number/visitors/no_magic_number_rule_visitor.dart Outdated
Comment thread test/src/lints/no_magic_number/no_magic_number_rule_test.dart
@solid-illiaaihistov

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread lib/src/lints/no_magic_number/visitors/no_magic_number_rule_visitor.dart Outdated
@solid-illiaaihistov

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@solid-illiaaihistov

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 solid-danylosafonov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can refactor the Visitor a bit

Comment on lines +27 to +36
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we invert them?

Suggested change
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;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Comment on lines +99 to +105
bool _isNotDefaultValue(Literal literal) {
return literal.thisOrAncestorOfType<DefaultFormalParameter>() == null;
}

bool _isNotInConstructorInitializer(Literal literal) {
return literal.thisOrAncestorOfType<ConstructorInitializer>() == null;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the methods that look reusable into an extension.

@solid-danylosafonov solid-danylosafonov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate no_magic_number

2 participants