Skip to content

Commit

Permalink
Utilize nonNulls where possible (#8043)
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough authored Sep 11, 2024
1 parent 3227447 commit b36a318
Show file tree
Hide file tree
Showing 16 changed files with 22 additions and 38 deletions.
3 changes: 1 addition & 2 deletions app/lib/account/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,7 @@ String consentDedupId({
required List<String> args,
}) =>
[fromAgentId, email, kind, ...args]
.where((s) => s != null)
.whereType<String>()
.nonNulls
.map(Uri.encodeComponent)
.join('/');

Expand Down
3 changes: 1 addition & 2 deletions app/lib/admin/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,7 @@ class AdminBackend {
}

List<api.AdminUserEntry> _convertUsers(Iterable<User?> users) {
return users
.whereType<User>()
return users.nonNulls
.where((u) => !u.isDeleted)
.map(
(u) => api.AdminUserEntry(
Expand Down
2 changes: 1 addition & 1 deletion app/lib/fake/tool/init_data_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class FakeInitDataFileCommand extends Command {
r.toString(),
r.error,
r.stackTrace?.toString(),
].where((e) => e != null).join(' '));
].nonNulls.join(' '));
});

final analysis = argResults!['analysis'] as String;
Expand Down
3 changes: 1 addition & 2 deletions app/lib/frontend/handlers/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import '../../account/session_cookie.dart' as session_cookie;
import '../../audit/backend.dart';
import '../../frontend/request_context.dart';
import '../../package/backend.dart';
import '../../package/models.dart';
import '../../publisher/backend.dart';
import '../../publisher/models.dart';
import '../../scorecard/backend.dart';
Expand Down Expand Up @@ -283,7 +282,7 @@ Future<shelf.Response> accountPackagesPageHandler(shelf.Request request) async {
.lookupUserById(requestContext.authenticatedUserId!))!,
userSessionData: requestContext.sessionData!,
startPackage: next,
packageHits: hits.whereType<PackageView>().toList(),
packageHits: hits.nonNulls.toList(),
nextPackage: page.nextPackage,
);
return htmlResponse(html);
Expand Down
2 changes: 1 addition & 1 deletion app/lib/frontend/handlers/custom_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Future<shelf.Response> apiPackagesHandler(shelf.Request request) async {
final packagesJson = [];

final uri = activeConfiguration.primaryApiUri;
for (final version in pageVersions.whereType<PackageVersion>()) {
for (final version in pageVersions.nonNulls) {
final versionString = Uri.encodeComponent(version.version!);
final packageString = Uri.encodeComponent(version.package);

Expand Down
2 changes: 1 addition & 1 deletion app/lib/frontend/templates/package_misc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ d.Node tagsNodeFromPackageView({
}) {
searchForm ??= SearchForm();
final tags = package.tags;
final sdkTags = tags.where((s) => s.startsWith('sdk:')).toSet().toList();
final sdkTags = tags.where((s) => s.startsWith('sdk:')).toSet();
final simpleTags = <SimpleTag>[];
final badgeTags = <BadgeTag>[];
d.Node? discontinuedNode;
Expand Down
6 changes: 3 additions & 3 deletions app/lib/package/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,8 @@ class PackageBackend {
final newPublisherAdminEmails =
await publisherBackend.getAdminMemberEmails(request.publisherId!);
final allAdminEmails = <String>{
...preTxUploaderEmails.whereType<String>(),
...newPublisherAdminEmails.whereType<String>(),
...preTxUploaderEmails,
...newPublisherAdminEmails.nonNulls,
};

OutgoingEmail? email;
Expand Down Expand Up @@ -1440,7 +1440,7 @@ class PackageBackend {
final emails = package.publisherId == null
? await accountBackend.getEmailsOfUserIds(package.uploaders!)
: await publisherBackend.getAdminMemberEmails(package.publisherId!);
final existingEmails = emails.whereType<String>().toList();
final existingEmails = emails.nonNulls.toList();
if (existingEmails.isEmpty) {
// should not happen
throw AssertionError(
Expand Down
7 changes: 2 additions & 5 deletions app/lib/package/search_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ class SearchAdapter {
form,
result!.totalCount,
sdkLibraryHits: result.sdkLibraryHits,
packageHits: result.packageHits
.map((h) => views[h.package])
.where((v) => v != null)
.cast<PackageView>()
.toList(),
packageHits:
result.packageHits.map((h) => views[h.package]).nonNulls.toList(),
errorMessage: result.errorMessage,
statusCode: result.statusCode,
);
Expand Down
5 changes: 2 additions & 3 deletions app/lib/publisher/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ class PublisherBackend {
// - search using this for query parameters
_logger.shout('A user has more than 100 publishers.');
}
final publishers = (await _db.lookup<Publisher>(publisherKeys))
.whereType<Publisher>()
.toList();
final publishers =
(await _db.lookup<Publisher>(publisherKeys)).nonNulls.toList();
publishers.sort((a, b) => a.publisherId.compareTo(b.publisherId));
return PublisherPage(
publishers: publishers
Expand Down
2 changes: 1 addition & 1 deletion app/lib/service/youtube/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class _PkgOfWeekVideoFetcher {
}
return null;
},
).whereType<PkgOfWeekVideo>());
).nonNulls);
// next page
nextPageToken = rs.nextPageToken;
check = nextPageToken != null && nextPageToken.isNotEmpty;
Expand Down
7 changes: 1 addition & 6 deletions pkg/api_builder/lib/src/client_library_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,7 @@ code.Method _buildClientMethod(
Handler h,
) {
// Find url parameters
final params = _parser
.allMatches(h.route)
.map((m) => m[2])
.where((p) => p != null)
.cast<String>()
.toList();
final params = _parser.allMatches(h.route).map((m) => m[2]).nonNulls.toList();
// Create a url pattern that embeds parameters above
final urlPattern = h.route.replaceAllMapped(
_parser,
Expand Down
4 changes: 2 additions & 2 deletions pkg/code_coverage/lib/format_lcov.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Future main() async {
!e.key.startsWith('pkg/code_coverage/') &&
!e.key.startsWith('pkg/fake_gcloud/') &&
!e.key.startsWith('pkg/pub_integration/'))
].whereType<Entry>();
].nonNulls;
final pubDevEntry = Entry('pub-dev')
..covered = libEntries.map((e) => e.covered).reduce((a, b) => a + b)
..total = libEntries.map((e) => e.total).reduce((a, b) => a + b);
Expand Down Expand Up @@ -83,7 +83,7 @@ Future main() async {

final uncoveredRanges = _lineExecCounts.keys
.map((path) => _topUncoveredRange(path, _lineExecCounts[path]!))
.whereType<_UncoveredRange>()
.nonNulls
.where((u) => u.codeLineCount > 0)
.toList()
..sort((a, b) => -a.codeLineCount.compareTo(b.codeLineCount));
Expand Down
7 changes: 2 additions & 5 deletions pkg/pub_package_reader/lib/pub_package_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ Future<PackageSummary> summarizePackageArchive(
String? licensePath = tar.firstMatchingFileNameOrNull(licenseFileNames);

final contentBytes = await tar.scanAndReadFiles(
[readmePath, changelogPath, examplePath, licensePath]
.whereType<String>()
.toList(),
[readmePath, changelogPath, examplePath, licensePath].nonNulls.toList(),
maxLength: maxContentLength,
);

Expand Down Expand Up @@ -439,8 +437,7 @@ Iterable<ArchiveIssue> checkStrictVersions(Pubspec pubspec) sync* {
...pubspec.dependencyOverrides.values.expand(expandDependency),
];

yield* versions
.whereType<Version>() // only consider non-null values
yield* versions.nonNulls
.where((v) => _strictRegExp.matchAsPrefix(v.toString()) == null)
.map((v) => ArchiveIssue(
'Version value `$v` does not follow strict version pattern.'));
Expand Down
3 changes: 1 addition & 2 deletions pkg/web_app/lib/src/deferred/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ String? _getCsrfMetaContent() {
final values = document.head
?.querySelectorAll('meta[name="csrf-token"]')
.map((e) => e.attributes['content'])
.where((e) => e != null)
.cast<String>()
.nonNulls
.toList();
if (values == null || values.isEmpty) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion pkg/web_app/lib/src/mobile_nav.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void _setEventForMobileNav() {
final mask = document.querySelector('.site-header-mask');
final nav = document.querySelector('.site-header-nav');

final allElems = [nav, mask].where((e) => e != null).cast<Element>().toList();
final allElems = [nav, mask].nonNulls.toList();

hamburger?.onClick.listen((_) {
// This opacity hack enables smooth initialization, otherwise users would
Expand Down
2 changes: 1 addition & 1 deletion pkg/web_app/lib/src/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ String _openSectionParams() {
.where((e) =>
e.dataset.containsKey('section-tag') && e.classes.contains('-active'))
.map((e) => e.dataset['section-tag'])
.whereType<String>()
.nonNulls
.join(' ');
}

Expand Down

0 comments on commit b36a318

Please sign in to comment.