From 1784026843d2f7de6e449a5587da5dc30d467818 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Fri, 17 May 2024 17:34:26 +0200 Subject: [PATCH] fix: don't require commas between warnings in legacy mode (#11669) also add commas in migration closes #11666 --- .changeset/twelve-beans-drive.md | 5 ++ .../compiler/utils/extract_svelte_ignore.js | 47 +++++++++++++------ .../samples/svelte-ignore/input.svelte | 3 ++ .../samples/svelte-ignore/output.svelte | 3 ++ .../input.svelte | 6 +++ 5 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 .changeset/twelve-beans-drive.md diff --git a/.changeset/twelve-beans-drive.md b/.changeset/twelve-beans-drive.md new file mode 100644 index 0000000000..caa1ec8694 --- /dev/null +++ b/.changeset/twelve-beans-drive.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: don't require warning codes to be separated by commas in non-runes mode diff --git a/packages/svelte/src/compiler/utils/extract_svelte_ignore.js b/packages/svelte/src/compiler/utils/extract_svelte_ignore.js index 1f5d29db7d..aa2d2d3f95 100644 --- a/packages/svelte/src/compiler/utils/extract_svelte_ignore.js +++ b/packages/svelte/src/compiler/utils/extract_svelte_ignore.js @@ -31,16 +31,16 @@ export function extract_svelte_ignore(offset, text, runes) { /** @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]; + if (runes) { + // 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)) { + ignores.push(code); + } else { + const replacement = replacements[code] ?? code.replace(/-/g, '_'); - 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; @@ -51,13 +51,26 @@ export function extract_svelte_ignore(offset, text, runes) { 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; } } + } else { + // Non-runes mode: lax parsing, backwards compat with old codes + for (const match of text.slice(length).matchAll(/[\w$-]+/gm)) { + const code = match[0]; + + ignores.push(code); + + if (!w.codes.includes(code)) { + const replacement = replacements[code] ?? code.replace(/-/g, '_'); - if (!match[2]) { - break; + if (w.codes.includes(replacement)) { + ignores.push(replacement); + } + } } } @@ -76,8 +89,12 @@ export function migrate_svelte_ignore(text) { const length = match[0].length; return ( text.substring(0, length) + - text - .substring(length) - .replace(/\w+-\w+(-\w+)*/g, (code) => replacements[code] ?? code.replace(/-/g, '_')) + text.substring(length).replace(/\w+-\w+(-\w+)*/g, (code, _, idx) => { + let replacement = replacements[code] ?? code.replace(/-/g, '_'); + if (/\w+-\w+/.test(text.substring(length + idx + code.length))) { + replacement += ','; + } + return replacement; + }) ); } diff --git a/packages/svelte/tests/migrate/samples/svelte-ignore/input.svelte b/packages/svelte/tests/migrate/samples/svelte-ignore/input.svelte index fa734942f9..0ad53cc071 100644 --- a/packages/svelte/tests/migrate/samples/svelte-ignore/input.svelte +++ b/packages/svelte/tests/migrate/samples/svelte-ignore/input.svelte @@ -6,4 +6,7 @@ +
+ + \ No newline at end of file diff --git a/packages/svelte/tests/migrate/samples/svelte-ignore/output.svelte b/packages/svelte/tests/migrate/samples/svelte-ignore/output.svelte index fa305cefb1..ea68364ffd 100644 --- a/packages/svelte/tests/migrate/samples/svelte-ignore/output.svelte +++ b/packages/svelte/tests/migrate/samples/svelte-ignore/output.svelte @@ -6,4 +6,7 @@ + + + \ No newline at end of file diff --git a/packages/svelte/tests/validator/samples/ignore-warning-dash-backwards-compat/input.svelte b/packages/svelte/tests/validator/samples/ignore-warning-dash-backwards-compat/input.svelte index faae4636e5..0008e99737 100644 --- a/packages/svelte/tests/validator/samples/ignore-warning-dash-backwards-compat/input.svelte +++ b/packages/svelte/tests/validator/samples/ignore-warning-dash-backwards-compat/input.svelte @@ -12,3 +12,9 @@ + + +