|
| 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 from 'is-reference'; |
| 11 | +import get_object from '../utils/get_object'; |
| 12 | + |
| 13 | +const allowed_parents = new Set(['EachBlock', 'CatchBlock', 'ThenBlock', 'InlineComponent', 'SlotTemplate']); |
| 14 | + |
| 15 | +export default class ConstTag extends Node { |
| 16 | + type: 'ConstTag'; |
| 17 | + expression: Expression; |
| 18 | + contexts: Context[] = []; |
| 19 | + node: ConstTagType; |
| 20 | + scope: TemplateScope; |
| 21 | + |
| 22 | + assignees: Set<string> = new Set(); |
| 23 | + dependencies: Set<string> = new Set(); |
| 24 | + |
| 25 | + constructor(component: Component, parent: INodeAllowConstTag, scope: TemplateScope, info: ConstTagType) { |
| 26 | + super(component, parent, scope, info); |
| 27 | + |
| 28 | + 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 | + } |
| 31 | + this.node = info; |
| 32 | + this.scope = scope; |
| 33 | + |
| 34 | + const { assignees, dependencies } = this; |
| 35 | + |
| 36 | + extract_identifiers(info.expression.left).forEach(({ name }) => { |
| 37 | + assignees.add(name); |
| 38 | + const owner = this.scope.get_owner(name); |
| 39 | + if (owner === parent) { |
| 40 | + component.error(info, { code: 'invalid-const-declaration', message: `'${name}' has already been declared` }); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + walk(info.expression.right, { |
| 45 | + enter(node, parent) { |
| 46 | + if (is_reference(node, parent)) { |
| 47 | + const identifier = get_object(node); |
| 48 | + const { name } = identifier; |
| 49 | + dependencies.add(name); |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + parse_expression() { |
| 56 | + unpack_destructuring(this.contexts, this.node.expression.left); |
| 57 | + this.expression = new Expression(this.component, this, this.scope, this.node.expression.right); |
| 58 | + this.contexts.forEach(context => { |
| 59 | + const owner = this.scope.get_owner(context.key.name); |
| 60 | + 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 | + } |
| 63 | + this.scope.add(context.key.name, this.expression.dependencies, this); |
| 64 | + }); |
| 65 | + } |
| 66 | +} |
0 commit comments