mirror of https://github.com/sveltejs/svelte
parent
bcf0d2fa3c
commit
7bf907e5c8
@ -1,69 +1,83 @@
|
|||||||
import EachBlock from '../EachBlock';
|
export default class TemplateScope {
|
||||||
import ThenBlock from '../ThenBlock';
|
|
||||||
import CatchBlock from '../CatchBlock';
|
|
||||||
import InlineComponent from '../InlineComponent';
|
|
||||||
import Element from '../Element';
|
|
||||||
import SlotTemplate from '../SlotTemplate';
|
|
||||||
import ConstTag from '../ConstTag';
|
|
||||||
|
|
||||||
type NodeWithScope =
|
/** @type {Set<string>} */
|
||||||
| EachBlock
|
names;
|
||||||
| ThenBlock
|
|
||||||
| CatchBlock
|
|
||||||
| InlineComponent
|
|
||||||
| Element
|
|
||||||
| SlotTemplate
|
|
||||||
| ConstTag;
|
|
||||||
|
|
||||||
export default class TemplateScope {
|
/** @type {Map<string, Set<string>>} */
|
||||||
names: Set<string>;
|
dependencies_for_name;
|
||||||
dependencies_for_name: Map<string, Set<string>>;
|
|
||||||
owners: Map<string, NodeWithScope> = new Map();
|
/** @type {Map<string, NodeWithScope>} */
|
||||||
parent?: TemplateScope;
|
owners = new Map();
|
||||||
|
|
||||||
constructor(parent?: TemplateScope) {
|
/** @type {TemplateScope} */
|
||||||
this.parent = parent;
|
parent;
|
||||||
this.names = new Set(parent ? parent.names : []);
|
|
||||||
this.dependencies_for_name = new Map(parent ? parent.dependencies_for_name : []);
|
|
||||||
}
|
|
||||||
|
|
||||||
add(name, dependencies: Set<string>, owner) {
|
/** @param {TemplateScope} [parent] undefined */
|
||||||
this.names.add(name);
|
constructor(parent) {
|
||||||
this.dependencies_for_name.set(name, dependencies);
|
this.parent = parent;
|
||||||
this.owners.set(name, owner);
|
this.names = new Set(parent ? parent.names : []);
|
||||||
return this;
|
this.dependencies_for_name = new Map(parent ? parent.dependencies_for_name : []);
|
||||||
}
|
}
|
||||||
|
|
||||||
child() {
|
/**
|
||||||
const child = new TemplateScope(this);
|
* @param {any} name
|
||||||
return child;
|
* @param {Set<string>} 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) {
|
/** @param {string} name */
|
||||||
return !this.parent || (!this.names.has(name) && this.parent.is_top_level(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) {
|
/** @param {string} name */
|
||||||
const owner = this.get_owner(name);
|
is_let(name) {
|
||||||
return (
|
const owner = this.get_owner(name);
|
||||||
owner &&
|
return (owner &&
|
||||||
(owner.type === 'Element' ||
|
(owner.type === 'Element' ||
|
||||||
owner.type === 'InlineComponent' ||
|
owner.type === 'InlineComponent' ||
|
||||||
owner.type === 'SlotTemplate')
|
owner.type === 'SlotTemplate'));
|
||||||
);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
is_await(name: string) {
|
/** @param {string} name */
|
||||||
const owner = this.get_owner(name);
|
is_await(name) {
|
||||||
return owner && (owner.type === 'ThenBlock' || owner.type === 'CatchBlock');
|
const owner = this.get_owner(name);
|
||||||
}
|
return owner && (owner.type === 'ThenBlock' || owner.type === 'CatchBlock');
|
||||||
|
}
|
||||||
|
|
||||||
is_const(name: string) {
|
/** @param {string} name */
|
||||||
const owner = this.get_owner(name);
|
is_const(name) {
|
||||||
return owner && owner.type === 'ConstTag';
|
const owner = this.get_owner(name);
|
||||||
}
|
return owner && owner.type === 'ConstTag';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {| EachBlock
|
||||||
|
* | ThenBlock
|
||||||
|
* | CatchBlock
|
||||||
|
* | InlineComponent
|
||||||
|
* | Element
|
||||||
|
* | SlotTemplate
|
||||||
|
* | ConstTag} NodeWithScope
|
||||||
|
*/
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in new issue