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

fix: correctly handle disabling rules with false #822

Merged
merged 3 commits into from May 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* fix: correctly handle disabling rules with false.
* fix: dart-code-metrics crash saying `Bad state: No element` when running command.

## 4.14.0
Expand Down
38 changes: 26 additions & 12 deletions lib/src/config_builder/models/analysis_options.dart
Expand Up @@ -64,18 +64,32 @@ class AnalysisOptions {
}

if (data is Iterable<Object>) {
return Map.unmodifiable(Map<String, Map<String, Object>>.fromEntries([
...data.whereType<String>().map((node) => MapEntry(node, {})),
...data
.whereType<Map<String, Object>>()
.where((node) =>
node.keys.length == 1 &&
node.values.first is Map<String, Object>)
.map((node) => MapEntry(
node.keys.first,
node.values.first as Map<String, Object>,
)),
]));
return Map.unmodifiable(Map<String, Map<String, Object>>.fromEntries(
data.fold([], (previousValue, element) {
if (element is String) {
return [...previousValue, MapEntry(element, <String, Object>{})];
}

if (element is Map<String, Object>) {
final hasSingleKey = element.keys.length == 1;
final value = element.values.first;

if (hasSingleKey && value is Map<String, Object> || value is bool) {
final updatedValue = value is bool
? <String, Object>{'enabled': value}
: value as Map<String, Object>;

return [
...previousValue,
MapEntry(element.keys.first, updatedValue),
];
}
}

return previousValue;
}),
)..removeWhere((key, value) =>
(value['enabled'] is bool && value['enabled'] == false)));
} else if (data is Map<String, Object>) {
final rulesNode = data;

Expand Down
15 changes: 15 additions & 0 deletions test/src/config_builder/models/analysis_options_test.dart
Expand Up @@ -153,6 +153,12 @@ void main() {
'rule-id3',
],
'rules4': null,
'rules5': [
'rule-id1',
'rule-id2',
'rule-id3',
{'rule-id1': false},
],
},
});

Expand Down Expand Up @@ -185,6 +191,15 @@ void main() {
);

expect(options.readMapOfMap(['dart_code_metrics', 'rules4']), isEmpty);

expect(
options.readMapOfMap(['dart_code_metrics', 'rules5']),
allOf(
containsPair('rule-id2', <String, Object>{}),
containsPair('rule-id3', <String, Object>{}),
hasLength(2),
),
);
});

test(
Expand Down