From fa2daf479e76f0e113259642f7021782913cfff2 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Tue, 5 Mar 2024 17:05:57 +0100 Subject: [PATCH] illegal-attribute-character --- .../compiler/phases/2-analyze/validation.js | 25 +++++++++++++++++-- packages/svelte/src/compiler/warnings.js | 4 ++- .../illegal-attribute-character/_config.js | 3 --- 3 files changed, 26 insertions(+), 6 deletions(-) delete mode 100644 packages/svelte/tests/validator/samples/illegal-attribute-character/_config.js diff --git a/packages/svelte/src/compiler/phases/2-analyze/validation.js b/packages/svelte/src/compiler/phases/2-analyze/validation.js index 01afe0d694..94233681ca 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/validation.js +++ b/packages/svelte/src/compiler/phases/2-analyze/validation.js @@ -49,8 +49,12 @@ function validate_component(node, context) { error(attribute, 'invalid-event-modifier'); } - if (attribute.type === 'Attribute' && attribute.name === 'slot') { - validate_slot_attribute(context, attribute); + if (attribute.type === 'Attribute') { + validate_attribute_name(attribute, context); + + if (attribute.name === 'slot') { + validate_slot_attribute(context, attribute); + } } } @@ -76,6 +80,8 @@ function validate_element(node, context) { error(attribute, 'invalid-attribute-name', attribute.name); } + validate_attribute_name(attribute, context); + if (attribute.name.startsWith('on') && attribute.name.length > 2) { if (!is_expression_attribute(attribute)) { error(attribute, 'invalid-event-attribute-value'); @@ -166,6 +172,21 @@ function validate_element(node, context) { } } +/** + * @param {import('#compiler').Attribute} attribute + * @param {import('zimmerframe').Context} context + */ +function validate_attribute_name(attribute, context) { + if ( + attribute.name.includes(':') && + !attribute.name.startsWith('xmlns:') && + !attribute.name.startsWith('xlink:') && + !attribute.name.startsWith('xml:') + ) { + warn(context.state.analysis.warnings, attribute, context.path, 'illegal-attribute-character'); + } +} + /** * @param {import('zimmerframe').Context} context * @param {import('#compiler').Attribute} attribute diff --git a/packages/svelte/src/compiler/warnings.js b/packages/svelte/src/compiler/warnings.js index 20375bca76..c3f9b33364 100644 --- a/packages/svelte/src/compiler/warnings.js +++ b/packages/svelte/src/compiler/warnings.js @@ -15,7 +15,9 @@ const attributes = { 'avoid-is': () => 'The "is" attribute is not supported cross-browser and should be avoided', /** @param {string} name */ 'global-event-reference': (name) => - `You are referencing globalThis.${name}. Did you forget to declare a variable with that name?` + `You are referencing globalThis.${name}. Did you forget to declare a variable with that name?`, + 'illegal-attribute-character': () => + "Attributes should not contain ':' characters to prevent ambiguity with Svelte directives" }; /** @satisfies {Warnings} */ diff --git a/packages/svelte/tests/validator/samples/illegal-attribute-character/_config.js b/packages/svelte/tests/validator/samples/illegal-attribute-character/_config.js deleted file mode 100644 index 64fdc120d6..0000000000 --- a/packages/svelte/tests/validator/samples/illegal-attribute-character/_config.js +++ /dev/null @@ -1,3 +0,0 @@ -import { test } from '../../test'; - -export default test({ skip: true });