Skip to content

Commit 94a74a3

Browse files
authored
Take more advantage of null-aware operators (#8042)
1 parent 7858fd9 commit 94a74a3

File tree

9 files changed

+42
-43
lines changed

9 files changed

+42
-43
lines changed

analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ linter:
5959
- prefer_is_empty
6060
- prefer_is_not_empty
6161
- prefer_iterable_whereType
62+
- prefer_null_aware_operators
6263
- prefer_single_quotes
6364
- prefer_spread_collections
6465
- prefer_typing_uninitialized_variables

app/lib/frontend/dom/dom.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ Node a({
234234
if (rel != null) 'rel': rel,
235235
if (target != null) 'target': target,
236236
if (title != null) 'title': title,
237-
if (attributes != null) ...attributes,
237+
...?attributes,
238238
},
239239
children: children,
240240
child: child,
@@ -369,7 +369,7 @@ Node form({
369369
attributes: <String, String>{
370370
if (action != null) 'action': action,
371371
if (method != null) 'method': method,
372-
if (attributes != null) ...attributes,
372+
...?attributes,
373373
},
374374
children: children,
375375
child: child,
@@ -512,7 +512,7 @@ Node img({
512512
if (title != null) 'title': title,
513513
if (lazy) 'loading': 'lazy',
514514
if (image.role != null) 'role': image.role!,
515-
if (attributes != null) ...attributes,
515+
...?attributes,
516516
},
517517
children: children,
518518
);
@@ -546,7 +546,7 @@ Node input({
546546
if (value != null) 'value': value,
547547
if (autofocus) 'autofocus': 'autofocus',
548548
if (disabled) 'disabled': 'disabled',
549-
if (attributes != null) ...attributes,
549+
...?attributes,
550550
},
551551
children: children,
552552
child: child,
@@ -616,7 +616,7 @@ Node link({
616616
if (title != null) 'title': title,
617617
if (href != null) 'href': href,
618618
if (as != null) 'as': as,
619-
if (attributes != null) ...attributes,
619+
...?attributes,
620620
},
621621
children: children,
622622
child: child,
@@ -651,7 +651,7 @@ Node meta({
651651
if (content != null) 'content': content,
652652
if (rel != null) 'rel': rel,
653653
if (href != null) 'href': href,
654-
if (attributes != null) ...attributes,
654+
...?attributes,
655655
},
656656
children: children,
657657
child: child,
@@ -697,7 +697,7 @@ Node option({
697697
if (value != null) 'value': value,
698698
if (disabled) 'disabled': 'disabled',
699699
if (selected) 'selected': 'selected',
700-
if (attributes != null) ...attributes,
700+
...?attributes,
701701
},
702702
children: children,
703703
child: child,
@@ -767,7 +767,7 @@ Node script({
767767
if (async) 'async': 'async',
768768
if (defer) 'defer': 'defer',
769769
if (onload != null) 'onload': onload,
770-
if (attributes != null) ...attributes,
770+
...?attributes,
771771
},
772772
children: children,
773773
child: child,
@@ -981,7 +981,7 @@ Map<String, String>? _mergeAttributes(
981981
return <String, String>{
982982
if (id != null) 'id': id,
983983
if (classes != null && classes.isNotEmpty) 'class': classes.join(' '),
984-
if (attributes != null) ...attributes,
984+
...?attributes,
985985
};
986986
}
987987

app/lib/frontend/dom/material.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ d.Node button({
2424
if (raised) 'mdc-button--raised',
2525
if (unelevated) 'mdc-button--unelevated',
2626
if (customTypeClass != null) customTypeClass,
27-
if (classes != null) ...classes,
27+
...?classes,
2828
],
2929
attributes: {
3030
'data-mdc-auto-init': 'MDCRipple',
31-
if (attributes != null) ...attributes,
31+
...?attributes,
3232
},
3333
children: isSimpleLabel
3434
? [d.text(label)]
@@ -70,11 +70,11 @@ d.Node floatingActionButton({
7070
classes: [
7171
'mdc-fab',
7272
if (fabMini) 'mdc-fab--mini',
73-
if (classes != null) ...classes,
73+
...?classes,
7474
],
7575
attributes: {
7676
'data-mdc-auto-init': 'MDCRipple',
77-
if (attributes != null) ...attributes,
77+
...?attributes,
7878
},
7979
children: [
8080
d.div(classes: ['mdc-fab__ripple']),
@@ -257,7 +257,7 @@ d.Node dataTable<T>({
257257
(c) => d.th(
258258
classes: [
259259
'mdc-data-table__header-cell',
260-
if (c.headerClasses != null) ...c.headerClasses!,
260+
...?c.headerClasses,
261261
],
262262
attributes: {
263263
'role': 'columnheader',
@@ -357,7 +357,7 @@ d.Node dropdown({
357357
classes: [
358358
'mdc-select',
359359
'mdc-select--filled',
360-
if (classes != null) ...classes,
360+
...?classes,
361361
],
362362
attributes: {'data-mdc-auto-init': 'MDCSelect'},
363363
children: [

app/lib/frontend/templates/package.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ d.Node renderPkgInfoBox(PackagePageData data) {
4949
final uri = urls.parseValidUrl(href);
5050
if (uri == null) return;
5151

52-
final problemCode = urlProblems == null
53-
? null
54-
: urlProblems.firstWhereOrNull((p) => p.url == uri.toString())?.problem;
52+
final problemCode =
53+
urlProblems?.firstWhereOrNull((p) => p.url == uri.toString())?.problem;
5554
if (detectServiceProvider && problemCode == null) {
5655
final providerName = urls.inferServiceProviderName(href);
5756
if (providerName != null) {

app/lib/package/search_adapter.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,11 @@ class SearchAdapter {
3737
/// provide package names and perform search in that set.
3838
Future<SearchResultPage> search(SearchForm form,
3939
{required String? rateLimitKey}) async {
40-
final result = await _searchOrFallback(form, rateLimitKey, true);
41-
final views = await _getPackageViewsFromHits(
42-
[
43-
if (result?.packageHits != null) ...result!.packageHits,
44-
],
45-
);
40+
final result = (await _searchOrFallback(form, rateLimitKey, true))!;
41+
final views = await _getPackageViewsFromHits([...result.packageHits]);
4642
return SearchResultPage(
4743
form,
48-
result!.totalCount,
44+
result.totalCount,
4945
sdkLibraryHits: result.sdkLibraryHits,
5046
packageHits:
5147
result.packageHits.map((h) => views[h.package]).nonNulls.toList(),

app/lib/service/csp/default_csp.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ final _defaultContentSecurityPolicyMap = <String, List<String>>{
6969
String _serializeCSP(Map<String, String>? extraValues) {
7070
final keys = <String>{
7171
..._defaultContentSecurityPolicyMap.keys,
72-
if (extraValues != null) ...extraValues.keys,
72+
...?extraValues?.keys,
7373
};
7474
return keys.map((key) {
7575
final list = _defaultContentSecurityPolicyMap[key];

app/lib/shared/handlers.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ shelf.Response jsonResponse(
6666
body: body,
6767
headers: {
6868
...jsonResponseHeaders,
69-
if (headers != null) ...headers,
69+
...?headers,
7070
},
7171
);
7272
}

app/lib/task/backend.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ class TaskBackend {
710710
// Only update if new dependencies have been discovered.
711711
// This avoids unnecessary churn on datastore when there is no changes.
712712
if (state.dependencies != updatedDependencies &&
713-
!{...state.dependencies ?? []}.containsAll(updatedDependencies)) {
713+
!{...?state.dependencies}.containsAll(updatedDependencies)) {
714714
state.dependencies = updatedDependencies;
715715
}
716716
}

app/lib/task/models.dart

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,23 @@ class PackageState extends db.ExpandoModel<String> {
134134
/// * `scheduled + 31 days` for any version,
135135
/// * `scheduled + 24 hours` for any version where `dependencyChanged > scheduled`
136136
/// * `scheduled + 3 hours * attempts^2` for any version where `attempts > 0 && attempts < 3`.
137-
void derivePendingAt() => pendingAt = [
138-
// scheduled + 31 days
139-
...versions!.values.map((v) => v.scheduled.add(taskRetriggerInterval)),
140-
// scheduled + 24 hours, where scheduled < lastDependencyChanged
141-
...versions!.values
142-
.where((v) => v.scheduled.isBefore(lastDependencyChanged!))
143-
.map((v) => v.scheduled.add(taskDependencyRetriggerCoolOff)),
144-
// scheduled + 3 hours * attempts^2, where attempts > 0 && attempts < 3
145-
...versions!.values
146-
.where((v) => v.attempts > 0 && v.attempts < taskRetryLimit)
147-
.map((v) => v.scheduled.add(taskRetryDelay(v.attempts))),
148-
// Pick the minimum of the candidates, default scheduling in year 3k
149-
// if there is no date before that.
150-
].fold(DateTime(3000), (a, b) => a!.isBefore(b) ? a : b);
137+
void derivePendingAt() {
138+
final versionStates = versions!.values;
139+
pendingAt = [
140+
// scheduled + 31 days
141+
...versionStates.map((v) => v.scheduled.add(taskRetriggerInterval)),
142+
// scheduled + 24 hours, where scheduled < lastDependencyChanged
143+
...versionStates
144+
.where((v) => v.scheduled.isBefore(lastDependencyChanged!))
145+
.map((v) => v.scheduled.add(taskDependencyRetriggerCoolOff)),
146+
// scheduled + 3 hours * attempts^2, where attempts > 0 && attempts < 3
147+
...versionStates
148+
.where((v) => v.attempts > 0 && v.attempts < taskRetryLimit)
149+
.map((v) => v.scheduled.add(taskRetryDelay(v.attempts))),
150+
// Pick the minimum of the candidates, default scheduling in year 3k
151+
// if there is no date before that.
152+
].fold(DateTime(3000), (a, b) => a!.isBefore(b) ? a : b);
153+
}
151154

152155
/// Return a list of pending versions for this package.
153156
///
@@ -192,7 +195,7 @@ class PackageState extends db.ExpandoModel<String> {
192195
'package: $package',
193196
'runtimeVersion: $runtimeVersion',
194197
'versions:',
195-
...(versions ?? {}).entries.map((e) => ' ${e.key}: ${e.value}'),
198+
...?versions?.entries.map((e) => ' ${e.key}: ${e.value}'),
196199
'pendingAt: $pendingAt',
197200
'lastDependencyChanged: $lastDependencyChanged',
198201
'dependencies: [' + (dependencies ?? []).join(', ') + ']',

0 commit comments

Comments
 (0)