File size: 3,152 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
import AwaitBlock from './handlers/AwaitBlock.js';
import Comment from './handlers/Comment.js';
import DebugTag from './handlers/DebugTag.js';
import EachBlock from './handlers/EachBlock.js';
import Element from './handlers/Element.js';
import Head from './handlers/Head.js';
import HtmlTag from './handlers/HtmlTag.js';
import IfBlock from './handlers/IfBlock.js';
import InlineComponent from './handlers/InlineComponent.js';
import KeyBlock from './handlers/KeyBlock.js';
import Slot from './handlers/Slot.js';
import SlotTemplate from './handlers/SlotTemplate.js';
import Tag from './handlers/Tag.js';
import Text from './handlers/Text.js';
import Title from './handlers/Title.js';
import { collapse_template_literal } from '../utils/collapse_template_literal.js';
import { escape_template } from '../utils/stringify.js';

function noop() {}

/** @type {Record<string, {(node: any, renderer: Renderer, options: import('../../interfaces.js').CompileOptions): void}>} */
const handlers = {
	AwaitBlock,
	Body: noop,
	Comment,
	DebugTag,
	Document: noop,
	EachBlock,
	Element,
	Head,
	IfBlock,
	InlineComponent,
	KeyBlock,
	MustacheTag: Tag,
	Options: noop,
	RawMustacheTag: HtmlTag,
	Slot,
	SlotTemplate,
	Text,
	Title,
	Window: noop
};

export default class Renderer {
	has_bindings = false;

	/** @type {import('estree').Identifier} */
	name = undefined;

	/** @type {Array<{ current: { value: string }; literal: import('estree').TemplateLiteral }>} */
	stack = [];

	/** @type {{ value: string }} */
	current = undefined; // TODO can it just be `current: string`?

	/** @type {import('estree').TemplateLiteral} */
	literal = undefined;

	/** @type {import('../../interfaces.js').AppendTarget[]} */
	targets = [];
	constructor({ name }) {
		this.name = name;
		this.push();
	}

	/** @param {string} str */
	add_string(str) {
		this.current.value += escape_template(str);
	}

	/** @param {import('estree').Expression} node */
	add_expression(node) {
		this.literal.quasis.push({
			type: 'TemplateElement',
			value: { raw: this.current.value, cooked: null },
			tail: false
		});
		this.literal.expressions.push(node);
		this.current.value = '';
	}
	push() {
		const current = (this.current = { value: '' });
		const literal = (this.literal = {
			type: 'TemplateLiteral',
			expressions: [],
			quasis: []
		});
		this.stack.push({ current, literal });
	}
	pop() {
		this.literal.quasis.push({
			type: 'TemplateElement',
			value: { raw: this.current.value, cooked: null },
			tail: true
		});
		const popped = this.stack.pop();
		const last = this.stack[this.stack.length - 1];
		if (last) {
			this.literal = last.literal;
			this.current = last.current;
		}
		// Optimize the TemplateLiteral to remove unnecessary nodes
		collapse_template_literal(popped.literal);
		return popped.literal;
	}

	/**
	 * @param {import('../nodes/interfaces.js').INode[]} nodes
	 * @param {import('./private.js').RenderOptions} options
	 */
	render(nodes, options) {
		nodes.forEach((node) => {
			const handler = handlers[node.type];
			if (!handler) {
				throw new Error(`No handler for '${node.type}' nodes`);
			}
			handler(node, this, options);
		});
	}
}