|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { DEFAULT_CONFIG } from "../src/config"; |
| 6 | +import { analyzeRepository } from "../src/core/engine"; |
| 7 | +import { Registry } from "../src/core/registry"; |
| 8 | +import { createDefaultRegistry } from "../src/default-registry"; |
| 9 | +import { genericRecordCastsRule } from "../src/rules/generic-record-casts"; |
| 10 | + |
| 11 | +const tempDirs: string[] = []; |
| 12 | + |
| 13 | +afterEach(async () => { |
| 14 | + await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true }))); |
| 15 | +}); |
| 16 | + |
| 17 | +async function writeRepoFiles(rootDir: string, files: Record<string, string>): Promise<void> { |
| 18 | + for (const [relativePath, content] of Object.entries(files)) { |
| 19 | + const absolutePath = path.join(rootDir, relativePath); |
| 20 | + await mkdir(path.dirname(absolutePath), { recursive: true }); |
| 21 | + await writeFile(absolutePath, content); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +async function createTempRepo(files: Record<string, string>): Promise<string> { |
| 26 | + const rootDir = await mkdtemp(path.join(os.tmpdir(), "slop-scan-generic-record-casts-")); |
| 27 | + tempDirs.push(rootDir); |
| 28 | + await writeRepoFiles(rootDir, files); |
| 29 | + return rootDir; |
| 30 | +} |
| 31 | + |
| 32 | +function createCandidateRegistry(): Registry { |
| 33 | + const baseRegistry = createDefaultRegistry(); |
| 34 | + const registry = new Registry(); |
| 35 | + |
| 36 | + for (const language of baseRegistry.getLanguages()) { |
| 37 | + registry.registerLanguage(language); |
| 38 | + } |
| 39 | + |
| 40 | + for (const provider of baseRegistry.getFactProviders()) { |
| 41 | + registry.registerFactProvider(provider); |
| 42 | + } |
| 43 | + |
| 44 | + registry.registerRule(genericRecordCastsRule); |
| 45 | + return registry; |
| 46 | +} |
| 47 | + |
| 48 | +describe("generic-record-casts rule", () => { |
| 49 | + test("flags generic Record<string, unknown> casts on vague bag variables", async () => { |
| 50 | + const rootDir = await createTempRepo({ |
| 51 | + "src/slop.ts": [ |
| 52 | + "export function parseData(raw: string, response: unknown, value: unknown) {", |
| 53 | + " const parsed = JSON.parse(raw) as Record<string, unknown>;", |
| 54 | + " const payload = response as Record<string, unknown>;", |
| 55 | + " const data = value as Record<string, unknown>;", |
| 56 | + " return { parsed, payload, data };", |
| 57 | + "}", |
| 58 | + ].join("\n"), |
| 59 | + "src/legit.ts": [ |
| 60 | + "type UserConfig = { token: string };", |
| 61 | + "", |
| 62 | + "export function parseConfig(raw: string) {", |
| 63 | + " return JSON.parse(raw) as UserConfig;", |
| 64 | + "}", |
| 65 | + ].join("\n"), |
| 66 | + }); |
| 67 | + |
| 68 | + const result = await analyzeRepository(rootDir, DEFAULT_CONFIG, createCandidateRegistry()); |
| 69 | + const finding = result.findings.find( |
| 70 | + (nextFinding) => nextFinding.ruleId === "types.generic-record-casts", |
| 71 | + ); |
| 72 | + |
| 73 | + expect(finding).toBeDefined(); |
| 74 | + expect(finding?.path).toBe("src/slop.ts"); |
| 75 | + expect(finding?.evidence).toEqual([ |
| 76 | + "line 2: json-parse-record-cast", |
| 77 | + "line 3: record-string-unknown-cast", |
| 78 | + "line 4: record-string-unknown-cast", |
| 79 | + ]); |
| 80 | + expect(finding?.locations).toEqual([ |
| 81 | + { path: "src/slop.ts", line: 2 }, |
| 82 | + { path: "src/slop.ts", line: 3 }, |
| 83 | + { path: "src/slop.ts", line: 4 }, |
| 84 | + ]); |
| 85 | + expect(result.findings).toHaveLength(1); |
| 86 | + }); |
| 87 | + |
| 88 | + test("ignores giant bundled files that would otherwise create vendor noise", async () => { |
| 89 | + const hugeFile = [ |
| 90 | + ...Array.from({ length: 5001 }, (_, index) => `export const filler${index} = ${index};`), |
| 91 | + "const parsed = JSON.parse(raw) as Record<string, unknown>;", |
| 92 | + "", |
| 93 | + ].join("\n"); |
| 94 | + |
| 95 | + const rootDir = await createTempRepo({ |
| 96 | + "src/bundle.ts": hugeFile, |
| 97 | + }); |
| 98 | + |
| 99 | + const result = await analyzeRepository(rootDir, DEFAULT_CONFIG, createCandidateRegistry()); |
| 100 | + |
| 101 | + expect(result.findings).toHaveLength(0); |
| 102 | + }); |
| 103 | +}); |
0 commit comments