Skip to content

test(parse-fast): cover the size-dependent and export-prefix branches - #1075

Merged
motdotla merged 1 commit into
motdotla:masterfrom
kory-kaai:test/fast-parser-branch-coverage
Oct 16, 2026
Merged

motdotla merged 1 commit into
motdotla:masterfrom
kory-kaai:test/fast-parser-branch-coverage

Conversation

@kory-kaai

Copy link
Copy Markdown
Contributor

parseFast picks how to locate the comment marker in an unquoted value based on the size of the source:

if (len < 4096) {
  hash = str.indexOf('#', rawStart)
  if (hash === -1 || hash > nl) hash = nl
} else {
  hash = rawStart
  while (hash < nl && str.charCodeAt(hash) !== 35) hash++
}

Nothing in the suite reaches the second path, because every fixture and inline case sits well under 4096 characters. Running the suite with coverage enabled showed lines 209-211 of lib/main.js never executing, plus two neighbouring branches: the export prefix when no whitespace follows it (117-118), and the separator lookahead when whitespace runs to the end of the input (139).

Given how many of the recent fixes have been fast-parser consistency bugs, an untested path that only activates on larger .env files seemed worth closing.

What this adds

Three tests in tests/test-parse-fast.js, no source changes:

  • Comment scanning on either side of 4096 characters. Nine unquoted values are parsed twice, once as a short source and once padded with trailing comment lines so the source crosses the threshold while the assignments stay identical. Padding cannot change the result, so both paths have to agree.
  • export with no whitespace after it. export=1, exports=1 and exportFOO=bar keep export inside the key, while export: 1 still parses and export:1 is ignored because : requires whitespace.
  • A key whose whitespace runs to the end of the input. A , A\t and export A are ignored without swallowing a neighbouring assignment.

Following the convention already in the file, every case asserts that the fast parser matches the classic one and also pins the expected value, so the two cannot pass by both being wrong.

Verification

lib/main.js goes from 98.77% statements and 98.24% branches to 100% on both.

npm test passes at 1402 of 1403 with the one pre-existing skip, which covers the build, standard, the d.ts check and the TAP suite.

To confirm the tests actually bite, I reverted the behaviour each one describes:

Change to lib/main.js Result
large-input scan never finds # 8 failures
large-input scan ignores the line boundary 10 failures
treat export as a prefix without following whitespace 8 failures
pretend input continues when whitespace hits end of input 10 failures
force the manual scan for every input still passes

That last row is the point rather than a gap: making the manual scan handle every size is unobservable, which is exactly the equivalence these tests assert.

For what it is worth, I also ran a differential fuzzer over roughly 630k generated sources comparing parse against parse(src, { fast: true }) and found no disagreements, so I have no bug to report here. The fast parser looks solid; this only closes the blind spot in the suite.

Unquoted values locate the comment marker with indexOf below 4096
characters and with a manual scan at or above it, and nothing exercised
the larger path. Two neighbouring branches were untested as well:
'export' with no whitespace after it, and a key whose whitespace runs
to the end of the input.

Every case asserts the fast parser matches the classic one and also
pins the expected value, so the two cannot agree by both being wrong.
Brings lib/main.js to full statement and branch coverage.
@motdotla
motdotla merged commit 6d6e6c5 into motdotla:master Oct 16, 2026
8 checks passed
@motdotla

Copy link
Copy Markdown
Owner

nice contribution! thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants