File size: 2,005 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/** The scope of constructs within the Svelte template */
export default class TemplateScope {
	/**
	 * @typedef {import('../EachBlock').default
	 * 	| import('../ThenBlock').default
	 * 	| import('../CatchBlock').default
	 * 	| import('../InlineComponent').default
	 * 	| import('../Element').default
	 * 	| import('../SlotTemplate').default
	 * 	| import('../ConstTag').default} NodeWithScope
	 */

	/** @type {Set<string>} */
	names;

	/** @type {Map<string, Set<string>>} */
	dependencies_for_name;

	/** @type {Map<string, NodeWithScope>} */
	owners = new Map();

	/** @type {TemplateScope} */
	parent;

	/** @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 : []);
	}

	/**
	 * @param {any} name
	 * @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;
	}

	/** @param {string} name */
	is_top_level(name) {
		return !this.parent || (!this.names.has(name) && this.parent.is_top_level(name));
	}

	/**
	 * @param {string} name
	 * @returns {NodeWithScope}
	 */
	get_owner(name) {
		return this.owners.get(name) || (this.parent && this.parent.get_owner(name));
	}

	/** @param {string} name */
	is_let(name) {
		const owner = this.get_owner(name);
		return (
			owner &&
			(owner.type === 'Element' ||
				owner.type === 'InlineComponent' ||
				owner.type === 'SlotTemplate')
		);
	}

	/** @param {string} name */
	is_await(name) {
		const owner = this.get_owner(name);
		return owner && (owner.type === 'ThenBlock' || owner.type === 'CatchBlock');
	}

	/** @param {string} name */
	is_const(name) {
		const owner = this.get_owner(name);
		return owner && owner.type === 'ConstTag';
	}
}