Skip to content

feat: support warningFilter in valid-compile rule #984

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

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/great-turkeys-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': minor
---

feat: support `warningFilter` in `valid-compile` rule
21 changes: 19 additions & 2 deletions docs/rules/valid-compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Note that we exclude reports for some checks, such as `missing-declaration`, and

### Using `svelte.config.js`

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.
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.

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

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

#### warningFilter

This rule can use [`warningFilter`](https://svelte.dev/docs/svelte/svelte-compiler#ModuleCompileOptions).

Example:

```js
// svelte.config.js
export default {
warningFilter: (warning) => {
if (warning.code === 'a11y-distracting-elements') return false;
if (warning.code === 'a11y_distracting_elements') return false; // for Svelte v5
return true;
}
};
```

#### onwarn

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

Example:

Expand Down
18 changes: 12 additions & 6 deletions packages/eslint-plugin-svelte/src/rules/valid-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,22 @@ export default createRule('valid-compile', {
if (!sourceCode.parserServices.isSvelte) {
return {};
}
const onwarn = sourceCode.parserServices.svelteParseContext?.svelteConfig?.onwarn;
const { onwarn, warningFilter } =
sourceCode.parserServices.svelteParseContext?.svelteConfig ?? {};

const transform: (warning: Warning) => Warning | null = onwarn
const transform: (warning: Warning) => Warning | null = warningFilter
? (warning) => {
if (!warning.code) return warning;
let result: Warning | null = null;
onwarn(warning, (reportWarn) => (result = reportWarn));
return result;
return warningFilter(warning) ? warning : null;
}
: (warning) => warning;
: onwarn
? (warning) => {
if (!warning.code) return warning;
let result: Warning | null = null;
onwarn(warning, (reportWarn) => (result = reportWarn));
return result;
}
: (warning) => warning;

const ignoreWarnings = Boolean(context.options[0]?.ignoreWarnings);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @typedef {import("svelte/compiler").Warning} Warning
*/
module.exports = {
languageOptions: {
parserOptions: {
svelteConfig: {
warningFilter: (warning) => {
return (
warning.code !== 'a11y_missing_attribute' && warning.code !== 'a11y-missing-attribute'
);
}
}
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- message: |-
Avoid using autofocus
https://svelte.dev/e/a11y_autofocus(a11y_autofocus)
line: 5
column: 12
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let src = 'tutorial/image.gif';
</script>

<img {src} autofocus />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelte": ">=5.0.0-0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @typedef {import("svelte/compiler").Warning} Warning
*/
module.exports = {
languageOptions: {
parserOptions: {
svelteConfig: {
warningFilter: (warning) => {
return (
warning.code !== 'a11y_missing_attribute' && warning.code !== 'a11y-missing-attribute'
);
}
}
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let src = 'tutorial/image.gif';
</script>

<img {src} />
Loading