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 22
33## Unreleased
44
5+ * fix: ignore enum constant arguments for [ ` no-magic-number ` ] ( https://dartcodemetrics.dev/docs/rules/common/no-magic-number ) .
56* fix: correctly handle prefixed enums and static instance fields for [ ` prefer-moving-to-variable ` ] ( https://dartcodemetrics.dev/docs/rules/common/prefer-moving-to-variable ) .
67* feat: add static code diagnostic [ ` prefer-provide-intl-description ` ] ( https://dartcodemetrics.dev/docs/rules/intl/prefer-provide-intl-description ) .
78* 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 {
5353 .where (_isNotInsideConstConstructor)
5454 .where (_isNotInDateTime)
5555 .where (_isNotInsideIndexExpression)
56+ .where (_isNotInsideEnumConstantArguments)
5657 .map ((lit) => createIssue (
5758 rule: this ,
5859 location: nodeLocation (node: lit, source: source),
@@ -80,6 +81,14 @@ class NoMagicNumberRule extends CommonRule {
8081 ) ==
8182 null ;
8283
84+ bool _isNotInsideEnumConstantArguments (Literal l) {
85+ final node = l.thisOrAncestorMatching (
86+ (ancestor) => ancestor is EnumConstantArguments ,
87+ );
88+
89+ return node == null ;
90+ }
91+
8392 bool _isNotInsideCollectionLiteral (Literal l) => l.parent is ! TypedLiteral ;
8493
8594 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';
99const _exceptionsExamplePath =
1010 'no_magic_number/examples/exceptions_example.dart' ;
1111const _arrayExamplePath = 'no_magic_number/examples/array_example.dart' ;
12+ const _enumExamplePath = 'no_magic_number/examples/enum_example.dart' ;
1213
1314void main () {
1415 group ('NoMagicNumberRule' , () {
@@ -42,6 +43,13 @@ void main() {
4243 RuleTestHelper .verifyNoIssues (issues);
4344 });
4445
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+
4553 test ("doesn't report exceptional code" , () async {
4654 final unit = await RuleTestHelper .resolveFromFile (_exceptionsExamplePath);
4755 final issues = NoMagicNumberRule ().check (unit);
You can’t perform that action at this time.
0 commit comments