-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathTemplateScope.ts
45 lines (37 loc) · 1.28 KB
/
TemplateScope.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import EachBlock from '../EachBlock';
import ThenBlock from '../ThenBlock';
import CatchBlock from '../CatchBlock';
import InlineComponent from '../InlineComponent';
import Element from '../Element';
type NodeWithScope = EachBlock | ThenBlock | CatchBlock | InlineComponent | Element;
export default class TemplateScope {
names: Set<string>;
dependencies_for_name: Map<string, Set<string>>;
owners: Map<string, NodeWithScope> = new Map();
parent?: TemplateScope;
constructor(parent?: TemplateScope) {
this.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) {
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);
}
get_owner(name: string): NodeWithScope {
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');
}
}