-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: warn on unknown warning codes in runes mode (#11549)
Related to #11414 --------- Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com>
- Loading branch information
1 parent
f6b8004
commit 5cb432b
Showing
19 changed files
with
308 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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%`?) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 50 additions & 9 deletions
59
packages/svelte/src/compiler/utils/extract_svelte_ignore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/svelte/tests/parser-legacy/samples/comment-with-ignores/input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<!-- svelte-ignore foo bar --> | ||
<!-- svelte-ignore foo, bar --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/svelte/tests/validator/samples/ignore-warning-dash-backwards-compat/input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
1 change: 1 addition & 0 deletions
1
packages/svelte/tests/validator/samples/ignore-warning-dash-backwards-compat/warnings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/svelte/tests/validator/samples/ignore-warnings-cumulative/input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/svelte/tests/validator/samples/ignore-warnings-newline/input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/svelte/tests/validator/samples/ignore-warnings/input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/svelte/tests/validator/samples/unknown-code/input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Oops, something went wrong.