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

Commit c871a40

Browse files
authored
fix: no-magic-number not working in array of widgets (#551)
* fix: no-magic-number not working in array of widgets * test: add array testcase for no_magic_number rule * test: add lint comments to array_example of no_magic_number test * docs: add no_magic_number fix to CHANGELOG * fix: codestyle in no-magic-number rule and test * test: fix no-magic-number-rule-test for widget array
1 parent b5e9f39 commit c871a40

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* feat: add static code diagnostics `avoid-unnecessary-type-assertions`
77
* refactor: cleanup anti-patterns, metrics and rules documentation
88
* chore: activate self implemented rules: avoid-unnecessary-type-assertions, prefer-first, prefer-last, prefer-match-file-name
9+
* fix: no-magic-number not working in array of widgets
910

1011
## 4.6.0
1112

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ class NoMagicNumberRule extends CommonRule {
7373
) ==
7474
null;
7575

76-
bool _isNotInsideCollectionLiteral(Literal l) =>
77-
l.thisOrAncestorMatching((ancestor) => ancestor is TypedLiteral) == null;
76+
bool _isNotInsideCollectionLiteral(Literal l) => l.parent is! TypedLiteral;
7877

7978
bool _isNotInsideConstConstructor(Literal l) =>
8079
l.thisOrAncestorMatching((ancestor) =>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class ContainerWidget {
2+
int height;
3+
List<ContainerWidget>? children;
4+
ContainerWidget({required this.height, this.children});
5+
6+
ContainerWidget build() {
7+
return ContainerWidget(
8+
height: 83, // LINT
9+
children: [
10+
ContainerWidget(
11+
height: 83, // LINT
12+
),
13+
],
14+
);
15+
}
16+
}

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

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

1314
void main() {
1415
group('NoMagicNumberRule', () {
@@ -61,5 +62,20 @@ void main() {
6162

6263
RuleTestHelper.verifyNoIssues(issues);
6364
});
65+
66+
test('reports magic numbers in objects in widget array structures',
67+
() async {
68+
final unit = await RuleTestHelper.resolveFromFile(_arrayExamplePath);
69+
final issues = NoMagicNumberRule().check(unit);
70+
71+
RuleTestHelper.verifyIssues(
72+
issues: issues,
73+
startOffsets: [202, 275],
74+
startLines: [8, 11],
75+
startColumns: [15, 19],
76+
endOffsets: [204, 277],
77+
locationTexts: ['83', '83'],
78+
);
79+
});
6480
});
6581
}

0 commit comments

Comments
 (0)