[Fix] parse: a backslash inside single quotes must not escape the closing quote - #26
Merged
Merged
Conversation
ljharb
force-pushed
the
fix/parse-single-quote-backslash
branch
from
July 7, 2026 01:37
08e6d0c to
5d460a3
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #26 +/- ##
=======================================
Coverage 99.40% 99.40%
=======================================
Files 3 3
Lines 168 168
Branches 48 48
=======================================
Hits 167 167
Misses 1 1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ljharb
approved these changes
Jul 7, 2026
parse: a backslash inside single quotes must not escape the closing quote
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In a POSIX shell a single-quoted string is fully literal: a backslash inside single quotes is just a backslash and never escapes the closing quote.
parse's scanner already follows this (rule 1 in the comment inparse.js; the single-quote branch only doesout += c), but the chunker regex that splits the input into tokens does not.The single-quote matcher is:
The
\'alternative lets the regex treat a backslash-then-quote as an escaped quote inside a single-quoted token, so it walks past the real closing quote and keeps consuming the trailing whitespace and the next token. Two single-quoted tokens that end in a backslash get merged into one:This also breaks round-tripping through
quote. Since #20,quotewraps a backslash in single quotes without escaping it (quote(["\\"]) === "'\\'"), which is correct, butparsecannot read its own output back:The fix drops the
\'alternative so the single-quote matcher ends at the first closing quote, matching the scanner and POSIX:The normal way to place a quote next to single-quoted text,
'\''(close, escaped quote, reopen), is unaffected:parse("'\\'\\''")still returns["\\'"].Added three assertions in
test/parse.js(the two-token case, thequote/parseround-trip, and the'\''preservation case). The full suite passes. Reverting only theparse.jschange makes the two behavioral assertions fail.