Lint and format src/compiler/compile/render_dom/wrappers/Title.js

pull/8569/head
Simon Holthausen 3 years ago
parent c4bbf44aa0
commit 711dfa073b

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

Loading…
Cancel
Save