Merge pull request #1193 from sveltejs/TODO

[WIP] fix some TODOs
pull/1220/head
Rich Harris 8 years ago committed by GitHub
commit 0c3e44ac05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,10 +1,10 @@
import MagicString, { Bundle } from 'magic-string'; import MagicString, { Bundle } from 'magic-string';
import isReference from 'is-reference';
import { walk, childKeys } from 'estree-walker'; import { walk, childKeys } from 'estree-walker';
import { getLocator } from 'locate-character'; import { getLocator } from 'locate-character';
import deindent from '../utils/deindent'; import deindent from '../utils/deindent';
import CodeBuilder from '../utils/CodeBuilder'; import CodeBuilder from '../utils/CodeBuilder';
import getCodeFrame from '../utils/getCodeFrame'; import getCodeFrame from '../utils/getCodeFrame';
import isReference from '../utils/isReference';
import flattenReference from '../utils/flattenReference'; import flattenReference from '../utils/flattenReference';
import reservedNames from '../utils/reservedNames'; import reservedNames from '../utils/reservedNames';
import namespaces from '../utils/namespaces'; import namespaces from '../utils/namespaces';
@ -168,7 +168,7 @@ export default class Generator {
if (options.customElement === true) { if (options.customElement === true) {
this.customElement = { this.customElement = {
tag: this.tag, tag: this.tag,
props: this.props // TODO autofill this in props: this.props
} }
} else { } else {
this.customElement = options.customElement; this.customElement = options.customElement;
@ -752,12 +752,6 @@ export default class Generator {
if (node.type === 'Element' && (node.name === ':Component' || node.name === ':Self' || generator.components.has(node.name))) { if (node.type === 'Element' && (node.name === ':Component' || node.name === ':Self' || generator.components.has(node.name))) {
node.type = 'Component'; node.type = 'Component';
Object.setPrototypeOf(node, nodes.Component.prototype); Object.setPrototypeOf(node, nodes.Component.prototype);
} else if (node.name === ':Window') { // TODO do this in parse?
node.type = 'Window';
Object.setPrototypeOf(node, nodes.Window.prototype);
} else if (node.name === ':Head') { // TODO do this in parse?
node.type = 'Head';
Object.setPrototypeOf(node, nodes.Head.prototype);
} else if (node.type === 'Element' && node.name === 'title' && parentIsHead(parent)) { // TODO do this in parse? } else if (node.type === 'Element' && node.name === 'title' && parentIsHead(parent)) { // TODO do this in parse?
node.type = 'Title'; node.type = 'Title';
Object.setPrototypeOf(node, nodes.Title.prototype); Object.setPrototypeOf(node, nodes.Title.prototype);

@ -312,22 +312,19 @@ export default class Block {
if (this.hasOutroMethod) { if (this.hasOutroMethod) {
if (hasOutros) { if (hasOutros) {
properties.addBlock(deindent` properties.addBlock(deindent`
o: function outro(${this.alias('outrocallback')}) { o: function outro(#outrocallback) {
if (${outroing}) return; if (${outroing}) return;
${outroing} = true; ${outroing} = true;
${hasIntros && `${introing} = false;`} ${hasIntros && `${introing} = false;`}
var ${this.alias('outros')} = ${this.outros}; var #outros = ${this.outros};
${this.builders.outro} ${this.builders.outro}
}, },
`); `);
} else { } else {
// TODO should this be a helper?
properties.addBlock(deindent` properties.addBlock(deindent`
o: function outro(outrocallback) { o: @run,
outrocallback();
},
`); `);
} }
} }

@ -1,7 +1,7 @@
import MagicString from 'magic-string'; import MagicString from 'magic-string';
import isReference from 'is-reference';
import { parseExpressionAt } from 'acorn'; import { parseExpressionAt } from 'acorn';
import annotateWithScopes from '../../utils/annotateWithScopes'; import annotateWithScopes from '../../utils/annotateWithScopes';
import isReference from '../../utils/isReference';
import { walk } from 'estree-walker'; import { walk } from 'estree-walker';
import deindent from '../../utils/deindent'; import deindent from '../../utils/deindent';
import { stringify, escape } from '../../utils/stringify'; import { stringify, escape } from '../../utils/stringify';

@ -320,7 +320,7 @@ export default class Component extends Node {
${name} = new ${switch_vars.value}(${switch_vars.props}(state)); ${name} = new ${switch_vars.value}(${switch_vars.props}(state));
${name}._fragment.c(); ${name}._fragment.c();
${this.children.map(child => remount(generator, child, name))} ${this.children.map(child => child.remount(name))}
${name}._mount(${updateMountNode}, ${anchor}); ${name}._mount(${updateMountNode}, ${anchor});
${eventHandlers.map(handler => deindent` ${eventHandlers.map(handler => deindent`
@ -398,6 +398,10 @@ export default class Component extends Node {
`); `);
} }
} }
remount(name: string) {
return `${this.var}._mount(${name}._slotted${this.generator.legacy ? `["default"]` : `.default`}, null);`;
}
} }
function mungeAttribute(attribute: Node, block: Block): Attribute { function mungeAttribute(attribute: Node, block: Block): Attribute {
@ -547,32 +551,4 @@ function isComputed(node: Node) {
} }
return false; return false;
}
function remount(generator: DomGenerator, node: Node, name: string) {
// TODO make this a method of the nodes
if (node.type === 'Component') {
return `${node.var}._mount(${name}._slotted${generator.legacy ? `["default"]` : `.default`}, null);`;
}
if (node.type === 'Element') {
const slot = node.attributes.find(attribute => attribute.name === 'slot');
if (slot) {
return `@appendNode(${node.var}, ${name}._slotted.${node.getStaticAttributeValue('slot')});`;
}
return `@appendNode(${node.var}, ${name}._slotted${generator.legacy ? `["default"]` : `.default`});`;
}
if (node.type === 'Text' || node.type === 'MustacheTag' || node.type === 'RawMustacheTag') {
return `@appendNode(${node.var}, ${name}._slotted${generator.legacy ? `["default"]` : `.default`});`;
}
if (node.type === 'EachBlock') {
// TODO consider keyed blocks
return `for (var #i = 0; #i < ${node.iterations}.length; #i += 1) ${node.iterations}[#i].m(${name}._slotted${generator.legacy ? `["default"]` : `.default`}, null);`;
}
return `${node.var}.m(${name}._slotted${generator.legacy ? `["default"]` : `.default`}, null);`;
} }

@ -594,4 +594,9 @@ export default class EachBlock extends Node {
block.builders.destroy.addBlock(`@destroyEach(${iterations});`); block.builders.destroy.addBlock(`@destroyEach(${iterations});`);
} }
remount(name: string) {
// TODO consider keyed blocks
return `for (var #i = 0; #i < ${this.iterations}.length; #i += 1) ${this.iterations}[#i].m(${name}._slotted${this.generator.legacy ? `["default"]` : `.default`}, null);`;
}
} }

