Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/scanner/src/__tests__/scan-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,16 @@ describe("scanFiles()", () => {
const after = readFileRecord(projectId, "src/x.ts")!;
expect(after.candidates.length).toBe(candCountBefore);
});

it("throws on unknown matcher slugs", async () => {
const { root, projectId } = makeProject({ "a.ts": "x\n" });
await expect(
scanFiles({
projectId,
root,
filePaths: ["a.ts"],
matcherSlugs: ["xss", "does-not-exist", "also-fake"],
}),
).rejects.toThrow(/Unknown matcher slugs: does-not-exist, also-fake/);
});
});
22 changes: 22 additions & 0 deletions packages/scanner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,17 @@ export async function scan(params: {
languageStats: LanguageStats[];
}> {
const registry = buildMergedRegistry();

if (params.matcherSlugs) {
const unknown = registry.unknownSlugs(params.matcherSlugs);
if (unknown.length > 0) {
throw new Error(
`Unknown matcher slug${unknown.length > 1 ? "s" : ""}: ${unknown.join(", ")}.\n` +
` Available matchers: ${registry.slugs().sort().join(", ")}`,
);
}
}

const allSelected = params.matcherSlugs
? registry.getBySlugs(params.matcherSlugs)
: registry.getAll();
Expand Down Expand Up @@ -550,6 +561,17 @@ export async function scanFiles(params: {
skippedMatchers: string[];
}> {
const registry = buildMergedRegistry();

if (params.matcherSlugs) {
const unknown = registry.unknownSlugs(params.matcherSlugs);
if (unknown.length > 0) {
throw new Error(
`Unknown matcher slug${unknown.length > 1 ? "s" : ""}: ${unknown.join(", ")}.\n` +
` Available matchers: ${registry.slugs().sort().join(", ")}`,
);
}
}

const allSelected = params.matcherSlugs
? registry.getBySlugs(params.matcherSlugs)
: registry.getAll();
Expand Down
5 changes: 5 additions & 0 deletions packages/scanner/src/matcher-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export class MatcherRegistry {
.filter((m): m is MatcherPlugin => m !== undefined);
}

/** Returns slug strings from `requested` that don't exist in the registry. */
unknownSlugs(requested: string[]): string[] {
return requested.filter((s) => !this.matchers.has(s));
}

slugs(): string[] {
return Array.from(this.matchers.keys());
}
Expand Down