Skip to content

Commit

Permalink
Fix no_magic_number missing reports
Browse files Browse the repository at this point in the history
  • Loading branch information
vova-beloded-solid committed Nov 15, 2023
1 parent 1ce6a91 commit adfbfbd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/lints/no_magic_number/no_magic_number_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,16 @@ class NoMagicNumberRule extends SolidLintRule<NoMagicNumberParameters> {
(l is IntegerLiteral &&
!config.parameters.allowedNumbers.contains(l.value));

bool _isNotInsideVariable(Literal l) =>
l.thisOrAncestorMatching(
(ancestor) => ancestor is VariableDeclaration,
) ==
null;
bool _isNotInsideVariable(Literal l) {
AstNode? node = l;
while (node != null && node is! InstanceCreationExpression) {
if (node is VariableDeclaration) return false;

node = node.parent;
}

return true;
}

bool _isNotInDateTime(Literal l) =>
l.thisOrAncestorMatching(
Expand Down
14 changes: 14 additions & 0 deletions lint_test/no_magic_number_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ void fun() {

// Allowed in DateTime because it doesn't have cons constructor
final apocalypse = DateTime(2012, 12, 21);

const count = 5;
const total = 10;
// expect_lint: no_magic_number
final percent = TestMagicNumber(percent: (count / total) * 100);

// expect_lint: no_magic_number
TestMagicNumber(percent: (count / total) * 100);
}

class TestMagicNumber {
final double percent;

TestMagicNumber({required this.percent});
}

0 comments on commit adfbfbd

Please sign in to comment.