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

feat: add allow-only-once option to no-magic-number rule #1173

Merged
merged 4 commits into from
Jan 28, 2023
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 @@ -4,6 +4,7 @@

* fix: export missing parts of public API.
* feat: support `context.mounted` for [`use-setstate-synchronously`](https://dcm.dev/docs/individuals/rules/flutter/use-setstate-synchronously).
* feat: add `allow-only-once` option to [`no-magic-number`](https://dcm.dev/docs/individuals/rules/common/no-magic-number).

## 5.5.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ part of 'no_magic_number_rule.dart';
class _ConfigParser {
static const _allowedConfigName = 'allowed';

static const _allowOnlyOnce = 'allow-only-once';

static const _defaultMagicNumbers = [-1, 0, 1];

static Iterable<num> parseAllowedNumbers(Map<String, Object> config) =>
(config[_allowedConfigName] as Iterable?)?.cast<num>() ??
_defaultMagicNumbers;

static bool parseAllowOnlyOnce(Map<String, Object> config) =>
(config[_allowOnlyOnce] as bool?) ?? false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ class NoMagicNumberRule extends CommonRule {
'Avoid using magic numbers. Extract them to named constants or variables.';

final Iterable<num> _allowedMagicNumbers;
final bool _allowOnlyOnce;

NoMagicNumberRule([Map<String, Object> config = const {}])
: _allowedMagicNumbers = _ConfigParser.parseAllowedNumbers(config),
_allowOnlyOnce = _ConfigParser.parseAllowOnlyOnce(config),
super(
id: ruleId,
severity: readSeverity(config, Severity.warning),
Expand All @@ -45,7 +47,11 @@ class NoMagicNumberRule extends CommonRule {

source.unit.visitChildren(visitor);

return visitor.literals
final literals = _allowOnlyOnce
? _getNotSingleLiterals(visitor.literals)
: visitor.literals;

return literals
.where(_isMagicNumber)
.where(_isNotInsideVariable)
.where(_isNotInsideCollectionLiteral)
Expand All @@ -62,6 +68,24 @@ class NoMagicNumberRule extends CommonRule {
.toList(growable: false);
}

Iterable<Literal> _getNotSingleLiterals(Iterable<Literal> literals) {
final literalsCount = <num, int>{};
for (final l in literals) {
if (l is IntegerLiteral) {
final value = l.value;
if (value != null) {
literalsCount.update(value, (count) => ++count, ifAbsent: () => 1);
}
} else if (l is DoubleLiteral) {
literalsCount.update(l.value, (count) => ++count, ifAbsent: () => 1);
}
}

return literals.where((l) =>
l is IntegerLiteral && literalsCount[l.value] != 1 ||
l is DoubleLiteral && literalsCount[l.value] != 1);
}

bool _isMagicNumber(Literal l) =>
(l is DoubleLiteral && !_allowedMagicNumbers.contains(l.value)) ||
(l is IntegerLiteral && !_allowedMagicNumbers.contains(l.value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ void main() {
RuleTestHelper.verifyNoIssues(issues);
});

test('reports magic numbers used more than once', () async {
final unit = await RuleTestHelper.resolveFromFile(_incorrectExamplePath);
final config = {
'allow-only-once': true,
};

final issues = NoMagicNumberRule(config).check(unit);

RuleTestHelper.verifyIssues(
issues: issues,
startLines: [2, 4],
startColumns: [28, 25],
locationTexts: ['12', '12'],
);
});

test(
'reports magic numbers in objects in widget array structures',
() async {
Expand Down
1 change: 1 addition & 0 deletions website/docs/rules/common/no-magic-number.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ dart_code_metrics:
...
- no-magic-number:
allowed: [3.14, 100, 12]
allow-only-once: true
```