test(fuzz): CRLF and CR files mean the same as LF (line-endings invariant) #308

Merged
abiwab merged 2 commits from test/line-endings into dev/1.6 2026-11-29 11:15:42 +01:00
Owner

Lot 2 of .notes/v1.6/plan-finitions-techniques-1.6.md.

Fuzz invariant line-endings (fuzz/src/invariants.ts): every corpus/mutated input is compared with its CRLF and CR versions: same parse outcome, same compilation (the format-preserves-meaning comparison, no new rule in normalize.ts), same formatGram output, same lint codes at spans that correspond through mapSpan(), and preferred fixes that give the same text with the file's own line endings. One comparison rule, commented: an offset between the \r and \n of a line break counts as after it (a parse error on a line break covers its first character).

Bugs it found, fixed in @gram-lang/modules/lint (only on dev/1.6, so no changeset):

  • A lone \r (CR) was never treated as a line break by the rules (indexOf("\n") everywhere): MERGED_STEP_LINE, DEPRECATED_FRONTMATTER_KEY… were missed or misplaced. lintDocument now hands the rules the text with lone \r turned into \n (same length, so offsets don't move).
  • Fixes always wrote \n: a fix that inserts a line (MISSING_TITLE, MERGED_STEP_LINE) mixed line endings in a CRLF/CR file. They now write the line ending of the line break just before them (so a file that mixes them doesn't get \r + \n joined into one break).
  • The frontmatter fallback in gram-tokens.ts only matched ---\n…\n---: in CRLF, @ inside an unreadable frontmatter became a lost ingredient.
  • buildLineIndex() now counts lines after \r\n, \n and a lone \r, like the LSP.

Tests: packages/modules/tests/lint/line-endings.test.ts: CRLF and CR for DEPRECATED_FRONTMATTER_KEY (rename and whole-line removal, no orphan \r), SPACED_COMPOSITE, INVALID_DURATION, MERGED_STEP_LINE, the inserted MISSING_TITLE line, the two fuzz regressions, buildLineIndex. Sources built in code, no CRLF file committed.

Checks: modules/cli/language-server/parser/format/fuzz tests green, typecheck, lint, conformance. Two 50k fuzz sessions: everything found was fixed here, except two pre-existing fix-idempotent violations on dev/1.6, unrelated to line endings (not hit by the seeded test):

  • @flour ^&flour: the REFERENCE_TO_INGREDIENT fix writes ^@&flour and the diagnostic comes back.
  • ".\n##\n[e]": the MERGED_STEP_LINE fix breaks the parse.
Lot 2 of `.notes/v1.6/plan-finitions-techniques-1.6.md`. **Fuzz invariant `line-endings`** (`fuzz/src/invariants.ts`): every corpus/mutated input is compared with its CRLF and CR versions: same parse outcome, same compilation (the `format-preserves-meaning` comparison, no new rule in `normalize.ts`), same `formatGram` output, same lint codes at spans that correspond through `mapSpan()`, and preferred fixes that give the same text with the file's own line endings. One comparison rule, commented: an offset between the `\r` and `\n` of a line break counts as after it (a parse error on a line break covers its first character). **Bugs it found, fixed in `@gram-lang/modules/lint`** (only on `dev/1.6`, so no changeset): - A lone `\r` (CR) was never treated as a line break by the rules (`indexOf("\n")` everywhere): `MERGED_STEP_LINE`, `DEPRECATED_FRONTMATTER_KEY`… were missed or misplaced. `lintDocument` now hands the rules the text with lone `\r` turned into `\n` (same length, so offsets don't move). - Fixes always wrote `\n`: a fix that inserts a line (`MISSING_TITLE`, `MERGED_STEP_LINE`) mixed line endings in a CRLF/CR file. They now write the line ending of the line break just before them (so a file that mixes them doesn't get `\r` + `\n` joined into one break). - The frontmatter fallback in `gram-tokens.ts` only matched `---\n…\n---`: in CRLF, `@` inside an unreadable frontmatter became a lost ingredient. - `buildLineIndex()` now counts lines after `\r\n`, `\n` and a lone `\r`, like the LSP. **Tests**: `packages/modules/tests/lint/line-endings.test.ts`: CRLF and CR for `DEPRECATED_FRONTMATTER_KEY` (rename and whole-line removal, no orphan `\r`), `SPACED_COMPOSITE`, `INVALID_DURATION`, `MERGED_STEP_LINE`, the inserted `MISSING_TITLE` line, the two fuzz regressions, `buildLineIndex`. Sources built in code, no CRLF file committed. Checks: modules/cli/language-server/parser/format/fuzz tests green, typecheck, lint, conformance. Two 50k fuzz sessions: everything found was fixed here, except two **pre-existing** `fix-idempotent` violations on `dev/1.6`, unrelated to line endings (not hit by the seeded test): - `@flour ^&flour`: the `REFERENCE_TO_INGREDIENT` fix writes `^@&flour` and the diagnostic comes back. - `".\n##\n[e]"`: the `MERGED_STEP_LINE` fix breaks the parse.
abiwab merged commit ce9f9eedf8 into dev/1.6 2026-11-29 11:15:42 +01:00
Sign in to join this conversation.
No description provided.