Convert src/compiler/parse/errors.ts to JavaScript

pull/8569/head
Simon Holthausen 3 years ago
parent 48f3b5c435
commit 076cfa92b3

@ -1,216 +1,269 @@
// All parser errors should be listed and accessed from here // All parser errors should be listed and accessed from here
import list from '../utils/list'; import list from '../utils/list';
/** /**
* @internal * @internal
*/ */
export default { export default {
css_syntax_error: (message) => ({ css_syntax_error: /**
code: 'css-syntax-error', * @returns {{ code: string; message: any; }} */ (message) => ({
message code: 'css-syntax-error',
}), message
duplicate_attribute: { }),
code: 'duplicate-attribute', duplicate_attribute: {
message: 'Attributes need to be unique' code: 'duplicate-attribute',
}, message: 'Attributes need to be unique'
duplicate_element: (slug: string, name: string) => ({ },
code: `duplicate-${slug}`, duplicate_element: /**
message: `A component can only have one <${name}> tag` * @param {string} slug
}), * @param {string} name
duplicate_style: { * @returns {{ code: string; message: string; }}
code: 'duplicate-style', */ (slug, name) => ({
message: 'You can only have one top-level <style> tag per component' code: `duplicate-${slug}`,
}, message: `A component can only have one <${name}> tag`
empty_attribute_shorthand: { }),
code: 'empty-attribute-shorthand', duplicate_style: {
message: 'Attribute shorthand cannot be empty' code: 'duplicate-style',
}, message: 'You can only have one top-level <style> tag per component'
empty_directive_name: (type: string) => ({ },
code: 'empty-directive-name', empty_attribute_shorthand: {
message: `${type} name cannot be empty` code: 'empty-attribute-shorthand',
}), message: 'Attribute shorthand cannot be empty'
empty_global_selector: { },
code: 'css-syntax-error', empty_directive_name: /**
message: ':global() must contain a selector' * @param {string} type
}, * @returns {{ code: string; message: string; }}
expected_block_type: { */ (type) => ({
code: 'expected-block-type', code: 'empty-directive-name',
message: 'Expected if, each or await' message: `${type} name cannot be empty`
}, }),
expected_name: { empty_global_selector: {
code: 'expected-name', code: 'css-syntax-error',
message: 'Expected name' message: ':global() must contain a selector'
}, },
invalid_catch_placement_unclosed_block: (block) => ({ expected_block_type: {
code: 'invalid-catch-placement', code: 'expected-block-type',
message: `Expected to close ${block} before seeing {:catch} block` message: 'Expected if, each or await'
}), },
invalid_catch_placement_without_await: { expected_name: {
code: 'invalid-catch-placement', code: 'expected-name',
message: 'Cannot have an {:catch} block outside an {#await ...} block' message: 'Expected name'
}, },
invalid_component_definition: { invalid_catch_placement_unclosed_block: /**
code: 'invalid-component-definition', * @returns {{ code: string; message: string; }} */ (block) => ({
message: 'invalid component definition' code: 'invalid-catch-placement',
}, message: `Expected to close ${block} before seeing {:catch} block`
invalid_closing_tag_unopened: (name: string) => ({ }),
code: 'invalid-closing-tag', invalid_catch_placement_without_await: {
message: `</${name}> attempted to close an element that was not open` code: 'invalid-catch-placement',
}), message: 'Cannot have an {:catch} block outside an {#await ...} block'
invalid_closing_tag_autoclosed: (name: string, reason: string) => ({ },
code: 'invalid-closing-tag', invalid_component_definition: {
message: `</${name}> attempted to close <${name}> that was already automatically closed by <${reason}>` code: 'invalid-component-definition',
}), message: 'invalid component definition'
invalid_debug_args: { },
code: 'invalid-debug-args', invalid_closing_tag_unopened: /**
message: '{@debug ...} arguments must be identifiers, not arbitrary expressions' * @param {string} name
}, * @returns {{ code: string; message: string; }}
invalid_declaration: { */ (name) => ({
code: 'invalid-declaration', code: 'invalid-closing-tag',
message: 'Declaration cannot be empty' message: `</${name}> attempted to close an element that was not open`
}, }),
invalid_directive_value: { invalid_closing_tag_autoclosed: /**
code: 'invalid-directive-value', * @param {string} name
message: 'Directive value must be a JavaScript expression enclosed in curly braces' * @param {string} reason
}, * @returns {{ code: string; message: string; }}
invalid_elseif: { */ (name, reason) => ({
code: 'invalid-elseif', code: 'invalid-closing-tag',
message: "'elseif' should be 'else if'" message: `</${name}> attempted to close <${name}> that was already automatically closed by <${reason}>`
}, }),
invalid_elseif_placement_outside_if: { invalid_debug_args: {
code: 'invalid-elseif-placement', code: 'invalid-debug-args',
message: 'Cannot have an {:else if ...} block outside an {#if ...} block' message: '{@debug ...} arguments must be identifiers, not arbitrary expressions'
}, },
invalid_elseif_placement_unclosed_block: (block) => ({ invalid_declaration: {
code: 'invalid-elseif-placement', code: 'invalid-declaration',
message: `Expected to close ${block} before seeing {:else if ...} block` message: 'Declaration cannot be empty'
}), },
invalid_else_placement_outside_if: { invalid_directive_value: {
code: 'invalid-else-placement', code: 'invalid-directive-value',
message: 'Cannot have an {:else} block outside an {#if ...} or {#each ...} block' message: 'Directive value must be a JavaScript expression enclosed in curly braces'
}, },
invalid_else_placement_unclosed_block: (block) => ({ invalid_elseif: {
code: 'invalid-else-placement', code: 'invalid-elseif',
message: `Expected to close ${block} before seeing {:else} block` message: "'elseif' should be 'else if'"
}), },
invalid_element_content: (slug: string, name: string) => ({ invalid_elseif_placement_outside_if: {
code: `invalid-${slug}-content`, code: 'invalid-elseif-placement',
message: `<${name}> cannot have children` message: 'Cannot have an {:else if ...} block outside an {#if ...} block'
}), },
invalid_element_definition: { invalid_elseif_placement_unclosed_block: /**
code: 'invalid-element-definition', * @returns {{ code: string; message: string; }} */ (block) => ({
message: 'Invalid element definition' code: 'invalid-elseif-placement',
}, message: `Expected to close ${block} before seeing {:else if ...} block`
invalid_element_placement: (slug: string, name: string) => ({ }),
code: `invalid-${slug}-placement`, invalid_else_placement_outside_if: {
message: `<${name}> tags cannot be inside elements or blocks` code: 'invalid-else-placement',
}), message: 'Cannot have an {:else} block outside an {#if ...} or {#each ...} block'
invalid_logic_block_placement: (location: string, name: string) => ({ },
code: 'invalid-logic-block-placement', invalid_else_placement_unclosed_block: /**
message: `{#${name}} logic block cannot be ${location}` * @returns {{ code: string; message: string; }} */ (block) => ({
}), code: 'invalid-else-placement',
invalid_tag_placement: (location: string, name: string) => ({ message: `Expected to close ${block} before seeing {:else} block`
code: 'invalid-tag-placement', }),
message: `{@${name}} tag cannot be ${location}` invalid_element_content: /**
}), * @param {string} slug
invalid_ref_directive: (name: string) => ({ * @param {string} name
code: 'invalid-ref-directive', * @returns {{ code: string; message: string; }}
message: `The ref directive is no longer supported — use \`bind:this={${name}}\` instead` */ (slug, name) => ({
}), code: `invalid-${slug}-content`,
invalid_ref_selector: { message: `<${name}> cannot have children`
code: 'invalid-ref-selector', }),
message: 'ref selectors are no longer supported' invalid_element_definition: {
}, code: 'invalid-element-definition',
invalid_self_placement: { message: 'Invalid element definition'
code: 'invalid-self-placement', },
message: invalid_element_placement: /**
'<svelte:self> components can only exist inside {#if} blocks, {#each} blocks, or slots passed to components' * @param {string} slug
}, * @param {string} name
invalid_script_instance: { * @returns {{ code: string; message: string; }}
code: 'invalid-script', */ (slug, name) => ({
message: 'A component can only have one instance-level <script> element' code: `invalid-${slug}-placement`,
}, message: `<${name}> tags cannot be inside elements or blocks`
invalid_script_module: { }),
code: 'invalid-script', invalid_logic_block_placement: /**
message: 'A component can only have one <script context="module"> element' * @param {string} location
}, * @param {string} name
invalid_script_context_attribute: { * @returns {{ code: string; message: string; }}
code: 'invalid-script', */ (location, name) => ({
message: 'context attribute must be static' code: 'invalid-logic-block-placement',
}, message: `{#${name}} logic block cannot be ${location}`
invalid_script_context_value: { }),
code: 'invalid-script', invalid_tag_placement: /**
message: 'If the context attribute is supplied, its value must be "module"' * @param {string} location
}, * @param {string} name
invalid_tag_name: { * @returns {{ code: string; message: string; }}
code: 'invalid-tag-name', */ (location, name) => ({
message: 'Expected valid tag name' code: 'invalid-tag-placement',
}, message: `{@${name}} tag cannot be ${location}`
invalid_tag_name_svelte_element: (tags: string[], match: string) => ({ }),
code: 'invalid-tag-name', invalid_ref_directive: /**
message: `Valid <svelte:...> tag names are ${list(tags)}${ * @param {string} name
match ? ' (did you mean ' + match + '?)' : '' * @returns {{ code: string; message: string; }}
}` */ (name) => ({
}), code: 'invalid-ref-directive',
invalid_then_placement_unclosed_block: (block) => ({ message: `The ref directive is no longer supported — use \`bind:this={${name}}\` instead`
code: 'invalid-then-placement', }),
message: `Expected to close ${block} before seeing {:then} block` invalid_ref_selector: {
}), code: 'invalid-ref-selector',
invalid_then_placement_without_await: { message: 'ref selectors are no longer supported'
code: 'invalid-then-placement', },
message: 'Cannot have an {:then} block outside an {#await ...} block' invalid_self_placement: {
}, code: 'invalid-self-placement',
invalid_void_content: (name: string) => ({ message: '<svelte:self> components can only exist inside {#if} blocks, {#each} blocks, or slots passed to components'
code: 'invalid-void-content', },
message: `<${name}> is a void element and cannot have children, or a closing tag` invalid_script_instance: {
}), code: 'invalid-script',
missing_component_definition: { message: 'A component can only have one instance-level <script> element'
code: 'missing-component-definition', },
message: "<svelte:component> must have a 'this' attribute" invalid_script_module: {
}, code: 'invalid-script',
missing_attribute_value: { message: 'A component can only have one <script context="module"> element'
code: 'missing-attribute-value', },
message: 'Expected value for the attribute' invalid_script_context_attribute: {
}, code: 'invalid-script',
missing_element_definition: { message: 'context attribute must be static'
code: 'missing-element-definition', },
message: "<svelte:element> must have a 'this' attribute" invalid_script_context_value: {
}, code: 'invalid-script',
unclosed_script: { message: 'If the context attribute is supplied, its value must be "module"'
code: 'unclosed-script', },
message: '<script> must have a closing tag' invalid_tag_name: {
}, code: 'invalid-tag-name',
unclosed_style: { message: 'Expected valid tag name'
code: 'unclosed-style', },
message: '<style> must have a closing tag' invalid_tag_name_svelte_element: /**
}, * @param {string[]} tags
unclosed_comment: { * @param {string} match
code: 'unclosed-comment', * @returns {{ code: string; message: string; }}
message: 'comment was left open, expected -->' */ (tags, match) => ({
}, code: 'invalid-tag-name',
unclosed_attribute_value: (token: string) => ({ message: `Valid <svelte:...> tag names are ${list(tags)}${match ? ' (did you mean ' + match + '?)' : ''}`
code: 'unclosed-attribute-value', }),
message: `Expected to close the attribute value with ${token}` invalid_then_placement_unclosed_block: /**
}), * @returns {{ code: string; message: string; }} */ (block) => ({
unexpected_block_close: { code: 'invalid-then-placement',
code: 'unexpected-block-close', message: `Expected to close ${block} before seeing {:then} block`
message: 'Unexpected block closing tag' }),
}, invalid_then_placement_without_await: {
unexpected_eof: { code: 'invalid-then-placement',
code: 'unexpected-eof', message: 'Cannot have an {:then} block outside an {#await ...} block'
message: 'Unexpected end of input' },
}, invalid_void_content: /**
unexpected_eof_token: (token: string) => ({ * @param {string} name
code: 'unexpected-eof', * @returns {{ code: string; message: string; }}
message: `Unexpected ${token}` */ (name) => ({
}), code: 'invalid-void-content',
unexpected_token: (token: string) => ({ message: `<${name}> is a void element and cannot have children, or a closing tag`
code: 'unexpected-token', }),
message: `Expected ${token}` missing_component_definition: {
}), code: 'missing-component-definition',
unexpected_token_destructure: { message: "<svelte:component> must have a 'this' attribute"
code: 'unexpected-token', },
message: 'Expected identifier or destructure pattern' missing_attribute_value: {
} code: 'missing-attribute-value',
message: 'Expected value for the attribute'
},
missing_element_definition: {
code: 'missing-element-definition',
message: "<svelte:element> must have a 'this' attribute"
},
unclosed_script: {
code: 'unclosed-script',
message: '<script> must have a closing tag'
},
unclosed_style: {
code: 'unclosed-style',
message: '<style> must have a closing tag'
},
unclosed_comment: {
code: 'unclosed-comment',
message: 'comment was left open, expected -->'
},
unclosed_attribute_value: /**
* @param {string} token
* @returns {{ code: string; message: string; }}
*/ (token) => ({
code: 'unclosed-attribute-value',
message: `Expected to close the attribute value with ${token}`
}),
unexpected_block_close: {
code: 'unexpected-block-close',
message: 'Unexpected block closing tag'
},
unexpected_eof: {
code: 'unexpected-eof',
message: 'Unexpected end of input'
},
unexpected_eof_token: /**
* @param {string} token
* @returns {{ code: string; message: string; }}
*/ (token) => ({
code: 'unexpected-eof',
message: `Unexpected ${token}`
}),
unexpected_token: /**
* @param {string} token
* @returns {{ code: string; message: string; }}
*/ (token) => ({
code: 'unexpected-token',
message: `Expected ${token}`
}),
unexpected_token_destructure: {
code: 'unexpected-token',
message: 'Expected identifier or destructure pattern'
}
}; };

Loading…
Cancel
Save