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

fix: ignore enum constant arguments for no-magic-number #1150

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* fix: ignore enum constant arguments for [`no-magic-number`](https://dartcodemetrics.dev/docs/rules/common/no-magic-number).
* fix: correctly handle prefixed enums and static instance fields for [`prefer-moving-to-variable`](https://dartcodemetrics.dev/docs/rules/common/prefer-moving-to-variable).
* feat: add static code diagnostic [`prefer-provide-intl-description`](https://dartcodemetrics.dev/docs/rules/intl/prefer-provide-intl-description).
* feat: exclude `.freezed.dart` files by default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class NoMagicNumberRule extends CommonRule {
.where(_isNotInsideConstConstructor)
.where(_isNotInDateTime)
.where(_isNotInsideIndexExpression)
.where(_isNotInsideEnumConstantArguments)
.map((lit) => createIssue(
rule: this,
location: nodeLocation(node: lit, source: source),
Expand Down Expand Up @@ -80,6 +81,14 @@ class NoMagicNumberRule extends CommonRule {
) ==
null;

bool _isNotInsideEnumConstantArguments(Literal l) {
final node = l.thisOrAncestorMatching(
(ancestor) => ancestor is EnumConstantArguments,
);

return node == null;
}

bool _isNotInsideCollectionLiteral(Literal l) => l.parent is! TypedLiteral;

bool _isNotInsideConstMap(Literal l) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
enum ExampleMagicNumbers {
second(2),
third(3);

final int value;

const ExampleMagicNumbers(this.value);
}

enum ExampleNamedMagicNumbers {
second(value: 2),
third(value: 3);

final int value;

const ExampleNamedMagicNumbers({required this.value});
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const _incorrectExamplePath = 'no_magic_number/examples/incorrect_example.dart';
const _exceptionsExamplePath =
'no_magic_number/examples/exceptions_example.dart';
const _arrayExamplePath = 'no_magic_number/examples/array_example.dart';
const _enumExamplePath = 'no_magic_number/examples/enum_example.dart';

void main() {
group('NoMagicNumberRule', () {
Expand Down Expand Up @@ -42,6 +43,13 @@ void main() {
RuleTestHelper.verifyNoIssues(issues);
});

test("doesn't report enum arguments", () async {
final unit = await RuleTestHelper.resolveFromFile(_enumExamplePath);
final issues = NoMagicNumberRule().check(unit);

RuleTestHelper.verifyNoIssues(issues);
});

test("doesn't report exceptional code", () async {
final unit = await RuleTestHelper.resolveFromFile(_exceptionsExamplePath);
final issues = NoMagicNumberRule().check(unit);
Expand Down