Skip to content

Commit 5b9e56f

Browse files
Fix for magic number lint (#137)
* Fix for magic number lint * documentation for const class instances * doc fix * version change
1 parent b6d5d69 commit 5b9e56f

3 files changed

Lines changed: 71 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 0.1.5
22

3-
- Add `avoid_using_debug_print` rule
3+
- Added `avoid_using_debug_print` rule
4+
- Fixed an issue with no_magic_number lint
45

56
## 0.1.4
67

lib/src/lints/no_magic_number/no_magic_number_rule.dart

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';
5959
/// bool canDrive(int age, {bool isUSA = false}) {
6060
/// return isUSA ? age >= 16 : age > 18; // LINT
6161
/// }
62+
///
63+
/// class Circle {
64+
/// final int r;
65+
/// const Circle({required this.r});
66+
/// }
67+
/// Circle(r: 5); // LINT
68+
/// var circle = Circle(r: 10); // LINT
69+
/// final circle2 = Circle(r: 10); // LINT
6270
/// ```
6371
///
6472
/// #### GOOD:
@@ -74,6 +82,13 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';
7482
/// bool canDrive(int age, {bool isUSA = false}) {
7583
/// return isUSA ? age >= usaDrivingAge : age > worldWideDrivingAge;
7684
/// }
85+
///
86+
/// class Circle {
87+
/// final int r;
88+
/// const Circle({required this.r});
89+
/// }
90+
/// const Circle(r: 5);
91+
/// const circle = Circle(r: 10)
7792
/// ```
7893
///
7994
/// ### Allowed
@@ -174,11 +189,24 @@ class NoMagicNumberRule extends SolidLintRule<NoMagicNumberParameters> {
174189
(l is IntegerLiteral &&
175190
!config.parameters.allowedNumbers.contains(l.value));
176191

177-
bool _isNotInsideVariable(Literal l) =>
178-
l.thisOrAncestorMatching(
179-
(ancestor) => ancestor is VariableDeclaration,
180-
) ==
181-
null;
192+
bool _isNotInsideVariable(Literal l) {
193+
// Whether we encountered such node,
194+
// This is tracked because [InstanceCreationExpression] can be
195+
// inside [VariableDeclaration] removing unwanted literals
196+
197+
bool isInstanceCreationExpression = false;
198+
return l.thisOrAncestorMatching((ancestor) {
199+
if (ancestor is InstanceCreationExpression) {
200+
isInstanceCreationExpression = true;
201+
}
202+
if (isInstanceCreationExpression) {
203+
return false;
204+
} else {
205+
return ancestor is VariableDeclaration;
206+
}
207+
}) ==
208+
null;
209+
}
182210

183211
bool _isNotInDateTime(Literal l) =>
184212
l.thisOrAncestorMatching(
@@ -206,10 +234,9 @@ class NoMagicNumberRule extends SolidLintRule<NoMagicNumberParameters> {
206234
}
207235

208236
bool _isNotInsideConstConstructor(Literal l) =>
209-
l.thisOrAncestorMatching(
210-
(ancestor) =>
211-
ancestor is InstanceCreationExpression && ancestor.isConst,
212-
) ==
237+
l.thisOrAncestorMatching((ancestor) {
238+
return ancestor is InstanceCreationExpression && ancestor.isConst;
239+
}) ==
213240
null;
214241

215242
bool _isNotInsideIndexExpression(Literal l) => l.parent is! IndexExpression;

lint_test/no_magic_number_test.dart

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ double correctCircumference(double radius) =>
1515
radiusToDiameterCoefficient * pi * radius;
1616

1717
bool canDrive(int age, {bool isUSA = false}) {
18-
// expect_lint: no_magic_number
18+
// expect_lint: no_magic_number
1919
return isUSA ? age >= 16 : age > 18;
2020
}
2121

@@ -111,7 +111,39 @@ Widget build() {
111111
return MyWidget(
112112
// expect_lint: no_magic_number
113113
decoration: MyWidgetDecoration(size: 12),
114+
114115
// expect_lint: no_magic_number
115116
value: 23,
116117
);
117118
}
119+
120+
class TestOperation {
121+
final double res;
122+
123+
const TestOperation({required this.res});
124+
}
125+
126+
const n = 15;
127+
const m = 20;
128+
129+
void checkOperationInConstructor() {
130+
// expect_lint: no_magic_number
131+
TestOperation(res: (5 / 5) * 20);
132+
133+
// expect_lint: no_magic_number
134+
var variable = TestOperation(res: (n / m) * 20);
135+
136+
// expect_lint: no_magic_number
137+
final finalVar = TestOperation(res: (10 / m + 4 + n));
138+
139+
// Allowed for constant expressions
140+
const constVar = TestOperation(res: (10 / m + 4 + n));
141+
142+
var constVar2 = const TestOperation(res: 15 + (10 / n));
143+
144+
final l = [
145+
// expect_lint: no_magic_number
146+
TestOperation(res: 8),
147+
const TestOperation(res: 9),
148+
];
149+
}

0 commit comments

Comments
 (0)