Skip to content

Commit

Permalink
Fix for magic number lint (#137)
Browse files Browse the repository at this point in the history
* Fix for magic number lint

* documentation for const class instances

* doc fix

* version change
  • Loading branch information
Viktor-Mudrytskyi authored Mar 12, 2024
1 parent b6d5d69 commit 5b9e56f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.1.5

- Add `avoid_using_debug_print` rule
- Added `avoid_using_debug_print` rule
- Fixed an issue with no_magic_number lint

## 0.1.4

Expand Down
45 changes: 36 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 @@ -59,6 +59,14 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';
/// bool canDrive(int age, {bool isUSA = false}) {
/// return isUSA ? age >= 16 : age > 18; // LINT
/// }
///
/// class Circle {
/// final int r;
/// const Circle({required this.r});
/// }
/// Circle(r: 5); // LINT
/// var circle = Circle(r: 10); // LINT
/// final circle2 = Circle(r: 10); // LINT
/// ```
///
/// #### GOOD:
Expand All @@ -74,6 +82,13 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';
/// bool canDrive(int age, {bool isUSA = false}) {
/// return isUSA ? age >= usaDrivingAge : age > worldWideDrivingAge;
/// }
///
/// class Circle {
/// final int r;
/// const Circle({required this.r});
/// }
/// const Circle(r: 5);
/// const circle = Circle(r: 10)
/// ```
///
/// ### Allowed
Expand Down Expand Up @@ -174,11 +189,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 +234,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),
];
}

0 comments on commit 5b9e56f

Please sign in to comment.