more tidying up

pull/1367/head
Rich Harris 7 years ago
parent 11fdb086f9
commit d10072f6b7

@ -10,7 +10,6 @@ export interface BlockOptions {
generator?: DomGenerator;
comment?: string;
key?: string;
contexts?: Map<string, string>;
indexNames?: Map<string, string>;
listNames?: Map<string, string>;
dependencies?: Set<string>;
@ -24,7 +23,6 @@ export default class Block {
key: string;
first: string;
contexts: Map<string, string>;
dependencies: Set<string>;
indexNames: Map<string, string>;
listNames: Map<string, string>;
@ -63,7 +61,6 @@ export default class Block {
this.key = options.key;
this.first = null;
this.contexts = options.contexts;
this.dependencies = new Set();
this.indexNames = options.indexNames;

@ -52,14 +52,9 @@ export default class AwaitBlock extends Node {
child.block = block.child({
comment: createDebuggingComment(child, this.compiler),
name: this.compiler.getUniqueName(`create_${status}_block`),
contexts: new Map(block.contexts)
name: this.compiler.getUniqueName(`create_${status}_block`)
});
if (arg) {
child.block.contexts.set(arg, arg); // TODO should be using getUniqueName
}
child.initChildren(child.block, stripWhitespace, nextSibling);
this.compiler.blocks.push(child.block);

@ -17,6 +17,7 @@ const readOnlyMediaAttributes = new Set([
export default class Binding extends Node {
name: string;
value: Expression;
isContextual: boolean;
usesContext: boolean;
obj: string;
prop: string;
@ -30,6 +31,9 @@ export default class Binding extends Node {
let obj;
let prop;
const { name } = getObject(this.value.node);
this.isContextual = scope.names.has(name);
if (this.value.node.type === 'MemberExpression') {
prop = `[✂${this.value.node.property.start}-${this.value.node.property.end}✂]`;
if (!this.value.node.computed) prop = `'${prop}'`;
@ -37,7 +41,6 @@ export default class Binding extends Node {
this.usesContext = true;
} else {
const { name } = getObject(this.value.node);
obj = 'ctx';
prop = `'${name}'`;
@ -182,11 +185,12 @@ function getEventHandler(
snippet: string,
dependencies: string[],
value: string,
isContextual: boolean
) {
const storeDependencies = [...dependencies].filter(prop => prop[0] === '$').map(prop => prop.slice(1));
dependencies = [...dependencies].filter(prop => prop[0] !== '$');
if (block.contexts.has(name)) {
if (binding.isContextual) {
const tail = binding.value.node.type === 'MemberExpression'
? getTailSnippet(binding.value.node)
: '';

@ -233,7 +233,7 @@ export default class Component extends Node {
let setFromChild;
if (block.contexts.has(key)) { // TODO remove block.contexts
if (binding.isContextual) {
const computed = isComputed(binding.value.node);
const tail = binding.value.node.type === 'MemberExpression' ? getTailSnippet(binding.value.node) : '';

@ -71,11 +71,8 @@ export default class EachBlock extends Node {
this.block = block.child({
comment: createDebuggingComment(this, this.compiler),
name: this.compiler.getUniqueName('create_each_block'),
context: this.context,
key: this.key,
contexts: new Map(block.contexts),
indexNames: new Map(block.indexNames),
listNames: new Map(block.listNames)
});
@ -90,16 +87,6 @@ export default class EachBlock extends Node {
this.block.getUniqueName(this.index); // this prevents name collisions (#1254)
}
const context = this.block.getUniqueName(this.context);
this.block.contexts.set(this.context, context); // TODO this is now redundant?
if (this.destructuredContexts) {
for (let i = 0; i < this.destructuredContexts.length; i += 1) {
const context = this.block.getUniqueName(this.destructuredContexts[i]);
this.block.contexts.set(this.destructuredContexts[i], context);
}
}
this.contextProps = [
`${listName}: ${listName}`,
`${this.context}: ${listName}[#i]`,

@ -24,8 +24,6 @@ export default class Fragment extends Node {
name: '@create_main_fragment',
key: null,
contexts: new Map(),
indexNames: new Map(),
listNames: new Map(),

@ -6,10 +6,18 @@ import mapChildren from './shared/mapChildren';
export default class Title extends Node {
type: 'Title';
children: any[]; // TODO
shouldCache: boolean;
constructor(compiler, parent, scope, info) {
super(compiler, parent, scope, info);
this.children = mapChildren(compiler, parent, scope, info.children);
this.shouldCache = info.children.length === 1
? (
info.children[0].type !== 'Identifier' ||
scope.names.has(info.children[0].name)
)
: true;
}
build(
@ -23,7 +31,6 @@ export default class Title extends Node {
let value;
const allDependencies = new Set();
let shouldCache;
// 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
@ -36,11 +43,6 @@ export default class Title extends Node {
dependencies.forEach(d => {
allDependencies.add(d);
});
shouldCache = (
expression.type !== 'Identifier' ||
block.contexts.has(expression.name)
);
} else {
// '{foo} {bar}' — treat as string concatenation
value =
@ -60,23 +62,21 @@ export default class Title extends Node {
}
})
.join(' + ');
shouldCache = true;
}
const last = shouldCache && block.getUniqueName(
const last = this.shouldCache && block.getUniqueName(
`title_value`
);
if (shouldCache) block.addVariable(last);
if (this.shouldCache) block.addVariable(last);
let updater;
const init = shouldCache ? `${last} = ${value}` : value;
const init = this.shouldCache ? `${last} = ${value}` : value;
block.builders.init.addLine(
`document.title = ${init};`
);
updater = `document.title = ${shouldCache ? last : value};`;
updater = `document.title = ${this.shouldCache ? last : value};`;
if (allDependencies.size) {
const dependencies = Array.from(allDependencies);
@ -87,7 +87,7 @@ export default class Title extends Node {
const updateCachedValue = `${last} !== (${last} = ${value})`;
const condition = shouldCache ?
const condition = this.shouldCache ?
( dependencies.length ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue ) :
changedCheck;

@ -12,8 +12,6 @@ export default class Block {
generator: SsrGenerator;
conditions: string[];
contexts: Map<string, string>;
constructor(options: BlockOptions) {
Object.assign(this, options);
}

@ -59,7 +59,6 @@ export default function ssr(
// create main render() function
const mainBlock = new Block({
generator,
contexts: new Map(),
conditions: [],
});

@ -10,14 +10,7 @@ export default function visitAwaitBlock(
) {
const { snippet } = node.expression;
// TODO should this be the generator's job? It's duplicated between
// here and the equivalent DOM compiler visitor
const contexts = new Map(block.contexts);
contexts.set(node.value, '__value');
const childBlock = block.child({
contexts
});
const childBlock = block.child({});
generator.append('${(function(__value) { if(__isPromise(__value)) return `');

@ -20,20 +20,7 @@ export default function visitEachBlock(
const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}__each(${snippet}, ${getContext}, ctx => \``;
generator.append(open);
// TODO should this be the generator's job? It's duplicated between
// here and the equivalent DOM compiler visitor
const contexts = new Map(block.contexts);
contexts.set(node.context, node.context);
if (node.destructuredContexts) {
for (let i = 0; i < node.destructuredContexts.length; i += 1) {
contexts.set(node.destructuredContexts[i], `${node.context}[${i}]`);
}
}
const childBlock = block.child({
contexts
});
const childBlock = block.child({});
node.children.forEach((child: Node) => {
visit(generator, childBlock, child);

Loading…
Cancel
Save