fix(parse): keep the alias when an expression continues past a group - #339
Merged
Conversation
Query<DB, 'select (id + 1) * 2 as x from users'>
// was { '* 2 as x': unknown }[]
The paren-entry branch returned whatever followed the closing paren as the
alias. That holds for `(id + 1) as next`, and not at all for an expression
that carries on past the group, where the tail is part of the expression and
the alias sits at the end of it.
When the tail is operator-shaped, the entry is now split at its top-level
AS, the same signal the rest of the parser uses; the bare-entry branch has
guarded this case with IsOperatorExpression all along. Without an AS there
is nothing to tell an alias from the rest of the expression, so the whole
entry becomes the key - the answer the bare-entry branch gives too, and
better than naming a fragment of it.
The value stays `unknown`: an arithmetic expression is not typed. Only the
key was wrong.
Fixes #290
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
tiagolauer
force-pushed
the
fix/290-paren-expression-alias
branch
from
August 2, 2026 13:04
bd6a30e to
4f31ab9
Compare
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.
Fixes #290.
The bug
Expected
{ x: unknown }[]— the value beingunknownis fine, the key is the bug.The fix
The paren-entry branch assumed whatever follows the closing paren is an alias and returned
[Trim<After>, Expr]directly. The bare-entry branch guards the same situation withIsOperatorExpression; this branch had no such check.When the tail is operator-shaped, the entry is now split at its top-level
AS— the same signal the rest of the parser uses — so(id + 1) * 2 as xgives aliasxand expression(id + 1) * 2.Without an
ASthere is nothing to tell an alias from the rest of the expression, so the whole entry becomes the key ('(id + 1) * 2 x'). That is the answer the bare-entry branch gives for the same shape, and better than the old'* 2 x', which named a fragment. That case is pinned by a test.Verification
tests/paren-expression-alias.test-d.ts, eight cases. Four red on master:* 2 as x,/ 2 as average, the no-AS form, and one among ordinary columnsControls:
(id + 1) as next,(id + 1) next, a scalar subquery with an alias, andcount(*) as totalare all unchanged.tsc --noEmitclean, budget 192,636 (+150).