Skip to content

Commit 2bbc535

Browse files
committed
move error to compiler_errors
1 parent 415b76f commit 2bbc535

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

src/compiler/compile/compiler_errors.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export default {
4040
code: 'invalid-binding',
4141
message: 'Cannot bind to a variable declared with {#await ... then} or {:catch} blocks'
4242
},
43+
invalid_binding_const: {
44+
code: 'invalid-binding',
45+
message: 'Cannot bind to a variable declared with {@const ...}'
46+
},
4347
invalid_binding_writibale: {
4448
code: 'invalid-binding',
4549
message: 'Cannot bind to a variable which is not writable'
@@ -249,5 +253,21 @@ export default {
249253
invalid_directive_value: {
250254
code: 'invalid-directive-value',
251255
message: 'Can only bind to an identifier (e.g. `foo`) or a member expression (e.g. `foo.bar` or `foo[baz]`)'
252-
}
256+
},
257+
invalid_const_placement: {
258+
code: 'invalid-const-placement',
259+
message: '{@const} must be the immediate child of {#each}, {:then}, {:catch}, <svelte:fragment> and <Component>'
260+
},
261+
invalid_const_declaration: (name: string) => ({
262+
code: 'invalid-const-declaration',
263+
message: `'${name}' has already been declared`
264+
}),
265+
invalid_const_update: (name: string) => ({
266+
code: 'invalid-const-update',
267+
message: `'${name}' is declared using {@const ...} and it is read-only`
268+
}),
269+
cyclical_constant_tags: (cycle: string[]) => ({
270+
code: 'cyclical-constant-tags',
271+
message: `Cyclical dependency detected: ${cycle.join(' → ')}`
272+
})
253273
};

src/compiler/compile/nodes/Binding.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ export default class Binding extends Node {
5858
return;
5959
}
6060
if (scope.is_const(name)) {
61-
component.error(this, {
62-
code: 'invalid-binding',
63-
message: 'Cannot bind to a variable declared with {@const ...}'
64-
});
61+
component.error(this, compiler_errors.invalid_binding_const);
6562
}
6663

6764
scope.dependencies_for_name.get(name).forEach(name => {

src/compiler/compile/nodes/ConstTag.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { walk } from 'estree-walker';
99
import { extract_identifiers } from 'periscopic';
1010
import is_reference from 'is-reference';
1111
import get_object from '../utils/get_object';
12+
import compiler_errors from '../compiler_errors';
1213

1314
const allowed_parents = new Set(['EachBlock', 'CatchBlock', 'ThenBlock', 'InlineComponent', 'SlotTemplate']);
1415

@@ -26,7 +27,7 @@ export default class ConstTag extends Node {
2627
super(component, parent, scope, info);
2728

2829
if (!allowed_parents.has(parent.type)) {
29-
component.error(info, { code: 'invalid-const-placement', message: '{@const} must be the immediate child of {#each}, {:then}, {:catch}, <svelte:fragment> and <Component>' });
30+
component.error(info, compiler_errors.invalid_const_placement);
3031
}
3132
this.node = info;
3233
this.scope = scope;
@@ -37,7 +38,7 @@ export default class ConstTag extends Node {
3738
assignees.add(name);
3839
const owner = this.scope.get_owner(name);
3940
if (owner === parent) {
40-
component.error(info, { code: 'invalid-const-declaration', message: `'${name}' has already been declared` });
41+
component.error(info, compiler_errors.invalid_const_declaration(name));
4142
}
4243
});
4344

@@ -51,14 +52,14 @@ export default class ConstTag extends Node {
5152
}
5253
});
5354
}
54-
55+
5556
parse_expression() {
5657
unpack_destructuring(this.contexts, this.node.expression.left);
5758
this.expression = new Expression(this.component, this, this.scope, this.node.expression.right);
5859
this.contexts.forEach(context => {
5960
const owner = this.scope.get_owner(context.key.name);
6061
if (owner && owner.type === 'ConstTag' && owner.parent === this.parent) {
61-
this.component.error(this.node, { code: 'invalid-const-declaration', message: `'${context.key.name}' has already been declared` });
62+
this.component.error(this.node, compiler_errors.invalid_const_declaration(context.key.name));
6263
}
6364
this.scope.add(context.key.name, this.expression.dependencies, this);
6465
});

src/compiler/compile/nodes/shared/Expression.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ export default class Expression {
134134
names.forEach(name => {
135135
if (template_scope.names.has(name)) {
136136
if (template_scope.is_const(name)) {
137-
component.error(node, {
138-
code: 'invalid-const-update',
139-
message: `'${name}' is declared using {@const ...} and it is read-only`
140-
});
137+
component.error(node, compiler_errors.invalid_const_update(name));
141138
}
142139

143140
template_scope.dependencies_for_name.get(name).forEach(name => {

src/compiler/compile/nodes/shared/get_const_tags.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ConstTag from '../ConstTag';
44
import map_children from './map_children';
55
import { INodeAllowConstTag, INode } from '../interfaces';
66
import check_graph_for_cycles from '../../utils/check_graph_for_cycles';
7+
import compiler_errors from '../../compiler_errors';
78

89
export default function get_const_tags(children: TemplateNode[], component: Component, node: INodeAllowConstTag, parent: INode): [ConstTag[], Array<Exclude<INode, ConstTag>>] {
910
const const_tags: ConstTagType[] = [];
@@ -67,10 +68,7 @@ function sort_consts_nodes(consts_nodes: ConstTag[], component: Component) {
6768
if (cycle && cycle.length) {
6869
const nodeList = lookup.get(cycle[0]);
6970
const node = nodeList[0];
70-
component.error(node.node, {
71-
code: 'cyclical-constant-tags',
72-
message: `Cyclical dependency detected: ${cycle.join(' → ')}`
73-
});
71+
component.error(node.node, compiler_errors.cyclical_constant_tags(cycle));
7472
}
7573

7674
const add_node = (node: ConstNode) => {

0 commit comments

Comments
 (0)