Skip to content

Commit ac55881

Browse files
committed
fix: handle ANY as a combination of the other types
1 parent d0fe8a4 commit ac55881

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/inferQueryResult.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export enum ColumnType {
66
String = 1 << 2,
77
Buffer = 1 << 3,
88
Null = 1 << 4,
9-
Any = 1 << 5,
109
}
1110

1211
export interface ColumnInfo {
@@ -28,7 +27,7 @@ SELECT
2827
WHEN (TYPE LIKE '%REAL%')
2928
OR (TYPE LIKE '%FLOA%')
3029
OR (TYPE LIKE '%DOUB%') THEN ${ColumnType.Number.toString()}
31-
WHEN (TYPE == 'ANY' AND (SELECT strict FROM pragma_table_list(:tableName)) == 1) THEN ${ColumnType.Any.toString()}
30+
WHEN (TYPE == 'ANY' AND (SELECT strict FROM pragma_table_list(:tableName)) == 1) THEN ${(ColumnType.String | ColumnType.Number | ColumnType.Buffer).toString()}
3231
ELSE ${ColumnType.Number.toString()}
3332
END type
3433
FROM

src/rules/typed-result.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,13 @@ function columnTypeToJSType(type: ColumnType): string {
225225
if (type & ColumnType.Unknown) {
226226
typeParts.push("unknown");
227227
}
228-
if (type & ColumnType.Any || type & ColumnType.Number) {
228+
if (type & ColumnType.Number) {
229229
typeParts.push("number");
230230
}
231-
if (type & ColumnType.Any || type & ColumnType.String) {
231+
if (type & ColumnType.String) {
232232
typeParts.push("string");
233233
}
234-
if (type & ColumnType.Any || type & ColumnType.Buffer) {
234+
if (type & ColumnType.Buffer) {
235235
typeParts.push("Buffer");
236236
}
237237
if (type & ColumnType.Null) {

tests/inferQueryResult.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ it("should support column with strict ANY type", () => {
6666
);
6767

6868
expect(result).toStrictEqual<typeof result>([
69-
{ name: "id", type: ColumnType.Any | ColumnType.Null },
69+
{
70+
name: "id",
71+
type:
72+
ColumnType.String |
73+
ColumnType.Number |
74+
ColumnType.Buffer |
75+
ColumnType.Null,
76+
},
7077
]);
7178
});
7279

tests/rules/typed-result.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ ruleTester.run("typed-result", rule, {
5858
"db.prepare<[]>('DELETE FROM foo')",
5959
// Should allow the user to set another type for unknown
6060
`db.prepare<[], {"random()": (number | null)}>("SELECT random();")`,
61+
// Test that no errors are reported for the outputs from the invalid cases
62+
`db.prepare<[], {"id": number}>("SELECT id FROM users")`,
63+
'db.prepare<[], {"id": number}>(`SELECT id FROM users`)',
64+
`const query = 'SELECT id FROM users';db.prepare<[], {"id": number}>(query);`,
65+
`db.prepare<[], {"id": number | null}>("SELECT * FROM foo")`,
66+
`db.prepare<[], {"id": number | string | Buffer}>("SELECT id FROM test")`,
67+
`db.prepare<[], {"name": number | string | Buffer | null}>("SELECT name FROM test")`,
68+
`db.prepare<[]>("DELETE FROM foo")`,
69+
`db.prepare<[], {"random()": (foo | number), "id": number}>("SELECT random(), id FROM users")`,
6170
],
6271
invalid: [
6372
// Query as string Literal

0 commit comments

Comments
 (0)