Skip to content

Commit

Permalink
Ignore magic number in constructor initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
vova-beloded-solid committed Nov 16, 2023
1 parent eb00fb4 commit 535db47
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(_isNotConstructorInitializer);

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 _isNotConstructorInitializer(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 535db47

Please sign in to comment.