This repository was archived by the owner on Jul 16, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +35
-0
lines changed
lib/src/analyzers/lint_analyzer/rules/rules_list/no_magic_number
test/src/analyzers/lint_analyzer/rules/rules_list/no_magic_number Expand file tree Collapse file tree 4 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## Unreleased
4
4
5
+ * fix: ignore enum constant arguments for [ ` no-magic-number ` ] ( https://dartcodemetrics.dev/docs/rules/common/no-magic-number ) .
5
6
* fix: correctly handle prefixed enums and static instance fields for [ ` prefer-moving-to-variable ` ] ( https://dartcodemetrics.dev/docs/rules/common/prefer-moving-to-variable ) .
6
7
* feat: add static code diagnostic [ ` prefer-provide-intl-description ` ] ( https://dartcodemetrics.dev/docs/rules/intl/prefer-provide-intl-description ) .
7
8
* feat: exclude ` .freezed.dart ` files by default.
Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ class NoMagicNumberRule extends CommonRule {
53
53
.where (_isNotInsideConstConstructor)
54
54
.where (_isNotInDateTime)
55
55
.where (_isNotInsideIndexExpression)
56
+ .where (_isNotInsideEnumConstantArguments)
56
57
.map ((lit) => createIssue (
57
58
rule: this ,
58
59
location: nodeLocation (node: lit, source: source),
@@ -80,6 +81,14 @@ class NoMagicNumberRule extends CommonRule {
80
81
) ==
81
82
null ;
82
83
84
+ bool _isNotInsideEnumConstantArguments (Literal l) {
85
+ final node = l.thisOrAncestorMatching (
86
+ (ancestor) => ancestor is EnumConstantArguments ,
87
+ );
88
+
89
+ return node == null ;
90
+ }
91
+
83
92
bool _isNotInsideCollectionLiteral (Literal l) => l.parent is ! TypedLiteral ;
84
93
85
94
bool _isNotInsideConstMap (Literal l) {
Original file line number Diff line number Diff line change
1
+ enum ExampleMagicNumbers {
2
+ second (2 ),
3
+ third (3 );
4
+
5
+ final int value;
6
+
7
+ const ExampleMagicNumbers (this .value);
8
+ }
9
+
10
+ enum ExampleNamedMagicNumbers {
11
+ second (value: 2 ),
12
+ third (value: 3 );
13
+
14
+ final int value;
15
+
16
+ const ExampleNamedMagicNumbers ({required this .value});
17
+ }
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ const _incorrectExamplePath = 'no_magic_number/examples/incorrect_example.dart';
9
9
const _exceptionsExamplePath =
10
10
'no_magic_number/examples/exceptions_example.dart' ;
11
11
const _arrayExamplePath = 'no_magic_number/examples/array_example.dart' ;
12
+ const _enumExamplePath = 'no_magic_number/examples/enum_example.dart' ;
12
13
13
14
void main () {
14
15
group ('NoMagicNumberRule' , () {
@@ -42,6 +43,13 @@ void main() {
42
43
RuleTestHelper .verifyNoIssues (issues);
43
44
});
44
45
46
+ test ("doesn't report enum arguments" , () async {
47
+ final unit = await RuleTestHelper .resolveFromFile (_enumExamplePath);
48
+ final issues = NoMagicNumberRule ().check (unit);
49
+
50
+ RuleTestHelper .verifyNoIssues (issues);
51
+ });
52
+
45
53
test ("doesn't report exceptional code" , () async {
46
54
final unit = await RuleTestHelper .resolveFromFile (_exceptionsExamplePath);
47
55
final issues = NoMagicNumberRule ().check (unit);
You can’t perform that action at this time.
0 commit comments