fix(plugin): scope diagnostics to the first branch of a set operation - #331
Merged
Conversation
select id from users union select id from posts
^^ ambiguous column: id
findSources regex-scans every from/join in the whole statement into one
scope, and the select-list check then counts the first SELECT's columns
against that pooled list. UNION branches have to be column-compatible, so
sharing a name is the normal case - this put a squiggle on nearly every
UNION, including the shape type-locked in tests/union.test-d.ts, which the
core types cleanly.
Diagnostics now stop at the first top-level set operator. That matches what
the core does (the row shape comes from the first branch) and the plugin's
own policy: later branches are not reported on, instead of being reported
wrongly. Truncating rather than rescoping keeps every offset before the cut
aligned with the source, so no span arithmetic changes.
The scan is paren-depth aware, so a union inside a subquery does not cut the
statement short.
Fixes #294
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
tiagolauer
force-pushed
the
fix/294-plugin-union-scope
branch
from
August 2, 2026 13:03
caa18e0 to
a2472d2
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 #294.
The bug
Since UNION branches must be column-compatible, sharing column names is the normal case — which makes this a squiggle on nearly every UNION. The shape type-locked in
tests/union.test-d.tsreproduces it identically, and the core types the query cleanly (first-branch shape, per the README).The fix
findSourcesregex-scans everyfrom/joinin the whole statement into one scope, and the select-list check counts the first SELECT's columns against that pooled scope, reporting anything found twice as ambiguous.The issue offers per-branch scopes or skipping UNION statements entirely. This does the narrower thing that keeps diagnostics working: stop at the first top-level set operator. The first branch is exactly the branch the core takes the row shape from, so what is diagnosed matches what is typed, and later branches fall under the plugin's stated "not reported on, instead of reported wrongly" policy.
Truncating rather than rescoping matters for correctness of the spans: every offset before the cut is unchanged, so no position arithmetic in this file had to move.
The scan is paren-depth aware, so a
unioninside a subquery does not cut the statement short.Verification
ts-plugin/tests/diagnostics.test.ts, five new cases (two red on master —PASS (43) FAIL (2)with the src change stashed):select id from users union select id from postsreports nothingunion all,intersect,exceptunknown column: nope)users join posts, then a union)Full plugin suite passes;
tsc --noEmit -p ts-plugin/tsconfig.jsonclean.