Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix for magic number lint
  • Loading branch information
Viktor-Mudrytskyi committed Mar 12, 2024
commit 262e366be2aad6e64865150dff6717e3881fca69
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.6

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's make it part of 0.1.5 version

Suggested change
## 0.1.6


- Fixed an issue with no_magic_number lint

## 0.1.5

- Add `avoid_using_debug_print` rule
Expand Down
30 changes: 21 additions & 9 deletions lib/src/lints/no_magic_number/no_magic_number_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,24 @@ 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) {
// Whether we encountered such node,
// This is tracked because [InstanceCreationExpression] can be
// inside [VariableDeclaration] removing unwanted literals

bool isInstanceCreationExpression = false;
return l.thisOrAncestorMatching((ancestor) {
if (ancestor is InstanceCreationExpression) {
isInstanceCreationExpression = true;
}
if (isInstanceCreationExpression) {
return false;
} else {
return ancestor is VariableDeclaration;
}
}) ==
null;
}

bool _isNotInDateTime(Literal l) =>
l.thisOrAncestorMatching(
Expand Down Expand Up @@ -206,10 +219,9 @@ class NoMagicNumberRule extends SolidLintRule<NoMagicNumberParameters> {
}

bool _isNotInsideConstConstructor(Literal l) =>
l.thisOrAncestorMatching(
(ancestor) =>
ancestor is InstanceCreationExpression && ancestor.isConst,
) ==
l.thisOrAncestorMatching((ancestor) {
return ancestor is InstanceCreationExpression && ancestor.isConst;
}) ==
null;

bool _isNotInsideIndexExpression(Literal l) => l.parent is! IndexExpression;
Expand Down
34 changes: 33 additions & 1 deletion lint_test/no_magic_number_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ double correctCircumference(double radius) =>
radiusToDiameterCoefficient * pi * radius;

bool canDrive(int age, {bool isUSA = false}) {
// expect_lint: no_magic_number
// expect_lint: no_magic_number
return isUSA ? age >= 16 : age > 18;
}

Expand Down Expand Up @@ -111,7 +111,39 @@ Widget build() {
return MyWidget(
// expect_lint: no_magic_number
decoration: MyWidgetDecoration(size: 12),

// expect_lint: no_magic_number
value: 23,
);
}

class TestOperation {
final double res;

const TestOperation({required this.res});
}

const n = 15;
const m = 20;

void checkOperationInConstructor() {
// expect_lint: no_magic_number
TestOperation(res: (5 / 5) * 20);

// expect_lint: no_magic_number
var variable = TestOperation(res: (n / m) * 20);

// expect_lint: no_magic_number
final finalVar = TestOperation(res: (10 / m + 4 + n));

// Allowed for constant expressions
const constVar = TestOperation(res: (10 / m + 4 + n));

var constVar2 = const TestOperation(res: 15 + (10 / n));

final l = [
// expect_lint: no_magic_number
TestOperation(res: 8),
const TestOperation(res: 9),
];
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: solid_lints
description:
Lints for Dart and Flutter based on software industry standards and best
practices.
version: 0.1.5
version: 0.1.6
homepage: https://github.com/solid-software/solid_lints/
documentation: https://solid-software.github.io/solid_lints/docs/intro
topics: [lints, linter, lint, analysis, analyzer]
Expand Down