Lint and format src/compiler/compile/render_ssr/Renderer.js

pull/8569/head
Simon Holthausen 3 years ago
parent 845a880f81
commit 6d5d01efe3

@ -15,122 +15,120 @@ import Text from './handlers/Text.js';
import Title from './handlers/Title.js'; import Title from './handlers/Title.js';
import { collapse_template_literal } from '../utils/collapse_template_literal.js'; import { collapse_template_literal } from '../utils/collapse_template_literal.js';
import { escape_template } from '../utils/stringify.js'; import { escape_template } from '../utils/stringify.js';
function noop() { } function noop() {}
/** @type {Record<string, Handler>} */ /** @type {Record<string, Handler>} */
const handlers = { const handlers = {
AwaitBlock, AwaitBlock,
Body: noop, Body: noop,
Comment, Comment,
DebugTag, DebugTag,
Document: noop, Document: noop,
EachBlock, EachBlock,
Element, Element,
Head, Head,
IfBlock, IfBlock,
InlineComponent, InlineComponent,
KeyBlock, KeyBlock,
MustacheTag: Tag, MustacheTag: Tag,
Options: noop, Options: noop,
RawMustacheTag: HtmlTag, RawMustacheTag: HtmlTag,
Slot, Slot,
SlotTemplate, SlotTemplate,
Text, Text,
Title, Title,
Window: noop Window: noop
}; };
/** */ /** */
export default class Renderer { export default class Renderer {
/** @default false */
has_bindings = false;
/** @default false */ /** @type {import('estree').Identifier} */
has_bindings = false; name = undefined;
/** @type {import('estree').Identifier} */ /**
name = undefined; * @default []
* @type {Array<{ current: { value: string }; literal: TemplateLiteral }>}
*/
stack = [];
/** /** @type {{ value: string }} */
* @default [] current = undefined; // TODO can it just be `current: string`?
* @type {Array<{ current: { value: string }; literal: TemplateLiteral }>}
*/
stack = [];
/** @type {{ value: string }} */ /** @type {import('estree').TemplateLiteral} */
current = undefined; // TODO can it just be `current: string`? literal = undefined;
/** @type {import('estree').TemplateLiteral} */ /**
literal = undefined; * @default []
* @type {AppendTarget[]}
*/
targets = [];
constructor({ name }) {
this.name = name;
this.push();
}
/** /** @param {string} str */
* @default [] add_string(str) {
* @type {AppendTarget[]} this.current.value += escape_template(str);
*/ }
targets = [];
constructor({ name }) {
this.name = name;
this.push();
}
/** @param {string} str */ /** @param {import('estree').Expression} node */
add_string(str) { add_expression(node) {
this.current.value += escape_template(str); 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('estree').Expression} node */ /**
add_expression(node) { * @param {INode[]} nodes
this.literal.quasis.push({ * @param {RenderOptions} options
type: 'TemplateElement', */
value: { raw: this.current.value, cooked: null }, render(nodes, options) {
tail: false nodes.forEach((node) => {
}); const handler = handlers[node.type];
this.literal.expressions.push(node); if (!handler) {
this.current.value = ''; throw new Error(`No handler for '${node.type}' nodes`);
} }
push() { handler(node, this, options);
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 {INode[]} nodes
* @param {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);
});
}
} }
/** @typedef {(node: any, renderer: Renderer, options: CompileOptions) => void} Handler */ /** @typedef {(node: any, renderer: Renderer, options: CompileOptions) => void} Handler */
/** @typedef {Object} RenderOptions /** @typedef {Object} RenderOptions
* @property {(c:number)=>{line:number;column:number}} locate * @property {(c:number)=>{line:number;column:number}} locate
* @property {string} [head_id] * @property {string} [head_id]
* @property {boolean} [has_added_svelte_hash] * @property {boolean} [has_added_svelte_hash]
*/ */

Loading…
Cancel
Save