ts-safeql / safeql

Validate and auto-generate TypeScript types from raw SQL queries in PostgreSQL.
https://safeql.dev
MIT License
1.35k stars 22 forks source link

Eslint cannot detect the type annotation on wrapper (pg with wrapper + sql-template-strings) #236

Closed Mange closed 5 months ago

Mange commented 5 months ago

Describe the bug

ESLint reports a missing type annotation, despite it being there. The applied fix will cause syntax errors.

To Reproduce

{
    "root": true,
    "env": {"es2021": true},

    "ignorePatterns": ["node_modules/**/*", "tsconfig.eslint.json"],

    "plugins": ["@typescript-eslint", "@ts-safeql/eslint-plugin"],
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaVersion": 12,
        "sourceType": "module",
        "project": ["./tsconfig.eslint.json"]
    },
    "rules": {
        "@ts-safeql/check-sql": [
            "error",
            {
                "connections": [{
                    "targets": [{"wrapper": "db.query"}],
                    "databaseUrl": "postgres://postgres:postgres@localhost:5466/test"
                }]
            }
        ]
    }
}
const result = await db.query<{ rate: number }>(SQL`
  SELECT 1 AS rate
`)

Reported error:

eslint: Query is missing type annotation
  Fix with: { rate: number }

Applying the autofix yields this result:

const result = await db.query<{ rate: number }><{ rate: number }>(SQL`
  SELECT 1 AS rate
`)

Expected behavior

No error should be emitted.

Screenshots

image

Desktop (please complete the following information):

"@ts-safeql/eslint-plugin": "^3.3.1",
"pg": "^8.12.0",
"sql-template-strings": "^2.2.2",
"typescript": "^5.4.5"
Mange commented 5 months ago

This seems to be caused by an older version of eslint being required by ts-standard.

Removing that plugin and reinstalling everything at their latest version using

npm install --save-dev eslint @eslint/js @types/eslint__js typescript typescript-eslint

Seems to have fixed it.