diff --git a/.changeset/little-seals-reflect.md b/.changeset/little-seals-reflect.md new file mode 100644 index 000000000000..2c3b234b1547 --- /dev/null +++ b/.changeset/little-seals-reflect.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +feat: add ability to ignore warnings through compiler option diff --git a/packages/svelte/src/compiler/state.js b/packages/svelte/src/compiler/state.js index 1e094d95fd46..30a279cde4a3 100644 --- a/packages/svelte/src/compiler/state.js +++ b/packages/svelte/src/compiler/state.js @@ -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); + } } diff --git a/packages/svelte/src/compiler/types/index.d.ts b/packages/svelte/src/compiler/types/index.d.ts index 129b5da175cc..849d6fe4d711 100644 --- a/packages/svelte/src/compiler/types/index.d.ts +++ b/packages/svelte/src/compiler/types/index.d.ts @@ -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 diff --git a/packages/svelte/src/compiler/validate-options.js b/packages/svelte/src/compiler/validate-options.js index bd997a36ab8c..3ddc18f840d3 100644 --- a/packages/svelte/src/compiler/validate-options.js +++ b/packages/svelte/src/compiler/validate-options.js @@ -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} diff --git a/packages/svelte/tests/validator/samples/ignore-warning-compiler-option/_config.js b/packages/svelte/tests/validator/samples/ignore-warning-compiler-option/_config.js new file mode 100644 index 000000000000..36d6982b4483 --- /dev/null +++ b/packages/svelte/tests/validator/samples/ignore-warning-compiler-option/_config.js @@ -0,0 +1,9 @@ +import { test } from '../../test'; + +export default test({ + compileOptions: { + warnings: { + ignore: ['a11y_missing_attribute', 'a11y_misplaced_scope'] + } + } +}); diff --git a/packages/svelte/tests/validator/samples/ignore-warning-compiler-option/input.svelte b/packages/svelte/tests/validator/samples/ignore-warning-compiler-option/input.svelte new file mode 100644 index 000000000000..b09c9e9ff9e5 --- /dev/null +++ b/packages/svelte/tests/validator/samples/ignore-warning-compiler-option/input.svelte @@ -0,0 +1,8 @@ +