diff --git a/packages/svelte/messages/compile-warnings/misc.md b/packages/svelte/messages/compile-warnings/misc.md new file mode 100644 index 0000000000..3b977db1be --- /dev/null +++ b/packages/svelte/messages/compile-warnings/misc.md @@ -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%`?) diff --git a/packages/svelte/scripts/process-messages/index.js b/packages/svelte/scripts/process-messages/index.js index a9fc519b60..c45222fa47 100644 --- a/packages/svelte/scripts/process-messages/index.js +++ b/packages/svelte/scripts/process-messages/index.js @@ -61,7 +61,7 @@ function transform(name, dest) { const comments = []; - const ast = acorn.parse(source, { + let ast = acorn.parse(source, { ecmaVersion: 'latest', sourceType: 'module', onComment: (block, value, start, end) => { @@ -80,7 +80,7 @@ function transform(name, dest) { } }); - walk(ast, null, { + ast = walk(ast, null, { _(node, { next }) { let comment; @@ -100,6 +100,18 @@ function transform(name, dest) { node.trailingComments = [comments.shift()]; } } + }, + // @ts-expect-error + Identifier(node, context) { + if (node.name === 'CODES') { + return { + type: 'ArrayExpression', + elements: Object.keys(messages[name]).map((code) => ({ + type: 'Literal', + value: code + })) + }; + } } }); diff --git a/packages/svelte/scripts/process-messages/templates/compile-warnings.js b/packages/svelte/scripts/process-messages/templates/compile-warnings.js index cac547de60..e96558edad 100644 --- a/packages/svelte/scripts/process-messages/templates/compile-warnings.js +++ b/packages/svelte/scripts/process-messages/templates/compile-warnings.js @@ -42,6 +42,8 @@ function w(node, code, message) { }); } +export const codes = CODES; + /** * MESSAGE * @param {null | NodeLike} node diff --git a/packages/svelte/src/compiler/legacy.js b/packages/svelte/src/compiler/legacy.js index 0357953060..513d71452a 100644 --- a/packages/svelte/src/compiler/legacy.js +++ b/packages/svelte/src/compiler/legacy.js @@ -201,7 +201,7 @@ export function convert(source, ast) { Comment(node) { return { ...node, - ignores: extract_svelte_ignore(node.data) + ignores: extract_svelte_ignore(node.start, node.data, false) }; }, ComplexSelector(node) { diff --git a/packages/svelte/src/compiler/phases/2-analyze/a11y.js b/packages/svelte/src/compiler/phases/2-analyze/a11y.js index a45c3c1f97..f40fc7c12e 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/a11y.js +++ b/packages/svelte/src/compiler/phases/2-analyze/a11y.js @@ -727,6 +727,9 @@ function check_element(node, state) { for (const attribute of node.attributes) { if (attribute.type !== 'Attribute') continue; + // @ts-expect-error gross hack + attribute.ignores = node.ignores; + const name = attribute.name.toLowerCase(); // aria-props if (name.startsWith('aria-')) { diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index 7c4c65d57a..041f8d8dd2 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -559,10 +559,14 @@ export function analyze_component(root, source, options) { prune(analysis.css.ast, element); } - if ( - !analysis.css.ast.content.comment || - !extract_svelte_ignore(analysis.css.ast.content.comment.data).includes('css_unused_selector') - ) { + const { comment } = analysis.css.ast.content; + const should_ignore_unused = + comment && + extract_svelte_ignore(comment.start, comment.data, analysis.runes).includes( + 'css_unused_selector' + ); + + if (!should_ignore_unused) { warn_unused(analysis.css.ast); } @@ -1105,7 +1109,8 @@ const common_visitors = { const ignores = []; for (const comment of comments) { - ignores.push(...extract_svelte_ignore(comment.value)); + const start = /** @type {any} */ (comment).start + 2; + ignores.push(...extract_svelte_ignore(start, comment.value, context.state.analysis.runes)); } if (ignores.length > 0) { @@ -1136,7 +1141,11 @@ const common_visitors = { } if (child.type === 'Comment') { - ignores.push(...extract_svelte_ignore(child.data)); + const start = + child.start + + (context.state.analysis.source.slice(child.start, child.start + 4) === ' \ No newline at end of file + diff --git a/packages/svelte/tests/parser-legacy/samples/comment-with-ignores/output.json b/packages/svelte/tests/parser-legacy/samples/comment-with-ignores/output.json index b149436a27..cf2dc9f752 100644 --- a/packages/svelte/tests/parser-legacy/samples/comment-with-ignores/output.json +++ b/packages/svelte/tests/parser-legacy/samples/comment-with-ignores/output.json @@ -2,13 +2,13 @@ "html": { "type": "Fragment", "start": 0, - "end": 30, + "end": 31, "children": [ { "type": "Comment", "start": 0, - "end": 30, - "data": " svelte-ignore foo bar ", + "end": 31, + "data": " svelte-ignore foo, bar ", "ignores": ["foo", "bar"] } ] 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 new file mode 100644 index 0000000000..faae4636e5 --- /dev/null +++ b/packages/svelte/tests/validator/samples/ignore-warning-dash-backwards-compat/input.svelte @@ -0,0 +1,14 @@ + + + +