diff --git a/src/__tests__/correlation-single-implementation-guard.test.ts b/src/__tests__/correlation-single-implementation-guard.test.ts index f9697ab84..708118b23 100644 --- a/src/__tests__/correlation-single-implementation-guard.test.ts +++ b/src/__tests__/correlation-single-implementation-guard.test.ts @@ -21,7 +21,7 @@ import { readFileSync } from "node:fs"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; -import { walkSourceFiles } from "./helpers/source-files"; +import { stripComments, walkSourceFiles } from "./helpers/source-files"; const SRC = join(process.cwd(), "src"); @@ -34,10 +34,6 @@ const PEARSON_DEFINITION = /(?:function\s+|const\s+)\w*(?:pearson|correlationCoefficient)\w*\s*(?:=|\()/i; const DELETED = /(? diff --git a/src/__tests__/dose-ledger-single-source-guard.test.ts b/src/__tests__/dose-ledger-single-source-guard.test.ts index aff5974b4..071e24536 100644 --- a/src/__tests__/dose-ledger-single-source-guard.test.ts +++ b/src/__tests__/dose-ledger-single-source-guard.test.ts @@ -16,7 +16,7 @@ import { readFileSync } from "node:fs"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; -import { walkSourceFiles } from "./helpers/source-files"; +import { stripComments, walkSourceFiles } from "./helpers/source-files"; const SRC = join(process.cwd(), "src"); @@ -31,10 +31,6 @@ const CALLERS: Record = { const CALL = /\breconstructDoseHistory\s*\(/; -function stripComments(text: string): string { - return text.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/.*$/gm, ""); -} - describe("one compliance ledger", () => { it("the dose rows are built only where this file says", () => { const callers = walkSourceFiles(SRC, { floor: 3000 }) diff --git a/src/__tests__/insights-cached-text-access-guard.test.ts b/src/__tests__/insights-cached-text-access-guard.test.ts index e9121158b..f5cff3900 100644 --- a/src/__tests__/insights-cached-text-access-guard.test.ts +++ b/src/__tests__/insights-cached-text-access-guard.test.ts @@ -19,7 +19,7 @@ import { join } from "node:path"; import { describe, expect, it } from "vitest"; -import { walkSourceFiles } from "./helpers/source-files"; +import { stripComments, walkSourceFiles } from "./helpers/source-files"; const SRC = join(process.cwd(), "src"); @@ -37,24 +37,15 @@ const ALLOWED: Readonly> = { "fills that input field from openInsightsCachedText", }; -/** The legacy name, not its sealed sibling, outside comment lines. */ +/** The legacy name, not its sealed sibling, outside comments. */ const LEGACY_NAME = /\binsightsCachedText\b(?!Encrypted)/; -function codeLines(source: string): string[] { - return source.split("\n").filter((line) => { - const t = line.trim(); - return !(t.startsWith("*") || t.startsWith("//") || t.startsWith("/*")); - }); -} - function filesSpellingTheLegacyName(): string[] { return walkSourceFiles(SRC, { floor: 2000 }) .filter((f) => !f.startsWith("generated/")) .filter((f) => !f.includes("__tests__/") && !/\.test\.tsx?$/.test(f)) .filter((f) => - codeLines(readFileSync(join(SRC, f), "utf8")).some((l) => - LEGACY_NAME.test(l), - ), + LEGACY_NAME.test(stripComments(readFileSync(join(SRC, f), "utf8"))), ); } diff --git a/src/__tests__/latest-per-type-read-guard.test.ts b/src/__tests__/latest-per-type-read-guard.test.ts index a0e7eb796..21f38c950 100644 --- a/src/__tests__/latest-per-type-read-guard.test.ts +++ b/src/__tests__/latest-per-type-read-guard.test.ts @@ -21,17 +21,15 @@ import { readFileSync } from "node:fs"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; -import { walkSourceFiles } from "./helpers/source-files"; +import { stripComments, walkSourceFiles } from "./helpers/source-files"; + +/** The shared comment strip, plus SQL line comments inside template strings. */ +function stripCode(text: string): string { + return stripComments(text).replace(/^\s*--.*$/gm, ""); +} const SRC = join(process.cwd(), "src"); -function stripComments(text: string): string { - return text - .replace(/\/\*[\s\S]*?\*\//g, "") - .replace(/^\s*\/\/.*$/gm, "") - .replace(/^\s*--.*$/gm, ""); -} - /** `DISTINCT ON (type)` or `DISTINCT ON (m."type")`, the type alone. */ const LATEST_PER_TYPE = /DISTINCT\s+ON\s*\(\s*(?:\w+\s*\.\s*)?"?type"?\s*\)/i; @@ -64,18 +62,14 @@ describe("latest-per-type reads", () => { it("no production read keeps one row per type with DISTINCT ON", () => { const offenders = productionFiles().filter((file) => - LATEST_PER_TYPE.test( - stripComments(readFileSync(join(SRC, file), "utf8")), - ), + LATEST_PER_TYPE.test(stripCode(readFileSync(join(SRC, file), "utf8"))), ); expect(offenders).toEqual([]); }); it("the summaries slice is built only behind its cache cell", () => { const callers = productionFiles().filter((file) => - SLICE_BUILDER_CALL.test( - stripComments(readFileSync(join(SRC, file), "utf8")), - ), + SLICE_BUILDER_CALL.test(stripCode(readFileSync(join(SRC, file), "utf8"))), ); expect(callers).toEqual(["lib/analytics/summaries-slice.ts"]); }); diff --git a/src/__tests__/naive-timestamp-zone-shift-guard.test.ts b/src/__tests__/naive-timestamp-zone-shift-guard.test.ts index cfb111b5a..1ae978874 100644 --- a/src/__tests__/naive-timestamp-zone-shift-guard.test.ts +++ b/src/__tests__/naive-timestamp-zone-shift-guard.test.ts @@ -21,7 +21,12 @@ import { readFileSync } from "node:fs"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; -import { walkSourceFiles } from "./helpers/source-files"; +import { stripComments, walkSourceFiles } from "./helpers/source-files"; + +/** The shared comment strip, plus SQL line comments inside template strings. */ +function stripCode(text: string): string { + return stripComments(text).replace(/^\s*--.*$/gm, ""); +} const SRC = join(process.cwd(), "src"); @@ -31,13 +36,6 @@ const SINGLE_SHIFT = const DOUBLE_SHIFT = /AT\s+TIME\s+ZONE\s+'UTC'\s*\)?\s*AT\s+TIME\s+ZONE\s+(?:\$\{|\$\d)/i; -function stripComments(text: string): string { - return text - .replace(/\/\*[\s\S]*?\*\//g, "") - .replace(/^\s*\/\/.*$/gm, "") - .replace(/^\s*--.*$/gm, ""); -} - describe("naive timestamp zone shifts", () => { it("the matcher fires on the single shift and spares the double one", () => { expect( @@ -62,7 +60,7 @@ describe("naive timestamp zone shifts", () => { !/\.test\.tsx?$/.test(file), ); const code = (file: string) => - stripComments(readFileSync(join(SRC, file), "utf8")); + stripCode(readFileSync(join(SRC, file), "utf8")); // The reads that shift correctly, so a sweep that reads nothing fails. const doubleShifts = files.filter((file) => DOUBLE_SHIFT.test(code(file))); expect(doubleShifts.length).toBeGreaterThanOrEqual(5);