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 https://github.com/sveltejs/language-tools/issues/650
pull/12296/head
Simon Holthausen 2 years ago
parent eb3e677e05
commit 281debfc62

@ -0,0 +1,5 @@
---
'svelte': patch
---
feat: add ability to ignore warnings through compiler option

@ -44,7 +44,7 @@ export function pop_ignore() {
/**
* @param {string} source
* @param {{ filename?: string, rootDir?: string }} options
* @param {{ filename?: string, rootDir?: string, warnings?: { ignore?: string[] } }} options
*/
export function reset(source, options) {
const root_dir = options.rootDir?.replace(/\\/g, '/');
@ -63,4 +63,8 @@ export function reset(source, options) {
warnings = [];
ignore_stack = [];
ignore_map.clear();
if (options.warnings?.ignore) {
push_ignore(options.warnings.ignore);
}
}

@ -211,12 +211,20 @@ export interface ModuleCompileOptions {
* Used for debugging hints and sourcemaps. Your bundler plugin will set it automatically.
*/
filename?: string;
/**
* Used for ensuring filenames don't leak filesystem information. Your bundler plugin will set it automatically.
* @default process.cwd() on node-like environments, undefined elsewhere
*/
rootDir?: string;
/**
* Options related to Svelte's warnings
*/
warnings?: {
/**
* A list of warning codes to suppress. If a warning is emitted with a code in this list, it will be ignored.
*/
ignore?: string[];
};
}
// The following two somewhat scary looking types ensure that certain types are required but can be undefined still

@ -100,6 +100,10 @@ export const validate_component_options =
hmr: boolean(false),
warnings: object({
ignore: string_array([])
}),
sourcemap: validator(undefined, (input) => {
// Source maps can take on a variety of values, including string, JSON, map objects from magic-string and source-map,
// so there's no good way to check type validity here
@ -255,6 +259,20 @@ function string(fallback, allow_empty = true) {
});
}
/**
* @param {string[]} fallback
* @returns {Validator}
*/
function string_array(fallback) {
return validator(fallback, (input, keypath) => {
if (input && !Array.isArray(input)) {
throw_error(`${keypath} should be a string array, if specified`);
}
return input;
});
}
/**
* @param {boolean | undefined} fallback
* @returns {Validator}

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
compileOptions: {
warnings: {
ignore: ['a11y_missing_attribute', 'a11y_misplaced_scope']
}
}
});

@ -0,0 +1,8 @@
<div>
<img src="this-is-fine.jpg" />
<marquee>but this is still discouraged</marquee>
</div>
<div scope></div>
<img src="potato.jpg" />

@ -0,0 +1,14 @@
[
{
"code": "a11y_distracting_elements",
"end": {
"column": 49,
"line": 3
},
"message": "Avoid `<marquee>` elements",
"start": {
"column": 1,
"line": 3
}
}
]

@ -870,12 +870,20 @@ declare module 'svelte/compiler' {
* Used for debugging hints and sourcemaps. Your bundler plugin will set it automatically.
*/
filename?: string;
/**
* Used for ensuring filenames don't leak filesystem information. Your bundler plugin will set it automatically.
* @default process.cwd() on node-like environments, undefined elsewhere
*/
rootDir?: string;
/**
* Options related to Svelte's warnings
*/
warnings?: {
/**
* A list of warning codes to suppress. If a warning is emitted with a code in this list, it will be ignored.
*/
ignore?: string[];
};
}
type DeclarationKind =
@ -2693,12 +2701,20 @@ declare module 'svelte/types/compiler/interfaces' {
* Used for debugging hints and sourcemaps. Your bundler plugin will set it automatically.
*/
filename?: string;
/**
* Used for ensuring filenames don't leak filesystem information. Your bundler plugin will set it automatically.
* @default process.cwd() on node-like environments, undefined elsewhere
*/
rootDir?: string;
/**
* Options related to Svelte's warnings
*/
warnings?: {
/**
* A list of warning codes to suppress. If a warning is emitted with a code in this list, it will be ignored.
*/
ignore?: string[];
};
}
/**
* - `html` the default, for e.g. `<div>` or `<span>`

Loading…
Cancel
Save