-
-
Notifications
You must be signed in to change notification settings - Fork 28
feat: add support for namespaces #403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: 0eb6d2f The changes in this PR will be included in the next version bump. This PR includes changesets to release 17 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 |
WalkthroughAdds a changeset declaring a patch release for @ts-safeql/eslint-plugin and refactors type-resolution logic to handle namespace types in annotations. Introduces handlers for various TS nodes, safer fallbacks, and new tests covering inline namespace type usage in check-sql. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant ESLint as ESLint Rule (check-sql)
participant TS as TypeScript TypeChecker
participant Resolver as getResolvedTargetByTypeNode
participant SQL as SQL Schema Comparator
ESLint->>Resolver: resolve type for generic T (e.g., Namespace.Interface)
Resolver->>TS: inspect node kind, symbol, type flags
TS-->>Resolver: type info (literals/unions/objects/refs)
alt Namespaced reference
Resolver->>Resolver: extract object properties via symbol/valueDeclaration
else Primitive/array/union/etc.
Resolver->>Resolver: handle via dedicated kind handlers
end
Resolver-->>ESLint: ExpectedResolvedTarget (shape or typeName)
ESLint->>SQL: compare query columns vs resolved shape
alt Match
ESLint-->>ESLint: no report
else Mismatch
ESLint-->>Developer: report incorrectTypeAnnotations + autofix
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Assessment against linked issues
Assessment against linked issues: Out-of-scope changes
Poem
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (6)
packages/eslint-plugin/src/rules/check-sql.test.ts (1)
2133-2167: Add cases for array and optional fields on namespaced types.To guard against regressions in array handling and optional properties within namespaces, please add:
- Caregiver.Name[] and Array<Caregiver.Name>
- Optional field in the namespaced interface (e.g., middleName?: string | null) and the corresponding SELECT
Here’s a minimal addition:
@@ ruleTester.run("namespace import", rules["check-sql"], { valid: [ { name: "select statement with type imported from inline namespace", @@ }, + { + name: "array of namespaced type (both syntaxes)", + options: withConnection(connections.base), + code: ` + namespace Caregiver { + export interface Name { firstName: string; lastName: string; } + } + async function run() { + const r1 = conn.query<Caregiver.Name[]>(sql\` + select first_name as "firstName", last_name as "lastName" from caregiver + \`); + const r2 = conn.query<Array<Caregiver.Name>>(sql\` + select first_name as "firstName", last_name as "lastName" from caregiver + \`); + } + `, + }, + { + name: "optional property in namespaced type", + options: withConnection(connections.base), + code: ` + namespace Caregiver { + export interface Partial { middle_name?: string | null } + } + function run() { + const result = conn.query<Caregiver.Partial>(sql\` + select middle_name from caregiver + \`); + } + `, + }, ],packages/eslint-plugin/src/utils/get-resolved-target-by-type-node.ts (5)
279-304: Avoid expanding node_modules interface shapes by using declarations, not only valueDeclaration.Interfaces from node_modules don’t have
valueDeclaration, so they fall through to property expansion. That can be slow and noisy. Prefer returning a nominal{ kind: "type", value: symbol.name }when all declarations are in node_modules.- if (type.symbol.valueDeclaration) { - const declaration = type.symbol.valueDeclaration; - const sourceFile = declaration.getSourceFile(); - const filePath = sourceFile.fileName; - - if (!filePath.includes("node_modules")) { - return extractObjectProperties(type, params); - } - - return { kind: "type", value: type.symbol.name }; - } + if (type.symbol.declarations && type.symbol.declarations.length > 0) { + const declFiles = type.symbol.declarations.map((d) => d.getSourceFile().fileName); + const allInNodeModules = declFiles.every((p) => p.includes("node_modules")); + if (allInNodeModules) { + return { kind: "type", value: type.symbol.name }; + } + return extractObjectProperties(type, params); + }
208-225: Primitive detection: broaden coverage and simplify.
type.flagsoften combines bits (e.g.,TypeFlags.BooleanLiteral), so the current direct map can miss cases. Preferchecker.typeToStringfor literals already handled; otherwise gate booleans viachecker.typeToString.- const flagMap = { - [ts.TypeFlags.String]: "string", - [ts.TypeFlags.Number]: "number", - [ts.TypeFlags.Boolean]: "boolean", - [ts.TypeFlags.Null]: "null", - [ts.TypeFlags.Undefined]: "undefined", - [ts.TypeFlags.Any]: "any", - } as const; - - return flagMap[type.flags as keyof typeof flagMap] - ? { kind: "type", value: flagMap[type.flags as keyof typeof flagMap] } - : null; + if (type.flags & ts.TypeFlags.String) return { kind: "type", value: "string" }; + if (type.flags & ts.TypeFlags.Number) return { kind: "type", value: "number" }; + if (type.flags & ts.TypeFlags.Boolean) return { kind: "type", value: "boolean" }; + if (type.flags & ts.TypeFlags.Null) return { kind: "type", value: "null" }; + if (type.flags & ts.TypeFlags.Undefined) return { kind: "type", value: "undefined" }; + if (type.flags & ts.TypeFlags.Any) return { kind: "type", value: "any" }; + return null;
85-90: Literal handler: support negative numeric literals.
TSLiteralTypemay wrap aUnaryExpressionfor negatives; current code returns "unknown".- return typeNode.literal.type === TSESTree.AST_NODE_TYPES.Literal - ? { kind: "type", value: `'${typeNode.literal.value}'` } - : { kind: "type", value: "unknown" }; + if (typeNode.literal.type === TSESTree.AST_NODE_TYPES.Literal) { + return { kind: "type", value: `'${typeNode.literal.value}'` }; + } + // Fallback to exact text for unary (e.g., -1) + return { kind: "type", value: (typeNode as unknown as { raw: string })?.raw ?? "unknown" };
11-17:ExpectedResolvedTarget['literal']variant is never produced.Either emit
{ kind: "literal", ... }for literals or remove the unused union member.- | { kind: "literal"; value: string; base: ExpectedResolvedTarget } + // Remove if not used, or start emitting it from literal handlers.
172-206: Array detection onresolveType: safer suffix strip.
replace("[]", "")strips only the first occurrence. Use a suffix-only regex to be correct for nested arrays.- return { kind: "array", value: { kind: "type", value: typeAsString.replace("[]", "") } }; + return { kind: "array", value: { kind: "type", value: typeAsString.replace(/\[\]$/, "") } };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (3)
.changeset/lovely-readers-find.md(1 hunks)packages/eslint-plugin/src/rules/check-sql.test.ts(1 hunks)packages/eslint-plugin/src/utils/get-resolved-target-by-type-node.ts(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: check (ubuntu-latest, 20)
🔇 Additional comments (2)
.changeset/lovely-readers-find.md (1)
2-5: Patch note is crisp and scoped correctly.Good: patch release only for eslint-plugin; message matches the change.
packages/eslint-plugin/src/rules/check-sql.test.ts (1)
2111-2131: Great coverage for inline namespace happy-path.This validates TSQualifiedName generics end-to-end.
closes #402
Summary by CodeRabbit
New Features
Refactor
Tests
Chores