feat: treat jsdoc with @typescript-eslint/parser
#607*.svelte
file linting does not work with the parser alone. You should also use eslint-plugin-svelte with it.)9.3.0
eslint-plugin-svelte
and svelte-eslint-parser
are you using?import tsEslint from 'typescript-eslint'
import epSvelte from 'eslint-plugin-svelte'
import svelteParser from 'svelte-eslint-parser'
export default [
...tsEslint.configs.strictTypeChecked,
...epSvelte.configs['flat/recommended'],
{
languageOptions: {
parser: tsEslint.parser,
parserOptions: {
project: true,
programs: false,
extraFileExtensions: ['.svelte']
},
},
},
{
files: ['**/*.svelte'],
languageOptions: {
parser: svelteParser,
parserOptions: {
parser: tsEslint.parser,
}
},
},
]
<script>
/** @param {number} x */
function test(x) {
return x+1
}
</script>
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
I expected the same code to produce the same warnings, irrespective of appearing in a .svelte or .js file.
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?
https://github.com/falco467/svelte-jsdoc-typescript-example
Migrated from eslint-plugin-svelte since it is a parser problem: sveltejs/eslint-plugin-svelte#767
close: #533
@ota-meshi
In this PR, I made it so that scripts that appear to be JSDoc are parsed using @typescript-eslint/parser
. I thought this would apply the types, but judging from the tests, it doesn’t seem to be working. Do you have any idea what might be wrong?
Latest commit: cae678c
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
---|---|---|---|
src/parser/parser-options.ts | 43 | 45 | 95.56% |
Totals | |
---|---|
Change from base Build 12251662727: | 0.0% |
Covered Lines: | 10256 |
Relevant Lines: | 10693 |
There may be a problem with type inference in the typescript
core.
I think how typescript
infers types depends on ScriptKind. By default, ScriptKind is determined from the extension, so we may need to make ScriptKind recognize as JS. But I don't know how to do that well.
Thank you. For now, I will prioritize addressing other issues.