|
| 1 | +import Node from './shared/Node'; |
| 2 | +import Expression from './shared/Expression'; |
| 3 | +import Component from '../Component'; |
| 4 | +import TemplateScope from './shared/TemplateScope'; |
| 5 | +import { Context, unpack_destructuring } from './shared/Context'; |
| 6 | +import { ConstTag as ConstTagType } from '../../interfaces'; |
| 7 | +import { INodeAllowConstTag } from './interfaces'; |
| 8 | +import { walk } from 'estree-walker'; |
| 9 | +import { extract_identifiers } from 'periscopic'; |
| 10 | +import is_reference, { NodeWithPropertyDefinition } from 'is-reference'; |
| 11 | +import get_object from '../utils/get_object'; |
| 12 | +import compiler_errors from '../compiler_errors'; |
| 13 | + |
| 14 | +const allowed_parents = new Set(['EachBlock', 'CatchBlock', 'ThenBlock', 'InlineComponent', 'SlotTemplate']); |
| 15 | + |
| 16 | +export default class ConstTag extends Node { |
| 17 | + type: 'ConstTag'; |
| 18 | + expression: Expression; |
| 19 | + contexts: Context[] = []; |
| 20 | + node: ConstTagType; |
| 21 | + scope: TemplateScope; |
| 22 | + |
| 23 | + assignees: Set<string> = new Set(); |
| 24 | + dependencies: Set<string> = new Set(); |
| 25 | + |
| 26 | + constructor(component: Component, parent: INodeAllowConstTag, scope: TemplateScope, info: ConstTagType) { |
| 27 | + super(component, parent, scope, info); |
| 28 | + |
| 29 | + if (!allowed_parents.has(parent.type)) { |
| 30 | + component.error(info, compiler_errors.invalid_const_placement); |
| 31 | + } |
| 32 | + this.node = info; |
| 33 | + this.scope = scope; |
| 34 | + |
| 35 | + const { assignees, dependencies } = this; |
| 36 | + |
| 37 | + extract_identifiers(info.expression.left).forEach(({ name }) => { |
| 38 | + assignees.add(name); |
| 39 | + const owner = this.scope.get_owner(name); |
| 40 | + if (owner === parent) { |
| 41 | + component.error(info, compiler_errors.invalid_const_declaration(name)); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + walk(info.expression.right, { |
| 46 | + enter(node, parent) { |
| 47 | + if (is_reference(node as NodeWithPropertyDefinition, parent as NodeWithPropertyDefinition)) { |
| 48 | + const identifier = get_object(node as any); |
| 49 | + const { name } = identifier; |
| 50 | + dependencies.add(name); |
| 51 | + } |
| 52 | + } |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + parse_expression() { |
| 57 | + unpack_destructuring({ |
| 58 | + contexts: this.contexts, |
| 59 | + node: this.node.expression.left, |
| 60 | + scope: this.scope, |
| 61 | + component: this.component |
| 62 | + }); |
| 63 | + this.expression = new Expression(this.component, this, this.scope, this.node.expression.right); |
| 64 | + this.contexts.forEach(context => { |
| 65 | + const owner = this.scope.get_owner(context.key.name); |
| 66 | + if (owner && owner.type === 'ConstTag' && owner.parent === this.parent) { |
| 67 | + this.component.error(this.node, compiler_errors.invalid_const_declaration(context.key.name)); |
| 68 | + } |
| 69 | + this.scope.add(context.key.name, this.expression.dependencies, this); |
| 70 | + }); |
| 71 | + } |
| 72 | +} |
0 commit comments