mirror of https://github.com/sveltejs/svelte
feat: warn on unknown warning codes in runes mode (#11549)
Related to #11414 --------- Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com>pull/11606/head
parent
f6b800414d
commit
5cb432b7bd
@ -0,0 +1,9 @@
|
||||
## legacy_code
|
||||
|
||||
> `%code%` is no longer valid — please use `%suggestion%` instead
|
||||
|
||||
## unknown_code
|
||||
|
||||
> `%code%` is not a recognised code
|
||||
|
||||
> `%code%` is not a recognised code (did you mean `%suggestion%`?)
|
@ -1,17 +1,58 @@
|
||||
import { regex_whitespace } from '../phases/patterns.js';
|
||||
import fuzzymatch from '../phases/1-parse/utils/fuzzymatch.js';
|
||||
import * as w from '../warnings.js';
|
||||
|
||||
const regex_svelte_ignore = /^\s*svelte-ignore\s+([\s\S]+)\s*$/m;
|
||||
const regex_svelte_ignore = /^\s*svelte-ignore\s/;
|
||||
|
||||
/** @type {Record<string, string>} */
|
||||
const replacements = {
|
||||
'non-top-level-reactive-declaration': 'reactive_declaration_invalid_placement'
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {number} offset
|
||||
* @param {string} text
|
||||
* @param {boolean} runes
|
||||
* @returns {string[]}
|
||||
*/
|
||||
export function extract_svelte_ignore(text) {
|
||||
export function extract_svelte_ignore(offset, text, runes) {
|
||||
const match = regex_svelte_ignore.exec(text);
|
||||
return match
|
||||
? match[1]
|
||||
.split(regex_whitespace)
|
||||
.map(/** @param {any} x */ (x) => x.trim())
|
||||
.filter(Boolean)
|
||||
: [];
|
||||
if (!match) return [];
|
||||
|
||||
let length = match[0].length;
|
||||
offset += length;
|
||||
|
||||
/** @type {string[]} */
|
||||
const ignores = [];
|
||||
|
||||
// Warnings have to be separated by commas, everything after is interpreted as prose
|
||||
for (const match of text.slice(length).matchAll(/([\w$-]+)(,)?/gm)) {
|
||||
const code = match[1];
|
||||
|
||||
ignores.push(code);
|
||||
|
||||
if (!w.codes.includes(code)) {
|
||||
const replacement = replacements[code] ?? code.replace(/-/g, '_');
|
||||
|
||||
if (runes) {
|
||||
// The type cast is for some reason necessary to pass the type check in CI
|
||||
const start = offset + /** @type {number} */ (match.index);
|
||||
const end = start + code.length;
|
||||
|
||||
if (w.codes.includes(replacement)) {
|
||||
w.legacy_code({ start, end }, code, replacement);
|
||||
} else {
|
||||
const suggestion = fuzzymatch(code, w.codes);
|
||||
w.unknown_code({ start, end }, code, suggestion);
|
||||
}
|
||||
} else if (w.codes.includes(replacement)) {
|
||||
ignores.push(replacement);
|
||||
}
|
||||
}
|
||||
|
||||
if (!match[2]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ignores;
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
<!-- svelte-ignore foo bar -->
|
||||
<!-- svelte-ignore foo, bar -->
|
||||
|
@ -0,0 +1,14 @@
|
||||
<script>
|
||||
function foo() {
|
||||
// svelte-ignore non-top-level-reactive-declaration
|
||||
$: x = 1;
|
||||
}
|
||||
</script>
|
||||
|
||||
<!-- svelte-ignore a11y-missing-attribute -->
|
||||
<div>
|
||||
<img src="this-is-fine.jpg">
|
||||
</div>
|
||||
|
||||
<!-- svelte-ignore a11y-misplaced-scope -->
|
||||
<div scope></div>
|
@ -0,0 +1 @@
|
||||
[]
|
@ -0,0 +1,20 @@
|
||||
<svelte:options runes={true} />
|
||||
|
||||
<!-- svelte-ignore a11y-missing-attribute -->
|
||||
<div>
|
||||
<img src="this-is-fine.jpg">
|
||||
</div>
|
||||
|
||||
<!-- svelte-ignore ally_missing_attribute -->
|
||||
<div>
|
||||
<img src="this-is-fine.jpg">
|
||||
</div>
|
||||
|
||||
<!-- svelte-ignore a11y-misplaced-scope -->
|
||||
<div scope></div>
|
||||
|
||||
<!-- svelte-ignore a11y_misplaced_scope this is some prose -->
|
||||
<div scope></div>
|
||||
|
||||
<!-- svelte-ignore a11y_misplaced_scope this_is some-ambiguous prose -->
|
||||
<div scope></div>
|
@ -0,0 +1,74 @@
|
||||
[
|
||||
{
|
||||
"code": "legacy_code",
|
||||
"end": {
|
||||
"column": 41,
|
||||
"line": 3
|
||||
},
|
||||
"message": "`a11y-missing-attribute` is no longer valid — please use `a11y_missing_attribute` instead",
|
||||
"start": {
|
||||
"column": 19,
|
||||
"line": 3
|
||||
}
|
||||
},
|
||||
{
|
||||
"code": "unknown_code",
|
||||
"end": {
|
||||
"column": 41,
|
||||
"line": 8
|
||||
},
|
||||
"message": "`ally_missing_attribute` is not a recognised code (did you mean `a11y_missing_attribute`?)",
|
||||
"start": {
|
||||
"column": 19,
|
||||
"line": 8
|
||||
}
|
||||
},
|
||||
{
|
||||
"code": "legacy_code",
|
||||
"end": {
|
||||
"column": 39,
|
||||
"line": 13
|
||||
},
|
||||
"message": "`a11y-misplaced-scope` is no longer valid — please use `a11y_misplaced_scope` instead",
|
||||
"start": {
|
||||
"column": 19,
|
||||
"line": 13
|
||||
}
|
||||
},
|
||||
{
|
||||
"code": "a11y_missing_attribute",
|
||||
"end": {
|
||||
"column": 29,
|
||||
"line": 5
|
||||
},
|
||||
"message": "`<img>` element should have an alt attribute",
|
||||
"start": {
|
||||
"column": 1,
|
||||
"line": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"code": "a11y_missing_attribute",
|
||||
"end": {
|
||||
"column": 29,
|
||||
"line": 10
|
||||
},
|
||||
"message": "`<img>` element should have an alt attribute",
|
||||
"start": {
|
||||
"column": 1,
|
||||
"line": 10
|
||||
}
|
||||
},
|
||||
{
|
||||
"code": "a11y_misplaced_scope",
|
||||
"end": {
|
||||
"column": 10,
|
||||
"line": 14
|
||||
},
|
||||
"message": "The scope attribute should only be used with `<th>` elements",
|
||||
"start": {
|
||||
"column": 5,
|
||||
"line": 14
|
||||
}
|
||||
}
|
||||
]
|
Loading…
Reference in new issue