Skip to content

Commit 393981b

Browse files
authored
Support passthrough of unrelated linter rules in analysis option customization (#1512)
1 parent 240b88f commit 393981b

File tree

6 files changed

+69
-27
lines changed

6 files changed

+69
-27
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.23.1
2+
3+
- Support passthrough of unrelated linter rules in analysis option customization.
4+
15
## 0.23.0
26

37
**Breaking changes:**

lib/src/analysis_options.dart

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,18 @@ Future<http.Response> _httpGetWithRetry(Uri uri) async {
7171

7272
const _analyzerErrorKeys = <String>['uri_has_not_been_generated'];
7373

74+
/// Update the well-known [custom] `analysis_options.yaml` content with
75+
/// pass-through options from the package-provided [original] content.
76+
/// Such options are:
77+
/// - the `include:` predicate,
78+
/// - the `formatter:` options (without any filtering),
79+
/// - the `analyzer: / errors:` keys passing through keys
80+
/// from [_analyzerErrorKeys],
81+
/// - the `linter: / rules:` section, passing `true`/`false`
82+
/// values if their key is not present in [custom].
7483
String updatePassthroughOptions({
7584
required String? original,
7685
required String custom,
77-
bool keepInclude = false,
7886
}) {
7987
Map? origMap;
8088
if (original != null) {
@@ -115,18 +123,39 @@ String updatePassthroughOptions({
115123
}
116124
}
117125

126+
if (origMap case {'linter': {'rules': Map origRules}}) {
127+
final customLinter = customMap.putIfAbsent(
128+
'linter',
129+
() => <String, Object?>{},
130+
);
131+
var customRules = customLinter.putIfAbsent(
132+
'rules',
133+
() => <String, Object?>{},
134+
);
135+
if (customRules is List) {
136+
customRules = Map.fromEntries(customRules.map((e) => MapEntry(e, true)));
137+
customLinter['rules'] = customRules;
138+
}
139+
if (customRules is Map) {
140+
for (var e in origRules.entries) {
141+
if (customRules.containsKey(e.key)) {
142+
continue;
143+
}
144+
customRules[e.key] = e.value;
145+
}
146+
}
147+
}
148+
118149
final origFormatter = origMap['formatter'];
119150
if (origFormatter is Map) {
120151
final customFormatter =
121152
customMap.putIfAbsent('formatter', () => <String, dynamic>{}) as Map;
122153
customFormatter.addAll(origFormatter.cast<String, dynamic>());
123154
}
124155

125-
if (keepInclude) {
126-
final newInclude = customMap['include'] ?? origMap['include'];
127-
if (newInclude != null) {
128-
customMap['include'] = newInclude;
129-
}
156+
final newInclude = customMap['include'] ?? origMap['include'];
157+
if (newInclude != null) {
158+
customMap['include'] = newInclude;
130159
}
131160

132161
return json.encode(customMap);

lib/src/sdk_env.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ class ToolEnvironment {
216216
final customOptionsContent = updatePassthroughOptions(
217217
original: originalOptions,
218218
custom: rawOptionsContent,
219-
keepInclude: true,
220219
);
221220
try {
222221
await analysisOptionsFile.writeAsString(customOptionsContent);

lib/src/version.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: pana
22
description: PAckage aNAlyzer - produce a report summarizing the health and quality of a Dart package.
3-
version: 0.23.0
3+
version: 0.23.1-dev
44
repository: https://github.com/dart-lang/pana
55
topics:
66
- tool

test/analysis_options_test.dart

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,41 @@ formatter:
5555
});
5656
});
5757

58-
test('ignore include in original', () {
58+
test('passthrough linter rules', () {
5959
final content = updatePassthroughOptions(
60-
original: 'include: package:lints/other.yaml',
61-
custom: '',
60+
original: '''
61+
linter:
62+
rules:
63+
avoid_relative_lib_imports: true
64+
prefer_relative_imports: false
65+
public_member_api_docs: false
66+
''',
67+
custom: '''
68+
linter:
69+
rules:
70+
- prefer_relative_imports
71+
''',
6272
);
63-
expect(json.decode(content), <String, Object?>{});
73+
expect(json.decode(content), {
74+
'linter': {
75+
'rules': {
76+
'avoid_relative_lib_imports': true,
77+
'prefer_relative_imports': true,
78+
'public_member_api_docs': false,
79+
},
80+
},
81+
});
6482
});
6583

66-
test('include only in custom: keepInclude=false', () {
84+
test('update include from original', () {
6785
final content = updatePassthroughOptions(
68-
original: '',
69-
custom: 'include: package:lints/other.yaml',
86+
original: 'include: package:lints/other.yaml',
87+
custom: '',
7088
);
71-
expect(json.decode(content), <String, Object?>{
72-
'include': 'package:lints/other.yaml',
73-
});
89+
expect(json.decode(content), {'include': 'package:lints/other.yaml'});
7490
});
7591

76-
test('include only in custom: keepInclude=true', () {
92+
test('include only in custom', () {
7793
final content = updatePassthroughOptions(
7894
original: '',
7995
custom: 'include: package:lints/other.yaml',
@@ -87,7 +103,6 @@ formatter:
87103
final content = updatePassthroughOptions(
88104
original: 'include: package:lints/other.yaml',
89105
custom: '',
90-
keepInclude: true,
91106
);
92107
expect(json.decode(content), <String, Object?>{
93108
'include': 'package:lints/other.yaml',
@@ -98,19 +113,14 @@ formatter:
98113
final content = updatePassthroughOptions(
99114
original: 'include: package:lints/other.yaml',
100115
custom: 'include: package:lints/core.yaml',
101-
keepInclude: true,
102116
);
103117
expect(json.decode(content), <String, Object?>{
104118
'include': 'package:lints/core.yaml',
105119
});
106120
});
107121

108122
test('keep include without include value', () {
109-
final content = updatePassthroughOptions(
110-
original: '',
111-
custom: '',
112-
keepInclude: true,
113-
);
123+
final content = updatePassthroughOptions(original: '', custom: '');
114124
expect(json.decode(content), <String, Object?>{});
115125
});
116126
}

0 commit comments

Comments
 (0)