Skip to content

Commit

Permalink
Ignore magic number in constructor initializer (#73)
Browse files Browse the repository at this point in the history
* Ignore magic number in constructor initializer

* Minor naming improvement

---------

Co-authored-by: vladimir-beloded <[email protected]>
  • Loading branch information
solid-vovabeloded and vova-beloded-solid authored Nov 16, 2023
1 parent eb00fb4 commit fbc158d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/lints/no_magic_number/no_magic_number_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class NoMagicNumberRule extends SolidLintRule<NoMagicNumberParameters> {
.where(_isNotInDateTime)
.where(_isNotInsideIndexExpression)
.where(_isNotInsideEnumConstantArguments)
.where(_isNotDefaultValue);
.where(_isNotDefaultValue)
.where(_isNotInConstructorInitializer);

for (final magicNumber in magicNumbers) {
reporter.reportErrorForNode(code, magicNumber);
Expand Down Expand Up @@ -127,4 +128,8 @@ class NoMagicNumberRule extends SolidLintRule<NoMagicNumberParameters> {
bool _isNotDefaultValue(Literal literal) {
return literal.thisOrAncestorOfType<DefaultFormalParameter>() == null;
}

bool _isNotInConstructorInitializer(Literal literal) {
return literal.thisOrAncestorOfType<ConstructorInitializer>() == null;
}
}
7 changes: 7 additions & 0 deletions lint_test/no_magic_number_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,10 @@ class DefaultValues {
void topLevelFunctionWithDefaultParam({int value = 6}) {
({int value = 7}) {};
}

// Allowed for numbers in constructor initializer.
class ConstructorInitializer {
final int value;

ConstructorInitializer() : value = 10;
}

0 comments on commit fbc158d

Please sign in to comment.