couple of fixes

pull/3539/head
Richard Harris 6 years ago
parent 2bf4b9c870
commit ec238d746a

@ -3,11 +3,12 @@ import Wrapper from './shared/Wrapper';
import Renderer from '../Renderer'; import Renderer from '../Renderer';
import Block from '../Block'; import Block from '../Block';
import Title from '../../nodes/Title'; import Title from '../../nodes/Title';
import { stringify } from '../../utils/stringify'; import { stringify, string_literal } from '../../utils/stringify';
import add_to_set from '../../utils/add_to_set'; import add_to_set from '../../utils/add_to_set';
import Text from '../../nodes/Text'; import Text from '../../nodes/Text';
import { Identifier } from 'estree'; import { Identifier } from 'estree';
import { changed } from './shared/changed'; import { changed } from './shared/changed';
import MustacheTag from '../../nodes/MustacheTag';
export default class TitleWrapper extends Wrapper { export default class TitleWrapper extends Wrapper {
node: Title; node: Title;
@ -41,25 +42,21 @@ export default class TitleWrapper extends Wrapper {
add_to_set(all_dependencies, expression.dependencies); add_to_set(all_dependencies, expression.dependencies);
} else { } else {
// '{foo} {bar}' — treat as string concatenation // '{foo} {bar}' — treat as string concatenation
value = value = this.node.children
(this.node.children[0].type === 'Text' ? '' : `"" + `) + .map(chunk => {
this.node.children if (chunk.type === 'Text') return string_literal(chunk.data);
.map((chunk) => {
if (chunk.type === 'Text') { (chunk as MustacheTag).expression.dependencies.forEach(d => {
return stringify(chunk.data);
} else {
// @ts-ignore todo: check this
const snippet = chunk.expression.manipulate(block);
// @ts-ignore todo: check this
chunk.expression.dependencies.forEach(d => {
all_dependencies.add(d); all_dependencies.add(d);
}); });
// @ts-ignore todo: check this return (chunk as MustacheTag).expression.manipulate(block);
return chunk.expression.get_precedence() <= 13 ? `(${snippet})` : snippet;
}
}) })
.join(' + '); .reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
if (this.node.children[0].type !== 'Text') {
value = x`"" + ${value}`;
}
} }
const last = this.node.should_cache && block.get_unique_name( const last = this.node.should_cache && block.get_unique_name(
@ -68,7 +65,7 @@ export default class TitleWrapper extends Wrapper {
if (this.node.should_cache) block.add_variable(last); if (this.node.should_cache) block.add_variable(last);
const init = this.node.should_cache ? `${last} = ${value}` : value; const init = this.node.should_cache ? x`${last} = ${value}` : value;
block.chunks.init.push( block.chunks.init.push(
b`@_document.title = ${init};` b`@_document.title = ${init};`

@ -40,11 +40,14 @@ export default function bind_this(component: Component, block: Block, binding: B
`; `;
} }
const contextual_dependencies = Array.from(binding.expression.contextual_dependencies); const contextual_dependencies: Identifier[] = Array.from(binding.expression.contextual_dependencies).map(name => ({
type: 'Identifier',
name
}));
if (contextual_dependencies.length) { if (contextual_dependencies.length) {
component.partly_hoisted.push(b` component.partly_hoisted.push(b`
function ${fn}(${['$$value', ...contextual_dependencies]}) { function ${fn}($$value, ${contextual_dependencies}) {
if (${lhs} === $$value) return; if (${lhs} === $$value) return;
@binding_callbacks[$$value ? 'unshift' : 'push'](() => { @binding_callbacks[$$value ? 'unshift' : 'push'](() => {
${body} ${body}
@ -53,8 +56,7 @@ export default function bind_this(component: Component, block: Block, binding: B
`); `);
const args = []; const args = [];
for (const arg of contextual_dependencies) { for (const id of contextual_dependencies) {
const id: Identifier = { type: 'Identifier', name: arg };
args.push(id); args.push(id);
block.add_variable(id, x`#ctx.${id}`); block.add_variable(id, x`#ctx.${id}`);
} }
@ -63,11 +65,13 @@ export default function bind_this(component: Component, block: Block, binding: B
const unassign = block.get_unique_name(`unassign_${variable.name}`); const unassign = block.get_unique_name(`unassign_${variable.name}`);
block.chunks.init.push(b` block.chunks.init.push(b`
const ${assign} = () => #ctx.${fn}(${[variable].concat(args)}); const ${assign} = () => #ctx.${fn}(${variable}, ${args});
const ${unassign} = () => #ctx.${fn}(${['null'].concat(args)}); const ${unassign} = () => #ctx.${fn}(null, ${args});
`); `);
const condition = Array.from(contextual_dependencies).map(name => `${name} !== #ctx.${name}`).join(' || '); const condition = Array.from(contextual_dependencies)
.map(name => x`${name} !== #ctx.${name}`)
.reduce((lhs, rhs) => x`${lhs} || ${rhs}`);
// we push unassign and unshift assign so that references are // we push unassign and unshift assign so that references are
// nulled out before they're created, to avoid glitches // nulled out before they're created, to avoid glitches

Loading…
Cancel
Save