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

Commit 324baf8

Browse files
committed
Extra types
1 parent 3c5ea60 commit 324baf8

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
@@ -197,6 +197,12 @@ export async function queryPRInfo(prNumber: number) {
197197
}
198198
}
199199

200+
interface Refs {
201+
readonly head: string;
202+
readonly master: "master";
203+
readonly latestSuggestions: string;
204+
}
205+
200206
// The GQL response => Useful data for us
201207
export async function deriveStateForPR(
202208
info: ApolloQueryResult<PRQueryResult>,
@@ -233,7 +239,7 @@ export async function deriveStateForPR(
233239
review && !authorNotBot(review) && (
234240
!latest?.submittedAt || review.submittedAt && new Date (review.submittedAt) > new Date(latest.submittedAt))
235241
? review : latest, null)?.commit?.oid,
236-
};
242+
} as const;
237243
const pkgInfoEtc = await getPackageInfosEtc(
238244
noNullish(prInfo.files?.nodes).map(f => f.path).sort(),
239245
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
}
@@ -369,7 +375,9 @@ async function getPackageInfosEtc(
369375
return { pkgInfo: result, popularityLevel: downloadsToPopularityLevel(maxDownloads) };
370376
}
371377

372-
async function categorizeFile(path: string, getContents: (ref: string) => Promise<string | undefined>): Promise<[string|null, FileInfo]> {
378+
type GetContents = (ref: keyof Refs) => Promise<string | undefined>;
379+
380+
async function categorizeFile(path: string, getContents: GetContents): Promise<[string|null, FileInfo]> {
373381
// https://regex101.com/r/eFvtrz/1
374382
const match = /^types\/(.*?)\/.*?[^\/](?:\.(d\.ts|tsx?|md))?$/.exec(path);
375383
if (!match) return [null, { path, kind: "infrastructure" }];
@@ -385,8 +393,8 @@ async function categorizeFile(path: string, getContents: (ref: string) => Promis
385393
}
386394

387395
interface ConfigSuspicious {
388-
(path: string, getContents: (ref: string) => Promise<string | undefined>): Promise<{ suspect: string, sugestion?: Suggestion } | undefined>;
389-
[basename: string]: (newText: string, getContents: (ref: string) => Promise<string | undefined>) => Promise<{ suspect: string, suggestion?: Suggestion } | undefined>;
396+
(path: string, getContents: GetContents): Promise<{ suspect: string, sugestion?: Suggestion } | undefined>;
397+
[basename: string]: (newText: string, getContents: GetContents) => Promise<{ suspect: string, suggestion?: Suggestion } | undefined>;
390398
};
391399
const configSuspicious = <ConfigSuspicious>(async (path, getContents) => {
392400
const basename = path.replace(/.*\//, "");
@@ -429,7 +437,7 @@ configSuspicious["tsconfig.json"] = makeJsonCheckerFromCore(
429437
// to it, ignoring some keys (JSON Patch paths). The ignored properties are in most cases checked
430438
// elsewhere (dtslint), and in some cases they are irrelevant.
431439
function makeJsonCheckerFromCore(requiredForm: any, ignoredKeys: string[]) {
432-
return async (newText: string, getContents: (ref: string) => Promise<string | undefined>) => {
440+
return async (newText: string, getContents: GetContents) => {
433441
let suggestion: any;
434442
try { suggestion = JSON.parse(newText); } catch (e) { return { suspect: "couldn't parse json" }; }
435443
const newJson = jsonDiff.deepClone(suggestion);
@@ -450,7 +458,7 @@ function makeJsonCheckerFromCore(requiredForm: any, ignoredKeys: string[]) {
450458
};
451459

452460
// Apply any preexisting diffs to towardsIt
453-
async function ignoreExistingDiffs(ref: string) {
461+
async function ignoreExistingDiffs(ref: keyof Refs) {
454462
const diffFromReq = (json: any) => jsonDiff.compare(towardsIt, json);
455463
const newDiff = diffFromReq(newJson);
456464
if (newDiff.length === 0) return undefined;

0 commit comments

Comments
 (0)