Skip to content

Commit

Permalink
Take more advantage of null-aware operators (#8042)
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough authored Sep 12, 2024
1 parent 7858fd9 commit 94a74a3
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 43 deletions.
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ linter:
- prefer_is_empty
- prefer_is_not_empty
- prefer_iterable_whereType
- prefer_null_aware_operators
- prefer_single_quotes
- prefer_spread_collections
- prefer_typing_uninitialized_variables
Expand Down
18 changes: 9 additions & 9 deletions app/lib/frontend/dom/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Node a({
if (rel != null) 'rel': rel,
if (target != null) 'target': target,
if (title != null) 'title': title,
if (attributes != null) ...attributes,
...?attributes,
},
children: children,
child: child,
Expand Down Expand Up @@ -369,7 +369,7 @@ Node form({
attributes: <String, String>{
if (action != null) 'action': action,
if (method != null) 'method': method,
if (attributes != null) ...attributes,
...?attributes,
},
children: children,
child: child,
Expand Down Expand Up @@ -512,7 +512,7 @@ Node img({
if (title != null) 'title': title,
if (lazy) 'loading': 'lazy',
if (image.role != null) 'role': image.role!,
if (attributes != null) ...attributes,
...?attributes,
},
children: children,
);
Expand Down Expand Up @@ -546,7 +546,7 @@ Node input({
if (value != null) 'value': value,
if (autofocus) 'autofocus': 'autofocus',
if (disabled) 'disabled': 'disabled',
if (attributes != null) ...attributes,
...?attributes,
},
children: children,
child: child,
Expand Down Expand Up @@ -616,7 +616,7 @@ Node link({
if (title != null) 'title': title,
if (href != null) 'href': href,
if (as != null) 'as': as,
if (attributes != null) ...attributes,
...?attributes,
},
children: children,
child: child,
Expand Down Expand Up @@ -651,7 +651,7 @@ Node meta({
if (content != null) 'content': content,
if (rel != null) 'rel': rel,
if (href != null) 'href': href,
if (attributes != null) ...attributes,
...?attributes,
},
children: children,
child: child,
Expand Down Expand Up @@ -697,7 +697,7 @@ Node option({
if (value != null) 'value': value,
if (disabled) 'disabled': 'disabled',
if (selected) 'selected': 'selected',
if (attributes != null) ...attributes,
...?attributes,
},
children: children,
child: child,
Expand Down Expand Up @@ -767,7 +767,7 @@ Node script({
if (async) 'async': 'async',
if (defer) 'defer': 'defer',
if (onload != null) 'onload': onload,
if (attributes != null) ...attributes,
...?attributes,
},
children: children,
child: child,
Expand Down Expand Up @@ -981,7 +981,7 @@ Map<String, String>? _mergeAttributes(
return <String, String>{
if (id != null) 'id': id,
if (classes != null && classes.isNotEmpty) 'class': classes.join(' '),
if (attributes != null) ...attributes,
...?attributes,
};
}

Expand Down
12 changes: 6 additions & 6 deletions app/lib/frontend/dom/material.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ d.Node button({
if (raised) 'mdc-button--raised',
if (unelevated) 'mdc-button--unelevated',
if (customTypeClass != null) customTypeClass,
if (classes != null) ...classes,
...?classes,
],
attributes: {
'data-mdc-auto-init': 'MDCRipple',
if (attributes != null) ...attributes,
...?attributes,
},
children: isSimpleLabel
? [d.text(label)]
Expand Down Expand Up @@ -70,11 +70,11 @@ d.Node floatingActionButton({
classes: [
'mdc-fab',
if (fabMini) 'mdc-fab--mini',
if (classes != null) ...classes,
...?classes,
],
attributes: {
'data-mdc-auto-init': 'MDCRipple',
if (attributes != null) ...attributes,
...?attributes,
},
children: [
d.div(classes: ['mdc-fab__ripple']),
Expand Down Expand Up @@ -257,7 +257,7 @@ d.Node dataTable<T>({
(c) => d.th(
classes: [
'mdc-data-table__header-cell',
if (c.headerClasses != null) ...c.headerClasses!,
...?c.headerClasses,
],
attributes: {
'role': 'columnheader',
Expand Down Expand Up @@ -357,7 +357,7 @@ d.Node dropdown({
classes: [
'mdc-select',
'mdc-select--filled',
if (classes != null) ...classes,
...?classes,
],
attributes: {'data-mdc-auto-init': 'MDCSelect'},
children: [
Expand Down
5 changes: 2 additions & 3 deletions app/lib/frontend/templates/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ d.Node renderPkgInfoBox(PackagePageData data) {
final uri = urls.parseValidUrl(href);
if (uri == null) return;

final problemCode = urlProblems == null
? null
: urlProblems.firstWhereOrNull((p) => p.url == uri.toString())?.problem;
final problemCode =
urlProblems?.firstWhereOrNull((p) => p.url == uri.toString())?.problem;
if (detectServiceProvider && problemCode == null) {
final providerName = urls.inferServiceProviderName(href);
if (providerName != null) {
Expand Down
10 changes: 3 additions & 7 deletions app/lib/package/search_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,11 @@ class SearchAdapter {
/// provide package names and perform search in that set.
Future<SearchResultPage> search(SearchForm form,
{required String? rateLimitKey}) async {
final result = await _searchOrFallback(form, rateLimitKey, true);
final views = await _getPackageViewsFromHits(
[
if (result?.packageHits != null) ...result!.packageHits,
],
);
final result = (await _searchOrFallback(form, rateLimitKey, true))!;
final views = await _getPackageViewsFromHits([...result.packageHits]);
return SearchResultPage(
form,
result!.totalCount,
result.totalCount,
sdkLibraryHits: result.sdkLibraryHits,
packageHits:
result.packageHits.map((h) => views[h.package]).nonNulls.toList(),
Expand Down
2 changes: 1 addition & 1 deletion app/lib/service/csp/default_csp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ final _defaultContentSecurityPolicyMap = <String, List<String>>{
String _serializeCSP(Map<String, String>? extraValues) {
final keys = <String>{
..._defaultContentSecurityPolicyMap.keys,
if (extraValues != null) ...extraValues.keys,
...?extraValues?.keys,
};
return keys.map((key) {
final list = _defaultContentSecurityPolicyMap[key];
Expand Down
2 changes: 1 addition & 1 deletion app/lib/shared/handlers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ shelf.Response jsonResponse(
body: body,
headers: {
...jsonResponseHeaders,
if (headers != null) ...headers,
...?headers,
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion app/lib/task/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ class TaskBackend {
// Only update if new dependencies have been discovered.
// This avoids unnecessary churn on datastore when there is no changes.
if (state.dependencies != updatedDependencies &&
!{...state.dependencies ?? []}.containsAll(updatedDependencies)) {
!{...?state.dependencies}.containsAll(updatedDependencies)) {
state.dependencies = updatedDependencies;
}
}
Expand Down
33 changes: 18 additions & 15 deletions app/lib/task/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,23 @@ class PackageState extends db.ExpandoModel<String> {
/// * `scheduled + 31 days` for any version,
/// * `scheduled + 24 hours` for any version where `dependencyChanged > scheduled`
/// * `scheduled + 3 hours * attempts^2` for any version where `attempts > 0 && attempts < 3`.
void derivePendingAt() => pendingAt = [
// scheduled + 31 days
...versions!.values.map((v) => v.scheduled.add(taskRetriggerInterval)),
// scheduled + 24 hours, where scheduled < lastDependencyChanged
...versions!.values
.where((v) => v.scheduled.isBefore(lastDependencyChanged!))
.map((v) => v.scheduled.add(taskDependencyRetriggerCoolOff)),
// scheduled + 3 hours * attempts^2, where attempts > 0 && attempts < 3
...versions!.values
.where((v) => v.attempts > 0 && v.attempts < taskRetryLimit)
.map((v) => v.scheduled.add(taskRetryDelay(v.attempts))),
// Pick the minimum of the candidates, default scheduling in year 3k
// if there is no date before that.
].fold(DateTime(3000), (a, b) => a!.isBefore(b) ? a : b);
void derivePendingAt() {
final versionStates = versions!.values;
pendingAt = [
// scheduled + 31 days
...versionStates.map((v) => v.scheduled.add(taskRetriggerInterval)),
// scheduled + 24 hours, where scheduled < lastDependencyChanged
...versionStates
.where((v) => v.scheduled.isBefore(lastDependencyChanged!))
.map((v) => v.scheduled.add(taskDependencyRetriggerCoolOff)),
// scheduled + 3 hours * attempts^2, where attempts > 0 && attempts < 3
...versionStates
.where((v) => v.attempts > 0 && v.attempts < taskRetryLimit)
.map((v) => v.scheduled.add(taskRetryDelay(v.attempts))),
// Pick the minimum of the candidates, default scheduling in year 3k
// if there is no date before that.
].fold(DateTime(3000), (a, b) => a!.isBefore(b) ? a : b);
}

/// Return a list of pending versions for this package.
///
Expand Down Expand Up @@ -192,7 +195,7 @@ class PackageState extends db.ExpandoModel<String> {
'package: $package',
'runtimeVersion: $runtimeVersion',
'versions:',
...(versions ?? {}).entries.map((e) => ' ${e.key}: ${e.value}'),
...?versions?.entries.map((e) => ' ${e.key}: ${e.value}'),
'pendingAt: $pendingAt',
'lastDependencyChanged: $lastDependencyChanged',
'dependencies: [' + (dependencies ?? []).join(', ') + ']',
Expand Down

0 comments on commit 94a74a3

Please sign in to comment.