From ee9a75bb57fa7dad5701945cbd7bc0563ed02d0b Mon Sep 17 00:00:00 2001 From: tanhauhau Date: Sat, 24 Jul 2021 12:51:18 +0800 Subject: [PATCH] move error to compiler_errors --- src/compiler/compile/compiler_errors.ts | 22 ++++++++++++++++++- src/compiler/compile/nodes/Binding.ts | 5 +---- src/compiler/compile/nodes/ConstTag.ts | 9 ++++---- .../compile/nodes/shared/Expression.ts | 5 +---- .../compile/nodes/shared/get_const_tags.ts | 6 ++--- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/compiler/compile/compiler_errors.ts b/src/compiler/compile/compiler_errors.ts index 54263c3eb9..4ae66602d1 100644 --- a/src/compiler/compile/compiler_errors.ts +++ b/src/compiler/compile/compiler_errors.ts @@ -40,6 +40,10 @@ export default { code: 'invalid-binding', 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: { code: 'invalid-binding', message: 'Cannot bind to a variable which is not writable' @@ -241,5 +245,21 @@ export default { 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]`)' - } + }, + invalid_const_placement: { + code: 'invalid-const-placement', + message: '{@const} must be the immediate child of {#each}, {:then}, {:catch}, and ' + }, + 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(' → ')}` + }) }; diff --git a/src/compiler/compile/nodes/Binding.ts b/src/compiler/compile/nodes/Binding.ts index 65c0633600..39f6fa374e 100644 --- a/src/compiler/compile/nodes/Binding.ts +++ b/src/compiler/compile/nodes/Binding.ts @@ -58,10 +58,7 @@ export default class Binding extends Node { return; } if (scope.is_const(name)) { - component.error(this, { - code: 'invalid-binding', - message: 'Cannot bind to a variable declared with {@const ...}' - }); + component.error(this, compiler_errors.invalid_binding_const); } scope.dependencies_for_name.get(name).forEach(name => { diff --git a/src/compiler/compile/nodes/ConstTag.ts b/src/compiler/compile/nodes/ConstTag.ts index 322ef32a13..5fd5a71c29 100644 --- a/src/compiler/compile/nodes/ConstTag.ts +++ b/src/compiler/compile/nodes/ConstTag.ts @@ -9,6 +9,7 @@ import { walk } from 'estree-walker'; import { extract_identifiers } from 'periscopic'; import is_reference from 'is-reference'; import get_object from '../utils/get_object'; +import compiler_errors from '../compiler_errors'; 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); if (!allowed_parents.has(parent.type)) { - component.error(info, { code: 'invalid-const-placement', message: '{@const} must be the immediate child of {#each}, {:then}, {:catch}, and ' }); + component.error(info, compiler_errors.invalid_const_placement); } this.node = info; this.scope = scope; @@ -37,7 +38,7 @@ export default class ConstTag extends Node { assignees.add(name); const owner = this.scope.get_owner(name); 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)); } }); @@ -51,14 +52,14 @@ export default class ConstTag extends Node { } }); } - + parse_expression() { unpack_destructuring(this.contexts, this.node.expression.left); this.expression = new Expression(this.component, this, this.scope, this.node.expression.right); this.contexts.forEach(context => { const owner = this.scope.get_owner(context.key.name); 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); }); diff --git a/src/compiler/compile/nodes/shared/Expression.ts b/src/compiler/compile/nodes/shared/Expression.ts index 77f14ace2f..c54540f249 100644 --- a/src/compiler/compile/nodes/shared/Expression.ts +++ b/src/compiler/compile/nodes/shared/Expression.ts @@ -134,10 +134,7 @@ export default class Expression { names.forEach(name => { if (template_scope.names.has(name)) { if (template_scope.is_const(name)) { - component.error(node, { - code: 'invalid-const-update', - message: `'${name}' is declared using {@const ...} and it is read-only` - }); + component.error(node, compiler_errors.invalid_const_update(name)); } template_scope.dependencies_for_name.get(name).forEach(name => { diff --git a/src/compiler/compile/nodes/shared/get_const_tags.ts b/src/compiler/compile/nodes/shared/get_const_tags.ts index c87cad43ef..0bdf6e2a70 100644 --- a/src/compiler/compile/nodes/shared/get_const_tags.ts +++ b/src/compiler/compile/nodes/shared/get_const_tags.ts @@ -4,6 +4,7 @@ import ConstTag from '../ConstTag'; import map_children from './map_children'; import { INodeAllowConstTag, INode } from '../interfaces'; 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>] { const const_tags: ConstTagType[] = []; @@ -67,10 +68,7 @@ function sort_consts_nodes(consts_nodes: ConstTag[], component: Component) { if (cycle && cycle.length) { const nodeList = lookup.get(cycle[0]); const node = nodeList[0]; - component.error(node.node, { - code: 'cyclical-constant-tags', - message: `Cyclical dependency detected: ${cycle.join(' → ')}` - }); + component.error(node.node, compiler_errors.cyclical_constant_tags(cycle)); } const add_node = (node: ConstNode) => {