Skip to content

Commit 3c3a413

Browse files
authored
Support passthrough of unrelated analyzer errors in analysis option customization. (#1516)
1 parent d2eb696 commit 3c3a413

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 0.23.1
22

33
- Support passthrough of unrelated linter rules in analysis option customization.
4+
- Support passthrough of unrelated analyzer errors in analysis option customization.
45

56
## 0.23.0
67

lib/src/analysis_options.dart

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ const _analyzerErrorKeys = <String>['uri_has_not_been_generated'];
7676
/// Such options are:
7777
/// - the `include:` predicate,
7878
/// - the `formatter:` options (without any filtering),
79-
/// - the `analyzer: / errors:` keys passing through keys
80-
/// from [_analyzerErrorKeys],
79+
/// - the `analyzer: / errors:` keys passing the values if
80+
/// their key is not present in [custom] or are in [_analyzerErrorKeys],
8181
/// - the `linter: / rules:` section, passing `true`/`false`
8282
/// values if their key is not present in [custom].
8383
String updatePassthroughOptions({
@@ -93,21 +93,23 @@ String updatePassthroughOptions({
9393
origMap ??= {};
9494

9595
final customMap =
96-
json.decode(json.encode(yaml.loadYaml(custom))) ?? <String, dynamic>{};
96+
(json.decode(json.encode(yaml.loadYaml(custom))) as Map?) ??
97+
<String, dynamic>{};
9798

98-
final origAnalyzer = origMap['analyzer'];
99-
if (origAnalyzer is Map) {
100-
final origErrors = origAnalyzer['errors'];
101-
if (origErrors is Map) {
99+
final appliedCustomRules = _extractAppliedRules(customMap);
100+
101+
if (origMap case {'analyzer': Map origAnalyzer}) {
102+
if (origAnalyzer case {'errors': Map origErrors}) {
102103
final customAnalyzer =
103104
customMap.putIfAbsent('analyzer', () => <String, Object?>{}) as Map;
104105
final customErrors =
105106
customAnalyzer.putIfAbsent('errors', () => <String, Object?>{})
106107
as Map;
107108

108-
for (var key in _analyzerErrorKeys) {
109-
if (origErrors.containsKey(key)) {
110-
customErrors[key] = origErrors[key];
109+
for (var entry in origErrors.entries) {
110+
if (_analyzerErrorKeys.contains(entry.key) ||
111+
!appliedCustomRules.contains(entry.key)) {
112+
customErrors[entry.key] = entry.value;
111113
}
112114
}
113115
}
@@ -138,7 +140,7 @@ String updatePassthroughOptions({
138140
}
139141
if (customRules is Map) {
140142
for (var e in origRules.entries) {
141-
if (customRules.containsKey(e.key)) {
143+
if (appliedCustomRules.contains(e.key)) {
142144
continue;
143145
}
144146
customRules[e.key] = e.value;
@@ -160,3 +162,17 @@ String updatePassthroughOptions({
160162

161163
return json.encode(customMap);
162164
}
165+
166+
Set<String> _extractAppliedRules(Map map) {
167+
final appliedRules = <String>{};
168+
if (map case {'linter': {'rules': List rules}}) {
169+
appliedRules.addAll(rules.map((e) => e.toString()));
170+
}
171+
if (map case {'linter': {'rules': Map rules}}) {
172+
appliedRules.addAll(rules.keys.map((e) => e.toString()));
173+
}
174+
if (map case {'analyzer': {'errors': Map errors}}) {
175+
appliedRules.addAll(errors.keys.map((e) => e.toString()));
176+
}
177+
return appliedRules;
178+
}

test/analysis_options_test.dart

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void main() {
2525
expect(json.decode(content), <String, Object?>{});
2626
});
2727

28-
test('passthrough for some options', () {
28+
test('passthrough for most options', () {
2929
final content = updatePassthroughOptions(
3030
original: '''
3131
analyzer:
@@ -44,7 +44,7 @@ formatter:
4444
);
4545
expect(json.decode(content), {
4646
'analyzer': {
47-
'errors': {'uri_has_not_been_generated': 'ignore'},
47+
'errors': {'todo': 'ignore', 'uri_has_not_been_generated': 'ignore'},
4848
'enable-experiment': ['macros'],
4949
},
5050
'formatter': {
@@ -55,7 +55,34 @@ formatter:
5555
});
5656
});
5757

58-
test('passthrough linter rules', () {
58+
test('passthrough analysis errors except one', () {
59+
final content = updatePassthroughOptions(
60+
original: '''
61+
analyzer:
62+
errors:
63+
todo: ignore
64+
another-todo: ignore
65+
uri_has_not_been_generated: ignore
66+
''',
67+
custom: '''
68+
linter:
69+
rules:
70+
- todo
71+
- uri_has_not_been_generated
72+
''',
73+
);
74+
expect(json.decode(content), {
75+
'analyzer': {
76+
'errors': {
77+
'another-todo': 'ignore',
78+
'uri_has_not_been_generated': 'ignore',
79+
},
80+
},
81+
'linter': isNotEmpty,
82+
});
83+
});
84+
85+
test('passthrough linter rules except one', () {
5986
final content = updatePassthroughOptions(
6087
original: '''
6188
linter:

0 commit comments

Comments
 (0)