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
Linting with flat config (ESLint 9) does not work with TypeScript #732
Comments
Hey, I had it working with the following config: import js from "@eslint/js";
import eslintConfigPrettier from "eslint-config-prettier";
import eslintPluginSvelte from "eslint-plugin-svelte";
import globals from "globals";
import svelteParser from "svelte-eslint-parser";
import tsEslint from "typescript-eslint";
export default tsEslint.config(
js.configs.recommended,
...tsEslint.configs.recommended,
...eslintPluginSvelte.configs["flat/recommended"],
eslintConfigPrettier,
...eslintPluginSvelte.configs["flat/prettier"],
{
languageOptions: {
ecmaVersion: 2022,
sourceType: "module",
globals: { ...globals.node, ...globals.browser },
parser: svelteParser,
parserOptions: {
parser: tsEslint.parser,
extraFileExtensions: [".svelte"],
},
},
},
{
ignores: [
"**/.svelte-kit",
"**/.vercel",
"**/.yarn",
"**/build",
"**/node_modules",
"**/package",
],
},
); Hope it helps Edit: updated with better imports and ignores |
Also working for me. According to the rollout tracker there is support.
|
Thank you for helping out, i really appreciate it! This config works for my Svelte files it seems like, but in almost all my TypeScript files i get a |
Thank you for helping out, i appreciate it! This config works fine for me on TypeScript files, but it does not work on Svelte files. The funny thing is that i tried to merge your and @GauBen's configs (because the one works with TS and the other with Svelte), but with no luck. :/ |
@MathiasWP : I had the same problem as you, but I got it working by adding My understanding, is that by adding {
files: ["**/*.svelte"],
languageOptions: {
ecmaVersion: 2022,
sourceType: "module",
parser: svelteParser,
parserOptions: {
parser: tsParser,
extraFileExtensions: [".svelte"],
},
},
}, I am not sure what the |
Thank you so much! This solved the issue for me! 🎉 |
For completeness, here is a full Maybe an example of such a "flat file" config could be added to the doc of // eslint.config.cjs
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
import eslintPluginSvelte from 'eslint-plugin-svelte';
import js from '@eslint/js';
import svelteParser from 'svelte-eslint-parser';
import tsEslint from 'typescript-eslint';
import tsParser from '@typescript-eslint/parser';
export default [
js.configs.recommended,
...tsEslint.configs.strict,
...eslintPluginSvelte.configs['flat/recommended'],
eslintPluginPrettierRecommended, // must be last to override conflicting rules.
{
rules: {
semi: ['warn', 'always'],
quotes: [
'warn',
'single',
{ avoidEscape: true, allowTemplateLiterals: true },
],
'no-nested-ternary': 'error',
'linebreak-style': ['error', 'unix'],
'no-cond-assign': ['error', 'always'],
'no-console': 'error',
'@typescript-eslint/sort-type-constituents': 'error',
'sort-imports': [
'error',
{
ignoreCase: true,
ignoreDeclarationSort: false,
ignoreMemberSort: false,
memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
allowSeparatedGroups: true,
},
],
},
},
{
files: ['**/*.svelte'],
languageOptions: {
parser: svelteParser,
parserOptions: {
parser: tsParser,
},
},
rules: {
'svelte/no-target-blank': 'error',
'svelte/no-at-debug-tags': 'error',
'svelte/no-reactive-functions': 'error',
'svelte/no-reactive-literals': 'error',
},
},
]; |
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: |
I agree, having an example would help a lot |
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. |
Thanks, feels nice to remove |
I managed to lint both TypeScript import eslint from "@eslint/js";
import pluginImport from "eslint-plugin-import";
import svelteEslint from "eslint-plugin-svelte";
import globals from "globals";
import svelteParser from "svelte-eslint-parser";
import tsEslint from "typescript-eslint";
export default [
eslint.configs.recommended,
...tsEslint.configs.recommended,
...svelteEslint.configs["flat/recommended"],
{
files: ["**/*.svelte"],
languageOptions: {
parser: svelteParser,
parserOptions: {
parser: tsEslint.parser
},
globals: {
...globals.browser
}
}
},
{
files: ["**/*.ts"],
languageOptions: {
parser: tsEslint.parser
}
},
{
plugins: {
"@typescript-eslint": tsEslint.plugin,
import: pluginImport
},
rules: {
semi: "warn",
"svelte/sort-attributes": "warn"
}
}
]; |
Thanks for all the config hints - I'm new to svelte and eslint 9 is complaining with this config about eslint.config.cjs
Running it with |
@szinn Change the filename to |
Ah, I also reinstalled modules - prettier config caused grief as well |
This is wonderful! I was able to use and extend the above into a config that supports custom configs for my test files, with the addition of the markdown linter, and the jsdoc linter. Hopefully this helps others build even better ones! See my
|
@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
If you weren't getting any errors from removing this line, it could be that your config was not actually linting the |
I think that it helps you to specify import globals from 'globals';
import js from '@eslint/js';
import tseslint from 'typescript-eslint' // v7
import prettierConfig from 'eslint-config-prettier';
import svelte from 'eslint-plugin-svelte';
import svelteParser from 'svelte-eslint-parser';
/** @type {import('typescript-eslint').Config} */
export default = [
...[
js.configs.recommended,
...tseslint.configs.strictTypeChecked, // typescript-eslint set `['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts']` to `files`
prettierConfig, // eslint-config-prettier does not turn 'svelte/*' rules off
{ // overrides options should be after other config
languageOptions: {
parser: tseslint.parser,
parserOptions: {
sourceType: 'module',
extraFileExtensions: ['.svelte']
},
globals: { ...globals.browser, ...globals.node },
},
rules: { /* rules for js/ts/svelte */ } // don't set 'svelte/*' rules here
}
].map(conf => ({ ...conf, files: ['**/*.js', '**/*.ts', '**/*.svelte'] })), // To override `files` is so important!!
...svelte.configs['flat/recommended'], // eslint-plugin-svelte set `['*.svelte', '**/*.svelte']` to `files`
...svelte.configs['flat/prettier'], // if non svelte files occur 'svelte/*' error, these element should be set `files`
{ // your config should be after
files: ['*.svelte', '**/*.svelte'], // the same value as eslint-plugin-svelte `files` option
languageOptions: {
parser: svelteParser,
parserOptions: { parser: tseslint.parser }
},
// ↓ rule types; sveltejs/eslint-plugin-svelte #735
/** @type {import('eslint').Linter.RulesRecord} */
rules: { /* rules for svelte */ }
},
{
// other override settings. e.g. for `files: ['**/*.test.*']`
},
{ ignores: ['node_modules/', /* other ignores */] } // overrides global ignores
]; It because that Note
Using typescript-eslint helper function eslint.config.js using `config` helperimport globals from 'globals';
import js from '@eslint/js';
import tseslint from 'typescript-eslint' // v7
import prettierConfig from 'eslint-config-prettier';
import svelte from 'eslint-plugin-svelte';
import svelteParser from 'svelte-eslint-parser';
const defaultConfig = tseslint.config({
files: ['**/*.js', '**/*.ts', '**/*.svelte'],
extends: [
js.configs.recommended, // if it occurs a type error, you can install `@types/eslint__js` package
...tseslint.configs.strictTypeChecked,
prettierConfig // if it occurs a type error, you can install `@types/eslint-config-prettier` package
],
languageOptions: {
parser: tseslint.parser,
parserOptions: {
sourceType: 'module',
extraFileExtensions: ['.svelte']
},
globals: { ...globals.browser, ...globals.node },
},
rules: { /* rules for js/ts/svelte */ } // don't set 'svelte/*' rules here
});
const svelteConfig = tseslint.config({
extends: [
...svelte.configs['flat/recommended'],
...svelte.configs['flat/prettier']
],
languageOptions: {
parser: svelteParser,
parserOptions: { parser: tseslint.parser }
},
// ↓ rule types; sveltejs/eslint-plugin-svelte #735
/** @type {import('eslint').Linter.RulesRecord} */
rules: { /* rules for svelte */ }
});
/** @type {import('@typescript-eslint/utils').TSESLint.FlatConfig.ConfigArray} */
export default = [
...defaultConfig,
...svelteConfig,
{
// other override settings. e.g. for `files: ['**/*.test.*']`
},
{ ignores: ['node_modules/', /* other ignores */] } // overrides global ignores
]; |
Before You File a Bug Report Please Confirm You Have Done The Following...
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
What did you expect to happen?
The linter to be happy
What actually happened?
Link to GitHub Repo with Minimal Reproducible Example
https://github.com/MathiasWP/eslint-9-svelte-typescript-error
Additional comments
No response
The text was updated successfully, but these errors were encountered: