mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-14 14:40:52 +00:00
test(guards): strip comments the way the compiler reads them, in every guard
Forty-two guard files carried their own comment stripper, block comments first in one non-greedy pass, so a /* inside a line comment opened a block that hid the code after it, and the stricter copies hid code behind a /* inside a string, which every e2e route glob has. They all read through the shared stripComments now, and it scans the way the parser does: strings, template literals and regular expressions keep every character, a comment inside a template expression goes, and line numbers are kept. A parity test strips every source file both ways, with the scanner and with the TypeScript parser, and fails on the first that differs. No guard reports anything new. Each one fails when the stripper returns nothing; three of them passed on an empty file and assert that the code they ban is read at all now.
This commit is contained in:
1 parent
584f799da3
commit
c0d387025d
44 files changed
+392
-341
No files matched your search
@@ -2,6 +2,7 @@ import { readdirSync, readFileSync, statSync } from "node:fs";
|
||||
import { join, relative } from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { stripComments } from "../../../__tests__/helpers/source-files";
|
||||
|
||||
/**
|
||||
* Every chart drawn in days is a door to its days, or it says why not.
|
||||
@@ -120,17 +121,6 @@ function walk(path: string): string[] {
|
||||
|
||||
const files = SCAN.flatMap(walk).filter((f) => /\.tsx?$/.test(f));
|
||||
const rel = (abs: string) => relative(ROOT, abs);
|
||||
/**
|
||||
* The code without its comments: a docblock that names `<HealthChart>` is
|
||||
* not a mount. Block comments (JSX ones included) go whole; a line comment
|
||||
* goes from `//` to the line end unless the slashes follow a colon (a URL).
|
||||
*/
|
||||
export function stripComments(text: string): string {
|
||||
return text
|
||||
.replace(/\/\*[\s\S]*?\*\//g, "")
|
||||
.replace(/(^|[^:"'`])\/\/.*$/gm, "$1");
|
||||
}
|
||||
|
||||
const read = new Map(
|
||||
files.map((f) => [rel(f), stripComments(readFileSync(f, "utf8"))]),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user