From 7bf907e5c80ad754cfdd76ffac835dbf858783af Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Fri, 12 May 2023 17:29:26 +0200 Subject: [PATCH] Convert src/compiler/compile/nodes/shared/TemplateScope.ts to JavaScript --- src/compiler/compile/nodes/shared/Tag.js | 8 +- .../compile/nodes/shared/TemplateScope.ts | 130 ++++++++++-------- 2 files changed, 76 insertions(+), 62 deletions(-) diff --git a/src/compiler/compile/nodes/shared/Tag.js b/src/compiler/compile/nodes/shared/Tag.js index 4209ace674..274848b685 100644 --- a/src/compiler/compile/nodes/shared/Tag.js +++ b/src/compiler/compile/nodes/shared/Tag.js @@ -13,10 +13,10 @@ export default class Tag extends Node { should_cache; /** - * @param {any} component * - * @param {any} parent * - * @param {any} scope * - * @param {any} info undefined + * @param {any} component + * @param {any} parent + * @param {any} scope + * @param {any} info */ constructor(component, parent, scope, info) { super(component, parent, scope, info); diff --git a/src/compiler/compile/nodes/shared/TemplateScope.ts b/src/compiler/compile/nodes/shared/TemplateScope.ts index 518f5e9e84..e0ddf109c9 100644 --- a/src/compiler/compile/nodes/shared/TemplateScope.ts +++ b/src/compiler/compile/nodes/shared/TemplateScope.ts @@ -1,69 +1,83 @@ -import EachBlock from '../EachBlock'; -import ThenBlock from '../ThenBlock'; -import CatchBlock from '../CatchBlock'; -import InlineComponent from '../InlineComponent'; -import Element from '../Element'; -import SlotTemplate from '../SlotTemplate'; -import ConstTag from '../ConstTag'; +export default class TemplateScope { -type NodeWithScope = - | EachBlock - | ThenBlock - | CatchBlock - | InlineComponent - | Element - | SlotTemplate - | ConstTag; + /** @type {Set} */ + names; -export default class TemplateScope { - names: Set; - dependencies_for_name: Map>; - owners: Map = new Map(); - parent?: TemplateScope; + /** @type {Map>} */ + dependencies_for_name; + + /** @type {Map} */ + owners = new Map(); - constructor(parent?: TemplateScope) { - this.parent = parent; - this.names = new Set(parent ? parent.names : []); - this.dependencies_for_name = new Map(parent ? parent.dependencies_for_name : []); - } + /** @type {TemplateScope} */ + parent; - add(name, dependencies: Set, owner) { - this.names.add(name); - this.dependencies_for_name.set(name, dependencies); - this.owners.set(name, owner); - return this; - } + /** @param {TemplateScope} [parent] undefined */ + constructor(parent) { + this.parent = parent; + this.names = new Set(parent ? parent.names : []); + this.dependencies_for_name = new Map(parent ? parent.dependencies_for_name : []); + } - child() { - const child = new TemplateScope(this); - return child; - } + /** + * @param {any} name + * @param {Set} dependencies + * @param {any} owner + */ + add(name, dependencies, owner) { + this.names.add(name); + this.dependencies_for_name.set(name, dependencies); + this.owners.set(name, owner); + return this; + } + child() { + const child = new TemplateScope(this); + return child; + } - is_top_level(name: string) { - return !this.parent || (!this.names.has(name) && this.parent.is_top_level(name)); - } + /** @param {string} name */ + is_top_level(name) { + return !this.parent || (!this.names.has(name) && this.parent.is_top_level(name)); + } - get_owner(name: string): NodeWithScope { - return this.owners.get(name) || (this.parent && this.parent.get_owner(name)); - } + /** + * @param {string} name + * @returns {any} + */ + get_owner(name) { + return this.owners.get(name) || (this.parent && this.parent.get_owner(name)); + } - is_let(name: string) { - const owner = this.get_owner(name); - return ( - owner && - (owner.type === 'Element' || - owner.type === 'InlineComponent' || - owner.type === 'SlotTemplate') - ); - } + /** @param {string} name */ + is_let(name) { + const owner = this.get_owner(name); + return (owner && + (owner.type === 'Element' || + owner.type === 'InlineComponent' || + owner.type === 'SlotTemplate')); + } - is_await(name: string) { - const owner = this.get_owner(name); - return owner && (owner.type === 'ThenBlock' || owner.type === 'CatchBlock'); - } + /** @param {string} name */ + is_await(name) { + const owner = this.get_owner(name); + return owner && (owner.type === 'ThenBlock' || owner.type === 'CatchBlock'); + } - is_const(name: string) { - const owner = this.get_owner(name); - return owner && owner.type === 'ConstTag'; - } + /** @param {string} name */ + is_const(name) { + const owner = this.get_owner(name); + return owner && owner.type === 'ConstTag'; + } } + + +/** + * @typedef {| EachBlock + * | ThenBlock + * | CatchBlock + * | InlineComponent + * | Element + * | SlotTemplate + * | ConstTag} NodeWithScope + */ +