Skip to content

fix(string): keep a quoted identifier out of the comment scan - #345

Merged
tiagolauer merged 1 commit into
masterfrom
fix/287-quoted-identifier-comments
Aug 2, 2026
Merged

fix(string): keep a quoted identifier out of the comment scan#345
tiagolauer merged 1 commit into
masterfrom
fix/287-quoted-identifier-comments

Conversation

@tiagolauer

Copy link
Copy Markdown
Owner

Fixes #287.

The bug

// DB has a column literally named a--b
StrictRow<DB, 'select "a--b" from users'>
// QueryTypeError<'no FROM clause: cannot resolve column ""a"'>
// loose: { '"a': unknown }[]

Query<DB, `select "it's" from users`>
// the strict error text even leaked a non-literal ${string} into the message

The -- inside the quotes was taken as a line comment and ate the rest of the query; the ' opened a string-literal mask. Quoted identifiers are the documented escape hatch for special names, and these are exactly the names that need it.

The fix

StripCommentsAndMaskLiterals now recognizes the three identifier quotes and copies the body through verbatim — nothing inside a quoted name opens a comment or a literal. MaskQuotedIdentifiers already exists to protect ; from the same class of scan (#232); this extends that treatment to the pass that runs before it.

The structure of the scan changed with it, and it is the reason this is not expensive. Written as three more whole-string pattern matches per step, the fix measured 208,264 instantiations — over the 200,000 budget. Destructuring the leading character once and dispatching on it (Opener extends "'", Opener extends '-', …) makes every case a character comparison instead of a pattern match:

instantiations
master 192,486
fix, pattern-matched 208,264 (over budget)
fix, character-dispatched 184,737

So the fix lands 7,749 below master.

Verification

tests/quoted-identifier-punctuation.test-d.ts, eleven cases. Six red on master:

tests/quoted-identifier-punctuation.test-d.ts(28,3): error TS2344: Type 'false' does not satisfy the constraint 'true'.
tests/quoted-identifier-punctuation.test-d.ts(32,3): ... (36,3), (44,3), (50,3), (54,3)
  • "a--b" in both modes, "it's", "a/*b", "a$b", and one among ordinary columns
  • a real trailing -- comment and a real 'x' literal in the same query as a quoted name still work

Controls: plain "id", [weird-table] and `id` are unchanged.

tsc --noEmit clean, 192 runtime tests pass.

    // DB has a column literally named a--b
    StrictRow<DB, 'select "a--b" from users'>
    // was QueryTypeError<'no FROM clause: cannot resolve column ""a"'>

StripCommentsAndMaskLiterals had no double-quote awareness, so the `--`
inside a quoted name was taken as a line comment and swallowed the rest of
the query, and the `'` in `"it's"` opened a string-literal mask. Quoted
identifiers are the library's documented escape hatch for special names, and
these are exactly the names that need it. MaskQuotedIdentifiers already
exists to protect `;` from the same class of scan; this extends that
treatment to the comment/literal pass, which runs first.

The scan now destructures the leading character once and dispatches on it,
instead of matching the whole string against one pattern per case. Written
the original way, the three new quote cases measured +15,778 instantiations;
keyed on the character the step already read, the whole scan comes in at
184,737 against master's 192,486 - 7,749 cheaper than before the fix.

Fixes #287

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@tiagolauer
tiagolauer force-pushed the fix/287-quoted-identifier-comments branch from 9aac309 to 80838cc Compare August 2, 2026 13:04
@tiagolauer
tiagolauer merged commit df4e480 into master Aug 2, 2026
12 checks passed
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.

Quoted identifiers containing -- or ' break the parse

1 participant