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

Commit 13cc147

Browse files
committed
Extra types
1 parent 26d9bff commit 13cc147

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
@@ -184,6 +184,12 @@ export async function queryPRInfo(prNumber: number) {
184184
}
185185
}
186186

187+
interface Refs {
188+
readonly head: string;
189+
readonly master: "master";
190+
readonly latestSuggestions: string;
191+
}
192+
187193
// The GQL response => Useful data for us
188194
export async function deriveStateForPR(
189195
prInfo: PR_repository_pullRequest,
@@ -215,7 +221,7 @@ export async function deriveStateForPR(
215221
master: "master",
216222
latestSuggestions: max(noNullish(prInfo.reviews?.nodes).filter(review => !authorNotBot(review)), (a, b) =>
217223
Date.parse(a.submittedAt) - Date.parse(b.submittedAt))?.commit?.oid,
218-
};
224+
} as const;
219225
const pkgInfoEtc = await getPackageInfosEtc(
220226
noNullish(prInfo.files?.nodes).map(f => f.path).sort(),
221227
refs, fetchFile, async name => await getDownloads(name, lastPushDate));
@@ -294,11 +300,11 @@ function getLastMaintainerBlessingDate(timelineItems: PR_repository_pullRequest_
294300
}
295301

296302
async function getPackageInfosEtc(
297-
paths: string[], refs: { [ref: string]: string }, fetchFile: typeof defaultFetchFile, getDownloads: typeof getMonthlyDownloadCount
303+
paths: string[], refs: Refs, fetchFile: typeof defaultFetchFile, getDownloads: typeof getMonthlyDownloadCount
298304
): Promise<{pkgInfo: PackageInfo[], popularityLevel: PopularityLevel} | Error> {
299305
const infos = new Map<string|null, FileInfo[]>();
300306
for (const path of paths) {
301-
const [pkg, fileInfo] = await categorizeFile(path, async (ref: string) => fetchFile(`${refs[ref]}:${path}`));
307+
const [pkg, fileInfo] = await categorizeFile(path, async ref => fetchFile(`${refs[ref]}:${path}`));
302308
if (!infos.has(pkg)) infos.set(pkg, []);
303309
infos.get(pkg)!.push(fileInfo);
304310
}
@@ -333,7 +339,9 @@ async function getPackageInfosEtc(
333339
return { pkgInfo: result, popularityLevel: downloadsToPopularityLevel(maxDownloads) };
334340
}
335341

336-
async function categorizeFile(path: string, getContents: (ref: string) => Promise<string | undefined>): Promise<[string|null, FileInfo]> {
342+
type GetContents = (ref: keyof Refs) => Promise<string | undefined>;
343+
344+
async function categorizeFile(path: string, getContents: GetContents): Promise<[string|null, FileInfo]> {
337345
// https://regex101.com/r/eFvtrz/1
338346
const match = /^types\/(.*?)\/.*?[^\/](?:\.(d\.ts|tsx?|md))?$/.exec(path);
339347
if (!match) return [null, { path, kind: "infrastructure" }];
@@ -351,8 +359,8 @@ async function categorizeFile(path: string, getContents: (ref: string) => Promis
351359
}
352360

353361
interface ConfigSuspicious {
354-
(path: string, getContents: (ref: string) => Promise<string | undefined>): Promise<{ suspect: string, sugestion?: Suggestion } | undefined>;
355-
[basename: string]: (newText: string, getContents: (ref: string) => Promise<string | undefined>) => Promise<{ suspect: string, suggestion?: Suggestion } | undefined>;
362+
(path: string, getContents: GetContents): Promise<{ suspect: string, sugestion?: Suggestion } | undefined>;
363+
[basename: string]: (newText: string, getContents: GetContents) => Promise<{ suspect: string, suggestion?: Suggestion } | undefined>;
356364
}
357365
const configSuspicious = <ConfigSuspicious>(async (path, getContents) => {
358366
const basename = path.replace(/.*\//, "");
@@ -411,7 +419,7 @@ configSuspicious["tsconfig.json"] = makeChecker(
411419
// to it, ignoring some keys. The ignored properties are in most cases checked
412420
// elsewhere (dtslint), and in some cases they are irrelevant.
413421
function makeChecker(expectedForm: any, expectedFormUrl: string, options?: { parse: (text: string) => unknown } | { ignore: (data: any) => void }) {
414-
return async (newText: string, getContents: (ref: string) => Promise<string | undefined>) => {
422+
return async (newText: string, getContents: GetContents) => {
415423
let suggestion: any;
416424
if (options && "parse" in options) {
417425
suggestion = options.parse(newText);
@@ -436,7 +444,7 @@ function makeChecker(expectedForm: any, expectedFormUrl: string, options?: { par
436444
};
437445

438446
// Apply any preexisting diffs to towardsIt
439-
async function ignoreExistingDiffs(ref: string) {
447+
async function ignoreExistingDiffs(ref: keyof Refs) {
440448
const theExpectedForm = `[the expected form](${expectedFormUrl})`;
441449
const diffFromExpected = (data: any) => jsonDiff.compare(towardsIt, data);
442450
const newDiff = diffFromExpected(newData);

0 commit comments

Comments
 (0)