Skip to content

Commit

Permalink
chore: Improve tests and typing & package d.ts files. (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnturner committed May 20, 2022
1 parent 87fdd42 commit a622d28
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 18 deletions.
8 changes: 4 additions & 4 deletions .c8rc.json
@@ -1,6 +1,6 @@
{
"statements": "78",
"branches": "78",
"functions": "82",
"lines": "78"
"statements": "80",
"branches": "84",
"functions": "88",
"lines": "80"
}
10 changes: 3 additions & 7 deletions lib/allowlist.ts
@@ -1,5 +1,5 @@
import type { GitHubAdvisoryId } from "audit-types";
import { isGitHubAdvisoryId } from "./common";
import { deduplicate, isGitHubAdvisoryId } from "./common";
import type { AuditCiPreprocessedConfig } from "./config";

class Allowlist {
Expand Down Expand Up @@ -37,12 +37,8 @@ class Allowlist {
config: Pick<AuditCiPreprocessedConfig, "allowlist">
) {
const { allowlist } = config;
// It's possible someone duplicated the inputs.
// The solution is to merge into one array, change to set, and back to array.
// This will remove duplicates.
const set = new Set(allowlist || []);
const input = [...set];
const allowlistObject = new Allowlist(input);
const deduplicatedAllowlist = deduplicate(allowlist || []);
const allowlistObject = new Allowlist(deduplicatedAllowlist);
return allowlistObject;
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/audit.ts
Expand Up @@ -15,7 +15,9 @@ const PARTIAL_RETRY_ERROR_MSG = {
yarn: "503 Service Unavailable",
};

function getAuditor(packageManager: "npm" | "yarn" | "pnpm") {
function getAuditor(
packageManager: "npm" | "yarn" | "pnpm"
): typeof yarnAuditer | typeof npmAuditer | typeof pnpmAuditer {
switch (packageManager) {
case "yarn":
return yarnAuditer;
Expand Down
8 changes: 6 additions & 2 deletions lib/common.ts
Expand Up @@ -186,8 +186,8 @@ export function matchString(template: string, string_: string) {
: template === string_;
}

export function isGitHubAdvisoryId(id: string): id is GitHubAdvisoryId {
return id.startsWith("GHSA");
export function isGitHubAdvisoryId(id: unknown): id is GitHubAdvisoryId {
return typeof id === "string" && id.startsWith("GHSA");
}

export function gitHubAdvisoryUrlToAdvisoryId(url: string): GitHubAdvisoryId {
Expand All @@ -199,3 +199,7 @@ export function gitHubAdvisoryIdToUrl<T extends string>(
): `https://github.com/advisories/${T}` {
return `https://github.com/advisories/${id}`;
}

export function deduplicate(array: readonly string[]) {
return [...new Set(array)];
}

0 comments on commit a622d28

Please sign in to comment.