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

Commit 71df39a

Browse files
committed
Extra types
1 parent e82afde commit 71df39a

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
@@ -204,6 +204,12 @@ export async function queryPRInfo(prNumber: number) {
204204
}
205205
}
206206

207+
interface Refs {
208+
readonly head: string;
209+
readonly master: "master";
210+
readonly latestSuggestions: string;
211+
}
212+
207213
// The GQL response => Useful data for us
208214
export async function deriveStateForPR(
209215
info: ApolloQueryResult<PRQueryResult>,
@@ -238,7 +244,7 @@ export async function deriveStateForPR(
238244
master: "master",
239245
latestSuggestions: max(noNullish(prInfo.reviews?.nodes).filter(review => !authorNotBot(review)), (a, b) =>
240246
Date.parse(a.submittedAt) - Date.parse(b.submittedAt))?.commit?.oid,
241-
};
247+
} as const;
242248
const pkgInfoEtc = await getPackageInfosEtc(
243249
noNullish(prInfo.files?.nodes).map(f => f.path).sort(),
244250
refs, fetchFile, async name => await getDownloads(name, lastPushDate));
@@ -333,11 +339,11 @@ function getLastMaintainerBlessingDate(timelineItems: PR_repository_pullRequest_
333339
}
334340

335341
async function getPackageInfosEtc(
336-
paths: string[], refs: { [ref: string]: string }, fetchFile: typeof defaultFetchFile, getDownloads: typeof getMonthlyDownloadCount
342+
paths: string[], refs: Refs, fetchFile: typeof defaultFetchFile, getDownloads: typeof getMonthlyDownloadCount
337343
): Promise<{pkgInfo: PackageInfo[], popularityLevel: PopularityLevel} | Error> {
338344
const infos = new Map<string|null, FileInfo[]>();
339345
for (const path of paths) {
340-
const [pkg, fileInfo] = await categorizeFile(path, async (ref: string) => fetchFile(`${refs[ref]}:${path}`));
346+
const [pkg, fileInfo] = await categorizeFile(path, async ref => fetchFile(`${refs[ref]}:${path}`));
341347
if (!infos.has(pkg)) infos.set(pkg, []);
342348
infos.get(pkg)!.push(fileInfo);
343349
}
@@ -372,7 +378,9 @@ async function getPackageInfosEtc(
372378
return { pkgInfo: result, popularityLevel: downloadsToPopularityLevel(maxDownloads) };
373379
}
374380

375-
async function categorizeFile(path: string, getContents: (ref: string) => Promise<string | undefined>): Promise<[string|null, FileInfo]> {
381+
type GetContents = (ref: keyof Refs) => Promise<string | undefined>;
382+
383+
async function categorizeFile(path: string, getContents: GetContents): Promise<[string|null, FileInfo]> {
376384
// https://regex101.com/r/eFvtrz/1
377385
const match = /^types\/(.*?)\/.*?[^\/](?:\.(d\.ts|tsx?|md))?$/.exec(path);
378386
if (!match) return [null, { path, kind: "infrastructure" }];
@@ -389,8 +397,8 @@ async function categorizeFile(path: string, getContents: (ref: string) => Promis
389397
}
390398

391399
interface ConfigSuspicious {
392-
(path: string, getContents: (ref: string) => Promise<string | undefined>): Promise<{ suspect: string, sugestion?: Suggestion } | undefined>;
393-
[basename: string]: (newText: string, getContents: (ref: string) => Promise<string | undefined>) => Promise<{ suspect: string, suggestion?: Suggestion } | undefined>;
400+
(path: string, getContents: GetContents): Promise<{ suspect: string, sugestion?: Suggestion } | undefined>;
401+
[basename: string]: (newText: string, getContents: GetContents) => Promise<{ suspect: string, suggestion?: Suggestion } | undefined>;
394402
}
395403
const configSuspicious = <ConfigSuspicious>(async (path, getContents) => {
396404
const basename = path.replace(/.*\//, "");
@@ -436,7 +444,7 @@ configSuspicious["tsconfig.json"] = makeJsonCheckerFromCore(
436444
// to it, ignoring some keys (JSON Patch paths). The ignored properties are in most cases checked
437445
// elsewhere (dtslint), and in some cases they are irrelevant.
438446
function makeJsonCheckerFromCore(requiredForm: any, ignoredKeys: string[], requiredFormUrl: string) {
439-
return async (newText: string, getContents: (ref: string) => Promise<string | undefined>) => {
447+
return async (newText: string, getContents: GetContents) => {
440448
let suggestion: any;
441449
try { suggestion = JSON.parse(newText); } catch (e) { if (e instanceof SyntaxError) return { suspect: `couldn't parse json: ${e.message}` }; }
442450
const newJson = jsonDiff.deepClone(suggestion);
@@ -457,7 +465,7 @@ function makeJsonCheckerFromCore(requiredForm: any, ignoredKeys: string[], requi
457465
};
458466

459467
// Apply any preexisting diffs to towardsIt
460-
async function ignoreExistingDiffs(ref: string) {
468+
async function ignoreExistingDiffs(ref: keyof Refs) {
461469
const theRequiredForm = `[the required form](${requiredFormUrl})`;
462470
const diffFromReq = (json: any) => jsonDiff.compare(towardsIt, json);
463471
const newDiff = diffFromReq(newJson);

0 commit comments

Comments
 (0)