more tidying up

pull/2252/head
Richard Harris 7 years ago
parent 4d1c0c5800
commit 0bc78006e5

@ -1,7 +1,7 @@
import Node from './shared/Node'; import Node from './shared/Node';
import Expression from './shared/Expression'; import Expression from './shared/Expression';
import Component from '../Component'; import Component from '../Component';
import deindent from '../../utils/deindent'; import deindent from '../utils/deindent';
import Block from '../render-dom/Block'; import Block from '../render-dom/Block';
export default class EventHandler extends Node { export default class EventHandler extends Node {

@ -5,7 +5,7 @@ import flattenReference from '../../../utils/flattenReference';
import { create_scopes, Scope, extract_names } from '../../utils/scope'; import { create_scopes, Scope, extract_names } from '../../utils/scope';
import { Node } from '../../../interfaces'; import { Node } from '../../../interfaces';
import globalWhitelist from '../../../utils/globalWhitelist'; import globalWhitelist from '../../../utils/globalWhitelist';
import deindent from '../../../utils/deindent'; import deindent from '../../utils/deindent';
import Wrapper from '../../render-dom/wrappers/shared/Wrapper'; import Wrapper from '../../render-dom/wrappers/shared/Wrapper';
import sanitize from '../../../utils/sanitize'; import sanitize from '../../../utils/sanitize';
import TemplateScope from './TemplateScope'; import TemplateScope from './TemplateScope';
@ -467,7 +467,7 @@ export default class Expression {
if (declarations.length > 0) { if (declarations.length > 0) {
block.maintainContext = true; block.maintainContext = true;
declarations.forEach(declaration => { declarations.forEach(declaration => {
block.builders.init.addBlock(declaration); block.builders.init.add_block(declaration);
}); });
} }

@ -1,5 +1,5 @@
import CodeBuilder from '../../utils/CodeBuilder'; import CodeBuilder from '../utils/CodeBuilder';
import deindent from '../../utils/deindent'; import deindent from '../utils/deindent';
import Renderer from './Renderer'; import Renderer from './Renderer';
import Wrapper from './wrappers/shared/Wrapper'; import Wrapper from './wrappers/shared/Wrapper';
import EachBlockWrapper from './wrappers/EachBlock'; import EachBlockWrapper from './wrappers/EachBlock';
@ -159,18 +159,18 @@ export default class Block {
noDetach?: boolean noDetach?: boolean
) { ) {
this.addVariable(name); this.addVariable(name);
this.builders.create.addLine(`${name} = ${renderStatement};`); this.builders.create.add_line(`${name} = ${renderStatement};`);
if (this.renderer.options.hydratable) { if (this.renderer.options.hydratable) {
this.builders.claim.addLine(`${name} = ${claimStatement || renderStatement};`); this.builders.claim.add_line(`${name} = ${claimStatement || renderStatement};`);
} }
if (parentNode) { if (parentNode) {
this.builders.mount.addLine(`@append(${parentNode}, ${name});`); this.builders.mount.add_line(`@append(${parentNode}, ${name});`);
if (parentNode === 'document.head') this.builders.destroy.addLine(`@detachNode(${name});`); if (parentNode === 'document.head') this.builders.destroy.add_line(`@detachNode(${name});`);
} else { } else {
this.builders.mount.addLine(`@insert(#target, ${name}, anchor);`); this.builders.mount.add_line(`@insert(#target, ${name}, anchor);`);
if (!noDetach) this.builders.destroy.addConditional('detach', `@detachNode(${name});`); if (!noDetach) this.builders.destroy.add_conditional('detach', `@detachNode(${name});`);
} }
} }
@ -221,18 +221,18 @@ export default class Block {
if (this.hasOutros) { if (this.hasOutros) {
this.addVariable('#current'); this.addVariable('#current');
if (!this.builders.intro.isEmpty()) { if (!this.builders.intro.is_empty()) {
this.builders.intro.addLine(`#current = true;`); this.builders.intro.add_line(`#current = true;`);
this.builders.mount.addLine(`#current = true;`); this.builders.mount.add_line(`#current = true;`);
} }
if (!this.builders.outro.isEmpty()) { if (!this.builders.outro.is_empty()) {
this.builders.outro.addLine(`#current = false;`); this.builders.outro.add_line(`#current = false;`);
} }
} }
if (this.autofocus) { if (this.autofocus) {
this.builders.mount.addLine(`${this.autofocus}.focus();`); this.builders.mount.add_line(`${this.autofocus}.focus();`);
} }
this.renderListeners(); this.renderListeners();
@ -242,24 +242,24 @@ export default class Block {
const methodName = (short: string, long: string) => dev ? `${short}: function ${this.getUniqueName(long)}` : short; const methodName = (short: string, long: string) => dev ? `${short}: function ${this.getUniqueName(long)}` : short;
if (localKey) { if (localKey) {
properties.addBlock(`key: ${localKey},`); properties.add_block(`key: ${localKey},`);
} }
if (this.first) { if (this.first) {
properties.addBlock(`first: null,`); properties.add_block(`first: null,`);
this.builders.hydrate.addLine(`this.first = ${this.first};`); this.builders.hydrate.add_line(`this.first = ${this.first};`);
} }
if (this.builders.create.isEmpty() && this.builders.hydrate.isEmpty()) { if (this.builders.create.is_empty() && this.builders.hydrate.is_empty()) {
properties.addLine(`c: @noop,`); properties.add_line(`c: @noop,`);
} else { } else {
const hydrate = !this.builders.hydrate.isEmpty() && ( const hydrate = !this.builders.hydrate.is_empty() && (
this.renderer.options.hydratable this.renderer.options.hydratable
? `this.h()` ? `this.h()`
: this.builders.hydrate : this.builders.hydrate
); );
properties.addBlock(deindent` properties.add_block(deindent`
${methodName('c', 'create')}() { ${methodName('c', 'create')}() {
${this.builders.create} ${this.builders.create}
${hydrate} ${hydrate}
@ -267,31 +267,31 @@ export default class Block {
`); `);
} }
if (this.renderer.options.hydratable || !this.builders.claim.isEmpty()) { if (this.renderer.options.hydratable || !this.builders.claim.is_empty()) {
if (this.builders.claim.isEmpty() && this.builders.hydrate.isEmpty()) { if (this.builders.claim.is_empty() && this.builders.hydrate.is_empty()) {
properties.addLine(`l: @noop,`); properties.add_line(`l: @noop,`);
} else { } else {
properties.addBlock(deindent` properties.add_block(deindent`
${methodName('l', 'claim')}(nodes) { ${methodName('l', 'claim')}(nodes) {
${this.builders.claim} ${this.builders.claim}
${this.renderer.options.hydratable && !this.builders.hydrate.isEmpty() && `this.h();`} ${this.renderer.options.hydratable && !this.builders.hydrate.is_empty() && `this.h();`}
}, },
`); `);
} }
} }
if (this.renderer.options.hydratable && !this.builders.hydrate.isEmpty()) { if (this.renderer.options.hydratable && !this.builders.hydrate.is_empty()) {
properties.addBlock(deindent` properties.add_block(deindent`
${methodName('h', 'hydrate')}() { ${methodName('h', 'hydrate')}() {
${this.builders.hydrate} ${this.builders.hydrate}
}, },
`); `);
} }
if (this.builders.mount.isEmpty()) { if (this.builders.mount.is_empty()) {
properties.addLine(`m: @noop,`); properties.add_line(`m: @noop,`);
} else { } else {
properties.addBlock(deindent` properties.add_block(deindent`
${methodName('m', 'mount')}(#target, anchor) { ${methodName('m', 'mount')}(#target, anchor) {
${this.builders.mount} ${this.builders.mount}
}, },
@ -299,10 +299,10 @@ export default class Block {
} }
if (this.hasUpdateMethod || this.maintainContext) { if (this.hasUpdateMethod || this.maintainContext) {
if (this.builders.update.isEmpty() && !this.maintainContext) { if (this.builders.update.is_empty() && !this.maintainContext) {
properties.addLine(`p: @noop,`); properties.add_line(`p: @noop,`);
} else { } else {
properties.addBlock(deindent` properties.add_block(deindent`
${methodName('p', 'update')}(changed, ${this.maintainContext ? 'new_ctx' : 'ctx'}) { ${methodName('p', 'update')}(changed, ${this.maintainContext ? 'new_ctx' : 'ctx'}) {
${this.maintainContext && `ctx = new_ctx;`} ${this.maintainContext && `ctx = new_ctx;`}
${this.builders.update} ${this.builders.update}
@ -312,7 +312,7 @@ export default class Block {
} }
if (this.hasAnimation) { if (this.hasAnimation) {
properties.addBlock(deindent` properties.add_block(deindent`
${methodName('r', 'measure')}() { ${methodName('r', 'measure')}() {
${this.builders.measure} ${this.builders.measure}
}, },
@ -328,10 +328,10 @@ export default class Block {
} }
if (this.hasIntroMethod || this.hasOutroMethod) { if (this.hasIntroMethod || this.hasOutroMethod) {
if (this.builders.intro.isEmpty()) { if (this.builders.intro.is_empty()) {
properties.addLine(`i: @noop,`); properties.add_line(`i: @noop,`);
} else { } else {
properties.addBlock(deindent` properties.add_block(deindent`
${methodName('i', 'intro')}(#local) { ${methodName('i', 'intro')}(#local) {
${this.hasOutros && `if (#current) return;`} ${this.hasOutros && `if (#current) return;`}
${this.builders.intro} ${this.builders.intro}
@ -339,10 +339,10 @@ export default class Block {
`); `);
} }
if (this.builders.outro.isEmpty()) { if (this.builders.outro.is_empty()) {
properties.addLine(`o: @noop,`); properties.add_line(`o: @noop,`);
} else { } else {
properties.addBlock(deindent` properties.add_block(deindent`
${methodName('o', 'outro')}(#local) { ${methodName('o', 'outro')}(#local) {
${this.builders.outro} ${this.builders.outro}
}, },
@ -350,10 +350,10 @@ export default class Block {
} }
} }
if (this.builders.destroy.isEmpty()) { if (this.builders.destroy.is_empty()) {
properties.addLine(`d: @noop`); properties.add_line(`d: @noop`);
} else { } else {
properties.addBlock(deindent` properties.add_block(deindent`
${methodName('d', 'destroy')}(detach) { ${methodName('d', 'destroy')}(detach) {
${this.builders.destroy} ${this.builders.destroy}
} }
@ -369,7 +369,7 @@ export default class Block {
}) })
.join(', ')};`} .join(', ')};`}
${!this.builders.init.isEmpty() && this.builders.init} ${!this.builders.init.is_empty() && this.builders.init}
return { return {
${properties} ${properties}
@ -384,21 +384,21 @@ export default class Block {
this.addVariable(`#dispose${chunk}`); this.addVariable(`#dispose${chunk}`);
if (this.event_listeners.length === 1) { if (this.event_listeners.length === 1) {
this.builders.hydrate.addLine( this.builders.hydrate.add_line(
`#dispose${chunk} = ${this.event_listeners[0]};` `#dispose${chunk} = ${this.event_listeners[0]};`
); );
this.builders.destroy.addLine( this.builders.destroy.add_line(
`#dispose${chunk}();` `#dispose${chunk}();`
) )
} else { } else {
this.builders.hydrate.addBlock(deindent` this.builders.hydrate.add_block(deindent`
#dispose${chunk} = [ #dispose${chunk} = [
${this.event_listeners.join(',\n')} ${this.event_listeners.join(',\n')}
]; ];
`); `);
this.builders.destroy.addLine( this.builders.destroy.add_line(
`@run_all(#dispose${chunk});` `@run_all(#dispose${chunk});`
); );
} }

@ -2,7 +2,7 @@ import Block from './Block';
import { CompileOptions } from '../../interfaces'; import { CompileOptions } from '../../interfaces';
import Component from '../Component'; import Component from '../Component';
import FragmentWrapper from './wrappers/Fragment'; import FragmentWrapper from './wrappers/Fragment';
import CodeBuilder from '../../utils/CodeBuilder'; import CodeBuilder from '../utils/CodeBuilder';
export default class Renderer { export default class Renderer {
component: Component; // TODO Maybe Renderer shouldn't know about Component? component: Component; // TODO Maybe Renderer shouldn't know about Component?

@ -1,6 +1,6 @@
import deindent from '../../utils/deindent'; import deindent from '../utils/deindent';
import { stringify, escape } from '../../utils/stringify'; import { stringify, escape } from '../../utils/stringify';
import CodeBuilder from '../../utils/CodeBuilder'; import CodeBuilder from '../utils/CodeBuilder';
import Component from '../Component'; import Component from '../Component';
import Renderer from './Renderer'; import Renderer from './Renderer';
import { CompileOptions } from '../../interfaces'; import { CompileOptions } from '../../interfaces';
@ -24,12 +24,12 @@ export default function dom(
block.hasOutroMethod = true; block.hasOutroMethod = true;
// prevent fragment being created twice (#1063) // prevent fragment being created twice (#1063)
if (options.customElement) block.builders.create.addLine(`this.c = @noop;`); if (options.customElement) block.builders.create.add_line(`this.c = @noop;`);
const builder = new CodeBuilder(); const builder = new CodeBuilder();
if (component.compileOptions.dev) { if (component.compileOptions.dev) {
builder.addLine(`const ${renderer.fileVar} = ${JSON.stringify(component.file)};`); builder.add_line(`const ${renderer.fileVar} = ${JSON.stringify(component.file)};`);
} }
const css = component.stylesheet.render(options.filename, !options.customElement); const css = component.stylesheet.render(options.filename, !options.customElement);
@ -38,7 +38,7 @@ export default function dom(
css.code, { onlyEscapeAtSymbol: true }); css.code, { onlyEscapeAtSymbol: true });
if (styles && component.compileOptions.css !== false && !options.customElement) { if (styles && component.compileOptions.css !== false && !options.customElement) {
builder.addBlock(deindent` builder.add_block(deindent`
function @add_css() { function @add_css() {
var style = @createElement("style"); var style = @createElement("style");
style.id = '${component.stylesheet.id}-style'; style.id = '${component.stylesheet.id}-style';
@ -53,11 +53,11 @@ export default function dom(
const blocks = renderer.blocks.slice().reverse(); const blocks = renderer.blocks.slice().reverse();
blocks.forEach(block => { blocks.forEach(block => {
builder.addBlock(block.toString()); builder.add_block(block.toString());
}); });
if (options.dev && !options.hydratable) { if (options.dev && !options.hydratable) {
block.builders.claim.addLine( block.builders.claim.add_line(
'throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");' 'throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");'
); );
} }
@ -272,7 +272,7 @@ export default function dom(
args.push('$$props', '$$invalidate'); args.push('$$props', '$$invalidate');
} }
builder.addBlock(deindent` builder.add_block(deindent`
function create_fragment(ctx) { function create_fragment(ctx) {
${block.getContents()} ${block.getContents()}
} }
@ -379,7 +379,7 @@ export default function dom(
return $name; return $name;
}); });
builder.addBlock(deindent` builder.add_block(deindent`
function ${definition}(${args.join(', ')}) { function ${definition}(${args.join(', ')}) {
${reactive_store_declarations.length > 0 && `let ${reactive_store_declarations.join(', ')};`} ${reactive_store_declarations.length > 0 && `let ${reactive_store_declarations.join(', ')};`}
@ -412,7 +412,7 @@ export default function dom(
const prop_names = `[${props.map(v => JSON.stringify(v.export_name)).join(', ')}]`; const prop_names = `[${props.map(v => JSON.stringify(v.export_name)).join(', ')}]`;
if (options.customElement) { if (options.customElement) {
builder.addBlock(deindent` builder.add_block(deindent`
class ${name} extends @SvelteElement { class ${name} extends @SvelteElement {
constructor(options) { constructor(options) {
super(); super();
@ -448,7 +448,7 @@ export default function dom(
} else { } else {
const superclass = options.dev ? 'SvelteComponentDev' : 'SvelteComponent'; const superclass = options.dev ? 'SvelteComponentDev' : 'SvelteComponent';
builder.addBlock(deindent` builder.add_block(deindent`
class ${name} extends @${superclass} { class ${name} extends @${superclass} {
constructor(options) { constructor(options) {
super(${options.dev && `options`}); super(${options.dev && `options`});

@ -3,7 +3,7 @@ import Renderer from '../Renderer';
import Block from '../Block'; import Block from '../Block';
import AwaitBlock from '../../nodes/AwaitBlock'; import AwaitBlock from '../../nodes/AwaitBlock';
import createDebuggingComment from '../../../utils/createDebuggingComment'; import createDebuggingComment from '../../../utils/createDebuggingComment';
import deindent from '../../../utils/deindent'; import deindent from '../../utils/deindent';
import FragmentWrapper from './Fragment'; import FragmentWrapper from './Fragment';
import PendingBlock from '../../nodes/PendingBlock'; import PendingBlock from '../../nodes/PendingBlock';
import ThenBlock from '../../nodes/ThenBlock'; import ThenBlock from '../../nodes/ThenBlock';
@ -145,22 +145,22 @@ export default class AwaitBlockWrapper extends Wrapper {
this.pending.block.hasOutroMethod && `blocks: Array(3)` this.pending.block.hasOutroMethod && `blocks: Array(3)`
].filter(Boolean); ].filter(Boolean);
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
let ${info} = { let ${info} = {
${infoProps.join(',\n')} ${infoProps.join(',\n')}
}; };
`); `);
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
@handlePromise(${promise} = ${snippet}, ${info}); @handlePromise(${promise} = ${snippet}, ${info});
`); `);
block.builders.create.addBlock(deindent` block.builders.create.add_block(deindent`
${info}.block.c(); ${info}.block.c();
`); `);
if (parentNodes && this.renderer.options.hydratable) { if (parentNodes && this.renderer.options.hydratable) {
block.builders.claim.addBlock(deindent` block.builders.claim.add_block(deindent`
${info}.block.l(${parentNodes}); ${info}.block.l(${parentNodes});
`); `);
} }
@ -170,14 +170,14 @@ export default class AwaitBlockWrapper extends Wrapper {
const hasTransitions = this.pending.block.hasIntroMethod || this.pending.block.hasOutroMethod; const hasTransitions = this.pending.block.hasIntroMethod || this.pending.block.hasOutroMethod;
block.builders.mount.addBlock(deindent` block.builders.mount.add_block(deindent`
${info}.block.m(${initialMountNode}, ${info}.anchor = ${anchorNode}); ${info}.block.m(${initialMountNode}, ${info}.anchor = ${anchorNode});
${info}.mount = () => ${updateMountNode}; ${info}.mount = () => ${updateMountNode};
${info}.anchor = ${anchor}; ${info}.anchor = ${anchor};
`); `);
if (hasTransitions) { if (hasTransitions) {
block.builders.intro.addLine(`${info}.block.i();`); block.builders.intro.add_line(`${info}.block.i();`);
} }
const conditions = []; const conditions = [];
@ -194,12 +194,12 @@ export default class AwaitBlockWrapper extends Wrapper {
`@handlePromise(${promise}, ${info})` `@handlePromise(${promise}, ${info})`
); );
block.builders.update.addLine( block.builders.update.add_line(
`${info}.ctx = ctx;` `${info}.ctx = ctx;`
); );
if (this.pending.block.hasUpdateMethod) { if (this.pending.block.hasUpdateMethod) {
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
if (${conditions.join(' && ')}) { if (${conditions.join(' && ')}) {
// nothing // nothing
} else { } else {
@ -207,13 +207,13 @@ export default class AwaitBlockWrapper extends Wrapper {
} }
`); `);
} else { } else {
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
${conditions.join(' && ')} ${conditions.join(' && ')}
`); `);
} }
if (this.pending.block.hasOutroMethod) { if (this.pending.block.hasOutroMethod) {
block.builders.outro.addBlock(deindent` block.builders.outro.add_block(deindent`
for (let #i = 0; #i < 3; #i += 1) { for (let #i = 0; #i < 3; #i += 1) {
const block = ${info}.blocks[#i]; const block = ${info}.blocks[#i];
if (block) block.o(); if (block) block.o();
@ -221,7 +221,7 @@ export default class AwaitBlockWrapper extends Wrapper {
`); `);
} }
block.builders.destroy.addBlock(deindent` block.builders.destroy.add_block(deindent`
${info}.block.d(${parentNode ? '' : 'detach'}); ${info}.block.d(${parentNode ? '' : 'detach'});
${info} = null; ${info} = null;
`); `);

@ -1,6 +1,6 @@
import Block from '../Block'; import Block from '../Block';
import Wrapper from './shared/Wrapper'; import Wrapper from './shared/Wrapper';
import deindent from '../../../utils/deindent'; import deindent from '../../utils/deindent';
import Body from '../../nodes/Body'; import Body from '../../nodes/Body';
export default class BodyWrapper extends Wrapper { export default class BodyWrapper extends Wrapper {
@ -10,11 +10,11 @@ export default class BodyWrapper extends Wrapper {
this.node.handlers.forEach(handler => { this.node.handlers.forEach(handler => {
const snippet = handler.render(block); const snippet = handler.render(block);
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
document.body.addEventListener("${handler.name}", ${snippet}); document.body.addEventListener("${handler.name}", ${snippet});
`); `);
block.builders.destroy.addBlock(deindent` block.builders.destroy.add_block(deindent`
document.body.removeEventListener("${handler.name}", ${snippet}); document.body.removeEventListener("${handler.name}", ${snippet});
`); `);
}); });

@ -3,7 +3,7 @@ import Wrapper from './shared/Wrapper';
import Block from '../Block'; import Block from '../Block';
import DebugTag from '../../nodes/DebugTag'; import DebugTag from '../../nodes/DebugTag';
import add_to_set from '../../utils/add_to_set'; import add_to_set from '../../utils/add_to_set';
import deindent from '../../../utils/deindent'; import deindent from '../../utils/deindent';
export default class DebugTagWrapper extends Wrapper { export default class DebugTagWrapper extends Wrapper {
node: DebugTag; node: DebugTag;
@ -34,8 +34,8 @@ export default class DebugTagWrapper extends Wrapper {
}); });
const statement = `[✂${this.node.start + 1}-${this.node.start + 7}✂];`; const statement = `[✂${this.node.start + 1}-${this.node.start + 7}✂];`;
block.builders.create.addLine(statement); block.builders.create.add_line(statement);
block.builders.update.addLine(statement); block.builders.update.add_line(statement);
} else { } else {
const { code } = component; const { code } = component;
code.overwrite(this.node.start + 1, this.node.start + 7, 'log', { code.overwrite(this.node.start + 1, this.node.start + 7, 'log', {
@ -52,7 +52,7 @@ export default class DebugTagWrapper extends Wrapper {
const identifiers = this.node.expressions.map(e => e.node.name).join(', '); const identifiers = this.node.expressions.map(e => e.node.name).join(', ');
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
if (${condition}) { if (${condition}) {
const { ${identifiers} } = ctx; const { ${identifiers} } = ctx;
console.${log}({ ${identifiers} }); console.${log}({ ${identifiers} });
@ -60,7 +60,7 @@ export default class DebugTagWrapper extends Wrapper {
} }
`); `);
block.builders.create.addBlock(deindent` block.builders.create.add_block(deindent`
{ {
const { ${identifiers} } = ctx; const { ${identifiers} } = ctx;
console.${log}({ ${identifiers} }); console.${log}({ ${identifiers} });

@ -4,7 +4,7 @@ import Wrapper from './shared/Wrapper';
import createDebuggingComment from '../../../utils/createDebuggingComment'; import createDebuggingComment from '../../../utils/createDebuggingComment';
import EachBlock from '../../nodes/EachBlock'; import EachBlock from '../../nodes/EachBlock';
import FragmentWrapper from './Fragment'; import FragmentWrapper from './Fragment';
import deindent from '../../../utils/deindent'; import deindent from '../../utils/deindent';
import ElseBlock from '../../nodes/ElseBlock'; import ElseBlock from '../../nodes/ElseBlock';
class ElseBlockWrapper extends Wrapper { class ElseBlockWrapper extends Wrapper {
@ -182,7 +182,7 @@ export default class EachBlockWrapper extends Wrapper {
const snippet = this.node.expression.render(block); const snippet = this.node.expression.render(block);
block.builders.init.addLine(`var ${this.vars.each_block_value} = ${snippet};`); block.builders.init.add_line(`var ${this.vars.each_block_value} = ${snippet};`);
renderer.blocks.push(deindent` renderer.blocks.push(deindent`
function ${this.vars.get_each_context}(ctx, list, i) { function ${this.vars.get_each_context}(ctx, list, i) {
@ -199,7 +199,7 @@ export default class EachBlockWrapper extends Wrapper {
} }
if (this.block.hasIntroMethod || this.block.hasOutroMethod) { if (this.block.hasIntroMethod || this.block.hasOutroMethod) {
block.builders.intro.addBlock(deindent` block.builders.intro.add_block(deindent`
for (var #i = 0; #i < ${this.vars.data_length}; #i += 1) ${this.vars.iterations}[#i].i(); for (var #i = 0; #i < ${this.vars.data_length}; #i += 1) ${this.vars.iterations}[#i].i();
`); `);
} }
@ -216,17 +216,17 @@ export default class EachBlockWrapper extends Wrapper {
if (this.else) { if (this.else) {
const each_block_else = component.getUniqueName(`${this.var}_else`); const each_block_else = component.getUniqueName(`${this.var}_else`);
block.builders.init.addLine(`var ${each_block_else} = null;`); block.builders.init.add_line(`var ${each_block_else} = null;`);
// TODO neaten this up... will end up with an empty line in the block // TODO neaten this up... will end up with an empty line in the block
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
if (!${this.vars.data_length}) { if (!${this.vars.data_length}) {
${each_block_else} = ${this.else.block.name}(ctx); ${each_block_else} = ${this.else.block.name}(ctx);
${each_block_else}.c(); ${each_block_else}.c();
} }
`); `);
block.builders.mount.addBlock(deindent` block.builders.mount.add_block(deindent`
if (${each_block_else}) { if (${each_block_else}) {
${each_block_else}.m(${parentNode || '#target'}, null); ${each_block_else}.m(${parentNode || '#target'}, null);
} }
@ -235,7 +235,7 @@ export default class EachBlockWrapper extends Wrapper {
const initialMountNode = parentNode || `${this.vars.anchor}.parentNode`; const initialMountNode = parentNode || `${this.vars.anchor}.parentNode`;
if (this.else.block.hasUpdateMethod) { if (this.else.block.hasUpdateMethod) {
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
if (!${this.vars.data_length} && ${each_block_else}) { if (!${this.vars.data_length} && ${each_block_else}) {
${each_block_else}.p(changed, ctx); ${each_block_else}.p(changed, ctx);
} else if (!${this.vars.data_length}) { } else if (!${this.vars.data_length}) {
@ -248,7 +248,7 @@ export default class EachBlockWrapper extends Wrapper {
} }
`); `);
} else { } else {
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
if (${this.vars.data_length}) { if (${this.vars.data_length}) {
if (${each_block_else}) { if (${each_block_else}) {
${each_block_else}.d(1); ${each_block_else}.d(1);
@ -262,7 +262,7 @@ export default class EachBlockWrapper extends Wrapper {
`); `);
} }
block.builders.destroy.addBlock(deindent` block.builders.destroy.add_block(deindent`
if (${each_block_else}) ${each_block_else}.d(${parentNode ? '' : 'detach'}); if (${each_block_else}) ${each_block_else}.d(${parentNode ? '' : 'detach'});
`); `);
} }
@ -306,7 +306,7 @@ export default class EachBlockWrapper extends Wrapper {
); );
} }
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
const ${get_key} = ctx => ${this.node.key.render()}; const ${get_key} = ctx => ${this.node.key.render()};
for (var #i = 0; #i < ${this.vars.each_block_value}.${length}; #i += 1) { for (var #i = 0; #i < ${this.vars.each_block_value}.${length}; #i += 1) {
@ -320,17 +320,17 @@ export default class EachBlockWrapper extends Wrapper {
const updateMountNode = this.getUpdateMountNode(anchor); const updateMountNode = this.getUpdateMountNode(anchor);
const anchorNode = parentNode ? 'null' : 'anchor'; const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.create.addBlock(deindent` block.builders.create.add_block(deindent`
for (#i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].c(); for (#i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].c();
`); `);
if (parentNodes && this.renderer.options.hydratable) { if (parentNodes && this.renderer.options.hydratable) {
block.builders.claim.addBlock(deindent` block.builders.claim.add_block(deindent`
for (#i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].l(${parentNodes}); for (#i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].l(${parentNodes});
`); `);
} }
block.builders.mount.addBlock(deindent` block.builders.mount.add_block(deindent`
for (#i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].m(${initialMountNode}, ${anchorNode}); for (#i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].m(${initialMountNode}, ${anchorNode});
`); `);
@ -343,7 +343,7 @@ export default class EachBlockWrapper extends Wrapper {
? `@outroAndDestroyBlock` ? `@outroAndDestroyBlock`
: `@destroyBlock`; : `@destroyBlock`;
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
const ${this.vars.each_block_value} = ${snippet}; const ${this.vars.each_block_value} = ${snippet};
${this.block.hasOutros && `@group_outros();`} ${this.block.hasOutros && `@group_outros();`}
@ -354,12 +354,12 @@ export default class EachBlockWrapper extends Wrapper {
`); `);
if (this.block.hasOutros) { if (this.block.hasOutros) {
block.builders.outro.addBlock(deindent` block.builders.outro.add_block(deindent`
for (#i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].o(); for (#i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].o();
`); `);
} }
block.builders.destroy.addBlock(deindent` block.builders.destroy.add_block(deindent`
for (#i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].d(${parentNode ? '' : 'detach'}); for (#i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].d(${parentNode ? '' : 'detach'});
`); `);
} }
@ -380,7 +380,7 @@ export default class EachBlockWrapper extends Wrapper {
anchor anchor
} = this.vars; } = this.vars;
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
var ${iterations} = []; var ${iterations} = [];
for (var #i = 0; #i < ${data_length}; #i += 1) { for (var #i = 0; #i < ${data_length}; #i += 1) {
@ -392,21 +392,21 @@ export default class EachBlockWrapper extends Wrapper {
const updateMountNode = this.getUpdateMountNode(anchor); const updateMountNode = this.getUpdateMountNode(anchor);
const anchorNode = parentNode ? 'null' : 'anchor'; const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.create.addBlock(deindent` block.builders.create.add_block(deindent`
for (var #i = 0; #i < ${view_length}; #i += 1) { for (var #i = 0; #i < ${view_length}; #i += 1) {
${iterations}[#i].c(); ${iterations}[#i].c();
} }
`); `);
if (parentNodes && this.renderer.options.hydratable) { if (parentNodes && this.renderer.options.hydratable) {
block.builders.claim.addBlock(deindent` block.builders.claim.add_block(deindent`
for (var #i = 0; #i < ${view_length}; #i += 1) { for (var #i = 0; #i < ${view_length}; #i += 1) {
${iterations}[#i].l(${parentNodes}); ${iterations}[#i].l(${parentNodes});
} }
`); `);
} }
block.builders.mount.addBlock(deindent` block.builders.mount.add_block(deindent`
for (var #i = 0; #i < ${view_length}; #i += 1) { for (var #i = 0; #i < ${view_length}; #i += 1) {
${iterations}[#i].m(${initialMountNode}, ${anchorNode}); ${iterations}[#i].m(${initialMountNode}, ${anchorNode});
} }
@ -420,7 +420,7 @@ export default class EachBlockWrapper extends Wrapper {
const outroBlock = this.block.hasOutros && block.getUniqueName('outroBlock') const outroBlock = this.block.hasOutros && block.getUniqueName('outroBlock')
if (outroBlock) { if (outroBlock) {
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
function ${outroBlock}(i, detach, local) { function ${outroBlock}(i, detach, local) {
if (${iterations}[i]) { if (${iterations}[i]) {
if (detach) { if (detach) {
@ -493,7 +493,7 @@ export default class EachBlockWrapper extends Wrapper {
${remove_old_blocks} ${remove_old_blocks}
`; `;
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
if (${condition}) { if (${condition}) {
${update} ${update}
} }
@ -501,12 +501,12 @@ export default class EachBlockWrapper extends Wrapper {
} }
if (outroBlock) { if (outroBlock) {
block.builders.outro.addBlock(deindent` block.builders.outro.add_block(deindent`
${iterations} = ${iterations}.filter(Boolean); ${iterations} = ${iterations}.filter(Boolean);
for (let #i = 0; #i < ${view_length}; #i += 1) ${outroBlock}(#i, 0);` for (let #i = 0; #i < ${view_length}; #i += 1) ${outroBlock}(#i, 0);`
); );
} }
block.builders.destroy.addBlock(`@destroyEach(${iterations}, detach);`); block.builders.destroy.add_block(`@destroyEach(${iterations}, detach);`);
} }
} }

@ -3,7 +3,7 @@ import Block from '../../Block';
import fixAttributeCasing from '../../../../utils/fixAttributeCasing'; import fixAttributeCasing from '../../../../utils/fixAttributeCasing';
import ElementWrapper from './index'; import ElementWrapper from './index';
import { stringify } from '../../../../utils/stringify'; import { stringify } from '../../../../utils/stringify';
import deindent from '../../../../utils/deindent'; import deindent from '../../../utils/deindent';
export default class AttributeWrapper { export default class AttributeWrapper {
node: Attribute; node: Attribute;
@ -111,7 +111,7 @@ export default class AttributeWrapper {
const init = shouldCache ? `${last} = ${value}` : value; const init = shouldCache ? `${last} = ${value}` : value;
if (isLegacyInputType) { if (isLegacyInputType) {
block.builders.hydrate.addLine( block.builders.hydrate.add_line(
`@setInputType(${element.var}, ${init});` `@setInputType(${element.var}, ${init});`
); );
updater = `@setInputType(${element.var}, ${shouldCache ? last : value});`; updater = `@setInputType(${element.var}, ${shouldCache ? last : value});`;
@ -138,22 +138,22 @@ export default class AttributeWrapper {
} }
`; `;
block.builders.mount.addBlock(deindent` block.builders.mount.add_block(deindent`
${last} = ${value}; ${last} = ${value};
${updater} ${updater}
`); `);
} else if (propertyName) { } else if (propertyName) {
block.builders.hydrate.addLine( block.builders.hydrate.add_line(
`${element.var}.${propertyName} = ${init};` `${element.var}.${propertyName} = ${init};`
); );
updater = `${element.var}.${propertyName} = ${shouldCache ? last : value};`; updater = `${element.var}.${propertyName} = ${shouldCache ? last : value};`;
} else if (isDataSet) { } else if (isDataSet) {
block.builders.hydrate.addLine( block.builders.hydrate.add_line(
`${element.var}.dataset.${camelCaseName} = ${init};` `${element.var}.dataset.${camelCaseName} = ${init};`
); );
updater = `${element.var}.dataset.${camelCaseName} = ${shouldCache ? last : value};`; updater = `${element.var}.dataset.${camelCaseName} = ${shouldCache ? last : value};`;
} else { } else {
block.builders.hydrate.addLine( block.builders.hydrate.add_line(
`${method}(${element.var}, "${name}", ${init});` `${method}(${element.var}, "${name}", ${init});`
); );
updater = `${method}(${element.var}, "${name}", ${shouldCache ? last : value});`; updater = `${method}(${element.var}, "${name}", ${shouldCache ? last : value});`;
@ -173,7 +173,7 @@ export default class AttributeWrapper {
? (dependencies.length ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue) ? (dependencies.length ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue)
: changedCheck; : changedCheck;
block.builders.update.addConditional( block.builders.update.add_conditional(
condition, condition,
updater updater
); );
@ -191,7 +191,7 @@ export default class AttributeWrapper {
: `${method}(${element.var}, "${name}", ${value === true ? '""' : value});` : `${method}(${element.var}, "${name}", ${value === true ? '""' : value});`
); );
block.builders.hydrate.addLine(statement); block.builders.hydrate.add_line(statement);
// special case autofocus. has to be handled in a bit of a weird way // special case autofocus. has to be handled in a bit of a weird way
if (this.node.isTrue && name === 'autofocus') { if (this.node.isTrue && name === 'autofocus') {
@ -202,8 +202,8 @@ export default class AttributeWrapper {
if (isIndirectlyBoundValue) { if (isIndirectlyBoundValue) {
const updateValue = `${element.var}.value = ${element.var}.__value;`; const updateValue = `${element.var}.value = ${element.var}.__value;`;
block.builders.hydrate.addLine(updateValue); block.builders.hydrate.add_line(updateValue);
if (this.node.isDynamic) block.builders.update.addLine(updateValue); if (this.node.isDynamic) block.builders.update.add_line(updateValue);
} }
} }

@ -124,11 +124,11 @@ export default class BindingWrapper {
case 'group': case 'group':
const bindingGroup = getBindingGroup(parent.renderer, this.node.expression.node); const bindingGroup = getBindingGroup(parent.renderer, this.node.expression.node);
block.builders.hydrate.addLine( block.builders.hydrate.add_line(
`ctx.$$binding_groups[${bindingGroup}].push(${parent.var});` `ctx.$$binding_groups[${bindingGroup}].push(${parent.var});`
); );
block.builders.destroy.addLine( block.builders.destroy.add_line(
`ctx.$$binding_groups[${bindingGroup}].splice(ctx.$$binding_groups[${bindingGroup}].indexOf(${parent.var}), 1);` `ctx.$$binding_groups[${bindingGroup}].splice(ctx.$$binding_groups[${bindingGroup}].indexOf(${parent.var}), 1);`
); );
break; break;
@ -154,13 +154,13 @@ export default class BindingWrapper {
} }
if (updateDom) { if (updateDom) {
block.builders.update.addLine( block.builders.update.add_line(
updateConditions.length ? `if (${updateConditions.join(' && ')}) ${updateDom}` : updateDom updateConditions.length ? `if (${updateConditions.join(' && ')}) ${updateDom}` : updateDom
); );
} }
if (!/(currentTime|paused)/.test(this.node.name)) { if (!/(currentTime|paused)/.test(this.node.name)) {
block.builders.mount.addBlock(updateDom); block.builders.mount.add_block(updateDom);
} }
} }
} }

@ -49,7 +49,7 @@ export default class StyleAttributeWrapper extends AttributeWrapper {
dependencies.map(dependency => `changed.${dependency}`).join(' || ') dependencies.map(dependency => `changed.${dependency}`).join(' || ')
); );
block.builders.update.addConditional( block.builders.update.add_conditional(
condition, condition,
`@setStyle(${this.parent.var}, "${prop.key}", ${value});` `@setStyle(${this.parent.var}, "${prop.key}", ${value});`
); );
@ -58,7 +58,7 @@ export default class StyleAttributeWrapper extends AttributeWrapper {
value = stringify(prop.value[0].data); value = stringify(prop.value[0].data);
} }
block.builders.hydrate.addLine( block.builders.hydrate.add_line(
`@setStyle(${this.parent.var}, "${prop.key}", ${value});` `@setStyle(${this.parent.var}, "${prop.key}", ${value});`
); );
}); });

@ -9,7 +9,7 @@ import FragmentWrapper from '../Fragment';
import { stringify, escapeHTML, escape } from '../../../../utils/stringify'; import { stringify, escapeHTML, escape } from '../../../../utils/stringify';
import TextWrapper from '../Text'; import TextWrapper from '../Text';
import fixAttributeCasing from '../../../../utils/fixAttributeCasing'; import fixAttributeCasing from '../../../../utils/fixAttributeCasing';
import deindent from '../../../../utils/deindent'; import deindent from '../../../utils/deindent';
import { namespaces } from '../../../../utils/namespaces'; import { namespaces } from '../../../../utils/namespaces';
import AttributeWrapper from './Attribute'; import AttributeWrapper from './Attribute';
import StyleAttributeWrapper from './StyleAttribute'; import StyleAttributeWrapper from './StyleAttribute';
@ -230,43 +230,43 @@ export default class ElementWrapper extends Wrapper {
block.addVariable(node); block.addVariable(node);
const renderStatement = this.getRenderStatement(); const renderStatement = this.getRenderStatement();
block.builders.create.addLine( block.builders.create.add_line(
`${node} = ${renderStatement};` `${node} = ${renderStatement};`
); );
if (renderer.options.hydratable) { if (renderer.options.hydratable) {
if (parentNodes) { if (parentNodes) {
block.builders.claim.addBlock(deindent` block.builders.claim.add_block(deindent`
${node} = ${this.getClaimStatement(parentNodes)}; ${node} = ${this.getClaimStatement(parentNodes)};
var ${nodes} = @children(${this.node.name === 'template' ? `${node}.content` : node}); var ${nodes} = @children(${this.node.name === 'template' ? `${node}.content` : node});
`); `);
} else { } else {
block.builders.claim.addLine( block.builders.claim.add_line(
`${node} = ${renderStatement};` `${node} = ${renderStatement};`
); );
} }
} }
if (parentNode) { if (parentNode) {
block.builders.mount.addLine( block.builders.mount.add_line(
`@append(${parentNode}, ${node});` `@append(${parentNode}, ${node});`
); );
if (parentNode === 'document.head') { if (parentNode === 'document.head') {
block.builders.destroy.addLine(`@detachNode(${node});`); block.builders.destroy.add_line(`@detachNode(${node});`);
} }
} else { } else {
block.builders.mount.addLine(`@insert(#target, ${node}, anchor);`); block.builders.mount.add_line(`@insert(#target, ${node}, anchor);`);
// TODO we eventually need to consider what happens to elements // TODO we eventually need to consider what happens to elements
// that belong to the same outgroup as an outroing element... // that belong to the same outgroup as an outroing element...
block.builders.destroy.addConditional('detach', `@detachNode(${node});`); block.builders.destroy.add_conditional('detach', `@detachNode(${node});`);
} }
// insert static children with textContent or innerHTML // insert static children with textContent or innerHTML
if (!this.node.namespace && this.canUseInnerHTML && this.fragment.nodes.length > 0) { if (!this.node.namespace && this.canUseInnerHTML && this.fragment.nodes.length > 0) {
if (this.fragment.nodes.length === 1 && this.fragment.nodes[0].node.type === 'Text') { if (this.fragment.nodes.length === 1 && this.fragment.nodes[0].node.type === 'Text') {
block.builders.create.addLine( block.builders.create.add_line(
`${node}.textContent = ${stringify(this.fragment.nodes[0].data)};` `${node}.textContent = ${stringify(this.fragment.nodes[0].data)};`
); );
} else { } else {
@ -276,7 +276,7 @@ export default class ElementWrapper extends Wrapper {
.join('') .join('')
); );
block.builders.create.addLine( block.builders.create.add_line(
`${node}.innerHTML = \`${innerHTML}\`;` `${node}.innerHTML = \`${innerHTML}\`;`
); );
} }
@ -309,7 +309,7 @@ export default class ElementWrapper extends Wrapper {
this.addClasses(block); this.addClasses(block);
if (nodes && this.renderer.options.hydratable) { if (nodes && this.renderer.options.hydratable) {
block.builders.claim.addLine( block.builders.claim.add_line(
`${nodes}.forEach(@detachNode);` `${nodes}.forEach(@detachNode);`
); );
} }
@ -346,7 +346,7 @@ export default class ElementWrapper extends Wrapper {
if (renderer.options.dev) { if (renderer.options.dev) {
const loc = renderer.locate(this.node.start); const loc = renderer.locate(this.node.start);
block.builders.hydrate.addLine( block.builders.hydrate.add_line(
`@addLoc(${this.var}, ${renderer.fileVar}, ${loc.line}, ${loc.column}, ${this.node.start});` `@addLoc(${this.var}, ${renderer.fileVar}, ${loc.line}, ${loc.column}, ${this.node.start});`
); );
} }
@ -443,7 +443,7 @@ export default class ElementWrapper extends Wrapper {
// TODO dry this out — similar code for event handlers and component bindings // TODO dry this out — similar code for event handlers and component bindings
if (has_local_function) { if (has_local_function) {
// need to create a block-local function that calls an instance-level function // need to create a block-local function that calls an instance-level function
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
function ${handler}() { function ${handler}() {
${animation_frame && deindent` ${animation_frame && deindent`
cancelAnimationFrame(${animation_frame}); cancelAnimationFrame(${animation_frame});
@ -471,11 +471,11 @@ export default class ElementWrapper extends Wrapper {
const resize_listener = block.getUniqueName(`${this.var}_resize_listener`); const resize_listener = block.getUniqueName(`${this.var}_resize_listener`);
block.addVariable(resize_listener); block.addVariable(resize_listener);
block.builders.mount.addLine( block.builders.mount.add_line(
`${resize_listener} = @addResizeListener(${this.var}, ${callee}.bind(${this.var}));` `${resize_listener} = @addResizeListener(${this.var}, ${callee}.bind(${this.var}));`
); );
block.builders.destroy.addLine( block.builders.destroy.add_line(
`${resize_listener}.cancel();` `${resize_listener}.cancel();`
); );
} else { } else {
@ -491,20 +491,20 @@ export default class ElementWrapper extends Wrapper {
if (this.node.name === 'select' || group.bindings.find(binding => binding.node.name === 'indeterminate' || binding.isReadOnlyMediaAttribute())) { if (this.node.name === 'select' || group.bindings.find(binding => binding.node.name === 'indeterminate' || binding.isReadOnlyMediaAttribute())) {
const callback = has_local_function ? handler : `() => ${callee}.call(${this.var})`; const callback = has_local_function ? handler : `() => ${callee}.call(${this.var})`;
block.builders.hydrate.addLine( block.builders.hydrate.add_line(
`if (${someInitialStateIsUndefined}) @add_render_callback(${callback});` `if (${someInitialStateIsUndefined}) @add_render_callback(${callback});`
); );
} }
if (group.events[0] === 'resize') { if (group.events[0] === 'resize') {
block.builders.hydrate.addLine( block.builders.hydrate.add_line(
`@add_render_callback(() => ${callee}.call(${this.var}));` `@add_render_callback(() => ${callee}.call(${this.var}));`
); );
} }
}); });
if (lock) { if (lock) {
block.builders.update.addLine(`${lock} = false;`); block.builders.update.add_line(`${lock} = false;`);
} }
const this_binding = this.bindings.find(b => b.node.name === 'this'); const this_binding = this.bindings.find(b => b.node.name === 'this');
@ -532,9 +532,9 @@ export default class ElementWrapper extends Wrapper {
} }
`); `);
block.builders.mount.addLine(`@add_binding_callback(() => ctx.${name}(${[this.var, 'null'].concat(args).join(', ')}));`); block.builders.mount.add_line(`@add_binding_callback(() => ctx.${name}(${[this.var, 'null'].concat(args).join(', ')}));`);
block.builders.destroy.addLine(`ctx.${name}(${['null', this.var].concat(args).join(', ')});`); block.builders.destroy.add_line(`ctx.${name}(${['null', this.var].concat(args).join(', ')});`);
block.builders.update.addLine(deindent` block.builders.update.add_line(deindent`
if (changed.items) { if (changed.items) {
ctx.${name}(${['null', this.var].concat(args).join(', ')}); ctx.${name}(${['null', this.var].concat(args).join(', ')});
${args.map(a => `${a} = ctx.${a}`).join(', ')}; ${args.map(a => `${a} = ctx.${a}`).join(', ')};
@ -586,7 +586,7 @@ export default class ElementWrapper extends Wrapper {
} }
}); });
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
var ${levels} = [ var ${levels} = [
${initialProps.join(',\n')} ${initialProps.join(',\n')}
]; ];
@ -597,11 +597,11 @@ export default class ElementWrapper extends Wrapper {
} }
`); `);
block.builders.hydrate.addLine( block.builders.hydrate.add_line(
`@setAttributes(${this.var}, ${data});` `@setAttributes(${this.var}, ${data});`
); );
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
@setAttributes(${this.var}, @getSpreadUpdate(${levels}, [ @setAttributes(${this.var}, @getSpreadUpdate(${levels}, [
${updates.join(',\n')} ${updates.join(',\n')}
])); ]));
@ -644,23 +644,23 @@ export default class ElementWrapper extends Wrapper {
`; `;
if (intro.is_local) { if (intro.is_local) {
block.builders.intro.addBlock(deindent` block.builders.intro.add_block(deindent`
if (#local) { if (#local) {
${intro_block} ${intro_block}
} }
`); `);
block.builders.outro.addBlock(deindent` block.builders.outro.add_block(deindent`
if (#local) { if (#local) {
${outro_block} ${outro_block}
} }
`); `);
} else { } else {
block.builders.intro.addBlock(intro_block); block.builders.intro.add_block(intro_block);
block.builders.outro.addBlock(outro_block); block.builders.outro.add_block(outro_block);
} }
block.builders.destroy.addConditional('detach', `if (${name}) ${name}.end();`); block.builders.destroy.add_conditional('detach', `if (${name}) ${name}.end();`);
} }
else { else {
@ -686,7 +686,7 @@ export default class ElementWrapper extends Wrapper {
}); });
`; `;
block.builders.outro.addLine(`if (${introName}) ${introName}.invalidate();`); block.builders.outro.add_line(`if (${introName}) ${introName}.invalidate();`);
} else { } else {
intro_block = deindent` intro_block = deindent`
if (!${introName}) { if (!${introName}) {
@ -706,7 +706,7 @@ export default class ElementWrapper extends Wrapper {
`; `;
} }
block.builders.intro.addBlock(intro_block); block.builders.intro.add_block(intro_block);
} }
if (outro) { if (outro) {
@ -718,7 +718,7 @@ export default class ElementWrapper extends Wrapper {
const fn = component.qualify(outro.name); const fn = component.qualify(outro.name);
if (!intro) { if (!intro) {
block.builders.intro.addBlock(deindent` block.builders.intro.add_block(deindent`
if (${outroName}) ${outroName}.end(1); if (${outroName}) ${outroName}.end(1);
`); `);
} }
@ -737,9 +737,9 @@ export default class ElementWrapper extends Wrapper {
`; `;
} }
block.builders.outro.addBlock(outro_block); block.builders.outro.add_block(outro_block);
block.builders.destroy.addConditional('detach', `if (${outroName}) ${outroName}.end();`); block.builders.destroy.add_conditional('detach', `if (${outroName}) ${outroName}.end();`);
} }
} }
} }
@ -755,11 +755,11 @@ export default class ElementWrapper extends Wrapper {
block.addVariable(rect); block.addVariable(rect);
block.addVariable(stop_animation, '@noop'); block.addVariable(stop_animation, '@noop');
block.builders.measure.addBlock(deindent` block.builders.measure.add_block(deindent`
${rect} = ${this.var}.getBoundingClientRect(); ${rect} = ${this.var}.getBoundingClientRect();
`); `);
block.builders.fix.addBlock(deindent` block.builders.fix.add_block(deindent`
@fix_position(${this.var}); @fix_position(${this.var});
${stop_animation}(); ${stop_animation}();
`); `);
@ -768,7 +768,7 @@ export default class ElementWrapper extends Wrapper {
const name = component.qualify(this.node.animation.name); const name = component.qualify(this.node.animation.name);
block.builders.animate.addBlock(deindent` block.builders.animate.add_block(deindent`
${stop_animation}(); ${stop_animation}();
${stop_animation} = @create_animation(${this.var}, ${rect}, ${name}, ${params}); ${stop_animation} = @create_animation(${this.var}, ${rect}, ${name}, ${params});
`); `);
@ -791,14 +791,14 @@ export default class ElementWrapper extends Wrapper {
} }
const updater = `@toggleClass(${this.var}, "${name}", ${snippet});`; const updater = `@toggleClass(${this.var}, "${name}", ${snippet});`;
block.builders.hydrate.addLine(updater); block.builders.hydrate.add_line(updater);
if ((dependencies && dependencies.size > 0) || this.classDependencies.length) { if ((dependencies && dependencies.size > 0) || this.classDependencies.length) {
const allDeps = this.classDependencies.concat(...dependencies); const allDeps = this.classDependencies.concat(...dependencies);
const deps = allDeps.map(dependency => `changed${quotePropIfNecessary(dependency)}`).join(' || '); const deps = allDeps.map(dependency => `changed${quotePropIfNecessary(dependency)}`).join(' || ');
const condition = allDeps.length > 1 ? `(${deps})` : deps; const condition = allDeps.length > 1 ? `(${deps})` : deps;
block.builders.update.addConditional( block.builders.update.add_conditional(
condition, condition,
updater updater
); );

@ -6,7 +6,7 @@ import IfBlock from '../../nodes/IfBlock';
import createDebuggingComment from '../../../utils/createDebuggingComment'; import createDebuggingComment from '../../../utils/createDebuggingComment';
import ElseBlock from '../../nodes/ElseBlock'; import ElseBlock from '../../nodes/ElseBlock';
import FragmentWrapper from './Fragment'; import FragmentWrapper from './Fragment';
import deindent from '../../../utils/deindent'; import deindent from '../../utils/deindent';
function isElseIf(node: ElseBlock) { function isElseIf(node: ElseBlock) {
return ( return (
@ -160,7 +160,7 @@ export default class IfBlockWrapper extends Wrapper {
if (hasOutros) { if (hasOutros) {
this.renderCompoundWithOutros(block, parentNode, parentNodes, dynamic, vars); this.renderCompoundWithOutros(block, parentNode, parentNodes, dynamic, vars);
block.builders.outro.addLine(`if (${name}) ${name}.o();`); block.builders.outro.add_line(`if (${name}) ${name}.o();`);
} else { } else {
this.renderCompound(block, parentNode, parentNodes, dynamic, vars); this.renderCompound(block, parentNode, parentNodes, dynamic, vars);
} }
@ -168,20 +168,20 @@ export default class IfBlockWrapper extends Wrapper {
this.renderSimple(block, parentNode, parentNodes, dynamic, vars); this.renderSimple(block, parentNode, parentNodes, dynamic, vars);
if (hasOutros) { if (hasOutros) {
block.builders.outro.addLine(`if (${name}) ${name}.o();`); block.builders.outro.add_line(`if (${name}) ${name}.o();`);
} }
} }
block.builders.create.addLine(`${if_name}${name}.c();`); block.builders.create.add_line(`${if_name}${name}.c();`);
if (parentNodes && this.renderer.options.hydratable) { if (parentNodes && this.renderer.options.hydratable) {
block.builders.claim.addLine( block.builders.claim.add_line(
`${if_name}${name}.l(${parentNodes});` `${if_name}${name}.l(${parentNodes});`
); );
} }
if (hasIntros || hasOutros) { if (hasIntros || hasOutros) {
block.builders.intro.addLine(`if (${name}) ${name}.i();`); block.builders.intro.add_line(`if (${name}) ${name}.i();`);
} }
if (needsAnchor) { if (needsAnchor) {
@ -209,7 +209,7 @@ export default class IfBlockWrapper extends Wrapper {
const current_block_type = block.getUniqueName(`current_block_type`); const current_block_type = block.getUniqueName(`current_block_type`);
const current_block_type_and = hasElse ? '' : `${current_block_type} && `; const current_block_type_and = hasElse ? '' : `${current_block_type} && `;
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
function ${select_block_type}(ctx) { function ${select_block_type}(ctx) {
${this.branches ${this.branches
.map(({ condition, block }) => `${condition ? `if (${condition}) ` : ''}return ${block.name};`) .map(({ condition, block }) => `${condition ? `if (${condition}) ` : ''}return ${block.name};`)
@ -217,14 +217,14 @@ export default class IfBlockWrapper extends Wrapper {
} }
`); `);
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
var ${current_block_type} = ${select_block_type}(ctx); var ${current_block_type} = ${select_block_type}(ctx);
var ${name} = ${current_block_type_and}${current_block_type}(ctx); var ${name} = ${current_block_type_and}${current_block_type}(ctx);
`); `);
const initialMountNode = parentNode || '#target'; const initialMountNode = parentNode || '#target';
const anchorNode = parentNode ? 'null' : 'anchor'; const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.mount.addLine( block.builders.mount.add_line(
`${if_name}${name}.m(${initialMountNode}, ${anchorNode});` `${if_name}${name}.m(${initialMountNode}, ${anchorNode});`
); );
@ -241,7 +241,7 @@ export default class IfBlockWrapper extends Wrapper {
`; `;
if (dynamic) { if (dynamic) {
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
if (${current_block_type} === (${current_block_type} = ${select_block_type}(ctx)) && ${name}) { if (${current_block_type} === (${current_block_type} = ${select_block_type}(ctx)) && ${name}) {
${name}.p(changed, ctx); ${name}.p(changed, ctx);
} else { } else {
@ -249,14 +249,14 @@ export default class IfBlockWrapper extends Wrapper {
} }
`); `);
} else { } else {
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
if (${current_block_type} !== (${current_block_type} = ${select_block_type}(ctx))) { if (${current_block_type} !== (${current_block_type} = ${select_block_type}(ctx))) {
${changeBlock} ${changeBlock}
} }
`); `);
} }
block.builders.destroy.addLine(`${if_name}${name}.d(${parentNode ? '' : 'detach'});`); block.builders.destroy.add_line(`${if_name}${name}.d(${parentNode ? '' : 'detach'});`);
} }
// if any of the siblings have outros, we need to keep references to the blocks // if any of the siblings have outros, we need to keep references to the blocks
@ -281,7 +281,7 @@ export default class IfBlockWrapper extends Wrapper {
block.addVariable(current_block_type_index); block.addVariable(current_block_type_index);
block.addVariable(name); block.addVariable(name);
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
var ${if_block_creators} = [ var ${if_block_creators} = [
${this.branches.map(branch => branch.block.name).join(',\n')} ${this.branches.map(branch => branch.block.name).join(',\n')}
]; ];
@ -297,12 +297,12 @@ export default class IfBlockWrapper extends Wrapper {
`); `);
if (hasElse) { if (hasElse) {
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
${current_block_type_index} = ${select_block_type}(ctx); ${current_block_type_index} = ${select_block_type}(ctx);
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](ctx); ${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](ctx);
`); `);
} else { } else {
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
if (~(${current_block_type_index} = ${select_block_type}(ctx))) { if (~(${current_block_type_index} = ${select_block_type}(ctx))) {
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](ctx); ${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](ctx);
} }
@ -312,7 +312,7 @@ export default class IfBlockWrapper extends Wrapper {
const initialMountNode = parentNode || '#target'; const initialMountNode = parentNode || '#target';
const anchorNode = parentNode ? 'null' : 'anchor'; const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.mount.addLine( block.builders.mount.add_line(
`${if_current_block_type_index}${if_blocks}[${current_block_type_index}].m(${initialMountNode}, ${anchorNode});` `${if_current_block_type_index}${if_blocks}[${current_block_type_index}].m(${initialMountNode}, ${anchorNode});`
); );
@ -357,7 +357,7 @@ export default class IfBlockWrapper extends Wrapper {
`; `;
if (dynamic) { if (dynamic) {
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
var ${previous_block_index} = ${current_block_type_index}; var ${previous_block_index} = ${current_block_type_index};
${current_block_type_index} = ${select_block_type}(ctx); ${current_block_type_index} = ${select_block_type}(ctx);
if (${current_block_type_index} === ${previous_block_index}) { if (${current_block_type_index} === ${previous_block_index}) {
@ -367,7 +367,7 @@ export default class IfBlockWrapper extends Wrapper {
} }
`); `);
} else { } else {
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
var ${previous_block_index} = ${current_block_type_index}; var ${previous_block_index} = ${current_block_type_index};
${current_block_type_index} = ${select_block_type}(ctx); ${current_block_type_index} = ${select_block_type}(ctx);
if (${current_block_type_index} !== ${previous_block_index}) { if (${current_block_type_index} !== ${previous_block_index}) {
@ -376,7 +376,7 @@ export default class IfBlockWrapper extends Wrapper {
`); `);
} }
block.builders.destroy.addLine(deindent` block.builders.destroy.add_line(deindent`
${if_current_block_type_index}${if_blocks}[${current_block_type_index}].d(${parentNode ? '' : 'detach'}); ${if_current_block_type_index}${if_blocks}[${current_block_type_index}].d(${parentNode ? '' : 'detach'});
`); `);
} }
@ -390,14 +390,14 @@ export default class IfBlockWrapper extends Wrapper {
) { ) {
const branch = this.branches[0]; const branch = this.branches[0];
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
var ${name} = (${branch.condition}) && ${branch.block.name}(ctx); var ${name} = (${branch.condition}) && ${branch.block.name}(ctx);
`); `);
const initialMountNode = parentNode || '#target'; const initialMountNode = parentNode || '#target';
const anchorNode = parentNode ? 'null' : 'anchor'; const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.mount.addLine( block.builders.mount.add_line(
`if (${name}) ${name}.m(${initialMountNode}, ${anchorNode});` `if (${name}) ${name}.m(${initialMountNode}, ${anchorNode});`
); );
@ -444,7 +444,7 @@ export default class IfBlockWrapper extends Wrapper {
${name} = null; ${name} = null;
`; `;
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
if (${branch.condition}) { if (${branch.condition}) {
${enter} ${enter}
} else if (${name}) { } else if (${name}) {
@ -452,6 +452,6 @@ export default class IfBlockWrapper extends Wrapper {
} }
`); `);
block.builders.destroy.addLine(`${if_name}${name}.d(${parentNode ? '' : 'detach'});`); block.builders.destroy.add_line(`${if_name}${name}.d(${parentNode ? '' : 'detach'});`);
} }
} }

@ -6,7 +6,7 @@ import FragmentWrapper from '../Fragment';
import { quoteNameIfNecessary, quotePropIfNecessary } from '../../../../utils/quoteIfNecessary'; import { quoteNameIfNecessary, quotePropIfNecessary } from '../../../../utils/quoteIfNecessary';
import { stringify_props } from '../../../utils/stringify_props'; import { stringify_props } from '../../../utils/stringify_props';
import add_to_set from '../../../utils/add_to_set'; import add_to_set from '../../../utils/add_to_set';
import deindent from '../../../../utils/deindent'; import deindent from '../../../utils/deindent';
import Attribute from '../../../nodes/Attribute'; import Attribute from '../../../nodes/Attribute';
import getObject from '../../../../utils/getObject'; import getObject from '../../../../utils/getObject';
import flattenReference from '../../../../utils/flattenReference'; import flattenReference from '../../../../utils/flattenReference';
@ -207,7 +207,7 @@ export default class InlineComponentWrapper extends Wrapper {
} }
}); });
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
var ${levels} = [ var ${levels} = [
${initialProps.join(',\n')} ${initialProps.join(',\n')}
]; ];
@ -280,7 +280,7 @@ export default class InlineComponentWrapper extends Wrapper {
} }
`); `);
block.builders.destroy.addLine(`ctx.${fn}(null);`); block.builders.destroy.add_line(`ctx.${fn}(null);`);
return `@add_binding_callback(() => ctx.${fn}(${this.var}));`; return `@add_binding_callback(() => ctx.${fn}(${this.var}));`;
} }
@ -329,7 +329,7 @@ export default class InlineComponentWrapper extends Wrapper {
if (contextual_dependencies.length > 0) { if (contextual_dependencies.length > 0) {
args.push(`{ ${contextual_dependencies.join(', ')} }`); args.push(`{ ${contextual_dependencies.join(', ')} }`);
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
function ${name}(value) { function ${name}(value) {
if (ctx.${name}.call(null, value, ctx)) { if (ctx.${name}.call(null, value, ctx)) {
${updating} = true; ${updating} = true;
@ -339,7 +339,7 @@ export default class InlineComponentWrapper extends Wrapper {
block.maintainContext = true; // TODO put this somewhere more logical block.maintainContext = true; // TODO put this somewhere more logical
} else { } else {
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
function ${name}(value) { function ${name}(value) {
if (ctx.${name}.call(null, value)) { if (ctx.${name}.call(null, value)) {
${updating} = true; ${updating} = true;
@ -371,7 +371,7 @@ export default class InlineComponentWrapper extends Wrapper {
const snippet = this.node.expression.render(block); const snippet = this.node.expression.render(block);
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
var ${switch_value} = ${snippet}; var ${switch_value} = ${snippet};
function ${switch_props}(ctx) { function ${switch_props}(ctx) {
@ -389,17 +389,17 @@ export default class InlineComponentWrapper extends Wrapper {
} }
`); `);
block.builders.create.addLine( block.builders.create.add_line(
`if (${name}) ${name}.$$.fragment.c();` `if (${name}) ${name}.$$.fragment.c();`
); );
if (parentNodes && this.renderer.options.hydratable) { if (parentNodes && this.renderer.options.hydratable) {
block.builders.claim.addLine( block.builders.claim.add_line(
`if (${name}) ${name}.$$.fragment.l(${parentNodes});` `if (${name}) ${name}.$$.fragment.l(${parentNodes});`
); );
} }
block.builders.mount.addBlock(deindent` block.builders.mount.add_block(deindent`
if (${name}) { if (${name}) {
@mount_component(${name}, ${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'}); @mount_component(${name}, ${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});
} }
@ -409,12 +409,12 @@ export default class InlineComponentWrapper extends Wrapper {
const updateMountNode = this.getUpdateMountNode(anchor); const updateMountNode = this.getUpdateMountNode(anchor);
if (updates.length) { if (updates.length) {
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
${updates} ${updates}
`); `);
} }
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
if (${switch_value} !== (${switch_value} = ${snippet})) { if (${switch_value} !== (${switch_value} = ${snippet})) {
if (${name}) { if (${name}) {
@group_outros(); @group_outros();
@ -441,12 +441,12 @@ export default class InlineComponentWrapper extends Wrapper {
} }
`); `);
block.builders.intro.addBlock(deindent` block.builders.intro.add_block(deindent`
if (${name}) ${name}.$$.fragment.i(#local); if (${name}) ${name}.$$.fragment.i(#local);
`); `);
if (updates.length) { if (updates.length) {
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
else if (${switch_value}) { else if (${switch_value}) {
${name}.$set(${name_changes}); ${name}.$set(${name_changes});
} }
@ -455,17 +455,17 @@ export default class InlineComponentWrapper extends Wrapper {
`); `);
} }
block.builders.outro.addLine( block.builders.outro.add_line(
`if (${name}) ${name}.$$.fragment.o(#local);` `if (${name}) ${name}.$$.fragment.o(#local);`
); );
block.builders.destroy.addLine(`if (${name}) ${name}.$destroy(${parentNode ? '' : 'detach'});`); block.builders.destroy.add_line(`if (${name}) ${name}.$destroy(${parentNode ? '' : 'detach'});`);
} else { } else {
const expression = this.node.name === 'svelte:self' const expression = this.node.name === 'svelte:self'
? '__svelte:self__' // TODO conflict-proof this ? '__svelte:self__' // TODO conflict-proof this
: component.qualify(this.node.name); : component.qualify(this.node.name);
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
${(this.node.attributes.length || this.node.bindings.length) && deindent` ${(this.node.attributes.length || this.node.bindings.length) && deindent`
${props && `let ${props} = ${attributeObject};`}`} ${props && `let ${props} = ${attributeObject};`}`}
${statements} ${statements}
@ -475,35 +475,35 @@ export default class InlineComponentWrapper extends Wrapper {
${munged_handlers} ${munged_handlers}
`); `);
block.builders.create.addLine(`${name}.$$.fragment.c();`); block.builders.create.add_line(`${name}.$$.fragment.c();`);
if (parentNodes && this.renderer.options.hydratable) { if (parentNodes && this.renderer.options.hydratable) {
block.builders.claim.addLine( block.builders.claim.add_line(
`${name}.$$.fragment.l(${parentNodes});` `${name}.$$.fragment.l(${parentNodes});`
); );
} }
block.builders.mount.addLine( block.builders.mount.add_line(
`@mount_component(${name}, ${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});` `@mount_component(${name}, ${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});`
); );
block.builders.intro.addBlock(deindent` block.builders.intro.add_block(deindent`
${name}.$$.fragment.i(#local); ${name}.$$.fragment.i(#local);
`); `);
if (updates.length) { if (updates.length) {
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
${updates} ${updates}
${name}.$set(${name_changes}); ${name}.$set(${name_changes});
${postupdates.length > 0 && `${postupdates.join(' = ')} = false;`} ${postupdates.length > 0 && `${postupdates.join(' = ')} = false;`}
`); `);
} }
block.builders.destroy.addBlock(deindent` block.builders.destroy.add_block(deindent`
${name}.$destroy(${parentNode ? '' : 'detach'}); ${name}.$destroy(${parentNode ? '' : 'detach'});
`); `);
block.builders.outro.addLine( block.builders.outro.add_line(
`${name}.$$.fragment.o(#local);` `${name}.$$.fragment.o(#local);`
); );
} }

@ -3,7 +3,7 @@ import Block from '../Block';
import Node from '../../nodes/shared/Node'; import Node from '../../nodes/shared/Node';
import Tag from './shared/Tag'; import Tag from './shared/Tag';
import Wrapper from './shared/wrapper'; import Wrapper from './shared/wrapper';
import deindent from '../../../utils/deindent'; import deindent from '../../utils/deindent';
export default class RawMustacheTagWrapper extends Tag { export default class RawMustacheTagWrapper extends Tag {
var = 'raw'; var = 'raw';
@ -87,10 +87,10 @@ export default class RawMustacheTagWrapper extends Tag {
addAnchorAfter(); addAnchorAfter();
} }
block.builders.mount.addLine(insert(init)); block.builders.mount.add_line(insert(init));
if (!parentNode) { if (!parentNode) {
block.builders.destroy.addConditional('detach', needsAnchorBefore block.builders.destroy.add_conditional('detach', needsAnchorBefore
? `${detach}\n@detachNode(${anchorBefore});` ? `${detach}\n@detachNode(${anchorBefore});`
: detach); : detach);
} }

@ -3,7 +3,7 @@ import Renderer from '../Renderer';
import Block from '../Block'; import Block from '../Block';
import Slot from '../../nodes/Slot'; import Slot from '../../nodes/Slot';
import FragmentWrapper from './Fragment'; import FragmentWrapper from './Fragment';
import deindent from '../../../utils/deindent'; import deindent from '../../utils/deindent';
import sanitize from '../../../utils/sanitize'; import sanitize from '../../../utils/sanitize';
import add_to_set from '../../utils/add_to_set'; import add_to_set from '../../utils/add_to_set';
import get_slot_data from '../../../utils/get_slot_data'; import get_slot_data from '../../../utils/get_slot_data';
@ -94,18 +94,18 @@ export default class SlotWrapper extends Wrapper {
const slot = block.getUniqueName(`${sanitize(slot_name)}_slot`); const slot = block.getUniqueName(`${sanitize(slot_name)}_slot`);
const slot_definition = block.getUniqueName(`${sanitize(slot_name)}_slot`); const slot_definition = block.getUniqueName(`${sanitize(slot_name)}_slot`);
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
const ${slot_definition} = ctx.$$slot_${sanitize(slot_name)}; const ${slot_definition} = ctx.$$slot_${sanitize(slot_name)};
const ${slot} = @create_slot(${slot_definition}, ctx, ${get_slot_context}); const ${slot} = @create_slot(${slot_definition}, ctx, ${get_slot_context});
`); `);
let mountBefore = block.builders.mount.toString(); let mountBefore = block.builders.mount.toString();
block.builders.create.pushCondition(`!${slot}`); block.builders.create.push_condition(`!${slot}`);
block.builders.hydrate.pushCondition(`!${slot}`); block.builders.hydrate.push_condition(`!${slot}`);
block.builders.mount.pushCondition(`!${slot}`); block.builders.mount.push_condition(`!${slot}`);
block.builders.update.pushCondition(`!${slot}`); block.builders.update.push_condition(`!${slot}`);
block.builders.destroy.pushCondition(`!${slot}`); block.builders.destroy.push_condition(`!${slot}`);
const listeners = block.event_listeners; const listeners = block.event_listeners;
block.event_listeners = []; block.event_listeners = [];
@ -113,17 +113,17 @@ export default class SlotWrapper extends Wrapper {
block.renderListeners(`_${slot}`); block.renderListeners(`_${slot}`);
block.event_listeners = listeners; block.event_listeners = listeners;
block.builders.create.popCondition(); block.builders.create.pop_condition();
block.builders.hydrate.popCondition(); block.builders.hydrate.pop_condition();
block.builders.mount.popCondition(); block.builders.mount.pop_condition();
block.builders.update.popCondition(); block.builders.update.pop_condition();
block.builders.destroy.popCondition(); block.builders.destroy.pop_condition();
block.builders.create.addLine( block.builders.create.add_line(
`if (${slot}) ${slot}.c();` `if (${slot}) ${slot}.c();`
); );
block.builders.claim.addLine( block.builders.claim.add_line(
`if (${slot}) ${slot}.l(${parentNodes});` `if (${slot}) ${slot}.l(${parentNodes});`
); );
@ -131,7 +131,7 @@ export default class SlotWrapper extends Wrapper {
? `else` ? `else`
: `if (${slot})`; : `if (${slot})`;
block.builders.mount.addBlock(deindent` block.builders.mount.add_block(deindent`
${mountLeadin} { ${mountLeadin} {
${slot}.m(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'}); ${slot}.m(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});
} }
@ -140,13 +140,13 @@ export default class SlotWrapper extends Wrapper {
let update_conditions = [...this.dependencies].map(name => `changed.${name}`).join(' || '); let update_conditions = [...this.dependencies].map(name => `changed.${name}`).join(' || ');
if (this.dependencies.size > 1) update_conditions = `(${update_conditions})`; if (this.dependencies.size > 1) update_conditions = `(${update_conditions})`;
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
if (${slot} && ${slot}.p && ${update_conditions}) { if (${slot} && ${slot}.p && ${update_conditions}) {
${slot}.p(@get_slot_changes(${slot_definition}, ctx, changed, ${get_slot_changes}), @get_slot_context(${slot_definition}, ctx, ${get_slot_context})); ${slot}.p(@get_slot_changes(${slot_definition}, ctx, changed, ${get_slot_changes}), @get_slot_context(${slot_definition}, ctx, ${get_slot_context}));
} }
`); `);
block.builders.destroy.addLine( block.builders.destroy.add_line(
`if (${slot}) ${slot}.d(detach);` `if (${slot}) ${slot}.d(detach);`
); );
} }

@ -65,7 +65,7 @@ export default class TitleWrapper extends Wrapper {
let updater; let updater;
const init = this.node.shouldCache ? `${last} = ${value}` : value; const init = this.node.shouldCache ? `${last} = ${value}` : value;
block.builders.init.addLine( block.builders.init.add_line(
`document.title = ${init};` `document.title = ${init};`
); );
updater = `document.title = ${this.node.shouldCache ? last : value};`; updater = `document.title = ${this.node.shouldCache ? last : value};`;
@ -83,14 +83,14 @@ export default class TitleWrapper extends Wrapper {
( dependencies.length ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue ) : ( dependencies.length ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue ) :
changedCheck; changedCheck;
block.builders.update.addConditional( block.builders.update.add_conditional(
condition, condition,
updater updater
); );
} }
} else { } else {
const value = stringify(this.node.children[0].data); const value = stringify(this.node.children[0].data);
block.builders.hydrate.addLine(`document.title = ${value};`); block.builders.hydrate.add_line(`document.title = ${value};`);
} }
} }
} }

@ -2,7 +2,7 @@ import Renderer from '../Renderer';
import Block from '../Block'; import Block from '../Block';
import Node from '../../nodes/shared/Node'; import Node from '../../nodes/shared/Node';
import Wrapper from './shared/Wrapper'; import Wrapper from './shared/Wrapper';
import deindent from '../../../utils/deindent'; import deindent from '../../utils/deindent';
import addEventHandlers from './shared/addEventHandlers'; import addEventHandlers from './shared/addEventHandlers';
import Window from '../../nodes/Window'; import Window from '../../nodes/Window';
import addActions from './shared/addActions'; import addActions from './shared/addActions';
@ -90,7 +90,7 @@ export default class WindowWrapper extends Wrapper {
const x = bindings.scrollX && `this._state.${bindings.scrollX}`; const x = bindings.scrollX && `this._state.${bindings.scrollX}`;
const y = bindings.scrollY && `this._state.${bindings.scrollY}`; const y = bindings.scrollY && `this._state.${bindings.scrollY}`;
renderer.metaBindings.addBlock(deindent` renderer.metaBindings.add_block(deindent`
if (${condition}) { if (${condition}) {
window.scrollTo(${x || 'window.pageXOffset'}, ${y || 'window.pageYOffset'}); window.scrollTo(${x || 'window.pageXOffset'}, ${y || 'window.pageYOffset'});
} }
@ -108,7 +108,7 @@ export default class WindowWrapper extends Wrapper {
`); `);
} else { } else {
props.forEach(prop => { props.forEach(prop => {
renderer.metaBindings.addLine( renderer.metaBindings.add_line(
`this._state.${prop.name} = window.${prop.value};` `this._state.${prop.name} = window.${prop.value};`
); );
}); });
@ -130,7 +130,7 @@ export default class WindowWrapper extends Wrapper {
} }
`); `);
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
@add_render_callback(ctx.${handler_name}); @add_render_callback(ctx.${handler_name});
`); `);
@ -139,7 +139,7 @@ export default class WindowWrapper extends Wrapper {
// special case... might need to abstract this out if we add more special cases // special case... might need to abstract this out if we add more special cases
if (bindings.scrollX || bindings.scrollY) { if (bindings.scrollX || bindings.scrollY) {
block.builders.update.addBlock(deindent` block.builders.update.add_block(deindent`
if (${ if (${
[bindings.scrollX, bindings.scrollY].filter(Boolean).map( [bindings.scrollX, bindings.scrollY].filter(Boolean).map(
b => `changed.${b}` b => `changed.${b}`
@ -160,7 +160,7 @@ export default class WindowWrapper extends Wrapper {
// another special case. (I'm starting to think these are all special cases.) // another special case. (I'm starting to think these are all special cases.)
if (bindings.online) { if (bindings.online) {
const handler_name = block.getUniqueName(`onlinestatuschanged`); const handler_name = block.getUniqueName(`onlinestatuschanged`);
block.builders.init.addBlock(deindent` block.builders.init.add_block(deindent`
function ${handler_name}(event) { function ${handler_name}(event) {
${component.compileOptions.dev && `component._updatingReadonlyProperty = true;`} ${component.compileOptions.dev && `component._updatingReadonlyProperty = true;`}
#component.set({ ${bindings.online}: navigator.onLine }); #component.set({ ${bindings.online}: navigator.onLine });
@ -171,11 +171,11 @@ export default class WindowWrapper extends Wrapper {
`); `);
// add initial value // add initial value
renderer.metaBindings.addLine( renderer.metaBindings.add_line(
`this._state.${bindings.online} = navigator.onLine;` `this._state.${bindings.online} = navigator.onLine;`
); );
block.builders.destroy.addBlock(deindent` block.builders.destroy.add_block(deindent`
window.removeEventListener("online", ${handler_name}); window.removeEventListener("online", ${handler_name});
window.removeEventListener("offline", ${handler_name}); window.removeEventListener("offline", ${handler_name});
`); `);

@ -38,7 +38,7 @@ export default class Tag extends Wrapper {
? `(${changedCheck}) && ${updateCachedValue}` ? `(${changedCheck}) && ${updateCachedValue}`
: changedCheck; : changedCheck;
block.builders.update.addConditional( block.builders.update.add_conditional(
condition, condition,
update(content) update(content)
); );

@ -25,7 +25,7 @@ export default function addActions(
const fn = component.qualify(action.name); const fn = component.qualify(action.name);
block.builders.mount.addLine( block.builders.mount.add_line(
`${name} = ${fn}.call(null, ${target}${snippet ? `, ${snippet}` : ''}) || {};` `${name} = ${fn}.call(null, ${target}${snippet ? `, ${snippet}` : ''}) || {};`
); );
@ -34,13 +34,13 @@ export default function addActions(
const deps = dependencies.map(dependency => `changed.${dependency}`).join(' || '); const deps = dependencies.map(dependency => `changed.${dependency}`).join(' || ');
conditional += dependencies.length > 1 ? `(${deps})` : deps; conditional += dependencies.length > 1 ? `(${deps})` : deps;
block.builders.update.addConditional( block.builders.update.add_conditional(
conditional, conditional,
`${name}.update.call(null, ${snippet});` `${name}.update.call(null, ${snippet});`
); );
} }
block.builders.destroy.addLine( block.builders.destroy.add_line(
`if (${name} && typeof ${name}.destroy === 'function') ${name}.destroy();` `if (${name} && typeof ${name}.destroy === 'function') ${name}.destroy();`
); );
}); });

@ -1,4 +1,4 @@
import deindent from '../../utils/deindent'; import deindent from '../utils/deindent';
import Component from '../Component'; import Component from '../Component';
import { CompileOptions } from '../../interfaces'; import { CompileOptions } from '../../interfaces';
import { stringify } from '../../utils/stringify'; import { stringify } from '../../utils/stringify';

@ -1,4 +1,4 @@
import repeat from './repeat'; import repeat from '../../utils/repeat';
const whitespace = /^\s+$/; const whitespace = /^\s+$/;
@ -24,10 +24,10 @@ export default class CodeBuilder {
constructor(str = '') { constructor(str = '') {
this.current = this.last = this.root; this.current = this.last = this.root;
this.addLine(str); this.add_line(str);
} }
addConditional(condition: string, body: string) { add_conditional(condition: string, body: string) {
if (this.last.type === 'condition' && this.last.condition === condition) { if (this.last.type === 'condition' && this.last.condition === condition) {
if (body && !whitespace.test(body)) this.last.children.push({ type: 'line', line: body }); if (body && !whitespace.test(body)) this.last.children.push({ type: 'line', line: body });
} else { } else {
@ -37,17 +37,17 @@ export default class CodeBuilder {
} }
} }
addLine(line: string) { add_line(line: string) {
if (line && !whitespace.test(line)) this.current.children.push(this.last = { type: 'line', line }); if (line && !whitespace.test(line)) this.current.children.push(this.last = { type: 'line', line });
} }
addBlock(block: string) { add_block(block: string) {
if (block && !whitespace.test(block)) this.current.children.push(this.last = { type: 'line', line: block, block: true }); if (block && !whitespace.test(block)) this.current.children.push(this.last = { type: 'line', line: block, block: true });
} }
isEmpty() { return !findLine(this.root); } is_empty() { return !find_line(this.root); }
pushCondition(condition: string) { push_condition(condition: string) {
if (this.last.type === 'condition' && this.last.condition === condition) { if (this.last.type === 'condition' && this.last.condition === condition) {
this.current = this.last as BlockChunk; this.current = this.last as BlockChunk;
} else { } else {
@ -57,30 +57,30 @@ export default class CodeBuilder {
} }
} }
popCondition() { pop_condition() {
if (!this.current.parent) throw new Error(`Popping a condition that maybe wasn't pushed.`); if (!this.current.parent) throw new Error(`Popping a condition that maybe wasn't pushed.`);
this.current = this.current.parent; this.current = this.current.parent;
} }
toString() { toString() {
return chunkToString(this.root); return chunk_to_string(this.root);
} }
} }
function findLine(chunk: BlockChunk) { function find_line(chunk: BlockChunk) {
for (const c of chunk.children) { for (const c of chunk.children) {
if (c.type === 'line' || findLine(c as BlockChunk)) return true; if (c.type === 'line' || find_line(c as BlockChunk)) return true;
} }
return false; return false;
} }
function chunkToString(chunk: Chunk, level: number = 0, lastBlock?: boolean, first?: boolean): string { function chunk_to_string(chunk: Chunk, level: number = 0, lastBlock?: boolean, first?: boolean): string {
if (chunk.type === 'line') { if (chunk.type === 'line') {
return `${lastBlock || (!first && chunk.block) ? '\n' : ''}${chunk.line.replace(/^/gm, repeat('\t', level))}`; return `${lastBlock || (!first && chunk.block) ? '\n' : ''}${chunk.line.replace(/^/gm, repeat('\t', level))}`;
} else if (chunk.type === 'condition') { } else if (chunk.type === 'condition') {
let t = false; let t = false;
const lines = chunk.children.map((c, i) => { const lines = chunk.children.map((c, i) => {
const str = chunkToString(c, level + 1, t, i === 0); const str = chunk_to_string(c, level + 1, t, i === 0);
t = c.type !== 'line' || c.block; t = c.type !== 'line' || c.block;
return str; return str;
}).filter(l => !!l); }).filter(l => !!l);
@ -91,7 +91,7 @@ function chunkToString(chunk: Chunk, level: number = 0, lastBlock?: boolean, fir
} else if (chunk.type === 'root') { } else if (chunk.type === 'root') {
let t = false; let t = false;
const lines = chunk.children.map((c, i) => { const lines = chunk.children.map((c, i) => {
const str = chunkToString(c, 0, t, i === 0); const str = chunk_to_string(c, 0, t, i === 0);
t = c.type !== 'line' || c.block; t = c.type !== 'line' || c.block;
return str; return str;
}).filter(l => !!l); }).filter(l => !!l);

@ -84,30 +84,30 @@ describe('CodeBuilder', () => {
it('creates a block with a line', () => { it('creates a block with a line', () => {
const builder = new CodeBuilder(); const builder = new CodeBuilder();
builder.addLine('var answer = 42;'); builder.add_line('var answer = 42;');
assert.equal(builder.toString(), 'var answer = 42;'); assert.equal(builder.toString(), 'var answer = 42;');
}); });
it('creates a block with two lines', () => { it('creates a block with two lines', () => {
const builder = new CodeBuilder(); const builder = new CodeBuilder();
builder.addLine('var problems = 99;'); builder.add_line('var problems = 99;');
builder.addLine('var answer = 42;'); builder.add_line('var answer = 42;');
assert.equal(builder.toString(), 'var problems = 99;\nvar answer = 42;'); assert.equal(builder.toString(), 'var problems = 99;\nvar answer = 42;');
}); });
it('adds newlines around blocks', () => { it('adds newlines around blocks', () => {
const builder = new CodeBuilder(); const builder = new CodeBuilder();
builder.addLine('// line 1'); builder.add_line('// line 1');
builder.addLine('// line 2'); builder.add_line('// line 2');
builder.addBlock(deindent` builder.add_block(deindent`
if ( foo ) { if ( foo ) {
bar(); bar();
} }
`); `);
builder.addLine('// line 3'); builder.add_line('// line 3');
builder.addLine('// line 4'); builder.add_line('// line 4');
assert.equal( assert.equal(
builder.toString(), builder.toString(),
@ -128,7 +128,7 @@ describe('CodeBuilder', () => {
it('nests codebuilders with correct indentation', () => { it('nests codebuilders with correct indentation', () => {
const child = new CodeBuilder(); const child = new CodeBuilder();
child.addBlock(deindent` child.add_block(deindent`
var obj = { var obj = {
answer: 42 answer: 42
}; };
@ -136,15 +136,15 @@ describe('CodeBuilder', () => {
const builder = new CodeBuilder(); const builder = new CodeBuilder();
builder.addLine('// line 1'); builder.add_line('// line 1');
builder.addLine('// line 2'); builder.add_line('// line 2');
builder.addBlock(deindent` builder.add_block(deindent`
if ( foo ) { if ( foo ) {
${child} ${child}
} }
`); `);
builder.addLine('// line 3'); builder.add_line('// line 3');
builder.addLine('// line 4'); builder.add_line('// line 4');
assert.equal( assert.equal(
builder.toString(), builder.toString(),

@ -20,7 +20,7 @@ export default function deindent(
} }
// discard empty codebuilders // discard empty codebuilders
if (expression && expression.isEmpty && expression.isEmpty()) { if (expression && expression.is_empty && expression.is_empty()) {
expression = null; expression = null;
} }

@ -1,4 +1,4 @@
import deindent from '../utils/deindent'; import deindent from './utils/deindent';
import list from '../utils/list'; import list from '../utils/list';
import { CompileOptions, ModuleFormat, Node } from '../interfaces'; import { CompileOptions, ModuleFormat, Node } from '../interfaces';

Loading…
Cancel
Save