Skip to content

Commit 088f0d3

Browse files
committed
feat: add support for flat config
1 parent 22d25ca commit 088f0d3

File tree

10 files changed

+315
-12
lines changed

10 files changed

+315
-12
lines changed

.eslintrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ module.exports = {
5959
'mdx/code-blocks': true
6060
}
6161
},
62+
{
63+
files: ['*.md/**', '**/*.md/**'],
64+
rules: {
65+
'n/no-missing-import': 'off'
66+
}
67+
},
6268
{
6369
files: ['*.mjs'],
6470
parserOptions: {

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,38 @@ npm install --save-dev eslint eslint-plugin-svelte svelte
6666

6767
### Configuration
6868

69+
#### For ESLint>=v9 Config (Flat Config)
70+
71+
Use `eslint.config.js` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/configuration-files-new>.
72+
73+
Example **eslint.config.js**:
74+
75+
```mjs
76+
import eslintPluginSvelte from 'eslint-plugin-svelte';
77+
export default [
78+
// add more generic rule sets here, such as:
79+
// js.configs.recommended,
80+
...eslintPluginSvelte.configs['flat/recommended'],
81+
{
82+
rules: {
83+
// override/add rules settings here, such as:
84+
// 'svelte/rule-name': 'error'
85+
}
86+
}
87+
];
88+
```
89+
90+
This plugin provides configs:
91+
92+
- `eslintPluginSvelte.configs['flat/base']` ... Configuration to enable correct Svelte parsing.
93+
- `eslintPluginSvelte.configs['flat/recommended']` ... Above, plus rules to prevent errors or unintended behavior.
94+
- `eslintPluginSvelte.configs['flat/prettier']` ... Turns off rules that may conflict with [Prettier](https://prettier.io/) (You still need to configure prettier to work with svelte yourself, for example by using [prettier-plugin-svelte](https://github.com/sveltejs/prettier-plugin-svelte).).
95+
- `eslintPluginSvelte.configs['flat/all']` ... All rules. This configuration is not recommended for production use because it changes with every minor and major version of `eslint-plugin-svelte`. Use it at your own risk.
96+
97+
See [the rule list](https://sveltejs.github.io/eslint-plugin-svelte/rules/) to get the `rules` that this plugin provides.
98+
99+
#### Legacy Config (ESLint<v9)
100+
69101
Use `.eslintrc.*` file to configure rules. See also: <https://eslint.org/docs/user-guide/configuring>.
70102

71103
Example **.eslintrc.js**:

docs/user-guide.md

+32
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,38 @@ npm install --save-dev eslint eslint-plugin-svelte svelte
1919

2020
### Configuration
2121

22+
#### For ESLint>=v9 Config (Flat Config)
23+
24+
Use `eslint.config.js` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/configuration-files-new>.
25+
26+
Example **eslint.config.js**:
27+
28+
```mjs
29+
import eslintPluginSvelte from 'eslint-plugin-svelte';
30+
export default [
31+
// add more generic rule sets here, such as:
32+
// js.configs.recommended,
33+
...eslintPluginSvelte.configs['flat/recommended'],
34+
{
35+
rules: {
36+
// override/add rules settings here, such as:
37+
// 'svelte/rule-name': 'error'
38+
}
39+
}
40+
];
41+
```
42+
43+
This plugin provides configs:
44+
45+
- `eslintPluginSvelte.configs['flat/base']` ... Configuration to enable correct Svelte parsing.
46+
- `eslintPluginSvelte.configs['flat/recommended']` ... Above, plus rules to prevent errors or unintended behavior.
47+
- `eslintPluginSvelte.configs['flat/prettier']` ... Turns off rules that may conflict with [Prettier](https://prettier.io/) (You still need to configure prettier to work with svelte yourself, for example by using [prettier-plugin-svelte](https://github.com/sveltejs/prettier-plugin-svelte).).
48+
- `eslintPluginSvelte.configs['flat/all']` ... All rules. This configuration is not recommended for production use because it changes with every minor and major version of `eslint-plugin-svelte`. Use it at your own risk.
49+
50+
See [the rule list](./rules.md) to get the `rules` that this plugin provides.
51+
52+
#### Legacy Config (ESLint<v9)
53+
2254
Use `.eslintrc.*` file to configure rules. See also: <https://eslint.org/docs/user-guide/configuring>.
2355

2456
Example **.eslintrc.js**:

src/configs/flat/all.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { rules } from '../../utils/rules';
2+
import base from './base';
3+
export default [
4+
...base,
5+
{
6+
rules: Object.fromEntries(
7+
rules
8+
.map((rule) => [`svelte/${rule.meta.docs.ruleName}`, 'error'])
9+
.filter(
10+
([ruleName]) =>
11+
![
12+
// Does not work without options.
13+
'svelte/no-restricted-html-elements'
14+
].includes(ruleName)
15+
)
16+
)
17+
}
18+
];

src/configs/flat/base.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// IMPORTANT!
2+
// This file has been automatically generated,
3+
// in order to update its content execute "pnpm run update"
4+
import type { ESLint } from 'eslint';
5+
export default [
6+
{
7+
files: ['*.svelte', '**/*.svelte'],
8+
plugins: {
9+
get svelte(): ESLint.Plugin {
10+
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
11+
return require('../../index');
12+
}
13+
},
14+
languageOptions: {
15+
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
16+
parser: require('svelte-eslint-parser')
17+
},
18+
rules: {
19+
// ESLint core rules known to cause problems with `.svelte`.
20+
'no-inner-declarations': 'off', // The AST generated by svelte-eslint-parser will false positives in it rule because the root node of the script is not the `Program`.
21+
// "no-irregular-whitespace": "off",
22+
// Self assign is one of way to update reactive value in Svelte.
23+
'no-self-assign': 'off',
24+
25+
// eslint-plugin-svelte rules
26+
'svelte/comment-directive': 'error',
27+
'svelte/system': 'error'
28+
}
29+
}
30+
];

src/configs/flat/prettier.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// IMPORTANT!
2+
// This file has been automatically generated,
3+
// in order to update its content execute "pnpm run update"
4+
import base from './base';
5+
export default [
6+
...base,
7+
{
8+
rules: {
9+
// eslint-plugin-svelte rules
10+
'svelte/first-attribute-linebreak': 'off',
11+
'svelte/html-closing-bracket-spacing': 'off',
12+
'svelte/html-quotes': 'off',
13+
'svelte/html-self-closing': 'off',
14+
'svelte/indent': 'off',
15+
'svelte/max-attributes-per-line': 'off',
16+
'svelte/mustache-spacing': 'off',
17+
'svelte/no-spaces-around-equal-signs-in-attribute': 'off',
18+
'svelte/no-trailing-spaces': 'off',
19+
'svelte/shorthand-attribute': 'off',
20+
'svelte/shorthand-directive': 'off'
21+
}
22+
}
23+
];

src/configs/flat/recommended.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// IMPORTANT!
2+
// This file has been automatically generated,
3+
// in order to update its content execute "pnpm run update"
4+
import base from './base';
5+
export default [
6+
...base,
7+
{
8+
rules: {
9+
// eslint-plugin-svelte rules
10+
'svelte/comment-directive': 'error',
11+
'svelte/no-at-debug-tags': 'warn',
12+
'svelte/no-at-html-tags': 'error',
13+
'svelte/no-dupe-else-if-blocks': 'error',
14+
'svelte/no-dupe-style-properties': 'error',
15+
'svelte/no-dynamic-slot-name': 'error',
16+
'svelte/no-inner-declarations': 'error',
17+
'svelte/no-not-function-handler': 'error',
18+
'svelte/no-object-in-text-mustaches': 'error',
19+
'svelte/no-shorthand-style-property-overrides': 'error',
20+
'svelte/no-unknown-style-directive-property': 'error',
21+
'svelte/no-unused-svelte-ignore': 'error',
22+
'svelte/system': 'error',
23+
'svelte/valid-compile': 'error'
24+
}
25+
}
26+
];

src/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@ import base from './configs/base';
44
import recommended from './configs/recommended';
55
import prettier from './configs/prettier';
66
import all from './configs/all';
7+
import flatBase from './configs/flat/base';
8+
import flatRecommended from './configs/flat/recommended';
9+
import flatPrettier from './configs/flat/prettier';
10+
import flatAll from './configs/flat/all';
711
import * as processor from './processor';
812
import * as meta from './meta';
913

1014
const configs = {
1115
base,
1216
recommended,
1317
prettier,
14-
all
18+
all,
19+
'flat/base': flatBase,
20+
'flat/recommended': flatRecommended,
21+
'flat/prettier': flatPrettier,
22+
'flat/all': flatAll
1523
};
1624

1725
const rules = ruleList.reduce(

tests/src/configs/all.ts

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import assert from 'assert';
22
import plugin from '../../../src/index';
3-
import { LegacyESLint } from '../../utils/eslint-compat';
3+
import { LegacyESLint, ESLint } from '../../utils/eslint-compat';
44

55
describe('`all` config', () => {
6-
it('`all` config should work. ', async () => {
6+
it('legacy `all` config should work. ', async () => {
77
const code = `<script>const a = 1, b = 2;</script>{@html a+b}`;
88

99
const linter = new LegacyESLint({
@@ -22,10 +22,33 @@ describe('`all` config', () => {
2222
const messages = result[0].messages;
2323

2424
assert.deepStrictEqual(
25-
messages.map((m) => ({ ruleId: m.ruleId, line: m.line })),
25+
messages.map((m) => ({ ruleId: m.ruleId, line: m.line, message: m.message })),
26+
[
27+
{
28+
ruleId: 'svelte/no-at-html-tags',
29+
message: '`{@html}` can lead to XSS attack.',
30+
line: 1
31+
}
32+
]
33+
);
34+
});
35+
it('`all` config should work. ', async () => {
36+
const code = `<script>const a = 1, b = 2;</script>{@html a+b}`;
37+
38+
const linter = new ESLint({
39+
overrideConfigFile: true as any,
40+
// eslint-disable-next-line @typescript-eslint/no-var-requires -- for test
41+
overrideConfig: require('../../../src/index').configs['flat/all']
42+
});
43+
const result = await linter.lintText(code, { filePath: 'test.svelte' });
44+
const messages = result[0].messages;
45+
46+
assert.deepStrictEqual(
47+
messages.map((m) => ({ ruleId: m.ruleId, line: m.line, message: m.message })),
2648
[
2749
{
2850
ruleId: 'svelte/no-at-html-tags',
51+
message: '`{@html}` can lead to XSS attack.',
2952
line: 1
3053
}
3154
]

0 commit comments

Comments
 (0)