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 @@ + + + +
+ +
+ + +
diff --git a/packages/svelte/tests/validator/samples/ignore-warning-dash-backwards-compat/warnings.json b/packages/svelte/tests/validator/samples/ignore-warning-dash-backwards-compat/warnings.json new file mode 100644 index 0000000000..fe51488c70 --- /dev/null +++ b/packages/svelte/tests/validator/samples/ignore-warning-dash-backwards-compat/warnings.json @@ -0,0 +1 @@ +[] diff --git a/packages/svelte/tests/validator/samples/ignore-warning/input.svelte b/packages/svelte/tests/validator/samples/ignore-warning/input.svelte index cfbe0e538d..7a2328e57c 100644 --- a/packages/svelte/tests/validator/samples/ignore-warning/input.svelte +++ b/packages/svelte/tests/validator/samples/ignore-warning/input.svelte @@ -4,4 +4,7 @@ but this is still discouraged + +
+ diff --git a/packages/svelte/tests/validator/samples/ignore-warning/warnings.json b/packages/svelte/tests/validator/samples/ignore-warning/warnings.json index 926543bf3f..3997af9dbd 100644 --- a/packages/svelte/tests/validator/samples/ignore-warning/warnings.json +++ b/packages/svelte/tests/validator/samples/ignore-warning/warnings.json @@ -15,12 +15,12 @@ "code": "a11y_missing_attribute", "end": { "column": 22, - "line": 7 + "line": 10 }, "message": "`` element should have an alt attribute", "start": { "column": 0, - "line": 7 + "line": 10 } } ] diff --git a/packages/svelte/tests/validator/samples/ignore-warnings-cumulative/input.svelte b/packages/svelte/tests/validator/samples/ignore-warnings-cumulative/input.svelte index 371d06b6e7..10bc16b272 100644 --- a/packages/svelte/tests/validator/samples/ignore-warnings-cumulative/input.svelte +++ b/packages/svelte/tests/validator/samples/ignore-warnings-cumulative/input.svelte @@ -1,4 +1,4 @@ - +
diff --git a/packages/svelte/tests/validator/samples/ignore-warnings-newline/input.svelte b/packages/svelte/tests/validator/samples/ignore-warnings-newline/input.svelte index 8471d1d539..e0f1b6929b 100644 --- a/packages/svelte/tests/validator/samples/ignore-warnings-newline/input.svelte +++ b/packages/svelte/tests/validator/samples/ignore-warnings-newline/input.svelte @@ -1,4 +1,4 @@ -
diff --git a/packages/svelte/tests/validator/samples/ignore-warnings/input.svelte b/packages/svelte/tests/validator/samples/ignore-warnings/input.svelte index a3999ad901..3c58089a77 100644 --- a/packages/svelte/tests/validator/samples/ignore-warnings/input.svelte +++ b/packages/svelte/tests/validator/samples/ignore-warnings/input.svelte @@ -1,4 +1,4 @@ - +
but this is still discouraged diff --git a/packages/svelte/tests/validator/samples/unknown-code/input.svelte b/packages/svelte/tests/validator/samples/unknown-code/input.svelte new file mode 100644 index 0000000000..d8f3a232e7 --- /dev/null +++ b/packages/svelte/tests/validator/samples/unknown-code/input.svelte @@ -0,0 +1,20 @@ + + + +
+ +
+ + +
+ +
+ + +
+ + +
+ + +
diff --git a/packages/svelte/tests/validator/samples/unknown-code/warnings.json b/packages/svelte/tests/validator/samples/unknown-code/warnings.json new file mode 100644 index 0000000000..2781803b16 --- /dev/null +++ b/packages/svelte/tests/validator/samples/unknown-code/warnings.json @@ -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": "`` element should have an alt attribute", + "start": { + "column": 1, + "line": 5 + } + }, + { + "code": "a11y_missing_attribute", + "end": { + "column": 29, + "line": 10 + }, + "message": "`` 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 `` elements", + "start": { + "column": 5, + "line": 14 + } + } +]