File size: 3,929 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { string_literal } from '../utils/stringify.js';
import add_to_set from '../utils/add_to_set.js';
import Node from './shared/Node.js';
import Expression from './shared/Expression.js';
import { x } from 'code-red';
import compiler_warnings from '../compiler_warnings.js';

/** @extends Node<'Attribute' | 'Spread', import('./Element.js').default> */
export default class Attribute extends Node {
	/** @type {import('./shared/TemplateScope.js').default} */
	scope;

	/** @type {string} */
	name;

	/** @type {boolean} */
	is_spread;

	/** @type {boolean} */
	is_true;

	/** @type {boolean} */
	is_static;

	/** @type {import('./shared/Expression.js').default} */
	expression;

	/** @type {Array<import('./Text.js').default | import('./shared/Expression.js').default>} */
	chunks;

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

	/**
	 * @param {import('../Component.js').default} component
	 * @param {import('./shared/Node.js').default} parent
	 * @param {import('./shared/TemplateScope.js').default} scope
	 * @param {import('../../interfaces.js').TemplateNode} info
	 */
	constructor(component, parent, scope, info) {
		super(component, parent, scope, info);
		this.scope = scope;

		if (info.type === 'Spread') {
			this.name = null;
			this.is_spread = true;
			this.is_true = false;
			this.expression = new Expression(component, this, scope, info.expression);
			this.dependencies = this.expression.dependencies;
			this.chunks = null;
			this.is_static = false;
		} else {
			this.name = info.name;
			this.is_true = info.value === true;
			this.is_static = true;
			this.dependencies = new Set();
			this.chunks = this.is_true
				? []
				: info.value.map((node) => {
						if (node.type === 'Text') return node;
						this.is_static = false;
						const expression = new Expression(component, this, scope, node.expression);
						add_to_set(this.dependencies, expression.dependencies);
						return expression;
				  });
		}

		if (this.dependencies.size > 0) {
			parent.cannot_use_innerhtml();
			parent.not_static_content();
		}

		// TODO Svelte 5: Think about moving this into the parser and make it an error
		if (
			this.name &&
			this.name.includes(':') &&
			!this.name.startsWith('xmlns:') &&
			!this.name.startsWith('xlink:') &&
			!this.name.startsWith('xml:')
		) {
			component.warn(this, compiler_warnings.illegal_attribute_character);
		}
	}
	get_dependencies() {
		if (this.is_spread) return this.expression.dynamic_dependencies();

		/** @type {Set<string>} */
		const dependencies = new Set();
		this.chunks.forEach((chunk) => {
			if (chunk.type === 'Expression') {
				add_to_set(dependencies, chunk.dynamic_dependencies());
			}
		});
		return Array.from(dependencies);
	}

	/** @param {any} block */
	get_value(block) {
		if (this.is_true) return x`true`;
		if (this.chunks.length === 0) return x`""`;
		if (this.chunks.length === 1) {
			return this.chunks[0].type === 'Text'
				? string_literal(/** @type {import('./Text.js').default} */ (this.chunks[0]).data)
				: /** @type {import('./shared/Expression.js').default} */ (this.chunks[0]).manipulate(
						block
				  );
		}
		let expression = this.chunks
			.map(
				/** @param {any} chunk */ (chunk) =>
					chunk.type === 'Text' ? string_literal(chunk.data) : chunk.manipulate(block)
			)
			.reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
		if (this.chunks[0].type !== 'Text') {
			expression = x`"" + ${expression}`;
		}
		return expression;
	}
	get_static_value() {
		if (!this.is_static) return null;
		return this.is_true
			? true
			: this.chunks[0]
			? // method should be called only when `is_static = true`
			  /** @type {import('./Text.js').default} */ (this.chunks[0]).data
			: '';
	}
	should_cache() {
		return this.is_static
			? false
			: this.chunks.length === 1
			? // @ts-ignore todo: probably error
			  this.chunks[0].node.type !== 'Identifier' || this.scope.names.has(this.chunks[0].node.name)
			: true;
	}
}