Skip to content

Commit

Permalink
support cond expression syntax inside queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Newbie012 committed Sep 11, 2022
1 parent 08664df commit 52bfc3c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/four-islands-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ts-safeql/eslint-plugin": patch
---

Support conditional expression syntax inside queries
12 changes: 12 additions & 0 deletions demos/basic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ export function check(client: Db, idsFromParameter: ID[]) {
SELECT *
FROM person
WHERE TRUE
AND id = ${idsFromParameter[0] > 5 ? 5 : 5}
AND id = ANY(${idsFromParameter})
AND name = ${"John"} -- string literal
`);

// Conditional expression
client.query<{ id: number }>(sql`
SELECT id FROM starship WHERE id = ${idsFromParameter[0] > 5 ? 5 : 5}
`);

const x: number | null = 5;

client.query<{ id: number }>(sql`
SELECT id FROM starship WHERE id = ${x ?? 10}
`);
}
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ts-safeql/docs",
"private": true,
"version": "1.0.0",
"main": "index.js",
"scripts": {
"docs:dev": "vitepress dev",
"docs:build": "vitepress build",
Expand Down
33 changes: 33 additions & 0 deletions packages/eslint-plugin/src/utils/ts-pg.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,44 @@ const tsKindToPgTypeMap: Record<number, string> = {
[ts.SyntaxKind.BigIntLiteral]: "bigint",
};

const tsFlagToPgTypeMap: Record<number, string> = {
[ts.TypeFlags.String]: "text",
[ts.TypeFlags.Number]: "int",
[ts.TypeFlags.Boolean]: "boolean",
[ts.TypeFlags.BigInt]: "bigint",
[ts.TypeFlags.NumberLiteral]: "int",
[ts.TypeFlags.StringLiteral]: "text",
[ts.TypeFlags.BooleanLiteral]: "boolean",
[ts.TypeFlags.BigIntLiteral]: "bigint",
};

function mapTsTypeStringToPgType(params: {
checker: TypeChecker;
node: TSESTreeToTSNode<TSESTree.Expression>;
type: ts.Type;
}) {
if (params.node.kind === ts.SyntaxKind.ConditionalExpression) {
const whenTrue = params.checker.getTypeAtLocation(params.node.whenTrue);
const whenTrueType = tsFlagToPgTypeMap[whenTrue.flags];

const whenFalse = params.checker.getTypeAtLocation(params.node.whenFalse);
const whenFalseType = tsFlagToPgTypeMap[whenFalse.flags];

if (whenTrueType === undefined || whenFalseType === undefined) {
return either.left(
`Unsupported conditional expression flags (true = ${whenTrue.flags}, false = ${whenFalse.flags})`
);
}

if (whenTrueType !== whenFalseType) {
return either.left(
`Conditional expression must have the same type (true = ${whenTrueType}, false = ${whenFalseType})`
);
}

return either.right(whenTrueType);
}

if (params.node.kind in tsKindToPgTypeMap) {
return either.right(tsKindToPgTypeMap[params.node.kind]);
}
Expand Down

1 comment on commit 52bfc3c

@vercel
Copy link

@vercel vercel bot commented on 52bfc3c Sep 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.