Skip to content

Commit

Permalink
feat: add ability to ignore warnings through compiler option
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dummdidumm committed Jul 4, 2024
1 parent eb3e677 commit 281debf
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-seals-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: add ability to ignore warnings through compiler option
6 changes: 5 additions & 1 deletion packages/svelte/src/compiler/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/');
Expand All @@ -63,4 +63,8 @@ export function reset(source, options) {
warnings = [];
ignore_stack = [];
ignore_map.clear();

if (options.warnings?.ignore) {
push_ignore(options.warnings.ignore);
}
}
10 changes: 9 additions & 1 deletion packages/svelte/src/compiler/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions packages/svelte/src/compiler/validate-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from '../../test';

export default test({
compileOptions: {
warnings: {
ignore: ['a11y_missing_attribute', 'a11y_misplaced_scope']
}
}
});
Original file line number Diff line number Diff line change
@@ -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" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "a11y_distracting_elements",
"end": {
"column": 49,
"line": 3
},
"message": "Avoid `<marquee>` elements",
"start": {
"column": 1,
"line": 3
}
}
]
20 changes: 18 additions & 2 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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>`
Expand Down

0 comments on commit 281debf

Please sign in to comment.