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

Commit cd8ebd7

Browse files
committed
Extra types
1 parent 3b44fa6 commit cd8ebd7

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/pr-info.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ export async function queryPRInfo(prNumber: number) {
181181
});
182182
}
183183

184+
interface Refs {
185+
readonly head: string;
186+
readonly master: "master";
187+
readonly latestSuggestions: string;
188+
}
189+
184190
// The GQL response => Useful data for us
185191
export async function deriveStateForPR(
186192
info: ApolloQueryResult<PRQueryResult>,
@@ -218,7 +224,7 @@ export async function deriveStateForPR(
218224
!latest?.submittedAt || review.submittedAt && new Date (review.submittedAt) > new Date(latest.submittedAt)
219225
|| review.commit?.oid === headCommit.oid)
220226
? review : latest, null)?.commit?.oid,
221-
};
227+
} as const;
222228
const pkgInfoEtc = await getPackageInfosEtc(
223229
noNulls(prInfo.files?.nodes).map(f => f.path).sort(),
224230
refs, fetchFile, async name => await getDownloads(name, lastPushDate));
@@ -319,11 +325,11 @@ function getLastMaintainerBlessingDate(timelineItems: PR_repository_pullRequest_
319325
}
320326

321327
async function getPackageInfosEtc(
322-
paths: string[], refs: { [ref: string]: string }, fetchFile: typeof defaultFetchFile, getDownloads: typeof getMonthlyDownloadCount
328+
paths: string[], refs: Refs, fetchFile: typeof defaultFetchFile, getDownloads: typeof getMonthlyDownloadCount
323329
): Promise<{pkgInfo: PackageInfo[], popularityLevel: PopularityLevel} | Error> {
324330
const infos = new Map<string|null, FileInfo[]>();
325331
for (const path of paths) {
326-
const [pkg, fileInfo] = await categorizeFile(path, async (ref: string) => fetchFile(`${refs[ref]}:${path}`));
332+
const [pkg, fileInfo] = await categorizeFile(path, async ref => fetchFile(`${refs[ref]}:${path}`));
327333
if (!infos.has(pkg)) infos.set(pkg, []);
328334
infos.get(pkg)!.push(fileInfo);
329335
}
@@ -354,7 +360,9 @@ async function getPackageInfosEtc(
354360
return { pkgInfo: result, popularityLevel: downloadsToPopularityLevel(maxDownloads) };
355361
}
356362

357-
async function categorizeFile(path: string, getContents: (ref: string) => Promise<string | undefined>): Promise<[string|null, FileInfo]> {
363+
type GetContents = (ref: keyof Refs) => Promise<string | undefined>;
364+
365+
async function categorizeFile(path: string, getContents: GetContents): Promise<[string|null, FileInfo]> {
358366
// https://regex101.com/r/eFvtrz/1
359367
const match = /^types\/(.*?)\/.*?[^\/](?:\.(d\.ts|tsx?|md))?$/.exec(path);
360368
if (!match) return [null, { path, kind: "infrastructure" }];
@@ -370,8 +378,8 @@ async function categorizeFile(path: string, getContents: (ref: string) => Promis
370378
}
371379

372380
interface ConfigSuspicious {
373-
(path: string, getContents: (ref: string) => Promise<string | undefined>): Promise<{ suspect: string, sugestion?: Suggestion } | undefined>;
374-
[basename: string]: (newText: string, getContents: (ref: string) => Promise<string | undefined>) => Promise<{ suspect: string, suggestion?: Suggestion } | undefined>;
381+
(path: string, getContents: GetContents): Promise<{ suspect: string, sugestion?: Suggestion } | undefined>;
382+
[basename: string]: (newText: string, getContents: GetContents) => Promise<{ suspect: string, suggestion?: Suggestion } | undefined>;
375383
};
376384
const configSuspicious = <ConfigSuspicious>(async (path, getContents) => {
377385
const basename = path.replace(/.*\//, "");
@@ -414,7 +422,7 @@ configSuspicious["tsconfig.json"] = makeJsonCheckerFromCore(
414422
// to it, ignoring some keys (JSON Patch paths). The ignored properties are in most cases checked
415423
// elsewhere (dtslint), and in some cases they are irrelevant.
416424
function makeJsonCheckerFromCore(requiredForm: any, ignoredKeys: string[]) {
417-
return async (newText: string, getContents: (ref: string) => Promise<string | undefined>) => {
425+
return async (newText: string, getContents: GetContents) => {
418426
let suggestion: any;
419427
try { suggestion = JSON.parse(newText); } catch (e) { return { suspect: "couldn't parse json" }; }
420428
const newJson = jsonDiff.deepClone(suggestion);
@@ -435,7 +443,7 @@ function makeJsonCheckerFromCore(requiredForm: any, ignoredKeys: string[]) {
435443
};
436444

437445
// Apply any preexisting diffs to towardsIt
438-
async function ignoreExistingDiffs(ref: string) {
446+
async function ignoreExistingDiffs(ref: keyof Refs) {
439447
const diffFromReq = (json: any) => jsonDiff.compare(towardsIt, json);
440448
const newDiff = diffFromReq(newJson);
441449
if (newDiff.length === 0) return undefined;

0 commit comments

Comments
 (0)