Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit bcf88cd

Browse files
authored
Merge 24a5375 into 91cfb6a
2 parents 91cfb6a + 24a5375 commit bcf88cd

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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.

lib/src/analyzers/lint_analyzer/rules/rules_list/no_magic_number/no_magic_number_rule.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

test/src/analyzers/lint_analyzer/rules/rules_list/no_magic_number/no_magic_number_rule_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const _incorrectExamplePath = 'no_magic_number/examples/incorrect_example.dart';
99
const _exceptionsExamplePath =
1010
'no_magic_number/examples/exceptions_example.dart';
1111
const _arrayExamplePath = 'no_magic_number/examples/array_example.dart';
12+
const _enumExamplePath = 'no_magic_number/examples/enum_example.dart';
1213

1314
void 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);

0 commit comments

Comments
 (0)