feat(plugin-kysely): validate type annotations on embedded sql fragments#503
Conversation
🦋 Changeset detectedLatest commit: d1e9766 The changes in this PR will be included in the next version bump. This PR includes changesets to release 22 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (9)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (8)
📝 WalkthroughWalkthroughThe PR adds validation for typed raw Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
demos/plugin-kysely/src/builder.tsESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
⚪️ generate() performance
⚪️ check-sql lint-rule overhead
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In @.changeset/kysely-embedded-fragment-types.md:
- Line 9: The changeset text uses inline code with nested backticks, which
triggers markdownlint MD038. Update the affected examples in the prose around
the SafeQL description to avoid nested single-backtick formatting by using
double backticks or equivalent rewritten wording, and keep the content readable
while preserving the same examples.
In `@packages/plugins/kysely/src/plugin.ts`:
- Around line 821-833: The direct-argument check in the where-like method
handling is too broad and treats binary predicate operands as full boolean
conditions. Update the logic around the current parent.arguments / callee name
check so it only classifies actual condition expressions, not operands in forms
like where(column, operator, value). Use the existing whereLikeMethods and the
surrounding AST unwrap logic to distinguish single-argument boolean predicates
from multi-argument binary where calls, and avoid autofixing non-boolean sql
fragments such as sql<number> to sql<boolean>.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c882f882-14d7-4d52-8963-acc957b89610
📒 Files selected for processing (7)
.changeset/kysely-embedded-fragment-types.mdpackages/eslint-plugin/src/rules/check-sql.rule.tspackages/plugin-utils/src/index.contract.test.tspackages/plugin-utils/src/index.tspackages/plugins/kysely/src/builder-type-check.tspackages/plugins/kysely/src/plugin.integration.test.tspackages/plugins/kysely/src/plugin.ts
Greptile SummaryThis PR adds deferred
Confidence Score: 5/5Safe to merge; the new deferred typeCheck path is well-isolated and all three changed packages have integration tests covering valid/invalid fragment cases including multi-error reporting. The feature is additive: the typeCheck hook is only invoked when the plugin explicitly provides it, the core rule falls back to its existing behavior otherwise, and the boolean-unification logic uses stable TypeScript type flags rather than string matching. Integration tests cover parenthesized fragments, binary-where operands, SqlBool/boolean, and multi-error queries. The only finding is a stale return-type annotation on resolveBuilderQuery that does not affect correctness. packages/plugins/kysely/src/plugin.ts — the resolveBuilderQuery return type annotation no longer includes typeCheck? and should be updated to ResolvedQuery | "skip". Important Files Changed
|
485dacd to
26256a0
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/plugins/kysely/src/builder-type-check.ts`:
- Around line 95-100: The boolean type check in isBooleanType currently relies
on checker.typeToString(type) === "SqlBool", which misses aliased forms like
type MyBool = SqlBool. Update isBooleanType in builder-type-check.ts to inspect
the underlying symbol/type via the TypeChecker instead of matching the printed
name, and keep the existing boolean/union/intersection handling intact so
aliased SqlBool annotations are recognized correctly.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 04797fd0-7fc6-4a78-82ea-2cff1beb7596
📒 Files selected for processing (8)
.changeset/kysely-embedded-fragment-types.mddemos/plugin-kysely/src/builder.tspackages/eslint-plugin/src/rules/check-sql.rule.tspackages/plugin-utils/src/index.contract.test.tspackages/plugin-utils/src/index.tspackages/plugins/kysely/src/builder-type-check.tspackages/plugins/kysely/src/plugin.integration.test.tspackages/plugins/kysely/src/plugin.ts
✅ Files skipped from review due to trivial changes (1)
- .changeset/kysely-embedded-fragment-types.md
🚧 Files skipped from review as they are similar to previous changes (6)
- demos/plugin-kysely/src/builder.ts
- packages/plugin-utils/src/index.contract.test.ts
- packages/plugins/kysely/src/plugin.integration.test.ts
- packages/plugin-utils/src/index.ts
- packages/eslint-plugin/src/rules/check-sql.rule.ts
- packages/plugins/kysely/src/plugin.ts
5c35ef8 to
68950b7
Compare
Check the `<T>` a user writes on a raw `sql` fragment embedded in a Kysely
builder chain against the type the database returns, and autofix it on a
mismatch. Selections (`sql<T>`...`.as("alias")`) compare against the column
type; `where`-like conditions compare against `boolean` (accepting both
`SqlBool` and `boolean`). A query reports one result per bad fragment.
Adds a deferred `typeCheck` hook to `ResolvedQuery` in plugin-utils so a
plugin can validate annotations once the query's row type is known, and
wires the eslint rule to dispatch it through the standard
incorrect-annotation report and autofix.
Also makes the turbo `lint` task depend on `^build` so a change to the
eslint-plugin rule busts the cache of every package that lints against it
(the demos), instead of replaying a stale pass.
68950b7 to
d1e9766
Compare
Validates the
<T>annotation on rawsqlfragments embedded in Kysely builder chains, with an autofix on a mismatch.sql<T>....as("alias")) check against the column type.where-like conditions check againstboolean(bothSqlBoolandbooleanaccepted).Adds a deferred
typeCheckhook toResolvedQueryin plugin-utils, rendered through the rule's standard incorrect-annotation report. Playground demo changes are left out of this PR.Summary by CodeRabbit
sql<T>fragments inside builder chains, including selection aliases andwhere-style boolean conditions, with support for wrapped/parenthesized forms.typeChecksupport to power deferred typed fragment validation.SqlBool/boolean), with more reliable expected vs. actual type comparisons and corrections.sql<T>validation behavior and examples.