Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit 70ce9c8

Browse files
authored
Rename noNulls() -> noNullish() (#249)
#235 (comment) Rename to `noNullish()`. I went with "nullish" like ["nullish coalescing"](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing), "nullish checks", etc.
1 parent a88fa15 commit 70ce9c8

File tree

5 files changed

+19
-26
lines changed

5 files changed

+19
-26
lines changed

src/compute-pr-actions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Comments from "./comments";
22
import { PrInfo, BotError, BotEnsureRemovedFromProject, BotNoPackages, FileInfo } from "./pr-info";
33
import { CIResult } from "./util/CIResult";
44
import { ReviewInfo } from "./pr-info";
5-
import { noNulls, flatten, unique, sameUser, daysSince, sha256 } from "./util/util";
5+
import { noNullish, flatten, unique, sameUser, daysSince, sha256 } from "./util/util";
66

77
type ColumnName =
88
| "Needs Maintainer Action"
@@ -150,10 +150,10 @@ function extendPrInfo(info: PrInfo): ExtendedPrInfo {
150150
const noOtherOwners = allOwners.every(isAuthor);
151151
const tooManyOwners = allOwners.length > 50;
152152
const editsOwners = info.pkgInfo.some(p => p.kind === "edit" && p.addedOwners.length + p.deletedOwners.length > 0);
153-
const packages = noNulls(info.pkgInfo.map(p => p.name));
153+
const packages = noNullish(info.pkgInfo.map(p => p.name));
154154
const hasMultiplePackages = packages.length > 1;
155155
const hasTests = info.pkgInfo.some(p => p.files.some(f => f.kind === "test"));
156-
const newPackages = noNulls(info.pkgInfo.map(p => p.kind === "add" ? p.name : null));
156+
const newPackages = noNullish(info.pkgInfo.map(p => p.kind === "add" ? p.name : null));
157157
const hasNewPackages = newPackages.length > 0;
158158
const requireMaintainer = editsInfra || editsConfig || hasMultiplePackages || !hasTests || hasNewPackages || tooManyOwners;
159159
const blessable = !(hasNewPackages || editsInfra || noOtherOwners);
@@ -202,7 +202,7 @@ function extendPrInfo(info: PrInfo): ExtendedPrInfo {
202202
}
203203

204204
function getPendingCriticalPackages() {
205-
return noNulls(info.pkgInfo.map(p =>
205+
return noNullish(info.pkgInfo.map(p =>
206206
p.popularityLevel === "Critical" && !p.owners.some(o => approvedReviews.some(r => sameUser(o, r.reviewer)))
207207
? p.name : null));
208208
}

src/execute-pr-actions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { PR as PRQueryResult, PR_repository_pullRequest } from "./queries/schema
22
import { Actions, LabelNames, LabelName } from "./compute-pr-actions";
33
import { createMutation, mutate } from "./graphql-client";
44
import { getProjectBoardColumns, getLabels } from "./util/cachedQueries";
5-
import { noNulls, flatten } from "./util/util";
5+
import { noNullish, flatten } from "./util/util";
66
import * as comment from "./util/comment";
77

88
// https://github.com/DefinitelyTyped/DefinitelyTyped/projects/5
@@ -22,7 +22,7 @@ export const deleteProjectCard = `mutation($input: DeleteProjectCardInput!) { de
2222
export async function executePrActions(actions: Actions, info: PRQueryResult, dry?: boolean) {
2323
const pr = info.repository?.pullRequest!;
2424
const botComments: ParsedComment[] = getBotComments(pr);
25-
const mutations = noNulls([
25+
const mutations = noNullish([
2626
...await getMutationsForLabels(actions, pr),
2727
...await getMutationsForProjectChanges(actions, pr),
2828
...getMutationsForComments(actions, pr.id, botComments),
@@ -38,7 +38,7 @@ export async function executePrActions(actions: Actions, info: PRQueryResult, dr
3838

3939
async function getMutationsForLabels(actions: Actions, pr: PR_repository_pullRequest) {
4040
if (!actions.shouldUpdateLabels) return []
41-
const labels = noNulls(pr.labels?.nodes!).map(l => l.name);
41+
const labels = noNullish(pr.labels?.nodes).map(l => l.name);
4242
const makeMutations = async (pred: (l: LabelName) => boolean, query: string) => {
4343
const labels = LabelNames.filter(pred);
4444
return labels.length === 0 ? null
@@ -72,7 +72,7 @@ async function getMutationsForProjectChanges(actions: Actions, pr: PR_repository
7272
type ParsedComment = { id: string, body: string, tag: string, status: string };
7373

7474
function getBotComments(pr: PR_repository_pullRequest): ParsedComment[] {
75-
return noNulls((pr.comments.nodes ?? [])
75+
return noNullish((pr.comments.nodes ?? [])
7676
.filter(comment => comment?.author?.login === "typescript-bot")
7777
.map(c => {
7878
const { id, body } = c!, parsed = comment.parse(body);

src/pr-info.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { getMonthlyDownloadCount } from "./util/npm";
1616
import { client } from "./graphql-client";
1717
import { ApolloQueryResult } from "apollo-boost";
1818
import { fetchFile as defaultFetchFile } from "./util/fetchFile";
19-
import { noNulls, notUndefined, findLast, forEachReverse, sameUser, authorNotBot, latestDate } from "./util/util";
19+
import { noNullish, findLast, forEachReverse, sameUser, authorNotBot, latestDate } from "./util/util";
2020
import * as comment from "./util/comment";
2121
import * as HeaderParser from "definitelytyped-header-parser";
2222
import * as jsonDiff from "fast-json-patch";
@@ -219,15 +219,15 @@ export async function deriveStateForPR(
219219
const reopenedDate = getReopenedDate(prInfo.timelineItems);
220220

221221
const pkgInfoEtc = await getPackageInfosEtc(
222-
noNulls(prInfo.files?.nodes).map(f => f.path).sort(),
222+
noNullish(prInfo.files?.nodes).map(f => f.path).sort(),
223223
headCommit.oid, fetchFile, async name => await getDownloads(name, lastPushDate));
224224
if (pkgInfoEtc instanceof Error) return botError(prInfo.number, pkgInfoEtc.message);
225225
const { pkgInfo, popularityLevel } = pkgInfoEtc;
226226
if (!pkgInfo.some(p => p.name)) return botNoPackages(prInfo.number);
227227

228228
const reviews = getReviews(prInfo);
229229
const latestReview = latestDate(...reviews.map(r => r.date));
230-
const comments = noNulls(prInfo.comments.nodes || []);
230+
const comments = noNullish(prInfo.comments.nodes);
231231
const mergeOfferDate = getMergeOfferDate(comments, headCommit.abbreviatedOid);
232232
const mergeRequest = getMergeRequest(comments,
233233
pkgInfo.length === 1 ? [author, ...pkgInfo[0].owners] : [author],
@@ -465,7 +465,7 @@ function getReviews(prInfo: PR_repository_pullRequest) {
465465
const headCommitOid: string = prInfo.headRefOid;
466466
const reviews: ReviewInfo[] = [];
467467
// Do this in reverse order so we can detect up-to-date-reviews correctly
468-
for (const r of noNulls(prInfo.reviews.nodes).reverse()) {
468+
for (const r of noNullish(prInfo.reviews.nodes).reverse()) {
469469
const [reviewer, date] = [r?.author?.login, new Date(r.submittedAt)];
470470
// Skip nulls
471471
if (!(r?.commit && reviewer)) continue;
@@ -522,5 +522,5 @@ async function getOwnersOfPackage(packageName: string, version: string, fetchFil
522522
} catch (e) {
523523
if (e instanceof Error) return new Error(`error parsing owners: ${e.message}`);
524524
}
525-
return parsed!.contributors.map(c => c.githubUsername).filter(notUndefined);
525+
return noNullish(parsed!.contributors.map(c => c.githubUsername));
526526
}

src/util/cachedQueries.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,26 @@ import { GetProjectColumns as GetProjectColumnsResult } from "../queries/schema/
33
import { GetLabels as GetLabelsResult } from "../queries/schema/GetLabels";
44
import { createCache } from "../ttl-cache";
55
import { client } from "../graphql-client";
6+
import { noNullish } from "./util";
67

78
const cache = createCache();
89

910
export async function getProjectBoardColumns() {
1011
return cache.getAsync("project board colum names", Infinity, async () => {
11-
const res = (await query<GetProjectColumnsResult>(GetProjectColumns))
12-
.repository?.project?.columns.nodes?.filter(defined)
13-
?? [];
12+
const res = noNullish((await query<GetProjectColumnsResult>(GetProjectColumns))
13+
.repository?.project?.columns.nodes);
1414
return res.sort((a,b) => a.name.localeCompare(b.name));
1515
});
1616
}
1717

1818
export async function getLabels() {
1919
return await cache.getAsync("label ids", Infinity, async () => {
20-
const res = (await query<GetLabelsResult>(GetLabels))
21-
.repository?.labels?.nodes?.filter(defined)
22-
?? [];
20+
const res = noNullish((await query<GetLabelsResult>(GetLabels))
21+
.repository?.labels?.nodes);
2322
return res.sort((a,b) => a.name.localeCompare(b.name));
2423
});
2524
}
2625

27-
function defined<T>(arg: T | null | undefined): arg is T {
28-
return arg != null;
29-
}
30-
3126
async function query<T>(gql: any): Promise<T> {
3227
const res = await client.query<T>({
3328
query: gql,

src/util/util.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import * as crypto from "crypto";
22
import * as moment from "moment";
33

4-
export function noNulls<T>(arr: ReadonlyArray<T | null | undefined> | null | undefined): T[] {
4+
export function noNullish<T>(arr: ReadonlyArray<T | null | undefined> | null | undefined): T[] {
55
if (arr == null) return [];
66
return arr.filter(arr => arr != null) as T[];
77
}
88

9-
export function notUndefined<T>(arg: T | undefined): arg is T { return arg !== undefined; }
10-
119
export function flatten<T>(xs: T[][]) {
1210
return ([] as T[]).concat(...xs);
1311
}

0 commit comments

Comments
 (0)