Convert src/compiler/compile/render_dom/wrappers/Title.ts to JavaScript

pull/8569/head
Simon Holthausen 3 years ago
parent 63dcef5ced
commit 2e787e67f5

@ -1,98 +1,93 @@
import { b, x } from 'code-red'; import { b, x } from 'code-red';
import Wrapper from './shared/Wrapper'; import Wrapper from './shared/Wrapper.js';
import Renderer from '../Renderer'; import { string_literal } from '../../utils/stringify.js';
import Block from '../Block'; import add_to_set from '../../utils/add_to_set.js';
import Title from '../../nodes/Title';
import { string_literal } from '../../utils/stringify';
import add_to_set from '../../utils/add_to_set';
import Text from '../../nodes/Text';
import { Identifier } from 'estree';
import MustacheTag from '../../nodes/MustacheTag';
/** @extends Wrapper */
export default class TitleWrapper extends Wrapper { export default class TitleWrapper extends Wrapper {
node: Title;
constructor( /** @type {import('../../nodes/Title.js').default} */
renderer: Renderer, node;
block: Block,
parent: Wrapper, /**
node: Title, * @param {import('../Renderer.js').default} renderer
_strip_whitespace: boolean, * @param {import('../Block.js').default} block
_next_sibling: Wrapper * @param {import('./shared/Wrapper.js').default} parent
) { * @param {import('../../nodes/Title.js').default} node
super(renderer, block, parent, node); * @param {boolean} _strip_whitespace
} * @param {import('./shared/Wrapper.js').default} _next_sibling
*/
render(block: Block, _parent_node: Identifier, _parent_nodes: Identifier) { constructor(renderer, block, parent, node, _strip_whitespace, _next_sibling) {
const is_dynamic = !!this.node.children.find((node) => node.type !== 'Text'); super(renderer, block, parent, node);
}
if (is_dynamic) {
let value; /**
* @param {import('../Block.js').default} block
const all_dependencies: Set<string> = new Set(); * @param {import('estree').Identifier} _parent_node
* @param {import('estree').Identifier} _parent_nodes
// TODO some of this code is repeated in Tag.ts — would be good to */
// DRY it out if that's possible without introducing crazy indirection render(block, _parent_node, _parent_nodes) {
if (this.node.children.length === 1) { const is_dynamic = !!this.node.children.find((node) => node.type !== 'Text');
// single {tag} — may be a non-string if (is_dynamic) {
// @ts-ignore todo: check this let value;
const { expression } = this.node.children[0];
value = expression.manipulate(block); /** @type {Set<string>} */
add_to_set(all_dependencies, expression.dependencies); const all_dependencies = new Set();
} else { // TODO some of this code is repeated in Tag.ts — would be good to
// '{foo} {bar}' — treat as string concatenation // DRY it out if that's possible without introducing crazy indirection
value = this.node.children if (this.node.children.length === 1) {
.map((chunk) => { // single {tag} — may be a non-string
if (chunk.type === 'Text') return string_literal(chunk.data); // @ts-ignore todo: check this
const { expression } = this.node.children[0];
(chunk as MustacheTag).expression.dependencies.forEach((d) => { value = expression.manipulate(block);
all_dependencies.add(d); add_to_set(all_dependencies, expression.dependencies);
}); }
else {
return (chunk as MustacheTag).expression.manipulate(block); // '{foo} {bar}' — treat as string concatenation
}) value = this.node.children
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`); .map((chunk) => {
if (chunk.type === 'Text')
if (this.node.children[0].type !== 'Text') { return string_literal(chunk.data);
value = x`"" + ${value}`; ( /** @type {import('../../nodes/MustacheTag.js').default} */(chunk)).expression.dependencies.forEach((d) => {
} all_dependencies.add(d);
} });
return ( /** @type {import('../../nodes/MustacheTag.js').default} */(chunk)).expression.manipulate(block);
const last = this.node.should_cache && block.get_unique_name('title_value'); })
.reduce((lhs, rhs) => x `${lhs} + ${rhs}`);
if (this.node.should_cache) block.add_variable(last); if (this.node.children[0].type !== 'Text') {
value = x `"" + ${value}`;
const init = this.node.should_cache ? x`${last} = ${value}` : value; }
}
block.chunks.init.push(b`@_document.title = ${init};`); const last = this.node.should_cache && block.get_unique_name('title_value');
if (this.node.should_cache)
const updater = b`@_document.title = ${this.node.should_cache ? last : value};`; block.add_variable(last);
const init = this.node.should_cache ? x `${last} = ${value}` : value;
if (all_dependencies.size) { block.chunks.init.push(b `@_document.title = ${init};`);
const dependencies = Array.from(all_dependencies); const updater = b `@_document.title = ${this.node.should_cache ? last : value};`;
if (all_dependencies.size) {
let condition = block.renderer.dirty(dependencies); const dependencies = Array.from(all_dependencies);
let condition = block.renderer.dirty(dependencies);
if (block.has_outros) { if (block.has_outros) {
condition = x`!#current || ${condition}`; condition = x `!#current || ${condition}`;
} }
if (this.node.should_cache) {
if (this.node.should_cache) { condition = x `${condition} && (${last} !== (${last} = ${value}))`;
condition = x`${condition} && (${last} !== (${last} = ${value}))`; }
} block.chunks.update.push(b `
block.chunks.update.push(b`
if (${condition}) { if (${condition}) {
${updater} ${updater}
}`); }`);
} }
} else { }
const value = else {
this.node.children.length > 0 const value = this.node.children.length > 0
? string_literal((this.node.children[0] as Text).data) ? string_literal(( /** @type {import('../../nodes/Text.js').default} */(this.node.children[0])).data)
: x`""`; : x `""`;
block.chunks.hydrate.push(b `@_document.title = ${value};`);
block.chunks.hydrate.push(b`@_document.title = ${value};`); }
} }
}
} }

Loading…
Cancel
Save