Skip to content

Commit 35d80a5

Browse files
authored
feat: support warningFilter in valid-compile rule (#984)
1 parent faf90ef commit 35d80a5

File tree

18 files changed

+87
-8
lines changed

18 files changed

+87
-8
lines changed

.changeset/great-turkeys-hang.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': minor
3+
---
4+
5+
feat: support `warningFilter` in `valid-compile` rule

docs/rules/valid-compile.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Note that we exclude reports for some checks, such as `missing-declaration`, and
3535

3636
### Using `svelte.config.js`
3737

38-
If you want to suppress messages using [`onwarn` like `vite-plugin-svelte`](https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/config.md#onwarn), Use `eslint.config.js` and specify the information in `svelte.config.js` in your parser configuration.
38+
If you want to suppress messages using [`warningFilter`](https://svelte.dev/docs/svelte/svelte-compiler#ModuleCompileOptions) or `onwarn` like [`vite-plugin-svelte`](https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/config.md#onwarn), Use `eslint.config.js` and specify the information in `svelte.config.js` in your parser configuration.
3939

4040
```js
4141
import svelteConfig from './svelte.config.js';
@@ -54,9 +54,26 @@ export default [
5454

5555
See also [User Guide > Specify `svelte.config.js`](../user-guide.md#specify-svelte-config-js)
5656

57+
#### warningFilter
58+
59+
This rule can use [`warningFilter`](https://svelte.dev/docs/svelte/svelte-compiler#ModuleCompileOptions).
60+
61+
Example:
62+
63+
```js
64+
// svelte.config.js
65+
export default {
66+
warningFilter: (warning) => {
67+
if (warning.code === 'a11y-distracting-elements') return false;
68+
if (warning.code === 'a11y_distracting_elements') return false; // for Svelte v5
69+
return true;
70+
}
71+
};
72+
```
73+
5774
#### onwarn
5875

59-
This rule can use [`onwarn` like `vite-plugin-svelte`](https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/config.md#onwarn).
76+
This rule can use `onwarn` like [`vite-plugin-svelte`](https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/config.md#onwarn).
6077

6178
Example:
6279

packages/eslint-plugin-svelte/src/rules/valid-compile.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,22 @@ export default createRule('valid-compile', {
5555
if (!sourceCode.parserServices.isSvelte) {
5656
return {};
5757
}
58-
const onwarn = sourceCode.parserServices.svelteParseContext?.svelteConfig?.onwarn;
58+
const { onwarn, warningFilter } =
59+
sourceCode.parserServices.svelteParseContext?.svelteConfig ?? {};
5960

60-
const transform: (warning: Warning) => Warning | null = onwarn
61+
const transform: (warning: Warning) => Warning | null = warningFilter
6162
? (warning) => {
6263
if (!warning.code) return warning;
63-
let result: Warning | null = null;
64-
onwarn(warning, (reportWarn) => (result = reportWarn));
65-
return result;
64+
return warningFilter(warning) ? warning : null;
6665
}
67-
: (warning) => warning;
66+
: onwarn
67+
? (warning) => {
68+
if (!warning.code) return warning;
69+
let result: Warning | null = null;
70+
onwarn(warning, (reportWarn) => (result = reportWarn));
71+
return result;
72+
}
73+
: (warning) => warning;
6874

6975
const ignoreWarnings = Boolean(context.options[0]?.ignoreWarnings);
7076
const globalStyleRanges: [Position, Position][] = [];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @typedef {import("svelte/compiler").Warning} Warning
3+
*/
4+
module.exports = {
5+
languageOptions: {
6+
parserOptions: {
7+
svelteConfig: {
8+
warningFilter: (warning) => {
9+
return (
10+
warning.code !== 'a11y_missing_attribute' && warning.code !== 'a11y-missing-attribute'
11+
);
12+
}
13+
}
14+
}
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- message: |-
2+
Avoid using autofocus
3+
https://svelte.dev/e/a11y_autofocus(a11y_autofocus)
4+
line: 5
5+
column: 12
6+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let src = 'tutorial/image.gif';
3+
</script>
4+
5+
<img {src} autofocus />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelte": ">=5.0.0-0"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @typedef {import("svelte/compiler").Warning} Warning
3+
*/
4+
module.exports = {
5+
languageOptions: {
6+
parserOptions: {
7+
svelteConfig: {
8+
warningFilter: (warning) => {
9+
return (
10+
warning.code !== 'a11y_missing_attribute' && warning.code !== 'a11y-missing-attribute'
11+
);
12+
}
13+
}
14+
}
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let src = 'tutorial/image.gif';
3+
</script>
4+
5+
<img {src} />

0 commit comments

Comments
 (0)