Skip to content

JSDoc types are lost in svelte files (typescript-eslint) #767

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

Closed
2 tasks done
falco467 opened this issue Jun 3, 2024 · 4 comments
Closed
2 tasks done

JSDoc types are lost in svelte files (typescript-eslint) #767

falco467 opened this issue Jun 3, 2024 · 4 comments
Labels
help wanted Extra attention is needed

Comments

@falco467
Copy link

falco467 commented Jun 3, 2024

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

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.

What version of ESLint are you using?

9.3.0

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

2.39.0

What did you do?

Configuration
import eslint from '@eslint/js'
import globals from 'globals'
import tsEslint from 'typescript-eslint'
import epSvelte from 'eslint-plugin-svelte'
import svelteParser from 'svelte-eslint-parser'

export default [
  eslint.configs.recommended,
  ...tsEslint.configs.strictTypeChecked,
  ...tsEslint.configs.stylisticTypeChecked,
  ...epSvelte.configs['flat/recommended'],
  {
    languageOptions: {
      parser: tsEslint.parser,
      parserOptions: {
        project: true,
        programs: false,
        extraFileExtensions: ['.astro','.svelte']
      },
      globals: {
        ...globals.browser
      }
    },
  },
  {
    files: ['**/*.svelte'],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        parser: tsEslint.parser,
      }
    },
  },
]

While both .svelte and .js files are scanned by ESLint the same function yields different results:

// test.svelte
<script>
  /** @param {number} x */
  function test(x) {
    return x+1
  }
</script>
// test.js
  /** @param {number} x */
  function test(x) {
    return x+1
  }

npx eslint src

test.js
   2:10  error  'test' is defined but never used     @typescript-eslint/no-unused-vars

test.svelte
    3:12  error  'test' is defined but never used  @typescript-eslint/no-unused-vars
    4:5   error  Unsafe return of an `any` typed value  @typescript-eslint/no-unsafe-return
    5:12  error  Invalid operand for a '+' operation. Operands must each be a number or string. Got `any`  @typescript-eslint/restrict-plus-operands

What did you expect to happen?

I expected the same code to produce the same warnings, irrespective of appearing in a .svelte or .js file.

What actually happened?

It seems like the typescript-eslint parser cannot parse type information from the code in the .svelte files. Maybe the svelte-parser strips JSDoc comments from the code before forwarding it to the typescript parser?

Link to GitHub Repo with Minimal Reproducible Example

https://github.com/falco467/svelte-jsdoc-typescript-example

Additional comments

No response

@ota-meshi
Copy link
Member

Thank you for posting this issue.
Hmm... The parser used by this plugin is not designed to handle JSDoc type information well.
If that doesn't work, I recommend using TypeScript in your .svelte scripts too.

@falco467
Copy link
Author

falco467 commented Jun 4, 2024

@ota-meshi
If I understand it right when I use parserOptions: parser: typescript-eslint-parser,
should the svelte-parser not forward all JavaScript code to the typescript-eslint parser? Because it handles JSDoc Type Information perfectly well. So svelte-parser would not have to parse type information - it just needs to forward the comment-tokens as they are in the source code to typescript-eslint-parser, their parser will interpret the Type information from the comment-tokens.

I'm working with a big code-base which opted for JS with JSDoc Typing (like the svelte team did for the whole svelte code base and many other projects are too) - a rewrite to typescript is not a viable option for our an many other code bases.

@ota-meshi
Copy link
Member

Pull requests are welcome.

@falco467
Copy link
Author

@ota-meshi after trying to solve it, it seems this needs to be solved upstream in the svelte-eslint-parser. I migrated the issue to their repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants