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

Commit d54bbc2

Browse files
committed
Extra types
1 parent 840dfb5 commit d54bbc2

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>,
@@ -240,7 +246,7 @@ export async function deriveStateForPR(
240246
review && !authorNotBot(review) && (
241247
!latest?.submittedAt || review.submittedAt && new Date (review.submittedAt) > new Date(latest.submittedAt))
242248
? review : latest, null)?.commit?.oid,
243-
};
249+
} as const;
244250
const pkgInfoEtc = await getPackageInfosEtc(
245251
noNullish(prInfo.files?.nodes).map(f => f.path).sort(),
246252
refs, fetchFile, async name => await getDownloads(name, lastPushDate));
@@ -335,11 +341,11 @@ function getLastMaintainerBlessingDate(timelineItems: PR_repository_pullRequest_
335341
}
336342

337343
async function getPackageInfosEtc(
338-
paths: string[], refs: { [ref: string]: string }, fetchFile: typeof defaultFetchFile, getDownloads: typeof getMonthlyDownloadCount
344+
paths: string[], refs: Refs, fetchFile: typeof defaultFetchFile, getDownloads: typeof getMonthlyDownloadCount
339345
): Promise<{pkgInfo: PackageInfo[], popularityLevel: PopularityLevel} | Error> {
340346
const infos = new Map<string|null, FileInfo[]>();
341347
for (const path of paths) {
342-
const [pkg, fileInfo] = await categorizeFile(path, async (ref: string) => fetchFile(`${refs[ref]}:${path}`));
348+
const [pkg, fileInfo] = await categorizeFile(path, async ref => fetchFile(`${refs[ref]}:${path}`));
343349
if (!infos.has(pkg)) infos.set(pkg, []);
344350
infos.get(pkg)!.push(fileInfo);
345351
}
@@ -374,7 +380,9 @@ async function getPackageInfosEtc(
374380
return { pkgInfo: result, popularityLevel: downloadsToPopularityLevel(maxDownloads) };
375381
}
376382

377-
async function categorizeFile(path: string, getContents: (ref: string) => Promise<string | undefined>): Promise<[string|null, FileInfo]> {
383+
type GetContents = (ref: keyof Refs) => Promise<string | undefined>;
384+
385+
async function categorizeFile(path: string, getContents: GetContents): Promise<[string|null, FileInfo]> {
378386
// https://regex101.com/r/eFvtrz/1
379387
const match = /^types\/(.*?)\/.*?[^\/](?:\.(d\.ts|tsx?|md))?$/.exec(path);
380388
if (!match) return [null, { path, kind: "infrastructure" }];
@@ -391,8 +399,8 @@ async function categorizeFile(path: string, getContents: (ref: string) => Promis
391399
}
392400

393401
interface ConfigSuspicious {
394-
(path: string, getContents: (ref: string) => Promise<string | undefined>): Promise<{ suspect: string, sugestion?: Suggestion } | undefined>;
395-
[basename: string]: (newText: string, getContents: (ref: string) => Promise<string | undefined>) => Promise<{ suspect: string, suggestion?: Suggestion } | undefined>;
402+
(path: string, getContents: GetContents): Promise<{ suspect: string, sugestion?: Suggestion } | undefined>;
403+
[basename: string]: (newText: string, getContents: GetContents) => Promise<{ suspect: string, suggestion?: Suggestion } | undefined>;
396404
}
397405
const configSuspicious = <ConfigSuspicious>(async (path, getContents) => {
398406
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) { return { suspect: "couldn't parse json" }; }
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 = requiredFormUrl
462470
? `[the required form](${requiredFormUrl})`
463471
: "the required form";

0 commit comments

Comments
 (0)