eslint-plugin-svelte: Linting with flat config (ESLint 9) does not work with TypeScript
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.0.0
What version of eslint-plugin-svelte are you using?
2.36.0
What did you do?
Configuration
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
import eslintPluginSvelte from 'eslint-plugin-svelte';
export default [
{ ignores: ['.svelte-kit'] },
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
...eslintPluginSvelte.configs['flat/recommended'],
];
<script lang="ts">
let count: number = 0;
</script>
{count}
What did you expect to happen?
The linter to be happy
What actually happened?
2:11 error Parsing error: Unexpected token :
Link to GitHub Repo with Minimal Reproducible Example
https://github.com/MathiasWP/eslint-9-svelte-typescript-error
Additional comments
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 11
- Comments: 36 (4 by maintainers)
For completeness, here is a full
eslint“flat file” configuration that works for Svelte projects that use TypeScript. It also includes Prettier linting.Maybe an example of such a “flat file” config could be added to the doc of
eslint-plugin-svelte, because so far there are only examples of the “old” type of config file (if I’m not mistaken).Something to note that I haven’t see mentioned yet,
typescript-eslintversion 8 added stable support forprojectServicethat replacesproject:I think that it helps you to specify
filesfor each config object like below:It because that
typescript-eslintset['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts']tofiles.Using typescript-eslint helper function
config(...)as below provides the almost same flat-config array as above.eslint.config.js using `config` helper
This should be added to the readme in the parser configuration section. We also don’t have to import the ts parser see here https://typescript-eslint.io/packages/typescript-eslint/#manually-configuring-our-plugin-and-parser.
@falco467 Thanks for the suggestion! Setting programs to false seems to fix the issue I was running into. I tested various rules in .js, .ts, and .svelte files and all rules are working, including the type checked rules.
eslint.config.js
Thank you so much! This solved the issue for me! 🎉
Hey, I had it working with the following config:
Hope it helps
Edit: updated with better imports and ignores
That’s what works for me:
@kkirkfield
@kkirkfield It seems to work when I add
programs: falseto the parserOptions of the typescript-eslint block.While the provided configs work for the simple recommended ruleset I cannot get it to work with the actual type-checking configs of typescript-eslint. To enable actual type-parsing, we need to enable the
project: trueparser option. And as soon as that is enabled, the error reads:Parsing error: “parserOptions.programs” has been provided for @typescript-eslint/parser. The file was not found in any of the provided program instance(s): src\drehscheibe.svelte
And the inclusion of extra-file extensions doesn’t make a difference. Also including the project: true in both parserOptions doesn’t change anything. JS/TS Files get handled correctly, but svelte files are not recognized as soon as “project” is set to anything.
Thanks for all your comments! I still don’t know the best practices for integrating with typescript-eslint, but using typescript-eslint@v8-beta might work. https://x.com/tseslint/status/1795205941647884611
Did you look at the one I posted above (hidden in a
detailstag)? It continues to work, and I’ve upgraded since I posted to every single more recent release.The primary difference I see is I am not using the pre-release alpha, but the stable release:
I haven’t been able to get any of the above configs working with eslint 9.3.0 and typescript-eslint 8.0.0-alpha.20 for .svelte files but it does work for all .js and .ts files.
From https://github.com/typescript-eslint/typescript-eslint/issues/8211:
The error I’m seeing on .svelte files is:
0:0 error Parsing error: "parserOptions.programs" has been provided for @typescript-eslint/parser. The file was not found in any of the provided program instance(s): src\routes\+page.svelteHere is an example config that is getting this error.
eslint.config.js
tsconfig.eslint.json
tsconfig.json
@robinengler I wanted to answer this Q because it’s important to understand why thigns are done!
The ts parser will ignore files with file extensions it doesn’t expect (such as
.svelte). The svelte parser is passed a ts parser instance to handle parsing typescript inside svelte files (sorry if that’s a confusing explanation). To make sure the ts parser will run on this code, we pass theextraFileExtensionsparameter to the svelte parser, which in turn passes it to the ts parser to ensure it parses the typescript in svelte files for us. If you don’t, the ts parser will report the following error for every svelte file:If you weren’t getting any errors from removing this line, it could be that your config was not actually linting the
.sveltefiles in your project. Hope this helps!I agree, having an example would help a lot
I found why my config file was not working with ts files in vscode, yet the command line interface worked as expected!
I needed this config line in vscode:
"eslint.experimental.useFlatConfig": trueAlso working for me. According to the rollout tracker there is support.