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 Block from '../Block';
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 Text from '../../nodes/Text';
import { Identifier } from 'estree';
import { changed } from './shared/changed';
import MustacheTag from '../../nodes/MustacheTag';
export default class TitleWrapper extends Wrapper {
node: Title;
@ -41,25 +42,21 @@ export default class TitleWrapper extends Wrapper {
add_to_set(all_dependencies, expression.dependencies);
} else {
// '{foo} {bar}' — treat as string concatenation
value =
(this.node.children[0].type === 'Text' ? '' : `"" + `) +
this.node.children
.map((chunk) => {
if (chunk.type === 'Text') {
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);
});
// @ts-ignore todo: check this
return chunk.expression.get_precedence() <= 13 ? `(${snippet})` : snippet;
}
})
.join(' + ');
value = this.node.children
.map(chunk => {
if (chunk.type === 'Text') return string_literal(chunk.data);
(chunk as MustacheTag).expression.dependencies.forEach(d => {
all_dependencies.add(d);
});
return (chunk as MustacheTag).expression.manipulate(block);
})
.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(
@ -68,7 +65,7 @@ export default class TitleWrapper extends Wrapper {
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(
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) {
component.partly_hoisted.push(b`
function ${fn}(${['$$value', ...contextual_dependencies]}) {
function ${fn}($$value, ${contextual_dependencies}) {
if (${lhs} === $$value) return;
@binding_callbacks[$$value ? 'unshift' : 'push'](() => {
${body}
@ -53,8 +56,7 @@ export default function bind_this(component: Component, block: Block, binding: B
`);
const args = [];
for (const arg of contextual_dependencies) {
const id: Identifier = { type: 'Identifier', name: arg };
for (const id of contextual_dependencies) {
args.push(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}`);
block.chunks.init.push(b`
const ${assign} = () => #ctx.${fn}(${[variable].concat(args)});
const ${unassign} = () => #ctx.${fn}(${['null'].concat(args)});
const ${assign} = () => #ctx.${fn}(${variable}, ${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
// nulled out before they're created, to avoid glitches

Loading…
Cancel
Save