convert element errors

pull/11294/head
Rich Harris 2 years ago
parent 741b744194
commit abfbd06f35

@ -1,6 +1,6 @@
## invalid_textarea_content
A <textarea> can have either a value attribute or (equivalently) child content, but not both
A `<textarea>` can have either a value attribute or (equivalently) child content, but not both
## invalid_void_content
@ -12,7 +12,7 @@ Void elements cannot have children or closing tags
## invalid_tag_name
TODO
Expected valid tag name
## invalid_node_placement
@ -20,8 +20,8 @@ TODO
## illegal_title_attribute
TODO
`<title>` cannot have attributes nor directives
## invalid_title_content
TODO
`<title>` can only contain text and {tags}

@ -20,28 +20,6 @@ const internal = {
`Internal compiler error: ${message}. Please report this to https://github.com/sveltejs/svelte/issues`
};
/** @satisfies {Errors} */
const elements = {
'invalid-textarea-content': () =>
`A <textarea> can have either a value attribute or (equivalently) child content, but not both`,
'invalid-void-content': () => `Void elements cannot have children or closing tags`,
/** @param {string} name */
'invalid-element-content': (name) => `<${name}> cannot have children`,
'invalid-tag-name': () => 'Expected valid tag name',
/**
* @param {string} node
* @param {string} parent
*/
'invalid-node-placement': (node, parent) => `${node} is invalid inside <${parent}>`,
'illegal-title-attribute': () => '<title> cannot have attributes nor directives',
'invalid-title-content': () => '<title> can only contain text and {tags}'
};
/** @satisfies {Errors} */
const components = {
'invalid-component-directive': () => `This type of directive is not valid on components`
};
/** @satisfies {Errors} */
const attributes = {
'empty-attribute-shorthand': () => `Attribute shorthand cannot be empty`,
@ -171,8 +149,6 @@ const const_tag = {
/** @satisfies {Errors} */
const errors = {
...internal,
...elements,
...components,
...attributes,
...slots,
...bindings,

@ -105,9 +105,8 @@ export default function tag(parser) {
['svelte:options', 'svelte:window', 'svelte:body', 'svelte:document'].includes(name) &&
/** @type {import('#compiler').ElementLike} */ (parent).fragment.nodes.length
) {
error(
e.invalid_element_content(
/** @type {import('#compiler').ElementLike} */ (parent).fragment.nodes[0].start,
'invalid-element-content',
name
);
}
@ -169,7 +168,7 @@ export default function tag(parser) {
if (is_closing_tag) {
if (is_void(name)) {
error(start, 'invalid-void-content');
e.invalid_void_content(start);
}
parser.eat('>', true);
@ -416,7 +415,7 @@ function read_tag_name(parser) {
}
if (!valid_tag_name.test(name)) {
error(start, 'invalid-tag-name');
e.invalid_tag_name(start);
}
return name;

@ -45,7 +45,7 @@ function validate_component(node, context) {
attribute.type !== 'OnDirective' &&
attribute.type !== 'BindDirective'
) {
error(attribute, 'invalid-component-directive');
e.invalid_component_directive(attribute);
}
if (
@ -528,7 +528,7 @@ const validation = {
if (node.name === 'textarea' && node.fragment.nodes.length > 0) {
for (const attribute of node.attributes) {
if (attribute.type === 'Attribute' && attribute.name === 'value') {
error(node, 'invalid-textarea-content');
e.invalid_textarea_content(node);
}
}
}
@ -552,7 +552,7 @@ const validation = {
if (context.state.parent_element) {
if (!is_tag_valid_with_parent(node.name, context.state.parent_element)) {
error(node, 'invalid-node-placement', `<${node.name}>`, context.state.parent_element);
e.invalid_node_placement(node, `<${node.name}>`, context.state.parent_element);
}
}
@ -564,7 +564,7 @@ const validation = {
parent.name === node.name &&
interactive_elements.has(parent.name)
) {
error(node, 'invalid-node-placement', `<${node.name}>`, parent.name);
e.invalid_node_placement(node, `<${node.name}>`, parent.name);
}
}
}
@ -573,7 +573,7 @@ const validation = {
const path = context.path;
for (let parent of path) {
if (parent.type === 'RegularElement' && parent.name === 'p') {
error(node, 'invalid-node-placement', `<${node.name}>`, parent.name);
e.invalid_node_placement(node, `<${node.name}>`, parent.name);
}
}
}
@ -729,19 +729,19 @@ const validation = {
if (!node.parent) return;
if (context.state.parent_element && regex_not_whitespace.test(node.data)) {
if (!is_tag_valid_with_parent('#text', context.state.parent_element)) {
error(node, 'invalid-node-placement', 'Text node', context.state.parent_element);
e.invalid_node_placement(node, 'Text node', context.state.parent_element);
}
}
},
TitleElement(node) {
const attribute = node.attributes[0];
if (attribute) {
error(attribute, 'illegal-title-attribute');
e.illegal_title_attribute(attribute);
}
const child = node.fragment.nodes.find((n) => n.type !== 'Text' && n.type !== 'ExpressionTag');
if (child) {
error(child, 'invalid-title-content');
e.invalid_title_content(child);
}
},
UpdateExpression(node, context) {
@ -751,7 +751,7 @@ const validation = {
if (!node.parent) return;
if (context.state.parent_element) {
if (!is_tag_valid_with_parent('#text', context.state.parent_element)) {
error(node, 'invalid-node-placement', '{expression}', context.state.parent_element);
e.invalid_node_placement(node, '{expression}', context.state.parent_element);
}
}
}

Loading…
Cancel
Save