Skip to content

Commit 281debf

Browse files
committed
feat: add ability to ignore warnings through compiler option
Some people want to disable certain warnings for their whole application without having to add svelte-ignore comments everywhere. This new option makes that possible. closes #9188 part of sveltejs/language-tools#650
1 parent eb3e677 commit 281debf

File tree

8 files changed

+86
-4
lines changed

8 files changed

+86
-4
lines changed

.changeset/little-seals-reflect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
feat: add ability to ignore warnings through compiler option

packages/svelte/src/compiler/state.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function pop_ignore() {
4444

4545
/**
4646
* @param {string} source
47-
* @param {{ filename?: string, rootDir?: string }} options
47+
* @param {{ filename?: string, rootDir?: string, warnings?: { ignore?: string[] } }} options
4848
*/
4949
export function reset(source, options) {
5050
const root_dir = options.rootDir?.replace(/\\/g, '/');
@@ -63,4 +63,8 @@ export function reset(source, options) {
6363
warnings = [];
6464
ignore_stack = [];
6565
ignore_map.clear();
66+
67+
if (options.warnings?.ignore) {
68+
push_ignore(options.warnings.ignore);
69+
}
6670
}

packages/svelte/src/compiler/types/index.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,20 @@ export interface ModuleCompileOptions {
211211
* Used for debugging hints and sourcemaps. Your bundler plugin will set it automatically.
212212
*/
213213
filename?: string;
214-
215214
/**
216215
* Used for ensuring filenames don't leak filesystem information. Your bundler plugin will set it automatically.
217216
* @default process.cwd() on node-like environments, undefined elsewhere
218217
*/
219218
rootDir?: string;
219+
/**
220+
* Options related to Svelte's warnings
221+
*/
222+
warnings?: {
223+
/**
224+
* A list of warning codes to suppress. If a warning is emitted with a code in this list, it will be ignored.
225+
*/
226+
ignore?: string[];
227+
};
220228
}
221229

222230
// The following two somewhat scary looking types ensure that certain types are required but can be undefined still

packages/svelte/src/compiler/validate-options.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export const validate_component_options =
100100

101101
hmr: boolean(false),
102102

103+
warnings: object({
104+
ignore: string_array([])
105+
}),
106+
103107
sourcemap: validator(undefined, (input) => {
104108
// Source maps can take on a variety of values, including string, JSON, map objects from magic-string and source-map,
105109
// so there's no good way to check type validity here
@@ -255,6 +259,20 @@ function string(fallback, allow_empty = true) {
255259
});
256260
}
257261

262+
/**
263+
* @param {string[]} fallback
264+
* @returns {Validator}
265+
*/
266+
function string_array(fallback) {
267+
return validator(fallback, (input, keypath) => {
268+
if (input && !Array.isArray(input)) {
269+
throw_error(`${keypath} should be a string array, if specified`);
270+
}
271+
272+
return input;
273+
});
274+
}
275+
258276
/**
259277
* @param {boolean | undefined} fallback
260278
* @returns {Validator}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
compileOptions: {
5+
warnings: {
6+
ignore: ['a11y_missing_attribute', 'a11y_misplaced_scope']
7+
}
8+
}
9+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div>
2+
<img src="this-is-fine.jpg" />
3+
<marquee>but this is still discouraged</marquee>
4+
</div>
5+
6+
<div scope></div>
7+
8+
<img src="potato.jpg" />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "a11y_distracting_elements",
4+
"end": {
5+
"column": 49,
6+
"line": 3
7+
},
8+
"message": "Avoid `<marquee>` elements",
9+
"start": {
10+
"column": 1,
11+
"line": 3
12+
}
13+
}
14+
]

packages/svelte/types/index.d.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,12 +870,20 @@ declare module 'svelte/compiler' {
870870
* Used for debugging hints and sourcemaps. Your bundler plugin will set it automatically.
871871
*/
872872
filename?: string;
873-
874873
/**
875874
* Used for ensuring filenames don't leak filesystem information. Your bundler plugin will set it automatically.
876875
* @default process.cwd() on node-like environments, undefined elsewhere
877876
*/
878877
rootDir?: string;
878+
/**
879+
* Options related to Svelte's warnings
880+
*/
881+
warnings?: {
882+
/**
883+
* A list of warning codes to suppress. If a warning is emitted with a code in this list, it will be ignored.
884+
*/
885+
ignore?: string[];
886+
};
879887
}
880888

881889
type DeclarationKind =
@@ -2693,12 +2701,20 @@ declare module 'svelte/types/compiler/interfaces' {
26932701
* Used for debugging hints and sourcemaps. Your bundler plugin will set it automatically.
26942702
*/
26952703
filename?: string;
2696-
26972704
/**
26982705
* Used for ensuring filenames don't leak filesystem information. Your bundler plugin will set it automatically.
26992706
* @default process.cwd() on node-like environments, undefined elsewhere
27002707
*/
27012708
rootDir?: string;
2709+
/**
2710+
* Options related to Svelte's warnings
2711+
*/
2712+
warnings?: {
2713+
/**
2714+
* A list of warning codes to suppress. If a warning is emitted with a code in this list, it will be ignored.
2715+
*/
2716+
ignore?: string[];
2717+
};
27022718
}
27032719
/**
27042720
* - `html` — the default, for e.g. `<div>` or `<span>`

0 commit comments

Comments
 (0)