@ -671,6 +671,15 @@ export default class Element extends Node {
isMediaNode() { isMediaNode() {
return this.name === 'audio' || this.name === 'video'; return this.name === 'audio' || this.name === 'video';
} }
remount(name: string) {
const slot = this.attributes.find(attribute => attribute.name === 'slot');
if (slot) {
return `@appendNode(${this.var}, ${name}._slotted.${this.getStaticAttributeValue('slot')});`;
}
return `@appendNode(${this.var}, ${name}._slotted${this.generator.legacy ? `["default"]` : `.default`});`;
}
} }
function getRenderStatement( function getRenderStatement(

@ -101,7 +101,7 @@ export default class IfBlock extends Node {
? block.getUniqueName(`${name}_anchor`) ? block.getUniqueName(`${name}_anchor`)
: (this.next && this.next.var) || 'null'; : (this.next && this.next.var) || 'null';
const branches = getBranches(this.generator, block, parentNode, parentNodes, this); const branches = this.getBranches(block, parentNode, parentNodes, this);
const hasElse = isElseBranch(branches[branches.length - 1]); const hasElse = isElseBranch(branches[branches.length - 1]);
const if_name = hasElse ? '' : `if (${name}) `; const if_name = hasElse ? '' : `if (${name}) `;
@ -113,21 +113,12 @@ export default class IfBlock extends Node {
if (this.else) { if (this.else) {
if (hasOutros) { if (hasOutros) {
compoundWithOutros( this.buildCompoundWithOutros(block, parentNode, parentNodes, branches, dynamic, vars);
this.generator,
block,
parentNode,
parentNodes,
this,
branches,
dynamic,
vars
);
} else { } else {
compound(this.generator, block, parentNode, parentNodes, this, branches, dynamic, vars); this.buildCompound(block, parentNode, parentNodes, branches, dynamic, vars);
} }
} else { } else {
simple(this.generator, block, parentNode, parentNodes, this, branches[0], dynamic, vars); this.buildSimple(block, parentNode, parentNodes, branches[0], dynamic, vars);
} }
block.builders.create.addLine(`${if_name}${name}.c();`); block.builders.create.addLine(`${if_name}${name}.c();`);
@ -147,350 +138,334 @@ export default class IfBlock extends Node {
); );
} }
} }
}
buildCompound(
block: Block,
parentNode: string,
parentNodes: string,
branches,
dynamic,
{ name, anchor, hasElse, if_name }
) {
const select_block_type = this.generator.getUniqueName(`select_block_type`);
const current_block_type = block.getUniqueName(`current_block_type`);
const current_block_type_and = hasElse ? '' : `${current_block_type} && `;
block.builders.init.addBlock(deindent`
function ${select_block_type}(state) {
${branches
.map(({ condition, block }) => `${condition ? `if (${condition}) ` : ''}return ${block};`)
.join('\n')}
}
`);
block.builders.init.addBlock(deindent`
var ${current_block_type} = ${select_block_type}(state);
var ${name} = ${current_block_type_and}${current_block_type}(#component, state);
`);
const mountOrIntro = branches[0].hasIntroMethod ? 'i' : 'm';
// TODO move all this into the class const initialMountNode = parentNode || '#target';
const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.mount.addLine(
`${if_name}${name}.${mountOrIntro}(${initialMountNode}, ${anchorNode});`
);
function getBranches( const updateMountNode = this.getUpdateMountNode(anchor);
generator: DomGenerator,
block: Block,
parentNode: string,
parentNodes: string,
node: Node
) {
block.contextualise(node.expression); // TODO remove
const branches = [ const changeBlock = deindent`
{ ${hasElse
condition: node.metadata.snippet, ? deindent`
block: node.block.name, ${name}.u();
hasUpdateMethod: node.block.hasUpdateMethod, ${name}.d();
hasIntroMethod: node.block.hasIntroMethod, `
hasOutroMethod: node.block.hasOutroMethod, : deindent`
}, if (${name}) {
]; ${name}.u();
${name}.d();
}`}
${name} = ${current_block_type_and}${current_block_type}(#component, state);
${if_name}${name}.c();
${if_name}${name}.${mountOrIntro}(${updateMountNode}, ${anchor});
`;
visitChildren(generator, block, node); if (dynamic) {
block.builders.update.addBlock(deindent`
if (${current_block_type} === (${current_block_type} = ${select_block_type}(state)) && ${name}) {
${name}.p(changed, state);
} else {
${changeBlock}
}
`);
} else {
block.builders.update.addBlock(deindent`
if (${current_block_type} !== (${current_block_type} = ${select_block_type}(state))) {
${changeBlock}
}
`);
}
if (isElseIf(node.else)) { block.builders.unmount.addLine(`${if_name}${name}.u();`);
branches.push(
...getBranches(generator, block, parentNode, parentNodes, node.else.children[0])
);
} else {
branches.push({
condition: null,
block: node.else ? node.else.block.name : null,
hasUpdateMethod: node.else ? node.else.block.hasUpdateMethod : false,
hasIntroMethod: node.else ? node.else.block.hasIntroMethod : false,
hasOutroMethod: node.else ? node.else.block.hasOutroMethod : false,
});
if (node.else) { block.builders.destroy.addLine(`${if_name}${name}.d();`);
visitChildren(generator, block, node.else);
}
} }
return branches; // if any of the siblings have outros, we need to keep references to the blocks
} // (TODO does this only apply to bidi transitions?)
buildCompoundWithOutros(
block: Block,
parentNode: string,
parentNodes: string,
branches,
dynamic,
{ name, anchor, hasElse }
) {
const select_block_type = block.getUniqueName(`select_block_type`);
const current_block_type_index = block.getUniqueName(`current_block_type_index`);
const previous_block_index = block.getUniqueName(`previous_block_index`);
const if_block_creators = block.getUniqueName(`if_block_creators`);
const if_blocks = block.getUniqueName(`if_blocks`);
function visitChildren( const if_current_block_type_index = hasElse
generator: DomGenerator, ? ''
block: Block, : `if (~${current_block_type_index}) `;
node: Node
) {
node.children.forEach((child: Node) => {
child.build(node.block, null, 'nodes');
});
}
block.addVariable(current_block_type_index);
block.addVariable(name);
block.builders.init.addBlock(deindent`
var ${if_block_creators} = [
${branches.map(branch => branch.block).join(',\n')}
];
function simple( var ${if_blocks} = [];
generator: DomGenerator,
block: Block,
parentNode: string,
parentNodes: string,
node: Node,
branch,
dynamic,
{ name, anchor, if_name }
) {
block.builders.init.addBlock(deindent`
var ${name} = (${branch.condition}) && ${branch.block}(#component, state);
`);
const mountOrIntro = branch.hasIntroMethod ? 'i' : 'm'; function ${select_block_type}(state) {
const initialMountNode = parentNode || '#target'; ${branches
const anchorNode = parentNode ? 'null' : 'anchor'; .map(({ condition, block }, i) => `${condition ? `if (${condition}) ` : ''}return ${block ? i : -1};`)
.join('\n')}
}
`);
block.builders.mount.addLine( if (hasElse) {
`if (${name}) ${name}.${mountOrIntro}(${initialMountNode}, ${anchorNode});` block.builders.init.addBlock(deindent`
); ${current_block_type_index} = ${select_block_type}(state);
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](#component, state);
`);
} else {
block.builders.init.addBlock(deindent`
if (~(${current_block_type_index} = ${select_block_type}(state))) {
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](#component, state);
}
`);
}
const updateMountNode = node.getUpdateMountNode(anchor); const mountOrIntro = branches[0].hasIntroMethod ? 'i' : 'm';
const initialMountNode = parentNode || '#target';
const anchorNode = parentNode ? 'null' : 'anchor';
const enter = dynamic block.builders.mount.addLine(
? branch.hasIntroMethod `${if_current_block_type_index}${if_blocks}[${current_block_type_index}].${mountOrIntro}(${initialMountNode}, ${anchorNode});`
);
const updateMountNode = this.getUpdateMountNode(anchor);
const destroyOldBlock = deindent`
${name}.o(function() {
${if_blocks}[ ${previous_block_index} ].u();
${if_blocks}[ ${previous_block_index} ].d();
${if_blocks}[ ${previous_block_index} ] = null;
});
`;
const createNewBlock = deindent`
${name} = ${if_blocks}[${current_block_type_index}];
if (!${name}) {
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](#component, state);
${name}.c();
}
${name}.${mountOrIntro}(${updateMountNode}, ${anchor});
`;
const changeBlock = hasElse
? deindent` ? deindent`
if (${name}) { ${destroyOldBlock}
${name}.p(changed, state);
} else {
${name} = ${branch.block}(#component, state);
if (${name}) ${name}.c();
}
${name}.i(${updateMountNode}, ${anchor}); ${createNewBlock}
` `
: deindent` : deindent`
if (${name}) { if (${name}) {
${name}.p(changed, state); ${destroyOldBlock}
} else {
${name} = ${branch.block}(#component, state);
${name}.c();
${name}.m(${updateMountNode}, ${anchor});
} }
`
: branch.hasIntroMethod if (~${current_block_type_index}) {
? deindent` ${createNewBlock}
if (!${name}) { } else {
${name} = ${branch.block}(#component, state); ${name} = null;
${name}.c();
}
${name}.i(${updateMountNode}, ${anchor});
`
: deindent`
if (!${name}) {
${name} = ${branch.block}(#component, state);
${name}.c();
${name}.m(${updateMountNode}, ${anchor});
} }
`; `;
// no `p()` here — we don't want to update outroing nodes, if (dynamic) {
// as that will typically result in glitching block.builders.update.addBlock(deindent`
const exit = branch.hasOutroMethod var ${previous_block_index} = ${current_block_type_index};
? deindent` ${current_block_type_index} = ${select_block_type}(state);
${name}.o(function() { if (${current_block_type_index} === ${previous_block_index}) {
${name}.u(); ${if_current_block_type_index}${if_blocks}[${current_block_type_index}].p(changed, state);
${name}.d(); } else {
${name} = null; ${changeBlock}
}); }
` `);
: deindent` } else {
${name}.u(); block.builders.update.addBlock(deindent`
${name}.d(); var ${previous_block_index} = ${current_block_type_index};
${name} = null; ${current_block_type_index} = ${select_block_type}(state);
`; if (${current_block_type_index} !== ${previous_block_index}) {
${changeBlock}
block.builders.update.addBlock(deindent` }
if (${branch.condition}) { `);
${enter}
} else if (${name}) {
${exit}
} }
`);
block.builders.unmount.addLine(`${if_name}${name}.u();`); block.builders.destroy.addLine(deindent`
${if_current_block_type_index}{
block.builders.destroy.addLine(`${if_name}${name}.d();`); ${if_blocks}[${current_block_type_index}].u();
} ${if_blocks}[${current_block_type_index}].d();
}
`);
}
function compound( buildSimple(
generator: DomGenerator, block: Block,
block: Block, parentNode: string,
parentNode: string,
parentNodes: string, parentNodes: string,
node: Node, branch,
branches, dynamic,
dynamic, { name, anchor, if_name }
{ name, anchor, hasElse, if_name } ) {
) { block.builders.init.addBlock(deindent`
const select_block_type = generator.getUniqueName(`select_block_type`); var ${name} = (${branch.condition}) && ${branch.block}(#component, state);
const current_block_type = block.getUniqueName(`current_block_type`); `);
const current_block_type_and = hasElse ? '' : `${current_block_type} && `;
block.builders.init.addBlock(deindent`
function ${select_block_type}(state) {
${branches
.map(({ condition, block }) => `${condition ? `if (${condition}) ` : ''}return ${block};`)
.join('\n')}
}
var ${current_block_type} = ${select_block_type}(state);
var ${name} = ${current_block_type_and}${current_block_type}(#component, state);
`);
const mountOrIntro = branches[0].hasIntroMethod ? 'i' : 'm';
const initialMountNode = parentNode || '#target'; const mountOrIntro = branch.hasIntroMethod ? 'i' : 'm';
const anchorNode = parentNode ? 'null' : 'anchor'; const initialMountNode = parentNode || '#target';
block.builders.mount.addLine( const anchorNode = parentNode ? 'null' : 'anchor';
`${if_name}${name}.${mountOrIntro}(${initialMountNode}, ${anchorNode});`
);
const updateMountNode = node.getUpdateMountNode(anchor); block.builders.mount.addLine(
`if (${name}) ${name}.${mountOrIntro}(${initialMountNode}, ${anchorNode});`
);
const changeBlock = deindent` const updateMountNode = this.getUpdateMountNode(anchor);
${hasElse
const enter = dynamic
? branch.hasIntroMethod
? deindent`
if (${name}) {
${name}.p(changed, state);
} else {
${name} = ${branch.block}(#component, state);
if (${name}) ${name}.c();
}
${name}.i(${updateMountNode}, ${anchor});
`
: deindent`
if (${name}) {
${name}.p(changed, state);
} else {
${name} = ${branch.block}(#component, state);
${name}.c();
${name}.m(${updateMountNode}, ${anchor});
}
`
: branch.hasIntroMethod
? deindent`
if (!${name}) {
${name} = ${branch.block}(#component, state);
${name}.c();
}
${name}.i(${updateMountNode}, ${anchor});
`
: deindent`
if (!${name}) {
${name} = ${branch.block}(#component, state);
${name}.c();
${name}.m(${updateMountNode}, ${anchor});
}
`;
// no `p()` here — we don't want to update outroing nodes,
// as that will typically result in glitching
const exit = branch.hasOutroMethod
? deindent` ? deindent`
${name}.u(); ${name}.o(function() {
${name}.d();
`
: deindent`
if (${name}) {
${name}.u(); ${name}.u();
${name}.d(); ${name}.d();
}`} ${name} = null;
${name} = ${current_block_type_and}${current_block_type}(#component, state); });
${if_name}${name}.c(); `
${if_name}${name}.${mountOrIntro}(${updateMountNode}, ${anchor}); : deindent`
`; ${name}.u();
${name}.d();
${name} = null;
`;
if (dynamic) {
block.builders.update.addBlock(deindent`
if (${current_block_type} === (${current_block_type} = ${select_block_type}(state)) && ${name}) {
${name}.p(changed, state);
} else {
${changeBlock}
}
`);
} else {
block.builders.update.addBlock(deindent` block.builders.update.addBlock(deindent`
if (${current_block_type} !== (${current_block_type} = ${select_block_type}(state))) { if (${branch.condition}) {
${changeBlock} ${enter}
} else if (${name}) {
${exit}
} }
`); `);
}
block.builders.unmount.addLine(`${if_name}${name}.u();`); block.builders.unmount.addLine(`${if_name}${name}.u();`);
block.builders.destroy.addLine(`${if_name}${name}.d();`); block.builders.destroy.addLine(`${if_name}${name}.d();`);
} }
// if any of the siblings have outros, we need to keep references to the blocks getBranches(
// (TODO does this only apply to bidi transitions?) block: Block,
function compoundWithOutros( parentNode: string,
generator: DomGenerator, parentNodes: string,
block: Block, node: Node
parentNode: string, ) {
parentNodes: string, block.contextualise(node.expression); // TODO remove
node: Node,
branches, const branches = [
dynamic, {
{ name, anchor, hasElse } condition: node.metadata.snippet,
) { block: node.block.name,
const select_block_type = block.getUniqueName(`select_block_type`); hasUpdateMethod: node.block.hasUpdateMethod,
const current_block_type_index = block.getUniqueName(`current_block_type_index`); hasIntroMethod: node.block.hasIntroMethod,
const previous_block_index = block.getUniqueName(`previous_block_index`); hasOutroMethod: node.block.hasOutroMethod,
const if_block_creators = block.getUniqueName(`if_block_creators`); },
const if_blocks = block.getUniqueName(`if_blocks`);
const if_current_block_type_index = hasElse
? ''
: `if (~${current_block_type_index}) `;
block.addVariable(current_block_type_index);
block.addVariable(name);
block.builders.init.addBlock(deindent`
var ${if_block_creators} = [
${branches.map(branch => branch.block).join(',\n')}
]; ];
var ${if_blocks} = []; this.visitChildren(block, node);
function ${select_block_type}(state) { if (isElseIf(node.else)) {
${branches branches.push(
.map(({ condition, block }, i) => `${condition ? `if (${condition}) ` : ''}return ${block ? i : -1};`) ...this.getBranches(block, parentNode, parentNodes, node.else.children[0])
.join('\n')} );
} } else {
`); branches.push({
condition: null,
block: node.else ? node.else.block.name : null,
hasUpdateMethod: node.else ? node.else.block.hasUpdateMethod : false,
hasIntroMethod: node.else ? node.else.block.hasIntroMethod : false,
hasOutroMethod: node.else ? node.else.block.hasOutroMethod : false,
});
if (hasElse) { if (node.else) {
block.builders.init.addBlock(deindent` this.visitChildren(block, node.else);
${current_block_type_index} = ${select_block_type}(state);
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](#component, state);
`);
} else {
block.builders.init.addBlock(deindent`
if (~(${current_block_type_index} = ${select_block_type}(state))) {
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](#component, state);
} }
`);
}
const mountOrIntro = branches[0].hasIntroMethod ? 'i' : 'm';
const initialMountNode = parentNode || '#target';
const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.mount.addLine(
`${if_current_block_type_index}${if_blocks}[${current_block_type_index}].${mountOrIntro}(${initialMountNode}, ${anchorNode});`
);
const updateMountNode = node.getUpdateMountNode(anchor);
const destroyOldBlock = deindent`
${name}.o(function() {
${if_blocks}[ ${previous_block_index} ].u();
${if_blocks}[ ${previous_block_index} ].d();
${if_blocks}[ ${previous_block_index} ] = null;
});
`;
const createNewBlock = deindent`
${name} = ${if_blocks}[${current_block_type_index}];
if (!${name}) {
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](#component, state);
${name}.c();
} }
${name}.${mountOrIntro}(${updateMountNode}, ${anchor});
`;
const changeBlock = hasElse return branches;
? deindent`
${destroyOldBlock}
${createNewBlock}
`
: deindent`
if (${name}) {
${destroyOldBlock}
}
if (~${current_block_type_index}) {
${createNewBlock}
} else {
${name} = null;
}
`;
if (dynamic) {
block.builders.update.addBlock(deindent`
var ${previous_block_index} = ${current_block_type_index};
${current_block_type_index} = ${select_block_type}(state);
if (${current_block_type_index} === ${previous_block_index}) {
${if_current_block_type_index}${if_blocks}[${current_block_type_index}].p(changed, state);
} else {
${changeBlock}
}
`);
} else {
block.builders.update.addBlock(deindent`
var ${previous_block_index} = ${current_block_type_index};
${current_block_type_index} = ${select_block_type}(state);
if (${current_block_type_index} !== ${previous_block_index}) {
${changeBlock}
}
`);
} }
block.builders.destroy.addLine(deindent` visitChildren(block: Block, node: Node) {
${if_current_block_type_index}{ node.children.forEach((child: Node) => {
${if_blocks}[${current_block_type_index}].u(); child.build(node.block, null, 'nodes');
${if_blocks}[${current_block_type_index}].d(); });
} }
`);
} }

@ -26,4 +26,8 @@ export default class MustacheTag extends Tag {
parentNode parentNode
); );
} }
remount(name: string) {
return `@appendNode(${this.var}, ${name}._slotted${this.generator.legacy ? `["default"]` : `.default`});`;
}
} }

@ -89,4 +89,8 @@ export default class RawMustacheTag extends Tag {
addAnchorAfter(); addAnchorAfter();
} }
} }
remount(name: string) {
return `@appendNode(${this.var}, ${name}._slotted${this.generator.legacy ? `["default"]` : `.default`});`;
}
} }

@ -9,7 +9,7 @@ import Block from '../dom/Block';
export default class Slot extends Element { export default class Slot extends Element {
type: 'Element'; type: 'Element';
name: string; name: string;
attributes: Attribute[]; // TODO have more specific Attribute type attributes: Attribute[];
children: Node[]; children: Node[];
init( init(
@ -54,6 +54,9 @@ export default class Slot extends Element {
if (needsAnchorBefore) block.addVariable(anchorBefore); if (needsAnchorBefore) block.addVariable(anchorBefore);
if (needsAnchorAfter) block.addVariable(anchorAfter); if (needsAnchorAfter) block.addVariable(anchorAfter);
let mountBefore = block.builders.mount.toString();
let unmountBefore = block.builders.unmount.toString();
block.builders.create.pushCondition(`!${content_name}`); block.builders.create.pushCondition(`!${content_name}`);
block.builders.hydrate.pushCondition(`!${content_name}`); block.builders.hydrate.pushCondition(`!${content_name}`);
block.builders.mount.pushCondition(`!${content_name}`); block.builders.mount.pushCondition(`!${content_name}`);
@ -72,10 +75,13 @@ export default class Slot extends Element {
block.builders.unmount.popCondition(); block.builders.unmount.popCondition();
block.builders.destroy.popCondition(); block.builders.destroy.popCondition();
// TODO can we use an else here? const mountLeadin = block.builders.mount.toString() !== mountBefore
? `else`
: `if (${content_name})`;
if (parentNode) { if (parentNode) {
block.builders.mount.addBlock(deindent` block.builders.mount.addBlock(deindent`
if (${content_name}) { ${mountLeadin} {
${needsAnchorBefore && `@appendNode(${anchorBefore} || (${anchorBefore} = @createComment()), ${parentNode});`} ${needsAnchorBefore && `@appendNode(${anchorBefore} || (${anchorBefore} = @createComment()), ${parentNode});`}
@appendNode(${content_name}, ${parentNode}); @appendNode(${content_name}, ${parentNode});
${needsAnchorAfter && `@appendNode(${anchorAfter} || (${anchorAfter} = @createComment()), ${parentNode});`} ${needsAnchorAfter && `@appendNode(${anchorAfter} || (${anchorAfter} = @createComment()), ${parentNode});`}
@ -83,7 +89,7 @@ export default class Slot extends Element {
`); `);
} else { } else {
block.builders.mount.addBlock(deindent` block.builders.mount.addBlock(deindent`
if (${content_name}) { ${mountLeadin} {
${needsAnchorBefore && `@insertNode(${anchorBefore} || (${anchorBefore} = @createComment()), #target, anchor);`} ${needsAnchorBefore && `@insertNode(${anchorBefore} || (${anchorBefore} = @createComment()), #target, anchor);`}
@insertNode(${content_name}, #target, anchor); @insertNode(${content_name}, #target, anchor);
${needsAnchorAfter && `@insertNode(${anchorAfter} || (${anchorAfter} = @createComment()), #target, anchor);`} ${needsAnchorAfter && `@insertNode(${anchorAfter} || (${anchorAfter} = @createComment()), #target, anchor);`}
@ -95,28 +101,31 @@ export default class Slot extends Element {
// so that it can be reinserted later // so that it can be reinserted later
// TODO so that this can work with public API, component._slotted should // TODO so that this can work with public API, component._slotted should
// be all fragments, derived from options.slots. Not === options.slots // be all fragments, derived from options.slots. Not === options.slots
// TODO can we use an else here? const unmountLeadin = block.builders.unmount.toString() !== unmountBefore
? `else`
: `if (${content_name})`;
if (anchorBefore === 'null' && anchorAfter === 'null') { if (anchorBefore === 'null' && anchorAfter === 'null') {
block.builders.unmount.addBlock(deindent` block.builders.unmount.addBlock(deindent`
if (${content_name}) { ${unmountLeadin} {
@reinsertChildren(${parentNode}, ${content_name}); @reinsertChildren(${parentNode}, ${content_name});
} }
`); `);
} else if (anchorBefore === 'null') { } else if (anchorBefore === 'null') {
block.builders.unmount.addBlock(deindent` block.builders.unmount.addBlock(deindent`
if (${content_name}) { ${unmountLeadin} {
@reinsertBefore(${anchorAfter}, ${content_name}); @reinsertBefore(${anchorAfter}, ${content_name});
} }
`); `);
} else if (anchorAfter === 'null') { } else if (anchorAfter === 'null') {
block.builders.unmount.addBlock(deindent` block.builders.unmount.addBlock(deindent`
if (${content_name}) { ${unmountLeadin} {
@reinsertAfter(${anchorBefore}, ${content_name}); @reinsertAfter(${anchorBefore}, ${content_name});
} }
`); `);
} else { } else {
block.builders.unmount.addBlock(deindent` block.builders.unmount.addBlock(deindent`
if (${content_name}) { ${unmountLeadin} {
@reinsertBetween(${anchorBefore}, ${anchorAfter}, ${content_name}); @reinsertBetween(${anchorBefore}, ${anchorAfter}, ${content_name});
@detachNode(${anchorBefore}); @detachNode(${anchorBefore});
@detachNode(${anchorAfter}); @detachNode(${anchorAfter});

@ -58,4 +58,8 @@ export default class Text extends Node {
parentNode parentNode
); );
} }
remount(name: string) {
return `@appendNode(${this.var}, ${name}._slotted${this.generator.legacy ? `["default"]` : `.default`});`;
}
} }

@ -161,4 +161,8 @@ export default class Node {
getUpdateMountNode(anchor: string) { getUpdateMountNode(anchor: string) {
return this.parent.isDomNode() ? this.parent.var : `${anchor}.parentNode`; return this.parent.isDomNode() ? this.parent.var : `${anchor}.parentNode`;
} }
remount(name: string) {
return `${this.var}.m(${name}._slotted${this.generator.legacy ? `["default"]` : `.default`}, null);`;
}
} }

@ -109,10 +109,14 @@ export default function tag(parser: Parser) {
} }
} }
const type = metaTags.has(name)
? name.slice(1)
: 'Element'; // TODO in v2, capitalised name means 'Component'
const element: Node = { const element: Node = {
start, start,
end: null, // filled in later end: null, // filled in later
type: 'Element', type,
name, name,
attributes: [], attributes: [],
children: [], children: [],

@ -138,6 +138,10 @@ export function onDev(eventName, handler) {
return on.call(this, eventName, handler); return on.call(this, eventName, handler);
} }
export function run(fn) {
fn();
}
export function set(newState) { export function set(newState) {
this._set(assign({}, newState)); this._set(assign({}, newState));
if (this.root._lock) return; if (this.root._lock) return;

@ -1,34 +0,0 @@
import { Node } from '../interfaces';
export default function isReference(node: Node, parent: Node): boolean {
if (node.type === 'MemberExpression') {
return !node.computed && isReference(node.object, node);
}
if (node.type === 'Identifier') {
// the only time we could have an identifier node without a parent is
// if it's the entire body of a function without a block statement
// i.e. an arrow function expression like `a => a`
if (!parent) return true;
// TODO is this right?
if (
parent.type === 'MemberExpression' ||
parent.type === 'MethodDefinition'
) {
return parent.computed || node === parent.object;
}
// disregard the `bar` in `{ bar: foo }`, but keep it in `{ [bar]: foo }`
if (parent.type === 'Property')
return parent.computed || node === parent.value;
// disregard the `bar` in `class Foo { bar () {...} }`
if (parent.type === 'MethodDefinition') return false;
// disregard the `bar` in `export { foo as bar }`
if (parent.type === 'ExportSpecifier' && node !== parent.local) return;
return true;
}
}

@ -7,11 +7,6 @@ import flattenReference from '../../utils/flattenReference';
import { Validator } from '../index'; import { Validator } from '../index';
import { Node } from '../../interfaces'; import { Node } from '../../interfaces';
const meta = new Map([
[':Window', validateWindow],
[':Head', validateHead]
]);
function isEmptyBlock(node: Node) { function isEmptyBlock(node: Node) {
if (!/Block$/.test(node.type) || !node.children) return false; if (!/Block$/.test(node.type) || !node.children) return false;
if (node.children.length > 1) return false; if (node.children.length > 1) return false;
@ -26,11 +21,15 @@ export default function validateHtml(validator: Validator, html: Node) {
const elementStack: Node[] = []; const elementStack: Node[] = [];
function visit(node: Node) { function visit(node: Node) {
if (node.type === 'Element') { if (node.type === 'Window') {
if (meta.has(node.name)) { validateWindow(validator, node, refs, refCallees);
return meta.get(node.name)(validator, node, refs, refCallees); }
}
else if (node.type === 'Head') {
validateHead(validator, node, refs, refCallees);
}
else if (node.type === 'Element') {
const isComponent = const isComponent =
node.name === ':Self' || node.name === ':Self' ||
node.name === ':Component' || node.name === ':Component' ||
@ -49,7 +48,9 @@ export default function validateHtml(validator: Validator, html: Node) {
if (!isComponent) { if (!isComponent) {
a11y(validator, node, elementStack); a11y(validator, node, elementStack);
} }
} else if (node.type === 'EachBlock') { }
else if (node.type === 'EachBlock') {
if (validator.helpers.has(node.context)) { if (validator.helpers.has(node.context)) {
let c = node.expression.end; let c = node.expression.end;

@ -1,5 +1,5 @@
import { walk } from 'estree-walker'; import { walk } from 'estree-walker';
import isReference from '../../../utils/isReference'; import isReference from 'is-reference';
import { Node } from '../../../interfaces'; import { Node } from '../../../interfaces';
export default function usesThisOrArguments(node: Node) { export default function usesThisOrArguments(node: Node) {

@ -194,6 +194,7 @@ function create_main_fragment(component, state) {
if (state.foo) return create_if_block; if (state.foo) return create_if_block;
return create_if_block_1; return create_if_block_1;
} }
var current_block_type = select_block_type(state); var current_block_type = select_block_type(state);
var if_block = current_block_type(component, state); var if_block = current_block_type(component, state);

@ -8,6 +8,7 @@ function create_main_fragment(component, state) {
if (state.foo) return create_if_block; if (state.foo) return create_if_block;
return create_if_block_1; return create_if_block_1;
} }
var current_block_type = select_block_type(state); var current_block_type = select_block_type(state);
var if_block = current_block_type(component, state); var if_block = current_block_type(component, state);

Loading…
Cancel
Save