sveltejs / eslint-plugin-svelte

ESLint plugin for Svelte using AST
https://sveltejs.github.io/eslint-plugin-svelte/
MIT License
276 stars 30 forks source link

typescript-eslint's "no any" rules don't work with inferred function types from sveltekit #413

Open DetachHead opened 1 year ago

DetachHead commented 1 year ago

Before You File a Bug Report Please Confirm You Have Done The Following...

What version of ESLint are you using?

8.33.0

What version of eslint-plugin-svelte are you using?

2.22.0

What did you do?

Configuration ```json { "rules": { "@typescript-eslint/no-unsafe-member-access": "error" } } ```
// routes/api/+server.ts
export const GET = async ({ request }) => {
    // request is not any, since it's inferred as Request by sveltekit
    request.headers // error: @typescript-eslint/no-unsafe-member-access
}

What did you expect to happen?

no error

What actually happened?

@typescript-eslint/no-unsafe-member-access error

Link to GitHub Repo with Minimal Reproducible Example

https://github.com/DetachHead/eslint-plugin-svelte-repros/commit/7bf800de39d834d70525c1e5cc08343ce5fdb98f

Additional comments

originally raised at https://github.com/ota-meshi/eslint-plugin-svelte/issues/390#issuecomment-1477369726

ota-meshi commented 1 year ago

Where are the types for GET sourced from? I don't understand the code you provided because there are no type declarations. Could you please share a repository to reproduce the issue? I've only made static sites using svelte kit, so I'm not sure :sweat:

DetachHead commented 1 year ago

sveltekit infers the types so you don't have to annotate them explicitly. i've updated the issue with a github repo. to reproduce the error, checkout that branch then run the following commands

npm install
npm run check
npx eslint .
polyzen commented 1 year ago

Think you mean npx eslint .

DetachHead commented 1 year ago

true. i added ./node_modules/.bin to my path so i forgot about npx. updated my comment

polyzen commented 1 year ago

Inferring types is a new feature: https://svelte.dev/blog/zero-config-type-safety

ota-meshi commented 1 year ago

Thank you for the repo to reproduce. It seems that the svelte language server and svelte-check assign their own types. (Because raw tsc command fails.) I think we should check how it is implemented 👀.

ota-meshi commented 1 year ago

memo: Perhaps the following source code is injecting the type.

https://github.com/sveltejs/language-tools/blob/ac827294b4aff00ecac961b164155f29c0f5ed1d/packages/svelte2tsx/src/helpers/sveltekit.ts#L189

ota-meshi commented 1 year ago

Hmm.. We may need new typescript parser for the eslint (not svelte parser). We should check if it integrates well with svelte2tsx.

jasonpanosso commented 3 weeks ago

I still have this issue, is there any progress on this, or any known workaround/solution?

thenbe commented 2 weeks ago

Another minimal repro here: https://github.com/thenbe/repro-eslint-svelte-typecheck

Reproduction steps

  1. Clone and pnpm install
  2. Run: pnpm eslint src/routes/+page.svelte

Expected: We should get no eslint errors. Actual: We get two eslint errors. Error message:

src/routes/+page.svelte
   7:20  error  Unsafe member access .url on an `any` value       @typescript-eslint/no-unsafe-member-access
  11:8   error  Unsafe member access .greeting on an `any` value  @typescript-eslint/no-unsafe-member-access

Other

svelte-check (i.e. pnpm run check) passes without errors. Only eslint shows error.

ryoppippi commented 4 days ago

Got the same issue....

So,

export async function load({ fetch }) {
    return await fetch('/foo');
}

this causes an error because fetch is inferred as any in eslint. However, this works fine for svelte-check and svelte lsp