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

Commit 33d3184

Browse files
authored
fix: correcly handle const maps (#692)
* fix: correcly handle const maps * chore: leftovers * chore: update changelog
1 parent 3d92124 commit 33d3184

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
* chore: activate new lint rules.
6+
* fix: correctly handle const maps in `no-magic-number`.
67

78
## 4.11.0-dev.1
89

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class NoMagicNumberRule extends CommonRule {
4040
.where(_isMagicNumber)
4141
.where(_isNotInsideVariable)
4242
.where(_isNotInsideCollectionLiteral)
43+
.where(_isNotInsideConstMap)
4344
.where(_isNotInsideConstConstructor)
4445
.where(_isNotInDateTime)
4546
.map((lit) => createIssue(
@@ -75,6 +76,12 @@ class NoMagicNumberRule extends CommonRule {
7576

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

79+
bool _isNotInsideConstMap(Literal l) {
80+
final grandParent = l.parent?.parent;
81+
82+
return !(grandParent is SetOrMapLiteral && grandParent.isConst);
83+
}
84+
7885
bool _isNotInsideConstConstructor(Literal l) =>
7986
l.thisOrAncestorMatching((ancestor) =>
8087
ancestor is InstanceCreationExpression && ancestor.isConst) ==

test/src/analyzers/lint_analyzer/rules/rules_list/no_magic_number/examples/example.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,17 @@ const pi = 3.14;
22
const pi2 = pi * 2;
33
const str = 'Hello';
44

5-
void good_f4(int x) => a * pi;
6-
void good_f5() => good_f4(pi2);
5+
void fun1(int x) => a * pi;
6+
void fun2() => fun1(pi2);
7+
void fun3() => const {
8+
'Key.a': 0.1,
9+
'Key.b': 0.1,
10+
'Key.c': 0.1,
11+
};
12+
void fun4() {
13+
return const {1, 2};
14+
}
15+
16+
void fun5() => const {1, 2};
17+
18+
void fun6() => const Map<String, String>();

0 commit comments

Comments
 (0)