move error to compiler_errors

pull/6413/head
tanhauhau 5 years ago
parent ea2c7ee357
commit ee9a75bb57

@ -40,6 +40,10 @@ export default {
code: 'invalid-binding', code: 'invalid-binding',
message: 'Cannot bind to a variable declared with {#await ... then} or {:catch} blocks' message: 'Cannot bind to a variable declared with {#await ... then} or {:catch} blocks'
}, },
invalid_binding_const: {
code: 'invalid-binding',
message: 'Cannot bind to a variable declared with {@const ...}'
},
invalid_binding_writibale: { invalid_binding_writibale: {
code: 'invalid-binding', code: 'invalid-binding',
message: 'Cannot bind to a variable which is not writable' message: 'Cannot bind to a variable which is not writable'
@ -241,5 +245,21 @@ export default {
invalid_directive_value: { invalid_directive_value: {
code: 'invalid-directive-value', code: 'invalid-directive-value',
message: 'Can only bind to an identifier (e.g. `foo`) or a member expression (e.g. `foo.bar` or `foo[baz]`)' message: 'Can only bind to an identifier (e.g. `foo`) or a member expression (e.g. `foo.bar` or `foo[baz]`)'
} },
invalid_const_placement: {
code: 'invalid-const-placement',
message: '{@const} must be the immediate child of {#each}, {:then}, {:catch}, <svelte:fragment> and <Component>'
},
invalid_const_declaration: (name: string) => ({
code: 'invalid-const-declaration',
message: `'${name}' has already been declared`
}),
invalid_const_update: (name: string) => ({
code: 'invalid-const-update',
message: `'${name}' is declared using {@const ...} and it is read-only`
}),
cyclical_constant_tags: (cycle: string[]) => ({
code: 'cyclical-constant-tags',
message: `Cyclical dependency detected: ${cycle.join(' → ')}`
})
}; };

@ -58,10 +58,7 @@ export default class Binding extends Node {
return; return;
} }
if (scope.is_const(name)) { if (scope.is_const(name)) {
component.error(this, { component.error(this, compiler_errors.invalid_binding_const);
code: 'invalid-binding',
message: 'Cannot bind to a variable declared with {@const ...}'
});
} }
scope.dependencies_for_name.get(name).forEach(name => { scope.dependencies_for_name.get(name).forEach(name => {

@ -9,6 +9,7 @@ import { walk } from 'estree-walker';
import { extract_identifiers } from 'periscopic'; import { extract_identifiers } from 'periscopic';
import is_reference from 'is-reference'; import is_reference from 'is-reference';
import get_object from '../utils/get_object'; import get_object from '../utils/get_object';
import compiler_errors from '../compiler_errors';
const allowed_parents = new Set(['EachBlock', 'CatchBlock', 'ThenBlock', 'InlineComponent', 'SlotTemplate']); const allowed_parents = new Set(['EachBlock', 'CatchBlock', 'ThenBlock', 'InlineComponent', 'SlotTemplate']);
@ -26,7 +27,7 @@ export default class ConstTag extends Node {
super(component, parent, scope, info); super(component, parent, scope, info);
if (!allowed_parents.has(parent.type)) { if (!allowed_parents.has(parent.type)) {
component.error(info, { code: 'invalid-const-placement', message: '{@const} must be the immediate child of {#each}, {:then}, {:catch}, <svelte:fragment> and <Component>' }); component.error(info, compiler_errors.invalid_const_placement);
} }
this.node = info; this.node = info;
this.scope = scope; this.scope = scope;
@ -37,7 +38,7 @@ export default class ConstTag extends Node {
assignees.add(name); assignees.add(name);
const owner = this.scope.get_owner(name); const owner = this.scope.get_owner(name);
if (owner === parent) { if (owner === parent) {
component.error(info, { code: 'invalid-const-declaration', message: `'${name}' has already been declared` }); component.error(info, compiler_errors.invalid_const_declaration(name));
} }
}); });
@ -58,7 +59,7 @@ export default class ConstTag extends Node {
this.contexts.forEach(context => { this.contexts.forEach(context => {
const owner = this.scope.get_owner(context.key.name); const owner = this.scope.get_owner(context.key.name);
if (owner && owner.type === 'ConstTag' && owner.parent === this.parent) { if (owner && owner.type === 'ConstTag' && owner.parent === this.parent) {
this.component.error(this.node, { code: 'invalid-const-declaration', message: `'${context.key.name}' has already been declared` }); this.component.error(this.node, compiler_errors.invalid_const_declaration(context.key.name));
} }
this.scope.add(context.key.name, this.expression.dependencies, this); this.scope.add(context.key.name, this.expression.dependencies, this);
}); });

@ -134,10 +134,7 @@ export default class Expression {
names.forEach(name => { names.forEach(name => {
if (template_scope.names.has(name)) { if (template_scope.names.has(name)) {
if (template_scope.is_const(name)) { if (template_scope.is_const(name)) {
component.error(node, { component.error(node, compiler_errors.invalid_const_update(name));
code: 'invalid-const-update',
message: `'${name}' is declared using {@const ...} and it is read-only`
});
} }
template_scope.dependencies_for_name.get(name).forEach(name => { template_scope.dependencies_for_name.get(name).forEach(name => {

@ -4,6 +4,7 @@ import ConstTag from '../ConstTag';
import map_children from './map_children'; import map_children from './map_children';
import { INodeAllowConstTag, INode } from '../interfaces'; import { INodeAllowConstTag, INode } from '../interfaces';
import check_graph_for_cycles from '../../utils/check_graph_for_cycles'; import check_graph_for_cycles from '../../utils/check_graph_for_cycles';
import compiler_errors from '../../compiler_errors';
export default function get_const_tags(children: TemplateNode[], component: Component, node: INodeAllowConstTag, parent: INode): [ConstTag[], Array<Exclude<INode, ConstTag>>] { export default function get_const_tags(children: TemplateNode[], component: Component, node: INodeAllowConstTag, parent: INode): [ConstTag[], Array<Exclude<INode, ConstTag>>] {
const const_tags: ConstTagType[] = []; const const_tags: ConstTagType[] = [];
@ -67,10 +68,7 @@ function sort_consts_nodes(consts_nodes: ConstTag[], component: Component) {
if (cycle && cycle.length) { if (cycle && cycle.length) {
const nodeList = lookup.get(cycle[0]); const nodeList = lookup.get(cycle[0]);
const node = nodeList[0]; const node = nodeList[0];
component.error(node.node, { component.error(node.node, compiler_errors.cyclical_constant_tags(cycle));
code: 'cyclical-constant-tags',
message: `Cyclical dependency detected: ${cycle.join(' → ')}`
});
} }
const add_node = (node: ConstNode) => { const add_node = (node: ConstNode) => {

Loading…
Cancel
Save