From a5afc9d55b32f90f26d0ed6661056f0b5b43095b Mon Sep 17 00:00:00 2001 From: Simon He <13917107469@163.com> Date: Wed, 16 Nov 2022 18:40:38 +0800 Subject: [PATCH] refactor: tidy up compile/nodes --- src/compiler/compile/nodes/Binding.ts | 9 +- src/compiler/compile/nodes/ConstTag.ts | 26 +-- src/compiler/compile/nodes/DebugTag.ts | 4 +- src/compiler/compile/nodes/Element.ts | 83 ++++------ src/compiler/compile/nodes/EventHandler.ts | 8 +- src/compiler/compile/nodes/Head.ts | 2 +- src/compiler/compile/nodes/InlineComponent.ts | 10 +- src/compiler/compile/nodes/SlotTemplate.ts | 25 +-- src/compiler/compile/nodes/Text.ts | 7 +- src/compiler/compile/nodes/Title.ts | 2 +- src/compiler/compile/nodes/Window.ts | 9 +- .../compile/nodes/shared/AbstractBlock.ts | 2 +- src/compiler/compile/nodes/shared/Context.ts | 13 +- .../compile/nodes/shared/Expression.ts | 68 ++++---- src/compiler/compile/nodes/shared/Node.ts | 7 +- .../compile/nodes/shared/TemplateScope.ts | 3 +- .../compile/nodes/shared/get_const_tags.ts | 148 +++++++++--------- .../compile/nodes/shared/is_contextual.ts | 7 +- 18 files changed, 201 insertions(+), 232 deletions(-) diff --git a/src/compiler/compile/nodes/Binding.ts b/src/compiler/compile/nodes/Binding.ts index f826df4828..71e51960db 100644 --- a/src/compiler/compile/nodes/Binding.ts +++ b/src/compiler/compile/nodes/Binding.ts @@ -101,11 +101,10 @@ export default class Binding extends Node { validate_binding_rest_properties(scope: TemplateScope) { this.expression.references.forEach(name => { const each_block = scope.get_owner(name); - if (each_block && each_block.type === 'EachBlock') { - const rest_node = each_block.context_rest_properties.get(name); - if (rest_node) { - this.component.warn(rest_node as any, compiler_warnings.invalid_rest_eachblock_binding(name)); - } + if (!each_block || each_block.type !== 'EachBlock') return; + const rest_node = each_block.context_rest_properties.get(name); + if (rest_node) { + this.component.warn(rest_node as any, compiler_warnings.invalid_rest_eachblock_binding(name)); } }); } diff --git a/src/compiler/compile/nodes/ConstTag.ts b/src/compiler/compile/nodes/ConstTag.ts index 44a50aa005..4bcf3bd62f 100644 --- a/src/compiler/compile/nodes/ConstTag.ts +++ b/src/compiler/compile/nodes/ConstTag.ts @@ -23,7 +23,7 @@ export default class ConstTag extends Node { context_rest_properties: Map = new Map(); assignees: Set = new Set(); - dependencies: Set = new Set(); + dependencies: Set = new Set(); constructor(component: Component, parent: INodeAllowConstTag, scope: TemplateScope, info: ConstTagType) { super(component, parent, scope, info); @@ -37,22 +37,22 @@ export default class ConstTag extends Node { const { assignees, dependencies } = this; extract_identifiers(info.expression.left).forEach(({ name }) => { - assignees.add(name); + assignees.add(name); const owner = this.scope.get_owner(name); if (owner === parent) { component.error(info, compiler_errors.invalid_const_declaration(name)); } - }); + }); - walk(info.expression.right, { - enter(node, parent) { - if (is_reference(node as NodeWithPropertyDefinition, parent as NodeWithPropertyDefinition)) { - const identifier = get_object(node as any); - const { name } = identifier; - dependencies.add(name); - } - } - }); + walk(info.expression.right, { + enter(node, parent) { + if (is_reference(node as NodeWithPropertyDefinition, parent as NodeWithPropertyDefinition)) { + const identifier = get_object(node as any); + const { name } = identifier; + dependencies.add(name); + } + } + }); } parse_expression() { @@ -66,7 +66,7 @@ export default class ConstTag extends Node { 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) { + if (owner?.type === 'ConstTag' && owner?.parent === this.parent) { 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/DebugTag.ts b/src/compiler/compile/nodes/DebugTag.ts index 88e8dd90e6..fe71a19972 100644 --- a/src/compiler/compile/nodes/DebugTag.ts +++ b/src/compiler/compile/nodes/DebugTag.ts @@ -13,8 +13,6 @@ export default class DebugTag extends Node { constructor(component: Component, parent: INode, scope: TemplateScope, info: TemplateNode) { super(component, parent, scope, info); - this.expressions = info.identifiers.map((node: EsTreeNode) => { - return new Expression(component, parent, scope, node); - }); + this.expressions = info.identifiers.map((node: EsTreeNode) => new Expression(component, parent, scope, node)); } } diff --git a/src/compiler/compile/nodes/Element.ts b/src/compiler/compile/nodes/Element.ts index 303b2e9122..702b6530a3 100644 --- a/src/compiler/compile/nodes/Element.ts +++ b/src/compiler/compile/nodes/Element.ts @@ -261,24 +261,22 @@ export default class Element extends Node { } } - if (this.name === 'textarea') { - if (info.children.length > 0) { - const value_attribute = info.attributes.find(node => node.name === 'value'); - if (value_attribute) { - component.error(value_attribute, compiler_errors.textarea_duplicate_value); - return; - } + if (this.name === 'textarea' && info.children.length) { + const value_attribute = info.attributes.find(node => node.name === 'value'); + if (value_attribute) { + component.error(value_attribute, compiler_errors.textarea_duplicate_value); + return; + } - // this is an egregious hack, but it's the easiest way to get