From 38bf5b1d3f91c6bc0fda4b870eb6cf33ba32f30e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 10 Feb 2018 12:22:12 -0500 Subject: [PATCH 01/28] use ["default"] instead of .default in legacy mode - fixes #1166 --- src/generators/nodes/Component.ts | 21 +- src/generators/nodes/Slot.ts | 5 +- test/js/samples/legacy-default/_config.js | 5 + .../samples/legacy-default/expected-bundle.js | 288 ++++++++++++++++++ test/js/samples/legacy-default/expected.js | 85 ++++++ test/js/samples/legacy-default/input.html | 11 + 6 files changed, 407 insertions(+), 8 deletions(-) create mode 100644 test/js/samples/legacy-default/_config.js create mode 100644 test/js/samples/legacy-default/expected-bundle.js create mode 100644 test/js/samples/legacy-default/expected.js create mode 100644 test/js/samples/legacy-default/input.html diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 1b9b1bb8b6..0d29d98da9 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -5,10 +5,17 @@ import CodeBuilder from '../../utils/CodeBuilder'; import getTailSnippet from '../../utils/getTailSnippet'; import getObject from '../../utils/getObject'; import getExpressionPrecedence from '../../utils/getExpressionPrecedence'; +import isValidIdentifier from '../../utils/isValidIdentifier'; +import reservedNames from '../../utils/reservedNames'; import Node from './shared/Node'; import Block from '../dom/Block'; import Attribute from './Attribute'; +function quoteIfNecessary(name, legacy) { + if (!isValidIdentifier || (legacy && reservedNames.has(name))) return `"${name}"`; + return name; +} + export default class Component extends Node { type: 'Component'; name: string; @@ -71,11 +78,11 @@ export default class Component extends Node { const componentInitProperties = [`root: #component.root`]; if (this.children.length > 0) { - const slots = Array.from(this._slots).map(name => `${name}: @createFragment()`); + const slots = Array.from(this._slots).map(name => `${quoteIfNecessary(name, generator.legacy)}: @createFragment()`); componentInitProperties.push(`slots: { ${slots.join(', ')} }`); this.children.forEach((child: Node) => { - child.build(block, `${this.var}._slotted.default`, 'nodes'); + child.build(block, `${this.var}._slotted${generator.legacy ? `["default"]` : `.default`}`, 'nodes'); }); } @@ -584,7 +591,7 @@ 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.default, null);`; + return `${node.var}._mount(${name}._slotted${generator.legacy ? `["default"]` : `.default`}, null);`; } if (node.type === 'Element') { @@ -593,17 +600,17 @@ function remount(generator: DomGenerator, node: Node, name: string) { return `@appendNode(${node.var}, ${name}._slotted.${node.getStaticAttributeValue('slot')});`; } - return `@appendNode(${node.var}, ${name}._slotted.default);`; + 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.default);`; + 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.default, null);`; + 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.default, null);`; + return `${node.var}.m(${name}._slotted${generator.legacy ? `["default"]` : `.default`}, null);`; } \ No newline at end of file diff --git a/src/generators/nodes/Slot.ts b/src/generators/nodes/Slot.ts index 4413eea916..4bcd034a55 100644 --- a/src/generators/nodes/Slot.ts +++ b/src/generators/nodes/Slot.ts @@ -1,4 +1,6 @@ import deindent from '../../utils/deindent'; +import isValidIdentifier from '../../utils/isValidIdentifier'; +import reservedNames from '../../utils/reservedNames'; import Node from './shared/Node'; import Element from './Element'; import Attribute from './Attribute'; @@ -35,7 +37,8 @@ export default class Slot extends Element { generator.slots.add(slotName); const content_name = block.getUniqueName(`slot_content_${slotName}`); - block.addVariable(content_name, `#component._slotted.${slotName}`); + const prop = !isValidIdentifier(slotName) || (generator.legacy && reservedNames.has(slotName)) ? `["${slotName}"]` : `.${slotName}`; + block.addVariable(content_name, `#component._slotted${prop}`); const needsAnchorBefore = this.prev ? this.prev.type !== 'Element' : !parentNode; const needsAnchorAfter = this.next ? this.next.type !== 'Element' : !parentNode; diff --git a/test/js/samples/legacy-default/_config.js b/test/js/samples/legacy-default/_config.js new file mode 100644 index 0000000000..b5141be9ab --- /dev/null +++ b/test/js/samples/legacy-default/_config.js @@ -0,0 +1,5 @@ +export default { + options: { + legacy: true + } +}; \ No newline at end of file diff --git a/test/js/samples/legacy-default/expected-bundle.js b/test/js/samples/legacy-default/expected-bundle.js new file mode 100644 index 0000000000..3804eeb7df --- /dev/null +++ b/test/js/samples/legacy-default/expected-bundle.js @@ -0,0 +1,288 @@ +function noop() {} + +function assign(target) { + var k, + source, + i = 1, + len = arguments.length; + for (; i < len; i++) { + source = arguments[i]; + for (k in source) target[k] = source[k]; + } + + return target; +} + +function appendNode(node, target) { + target.appendChild(node); +} + +function insertNode(node, target, anchor) { + target.insertBefore(node, anchor); +} + +function detachNode(node) { + node.parentNode.removeChild(node); +} + +function reinsertBetween(before, after, target) { + while (before.nextSibling && before.nextSibling !== after) { + target.appendChild(before.parentNode.removeChild(before.nextSibling)); + } +} + +function createFragment() { + return document.createDocumentFragment(); +} + +function createElement(name) { + return document.createElement(name); +} + +function createText(data) { + return document.createTextNode(data); +} + +function createComment() { + return document.createComment(''); +} + +function blankObject() { + return Object.create(null); +} + +function destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; + + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; +} + +function differs(a, b) { + return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +} + +function dispatchObservers(component, group, changed, newState, oldState) { + for (var key in group) { + if (!changed[key]) continue; + + var newValue = newState[key]; + var oldValue = oldState[key]; + + var callbacks = group[key]; + if (!callbacks) continue; + + for (var i = 0; i < callbacks.length; i += 1) { + var callback = callbacks[i]; + if (callback.__calling) continue; + + callback.__calling = true; + callback.call(component, newValue, oldValue); + callback.__calling = false; + } + } +} + +function fire(eventName, data) { + var handlers = + eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; + + for (var i = 0; i < handlers.length; i += 1) { + handlers[i].call(this, data); + } +} + +function get(key) { + return key ? this._state[key] : this._state; +} + +function init(component, options) { + component._observers = { pre: blankObject(), post: blankObject() }; + component._handlers = blankObject(); + component._bind = options._bind; + + component.options = options; + component.root = options.root || component; + component.store = component.root.store || options.store; +} + +function observe(key, callback, options) { + var group = options && options.defer + ? this._observers.post + : this._observers.pre; + + (group[key] || (group[key] = [])).push(callback); + + if (!options || options.init !== false) { + callback.__calling = true; + callback.call(this, this._state[key]); + callback.__calling = false; + } + + return { + cancel: function() { + var index = group[key].indexOf(callback); + if (~index) group[key].splice(index, 1); + } + }; +} + +function on(eventName, handler) { + if (eventName === 'teardown') return this.on('destroy', handler); + + var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + var index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; +} + +function set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; +} + +function _set(newState) { + var oldState = this._state, + changed = {}, + dirty = false; + + for (var key in newState) { + if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + } + if (!dirty) return; + + this._state = assign({}, oldState, newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this._fragment.p(changed, this._state); + dispatchObservers(this, this._observers.post, changed, this._state, oldState); + } +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + +function _mount(target, anchor) { + this._fragment.m(target, anchor); +} + +function _unmount() { + if (this._fragment) this._fragment.u(); +} + +var proto = { + destroy: destroy, + get: get, + fire: fire, + observe: observe, + on: on, + set: set, + teardown: destroy, + _recompute: noop, + _set: _set, + _mount: _mount, + _unmount: _unmount +}; + +/* generated by Svelte vX.Y.Z */ +function create_main_fragment(state, component) { + var text, p, text_1, text_2, text_3, slot_content_default = component._slotted["default"], slot_content_default_before, slot_content_default_after; + + var foo = new Foo({ + root: component.root, + slots: { "default": createFragment() } + }); + + return { + c: function create() { + text = createText("\n\t"); + p = createElement("p"); + text_1 = createText("some default slotted content"); + text_2 = createText("\n"); + foo._fragment.c(); + text_3 = createText("\n\n"); + }, + + m: function mount(target, anchor) { + appendNode(text, foo._slotted["default"]); + appendNode(p, foo._slotted["default"]); + appendNode(text_1, p); + appendNode(text_2, foo._slotted["default"]); + foo._mount(target, anchor); + insertNode(text_3, target, anchor); + + if (slot_content_default) { + insertNode(slot_content_default_before || (slot_content_default_before = createComment()), target, anchor); + insertNode(slot_content_default, target, anchor); + insertNode(slot_content_default_after || (slot_content_default_after = createComment()), target, anchor); + } + }, + + p: noop, + + u: function unmount() { + foo._unmount(); + detachNode(text_3); + + if (slot_content_default) { + reinsertBetween(slot_content_default_before, slot_content_default_after, slot_content_default); + detachNode(slot_content_default_before); + detachNode(slot_content_default_after); + } + }, + + d: function destroy$$1() { + foo.destroy(false); + } + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._slotted = options.slots || {}; + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.slots = {}; + + this._fragment = create_main_fragment(this._state, this); + + if (options.target) { + this._fragment.c(); + this._fragment.m(options.target, options.anchor || null); + + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } +} + +assign(SvelteComponent.prototype, proto); + +export default SvelteComponent; diff --git a/test/js/samples/legacy-default/expected.js b/test/js/samples/legacy-default/expected.js new file mode 100644 index 0000000000..27cdd1896f --- /dev/null +++ b/test/js/samples/legacy-default/expected.js @@ -0,0 +1,85 @@ +/* generated by Svelte vX.Y.Z */ +import { appendNode, assign, callAll, createComment, createElement, createFragment, createText, detachNode, init, insertNode, noop, proto, reinsertBetween } from "svelte/shared.js"; + +function create_main_fragment(state, component) { + var text, p, text_1, text_2, text_3, slot_content_default = component._slotted["default"], slot_content_default_before, slot_content_default_after; + + var foo = new Foo({ + root: component.root, + slots: { "default": createFragment() } + }); + + return { + c: function create() { + text = createText("\n\t"); + p = createElement("p"); + text_1 = createText("some default slotted content"); + text_2 = createText("\n"); + foo._fragment.c(); + text_3 = createText("\n\n"); + }, + + m: function mount(target, anchor) { + appendNode(text, foo._slotted["default"]); + appendNode(p, foo._slotted["default"]); + appendNode(text_1, p); + appendNode(text_2, foo._slotted["default"]); + foo._mount(target, anchor); + insertNode(text_3, target, anchor); + + if (slot_content_default) { + insertNode(slot_content_default_before || (slot_content_default_before = createComment()), target, anchor); + insertNode(slot_content_default, target, anchor); + insertNode(slot_content_default_after || (slot_content_default_after = createComment()), target, anchor); + } + }, + + p: noop, + + u: function unmount() { + foo._unmount(); + detachNode(text_3); + + if (slot_content_default) { + reinsertBetween(slot_content_default_before, slot_content_default_after, slot_content_default); + detachNode(slot_content_default_before); + detachNode(slot_content_default_after); + } + }, + + d: function destroy() { + foo.destroy(false); + } + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._slotted = options.slots || {}; + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.slots = {}; + + this._fragment = create_main_fragment(this._state, this); + + if (options.target) { + this._fragment.c(); + this._fragment.m(options.target, options.anchor || null); + + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/legacy-default/input.html b/test/js/samples/legacy-default/input.html new file mode 100644 index 0000000000..b0d6cbf181 --- /dev/null +++ b/test/js/samples/legacy-default/input.html @@ -0,0 +1,11 @@ + +

some default slotted content

+
+ + + + \ No newline at end of file From 0b39cafc67d599adf42f3b15db06ea77ac388618 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 08:23:29 -0500 Subject: [PATCH 02/28] start implementing context --- src/generators/Generator.ts | 40 ++++++++++++---------- src/generators/dom/Block.ts | 10 +++--- src/generators/dom/index.ts | 4 +-- src/generators/nodes/AwaitBlock.ts | 23 ++++++------- src/generators/nodes/Component.ts | 8 ++--- src/generators/nodes/EachBlock.ts | 44 +++++++++++++----------- src/generators/nodes/Fragment.ts | 1 - src/generators/nodes/IfBlock.ts | 55 +++++++++++++++--------------- 8 files changed, 92 insertions(+), 93 deletions(-) diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 1f48424c95..5570fe0354 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -242,24 +242,26 @@ export default class Generator { if (name === 'event' && isEventHandler) { // noop } else if (contexts.has(name)) { - const contextName = contexts.get(name); - if (contextName !== name) { - // this is true for 'reserved' names like `state` and `component`, - // also destructured contexts - code.overwrite( - node.start, - node.start + name.length, - contextName, - { storeName: true, contentOnly: false } - ); - - const destructuredName = contextName.replace(/\[\d+\]/, ''); - if (destructuredName !== contextName) { - // so that hoisting the context works correctly - usedContexts.add(destructuredName); - } - } - + // const contextName = contexts.get(name); + // if (contextName !== name) { + // // this is true for 'reserved' names like `state` and `component`, + // // also destructured contexts + // code.overwrite( + // node.start, + // node.start + name.length, + // contextName, + // { storeName: true, contentOnly: false } + // ); + + // const destructuredName = contextName.replace(/\[\d+\]/, ''); + // if (destructuredName !== contextName) { + // // so that hoisting the context works correctly + // usedContexts.add(destructuredName); + // } + // } + + // TODO filthy, temporary hack + if (self.constructor.name === 'DomGenerator') code.prependRight(node.start, `state.`); usedContexts.add(name); } else if (helpers.has(name)) { let object = node; @@ -268,6 +270,8 @@ export default class Generator { const alias = self.templateVars.get(`helpers-${name}`); if (alias !== name) code.overwrite(object.start, object.end, alias); } else if (indexes.has(name)) { + if (self.constructor.name === 'DomGenerator') code.prependRight(node.start, `state.`); + const context = indexes.get(name); usedContexts.add(context); // TODO is this right? usedIndexes.add(name); diff --git a/src/generators/dom/Block.ts b/src/generators/dom/Block.ts index f92d9e0cb1..2a50b3cfdb 100644 --- a/src/generators/dom/Block.ts +++ b/src/generators/dom/Block.ts @@ -17,7 +17,6 @@ export interface BlockOptions { contextTypes?: Map; indexes?: Map; changeableIndexes?: Map; - params?: string[]; indexNames?: Map; listNames?: Map; indexName?: string; @@ -41,7 +40,6 @@ export default class Block { indexes: Map; changeableIndexes: Map; dependencies: Set; - params: string[]; indexNames: Map; listNames: Map; indexName: string; @@ -90,10 +88,10 @@ export default class Block { this.changeableIndexes = options.changeableIndexes; this.dependencies = new Set(); - this.params = options.params; this.indexNames = options.indexNames; this.listNames = options.listNames; + this.indexName = options.indexName; this.listName = options.listName; this.builders = { @@ -116,7 +114,7 @@ export default class Block { this.aliases = new Map(); this.variables = new Map(); - this.getUniqueName = this.generator.getUniqueNameMaker(options.params); + this.getUniqueName = this.generator.getUniqueNameMaker([]); // TODO this is wrong... we probably don't need this any more this.hasUpdateMethod = false; // determined later } @@ -254,7 +252,7 @@ export default class Block { properties.addBlock(`p: @noop,`); } else { properties.addBlock(deindent` - p: function update(changed, ${this.params.join(', ')}) { + p: function update(changed, state) { ${this.builders.update} }, `); @@ -328,7 +326,7 @@ export default class Block { return deindent` ${this.comment && `// ${escape(this.comment)}`} - function ${this.name}(${this.params.join(', ')}, #component${this.key ? `, ${localKey}` : ''}) { + function ${this.name}(#component, state${this.key ? `, ${localKey}` : ''}) { ${this.variables.size > 0 && `var ${Array.from(this.variables.keys()) .map(key => { diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 08bdf0012f..090dffbf3e 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -50,7 +50,7 @@ export class DomGenerator extends Generator { } getUniqueNameMaker(params: string[]) { - const localUsedNames = new Set(params); + const localUsedNames = new Set(params); // TODO is this ever called with params? function add(name: string) { localUsedNames.add(name); @@ -257,7 +257,7 @@ export default function dom( ${generator.slots.size && `this.slots = {};`} - this._fragment = @create_main_fragment(this._state, this); + this._fragment = @create_main_fragment(this, this._state); ${(templateProperties.oncreate) && deindent` this.root._oncreate.push(_oncreate); diff --git a/src/generators/nodes/AwaitBlock.ts b/src/generators/nodes/AwaitBlock.ts index 445767d27f..c5cce65758 100644 --- a/src/generators/nodes/AwaitBlock.ts +++ b/src/generators/nodes/AwaitBlock.ts @@ -45,7 +45,6 @@ export default class AwaitBlock extends Node { child.block = block.child({ comment: createDebuggingComment(child, this.generator), name: this.generator.getUniqueName(`create_${status}_block`), - params: block.params.concat(context), context, contexts, contextTypes @@ -75,8 +74,6 @@ export default class AwaitBlock extends Node { const anchor = this.getOrCreateAnchor(block, parentNode, parentNodes); const updateMountNode = this.getUpdateMountNode(anchor); - const params = block.params.join(', '); - block.contextualise(this.expression); const { snippet } = this.metadata; @@ -106,11 +103,11 @@ export default class AwaitBlock extends Node { // but it's probably not worth it block.builders.init.addBlock(deindent` - function ${replace_await_block}(${token}, type, ${value}, ${params}) { + function ${replace_await_block}(${token}, type, ${value}, state) { if (${token} !== ${await_token}) return; var ${old_block} = ${await_block}; - ${await_block} = (${await_block_type} = type)(${params}, ${resolved} = ${value}, #component); + ${await_block} = (${await_block_type} = type)(state, ${resolved} = ${value}, #component); if (${old_block}) { ${old_block}.u(); @@ -122,33 +119,33 @@ export default class AwaitBlock extends Node { } } - function ${handle_promise}(${promise}, ${params}) { + function ${handle_promise}(${promise}, state) { var ${token} = ${await_token} = {}; if (@isPromise(${promise})) { ${promise}.then(function(${value}) { var state = #component.get(); - ${replace_await_block}(${token}, ${create_then_block}, ${value}, ${params}); + ${replace_await_block}(${token}, ${create_then_block}, ${value}, state); }, function (${error}) { var state = #component.get(); - ${replace_await_block}(${token}, ${create_catch_block}, ${error}, ${params}); + ${replace_await_block}(${token}, ${create_catch_block}, ${error}, state); }); // if we previously had a then/catch block, destroy it if (${await_block_type} !== ${create_pending_block}) { - ${replace_await_block}(${token}, ${create_pending_block}, null, ${params}); + ${replace_await_block}(${token}, ${create_pending_block}, null, state); return true; } } else { ${resolved} = ${promise}; if (${await_block_type} !== ${create_then_block}) { - ${replace_await_block}(${token}, ${create_then_block}, ${resolved}, ${params}); + ${replace_await_block}(${token}, ${create_then_block}, ${resolved}, state); return true; } } } - ${handle_promise}(${promise} = ${snippet}, ${params}); + ${handle_promise}(${promise} = ${snippet}, state); `); block.builders.create.addBlock(deindent` @@ -177,7 +174,7 @@ export default class AwaitBlock extends Node { conditions.push( `${promise} !== (${promise} = ${snippet})`, - `${handle_promise}(${promise}, ${params})` + `${handle_promise}(${promise}, state)` ); if (this.pending.block.hasUpdateMethod) { @@ -185,7 +182,7 @@ export default class AwaitBlock extends Node { if (${conditions.join(' && ')}) { // nothing } else { - ${await_block}.p(changed, ${params}, ${resolved}); + ${await_block}.p(changed, state, ${resolved}); } `); } else { diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 493302c3d5..83e88f2bda 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -279,12 +279,10 @@ export default class Component extends Node { const anchor = this.getOrCreateAnchor(block, parentNode, parentNodes); - const params = block.params.join(', '); - block.builders.init.addBlock(deindent` var ${switch_vars.value} = ${snippet}; - function ${switch_vars.props}(${params}) { + function ${switch_vars.props}(state) { ${statements.length > 0 && statements.join('\n')} return { ${componentInitProperties.join(',\n')} @@ -292,7 +290,7 @@ export default class Component extends Node { } if (${switch_vars.value}) { - var ${name} = new ${expression}(${switch_vars.props}(${params})); + var ${name} = new ${expression}(${switch_vars.props}(state)); ${beforecreate} } @@ -327,7 +325,7 @@ export default class Component extends Node { if (${name}) ${name}.destroy(); if (${switch_vars.value}) { - ${name} = new ${switch_vars.value}(${switch_vars.props}(${params})); + ${name} = new ${switch_vars.value}(${switch_vars.props}(state)); ${name}._fragment.c(); ${this.children.map(child => remount(generator, child, name))} diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index 904536fda3..9a094affe4 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -29,6 +29,7 @@ export default class EachBlock extends Node { this.var = block.getUniqueName(`each`); this.iterations = block.getUniqueName(`${this.var}_blocks`); + this.each_context = block.getUniqueName(`${this.var}_context`); const { dependencies } = this.metadata; block.addDependencies(dependencies); @@ -80,8 +81,7 @@ export default class EachBlock extends Node { indexName, indexNames, - listNames, - params: block.params.concat(listName, context, indexName), + listNames }); this.generator.blocks.push(this.block); @@ -117,7 +117,6 @@ export default class EachBlock extends Node { const create_each_block = this.block.name; const each_block_value = this.block.listName; const iterations = this.iterations; - const params = block.params.join(', '); const needsAnchor = this.next ? !this.next.isDomNode() : !parentNode || !this.parent.isDomNode(); const anchor = needsAnchor @@ -138,7 +137,6 @@ export default class EachBlock extends Node { each_block_value, length, iterations, - params, anchor, mountOrIntro, }; @@ -171,7 +169,7 @@ export default class EachBlock extends Node { // TODO neaten this up... will end up with an empty line in the block block.builders.init.addBlock(deindent` if (!${each_block_value}.${length}) { - ${each_block_else} = ${this.else.block.name}(${params}, #component); + ${each_block_else} = ${this.else.block.name}(#component, state); ${each_block_else}.c(); } `); @@ -187,9 +185,9 @@ export default class EachBlock extends Node { if (this.else.block.hasUpdateMethod) { block.builders.update.addBlock(deindent` if (!${each_block_value}.${length} && ${each_block_else}) { - ${each_block_else}.p( changed, ${params} ); + ${each_block_else}.p(changed, state); } else if (!${each_block_value}.${length}) { - ${each_block_else} = ${this.else.block.name}(${params}, #component); + ${each_block_else} = ${this.else.block.name}(#component, state); ${each_block_else}.c(); ${each_block_else}.${mountOrIntro}(${initialMountNode}, ${anchor}); } else if (${each_block_else}) { @@ -207,7 +205,7 @@ export default class EachBlock extends Node { ${each_block_else} = null; } } else if (!${each_block_else}) { - ${each_block_else} = ${this.else.block.name}(${params}, #component); + ${each_block_else} = ${this.else.block.name}(#component, state); ${each_block_else}.c(); ${each_block_else}.${mountOrIntro}(${initialMountNode}, ${anchor}); } @@ -244,7 +242,6 @@ export default class EachBlock extends Node { create_each_block, each_block_value, length, - params, anchor, mountOrIntro, } @@ -275,7 +272,7 @@ export default class EachBlock extends Node { block.builders.init.addBlock(deindent` for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) { var ${key} = ${each_block_value}[#i].${this.key}; - var ${iteration} = ${lookup}[${key}] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component, ${key}); + var ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, state, ${key}); if (${last}) ${last}.next = ${iteration}; ${iteration}.last = ${last}; @@ -380,7 +377,7 @@ export default class EachBlock extends Node { var ${iteration} = ${lookup}[${key}]; ${dynamic && - `if (${iteration}) ${iteration}.p(changed, ${params}, ${each_block_value}, ${each_block_value}[#i], #i);`} + `if (${iteration}) ${iteration}.p(changed, state);`} if (${expected}) { if (${key} === ${expected}.key) { @@ -401,7 +398,7 @@ export default class EachBlock extends Node { if (!${expected}) ${iteration}.m(${updateMountNode}, ${anchor}); } else { // key is being inserted - ${iteration} = ${lookup}[${key}] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component, ${key}); + ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, state, ${key}); ${iteration}.c(); ${iteration}.${mountOrIntro}(${updateMountNode}, ${expected}.first); @@ -416,7 +413,7 @@ export default class EachBlock extends Node { ${iteration}.next = null; ${iteration}.m(${updateMountNode}, ${anchor}); } else { - ${iteration} = ${lookup}[${key}] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component, ${key}); + ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, state, ${key}); ${iteration}.c(); ${iteration}.${mountOrIntro}(${updateMountNode}, ${anchor}); } @@ -464,7 +461,6 @@ export default class EachBlock extends Node { each_block_value, length, iterations, - params, anchor, mountOrIntro, } @@ -473,7 +469,10 @@ export default class EachBlock extends Node { var ${iterations} = []; for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) { - ${iterations}[#i] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component); + ${iterations}[#i] = ${create_each_block}(#component, @assign({}, state, { + ${this.context}: ${each_block_value}[#i], + ${this.block.indexName}: #i + })); } `); @@ -517,24 +516,24 @@ export default class EachBlock extends Node { ? this.block.hasIntroMethod ? deindent` if (${iterations}[#i]) { - ${iterations}[#i].p(changed, ${params}, ${each_block_value}, ${each_block_value}[#i], #i); + ${iterations}[#i].p(changed, ${this.each_context}); } else { - ${iterations}[#i] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component); + ${iterations}[#i] = ${create_each_block}(#component, ${this.each_context}); ${iterations}[#i].c(); } ${iterations}[#i].i(${updateMountNode}, ${anchor}); ` : deindent` if (${iterations}[#i]) { - ${iterations}[#i].p(changed, ${params}, ${each_block_value}, ${each_block_value}[#i], #i); + ${iterations}[#i].p(changed, ${this.each_context}); } else { - ${iterations}[#i] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component); + ${iterations}[#i] = ${create_each_block}(#component, ${this.each_context}); ${iterations}[#i].c(); ${iterations}[#i].m(${updateMountNode}, ${anchor}); } ` : deindent` - ${iterations}[#i] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component); + ${iterations}[#i] = ${create_each_block}(#component, ${this.each_context}); ${iterations}[#i].c(); ${iterations}[#i].${mountOrIntro}(${updateMountNode}, ${anchor}); `; @@ -569,6 +568,11 @@ export default class EachBlock extends Node { if (${condition}) { for (var #i = ${start}; #i < ${each_block_value}.${length}; #i += 1) { + var ${this.each_context} = @assign({}, state, { + ${this.context}: ${each_block_value}[#i], + ${this.block.indexName}: #i + }); + ${forLoopBody} } diff --git a/src/generators/nodes/Fragment.ts b/src/generators/nodes/Fragment.ts index 875d3b6a24..e4eb559679 100644 --- a/src/generators/nodes/Fragment.ts +++ b/src/generators/nodes/Fragment.ts @@ -16,7 +16,6 @@ export default class Fragment extends Node { indexes: new Map(), changeableIndexes: new Map(), - params: ['state'], indexNames: new Map(), listNames: new Map(), diff --git a/src/generators/nodes/IfBlock.ts b/src/generators/nodes/IfBlock.ts index 3cec2245d9..17cd92bc29 100644 --- a/src/generators/nodes/IfBlock.ts +++ b/src/generators/nodes/IfBlock.ts @@ -100,7 +100,6 @@ export default class IfBlock extends Node { const anchor = needsAnchor ? block.getUniqueName(`${name}_anchor`) : (this.next && this.next.var) || 'null'; - const params = block.params.join(', '); const branches = getBranches(this.generator, block, parentNode, parentNodes, this); @@ -110,7 +109,7 @@ export default class IfBlock extends Node { const dynamic = branches[0].hasUpdateMethod; // can use [0] as proxy for all, since they necessarily have the same value const hasOutros = branches[0].hasOutroMethod; - const vars = { name, anchor, params, if_name, hasElse }; + const vars = { name, anchor, if_name, hasElse }; if (this.else) { if (hasOutros) { @@ -218,10 +217,10 @@ function simple( node: Node, branch, dynamic, - { name, anchor, params, if_name } + { name, anchor, if_name } ) { block.builders.init.addBlock(deindent` - var ${name} = (${branch.condition}) && ${branch.block}(${params}, #component); + var ${name} = (${branch.condition}) && ${branch.block}(#component, state); `); const mountOrIntro = branch.hasIntroMethod ? 'i' : 'm'; @@ -238,9 +237,9 @@ function simple( ? branch.hasIntroMethod ? deindent` if (${name}) { - ${name}.p(changed, ${params}); + ${name}.p(changed, state); } else { - ${name} = ${branch.block}(${params}, #component); + ${name} = ${branch.block}(#component, state); if (${name}) ${name}.c(); } @@ -248,9 +247,9 @@ function simple( ` : deindent` if (${name}) { - ${name}.p(changed, ${params}); + ${name}.p(changed, state); } else { - ${name} = ${branch.block}(${params}, #component); + ${name} = ${branch.block}(#component, state); ${name}.c(); ${name}.m(${updateMountNode}, ${anchor}); } @@ -258,14 +257,14 @@ function simple( : branch.hasIntroMethod ? deindent` if (!${name}) { - ${name} = ${branch.block}(${params}, #component); + ${name} = ${branch.block}(#component, state); ${name}.c(); } ${name}.i(${updateMountNode}, ${anchor}); ` : deindent` if (!${name}) { - ${name} = ${branch.block}(${params}, #component); + ${name} = ${branch.block}(#component, state); ${name}.c(); ${name}.m(${updateMountNode}, ${anchor}); } @@ -308,14 +307,14 @@ function compound( node: Node, branches, dynamic, - { name, anchor, params, hasElse, if_name } + { name, anchor, hasElse, if_name } ) { const select_block_type = generator.getUniqueName(`select_block_type`); const current_block_type = block.getUniqueName(`current_block_type`); const current_block_type_and = hasElse ? '' : `${current_block_type} && `; generator.blocks.push(deindent` - function ${select_block_type}(${params}) { + function ${select_block_type}(state) { ${branches .map(({ condition, block }) => `${condition ? `if (${condition}) ` : ''}return ${block};`) .join('\n')} @@ -323,8 +322,8 @@ function compound( `); block.builders.init.addBlock(deindent` - var ${current_block_type} = ${select_block_type}(${params}); - var ${name} = ${current_block_type_and}${current_block_type}(${params}, #component); + 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'; @@ -348,22 +347,22 @@ function compound( ${name}.u(); ${name}.d(); }`} - ${name} = ${current_block_type_and}${current_block_type}(${params}, #component); + ${name} = ${current_block_type_and}${current_block_type}(#component, state); ${if_name}${name}.c(); ${if_name}${name}.${mountOrIntro}(${updateMountNode}, ${anchor}); `; if (dynamic) { block.builders.update.addBlock(deindent` - if (${current_block_type} === (${current_block_type} = ${select_block_type}(${params})) && ${name}) { - ${name}.p(changed, ${params}); + 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}(${params}))) { + if (${current_block_type} !== (${current_block_type} = ${select_block_type}(state))) { ${changeBlock} } `); @@ -384,7 +383,7 @@ function compoundWithOutros( node: Node, branches, dynamic, - { name, anchor, params, hasElse } + { name, anchor, hasElse } ) { const select_block_type = block.getUniqueName(`select_block_type`); const current_block_type_index = block.getUniqueName(`current_block_type_index`); @@ -406,7 +405,7 @@ function compoundWithOutros( var ${if_blocks} = []; - function ${select_block_type}(${params}) { + function ${select_block_type}(state) { ${branches .map(({ condition, block }, i) => `${condition ? `if (${condition}) ` : ''}return ${block ? i : -1};`) .join('\n')} @@ -415,13 +414,13 @@ function compoundWithOutros( if (hasElse) { block.builders.init.addBlock(deindent` - ${current_block_type_index} = ${select_block_type}(${params}); - ${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](${params}, #component); + ${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}(${params}))) { - ${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](${params}, #component); + 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); } `); } @@ -447,7 +446,7 @@ function compoundWithOutros( 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}](${params}, #component); + ${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](#component, state); ${name}.c(); } ${name}.${mountOrIntro}(${updateMountNode}, ${anchor}); @@ -474,9 +473,9 @@ function compoundWithOutros( if (dynamic) { block.builders.update.addBlock(deindent` var ${previous_block_index} = ${current_block_type_index}; - ${current_block_type_index} = ${select_block_type}(${params}); + ${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, ${params}); + ${if_current_block_type_index}${if_blocks}[${current_block_type_index}].p(changed, state); } else { ${changeBlock} } @@ -484,7 +483,7 @@ function compoundWithOutros( } else { block.builders.update.addBlock(deindent` var ${previous_block_index} = ${current_block_type_index}; - ${current_block_type_index} = ${select_block_type}(${params}); + ${current_block_type_index} = ${select_block_type}(state); if (${current_block_type_index} !== ${previous_block_index}) { ${changeBlock} } From c0292cbca1333afa21a1a7ebaed43cafdfce1e1e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 08:24:55 -0500 Subject: [PATCH 03/28] update tests --- .../expected-bundle.js | 4 +-- .../expected.js | 4 +-- .../component-static/expected-bundle.js | 4 +-- test/js/samples/component-static/expected.js | 4 +-- .../computed-collapsed-if/expected-bundle.js | 4 +-- .../samples/computed-collapsed-if/expected.js | 4 +-- .../css-media-query/expected-bundle.js | 4 +-- test/js/samples/css-media-query/expected.js | 4 +-- .../expected-bundle.js | 4 +-- .../css-shadow-dom-keyframes/expected.js | 4 +-- .../deconflict-globals/expected-bundle.js | 4 +-- .../js/samples/deconflict-globals/expected.js | 4 +-- .../samples/do-use-dataset/expected-bundle.js | 4 +-- test/js/samples/do-use-dataset/expected.js | 4 +-- .../expected-bundle.js | 4 +-- .../dont-use-dataset-in-legacy/expected.js | 4 +-- .../expected-bundle.js | 4 +-- .../dont-use-dataset-in-svg/expected.js | 4 +-- .../expected-bundle.js | 32 ++++++++++------- .../each-block-changed-check/expected.js | 32 ++++++++++------- .../event-handlers-custom/expected-bundle.js | 4 +-- .../samples/event-handlers-custom/expected.js | 4 +-- .../head-no-whitespace/expected-bundle.js | 4 +-- .../js/samples/head-no-whitespace/expected.js | 4 +-- .../if-block-no-update/expected-bundle.js | 12 +++---- .../js/samples/if-block-no-update/expected.js | 12 +++---- .../if-block-simple/expected-bundle.js | 10 +++--- test/js/samples/if-block-simple/expected.js | 10 +++--- .../expected-bundle.js | 4 +-- .../expected.js | 4 +-- .../expected-bundle.js | 4 +-- .../inline-style-optimized-url/expected.js | 4 +-- .../inline-style-optimized/expected-bundle.js | 4 +-- .../inline-style-optimized/expected.js | 4 +-- .../expected-bundle.js | 4 +-- .../inline-style-unoptimized/expected.js | 4 +-- .../expected-bundle.js | 4 +-- .../input-without-blowback-guard/expected.js | 4 +-- .../legacy-input-type/expected-bundle.js | 4 +-- test/js/samples/legacy-input-type/expected.js | 4 +-- .../legacy-quote-class/expected-bundle.js | 4 +-- .../js/samples/legacy-quote-class/expected.js | 4 +-- .../samples/media-bindings/expected-bundle.js | 4 +-- test/js/samples/media-bindings/expected.js | 4 +-- .../non-imported-component/expected-bundle.js | 4 +-- .../non-imported-component/expected.js | 4 +-- .../expected-bundle.js | 4 +-- .../onrender-onteardown-rewritten/expected.js | 4 +-- .../samples/setup-method/expected-bundle.js | 4 +-- test/js/samples/setup-method/expected.js | 4 +-- test/js/samples/svg-title/expected-bundle.js | 4 +-- test/js/samples/svg-title/expected.js | 4 +-- test/js/samples/title/expected-bundle.js | 4 +-- test/js/samples/title/expected.js | 4 +-- .../expected-bundle.js | 34 +++++++++---------- .../use-elements-as-anchors/expected.js | 34 +++++++++---------- .../window-binding-scroll/expected-bundle.js | 4 +-- .../samples/window-binding-scroll/expected.js | 4 +-- 58 files changed, 196 insertions(+), 180 deletions(-) diff --git a/test/js/samples/collapses-text-around-comments/expected-bundle.js b/test/js/samples/collapses-text-around-comments/expected-bundle.js index 59cd2de125..33854eb0e6 100644 --- a/test/js/samples/collapses-text-around-comments/expected-bundle.js +++ b/test/js/samples/collapses-text-around-comments/expected-bundle.js @@ -208,7 +208,7 @@ function add_css() { appendNode(style, document.head); } -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var p, text; return { @@ -247,7 +247,7 @@ function SvelteComponent(options) { if (!document.getElementById("svelte-2794052100-style")) add_css(); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/collapses-text-around-comments/expected.js b/test/js/samples/collapses-text-around-comments/expected.js index aea4721e36..17e612b644 100644 --- a/test/js/samples/collapses-text-around-comments/expected.js +++ b/test/js/samples/collapses-text-around-comments/expected.js @@ -16,7 +16,7 @@ function add_css() { appendNode(style, document.head); } -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var p, text; return { @@ -55,7 +55,7 @@ function SvelteComponent(options) { if (!document.getElementById("svelte-2794052100-style")) add_css(); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/component-static/expected-bundle.js b/test/js/samples/component-static/expected-bundle.js index 708ef5be95..5047d48b5e 100644 --- a/test/js/samples/component-static/expected-bundle.js +++ b/test/js/samples/component-static/expected-bundle.js @@ -171,7 +171,7 @@ var proto = { /* generated by Svelte vX.Y.Z */ var Nested = window.Nested; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var nested = new Nested({ root: component.root, @@ -209,7 +209,7 @@ function SvelteComponent(options) { this._aftercreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/component-static/expected.js b/test/js/samples/component-static/expected.js index d75cce1253..a689848dc0 100644 --- a/test/js/samples/component-static/expected.js +++ b/test/js/samples/component-static/expected.js @@ -3,7 +3,7 @@ import { assign, callAll, init, noop, proto } from "svelte/shared.js"; var Nested = window.Nested; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var nested = new Nested({ root: component.root, @@ -41,7 +41,7 @@ function SvelteComponent(options) { this._aftercreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/computed-collapsed-if/expected-bundle.js b/test/js/samples/computed-collapsed-if/expected-bundle.js index b39f95a77b..64b09b4353 100644 --- a/test/js/samples/computed-collapsed-if/expected-bundle.js +++ b/test/js/samples/computed-collapsed-if/expected-bundle.js @@ -177,7 +177,7 @@ function b(x) { return x * 3; } -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { return { c: noop, @@ -197,7 +197,7 @@ function SvelteComponent(options) { this._state = assign({}, options.data); this._recompute({ x: 1 }, this._state); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/computed-collapsed-if/expected.js b/test/js/samples/computed-collapsed-if/expected.js index c2f5e3c2be..e4a2fe00ef 100644 --- a/test/js/samples/computed-collapsed-if/expected.js +++ b/test/js/samples/computed-collapsed-if/expected.js @@ -9,7 +9,7 @@ function b(x) { return x * 3; } -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { return { c: noop, @@ -29,7 +29,7 @@ function SvelteComponent(options) { this._state = assign({}, options.data); this._recompute({ x: 1 }, this._state); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/css-media-query/expected-bundle.js b/test/js/samples/css-media-query/expected-bundle.js index 91662053a4..66bb102184 100644 --- a/test/js/samples/css-media-query/expected-bundle.js +++ b/test/js/samples/css-media-query/expected-bundle.js @@ -200,7 +200,7 @@ function add_css() { appendNode(style, document.head); } -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div; return { @@ -233,7 +233,7 @@ function SvelteComponent(options) { if (!document.getElementById("svelte-3905933315-style")) add_css(); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/css-media-query/expected.js b/test/js/samples/css-media-query/expected.js index 37afa6b42a..6e03ca0a0a 100644 --- a/test/js/samples/css-media-query/expected.js +++ b/test/js/samples/css-media-query/expected.js @@ -12,7 +12,7 @@ function add_css() { appendNode(style, document.head); } -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div; return { @@ -45,7 +45,7 @@ function SvelteComponent(options) { if (!document.getElementById("svelte-3905933315-style")) add_css(); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js b/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js index 03e5cef1e2..486c2d1d82 100644 --- a/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js +++ b/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js @@ -181,7 +181,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div; return { @@ -214,7 +214,7 @@ class SvelteComponent extends HTMLElement { this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = ``; - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); this._fragment.c(); this._fragment.m(this.shadowRoot, null); diff --git a/test/js/samples/css-shadow-dom-keyframes/expected.js b/test/js/samples/css-shadow-dom-keyframes/expected.js index 496586e632..39dd83e5cb 100644 --- a/test/js/samples/css-shadow-dom-keyframes/expected.js +++ b/test/js/samples/css-shadow-dom-keyframes/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { assign, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div; return { @@ -34,7 +34,7 @@ class SvelteComponent extends HTMLElement { this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = ``; - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); this._fragment.c(); this._fragment.m(this.shadowRoot, null); diff --git a/test/js/samples/deconflict-globals/expected-bundle.js b/test/js/samples/deconflict-globals/expected-bundle.js index ba22542c4b..841aaf7111 100644 --- a/test/js/samples/deconflict-globals/expected-bundle.js +++ b/test/js/samples/deconflict-globals/expected-bundle.js @@ -179,7 +179,7 @@ function oncreate() { alert(JSON.stringify(data())); } -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { return { c: noop, @@ -204,7 +204,7 @@ function SvelteComponent(options) { this._oncreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); this.root._oncreate.push(_oncreate); diff --git a/test/js/samples/deconflict-globals/expected.js b/test/js/samples/deconflict-globals/expected.js index 8730f4e3c3..d740fe634d 100644 --- a/test/js/samples/deconflict-globals/expected.js +++ b/test/js/samples/deconflict-globals/expected.js @@ -11,7 +11,7 @@ function oncreate() { alert(JSON.stringify(data())); }; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { return { c: noop, @@ -36,7 +36,7 @@ function SvelteComponent(options) { this._oncreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); this.root._oncreate.push(_oncreate); diff --git a/test/js/samples/do-use-dataset/expected-bundle.js b/test/js/samples/do-use-dataset/expected-bundle.js index 2079e6607c..b6667c9c91 100644 --- a/test/js/samples/do-use-dataset/expected-bundle.js +++ b/test/js/samples/do-use-dataset/expected-bundle.js @@ -185,7 +185,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div, text, div_1; return { @@ -227,7 +227,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/do-use-dataset/expected.js b/test/js/samples/do-use-dataset/expected.js index 1ae7417469..27a335d0a6 100644 --- a/test/js/samples/do-use-dataset/expected.js +++ b/test/js/samples/do-use-dataset/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div, text, div_1; return { @@ -43,7 +43,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/dont-use-dataset-in-legacy/expected-bundle.js b/test/js/samples/dont-use-dataset-in-legacy/expected-bundle.js index efcfcc8f30..643e302968 100644 --- a/test/js/samples/dont-use-dataset-in-legacy/expected-bundle.js +++ b/test/js/samples/dont-use-dataset-in-legacy/expected-bundle.js @@ -189,7 +189,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div, text, div_1; return { @@ -231,7 +231,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/dont-use-dataset-in-legacy/expected.js b/test/js/samples/dont-use-dataset-in-legacy/expected.js index 03eef26ced..e9062d7ba1 100644 --- a/test/js/samples/dont-use-dataset-in-legacy/expected.js +++ b/test/js/samples/dont-use-dataset-in-legacy/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { assign, createElement, createText, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div, text, div_1; return { @@ -43,7 +43,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/dont-use-dataset-in-svg/expected-bundle.js b/test/js/samples/dont-use-dataset-in-svg/expected-bundle.js index cf43bbd130..5c9ef31f54 100644 --- a/test/js/samples/dont-use-dataset-in-svg/expected-bundle.js +++ b/test/js/samples/dont-use-dataset-in-svg/expected-bundle.js @@ -189,7 +189,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var svg, g, g_1; return { @@ -229,7 +229,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/dont-use-dataset-in-svg/expected.js b/test/js/samples/dont-use-dataset-in-svg/expected.js index b87f152298..9e541886bc 100644 --- a/test/js/samples/dont-use-dataset-in-svg/expected.js +++ b/test/js/samples/dont-use-dataset-in-svg/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { appendNode, assign, createSvgElement, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var svg, g, g_1; return { @@ -41,7 +41,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js index e35826e354..f90627f266 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -201,7 +201,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var text, p, text_1; var comments = state.comments; @@ -209,7 +209,10 @@ function create_main_fragment(state, component) { var each_blocks = []; for (var i = 0; i < comments.length; i += 1) { - each_blocks[i] = create_each_block(state, comments, comments[i], i, component); + each_blocks[i] = create_each_block(component, assign({}, state, { + comment: comments[i], + i: i + })); } return { @@ -238,10 +241,15 @@ function create_main_fragment(state, component) { if (changed.comments || changed.elapsed || changed.time) { for (var i = 0; i < comments.length; i += 1) { + var each_context = assign({}, state, { + comment: comments[i], + i: i + }); + if (each_blocks[i]) { - each_blocks[i].p(changed, state, comments, comments[i], i); + each_blocks[i].p(changed, each_context); } else { - each_blocks[i] = create_each_block(state, comments, comments[i], i, component); + each_blocks[i] = create_each_block(component, each_context); each_blocks[i].c(); each_blocks[i].m(text.parentNode, text); } @@ -275,14 +283,14 @@ function create_main_fragment(state, component) { } // (1:0) {{#each comments as comment, i}} -function create_each_block(state, comments, comment, i, component) { - var div, strong, text, text_1, span, text_2_value = comment.author, text_2, text_3, text_4_value = state.elapsed(comment.time, state.time), text_4, text_5, text_6, raw_value = comment.html, raw_before; +function create_each_block(component, state) { + var div, strong, text, text_1, span, text_2_value = state.comment.author, text_2, text_3, text_4_value = state.elapsed(state.comment.time, state.time), text_4, text_5, text_6, raw_value = state.comment.html, raw_before; return { c: function create() { div = createElement("div"); strong = createElement("strong"); - text = createText(i); + text = createText(state.i); text_1 = createText("\n\n\t\t"); span = createElement("span"); text_2 = createText(text_2_value); @@ -314,16 +322,16 @@ function create_each_block(state, comments, comment, i, component) { raw_before.insertAdjacentHTML("afterend", raw_value); }, - p: function update(changed, state, comments, comment, i) { - if ((changed.comments) && text_2_value !== (text_2_value = comment.author)) { + p: function update(changed, state) { + if ((changed.comments) && text_2_value !== (text_2_value = state.comment.author)) { text_2.data = text_2_value; } - if ((changed.elapsed || changed.comments || changed.time) && text_4_value !== (text_4_value = state.elapsed(comment.time, state.time))) { + if ((changed.elapsed || changed.comments || changed.time) && text_4_value !== (text_4_value = state.elapsed(state.comment.time, state.time))) { text_4.data = text_4_value; } - if ((changed.comments) && raw_value !== (raw_value = comment.html)) { + if ((changed.comments) && raw_value !== (raw_value = state.comment.html)) { detachAfter(raw_before); raw_before.insertAdjacentHTML("afterend", raw_value); } @@ -343,7 +351,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index 29a57322e0..414fcb3f06 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { appendNode, assign, createElement, createText, destroyEach, detachAfter, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var text, p, text_1; var comments = state.comments; @@ -9,7 +9,10 @@ function create_main_fragment(state, component) { var each_blocks = []; for (var i = 0; i < comments.length; i += 1) { - each_blocks[i] = create_each_block(state, comments, comments[i], i, component); + each_blocks[i] = create_each_block(component, assign({}, state, { + comment: comments[i], + i: i + })); } return { @@ -38,10 +41,15 @@ function create_main_fragment(state, component) { if (changed.comments || changed.elapsed || changed.time) { for (var i = 0; i < comments.length; i += 1) { + var each_context = assign({}, state, { + comment: comments[i], + i: i + }); + if (each_blocks[i]) { - each_blocks[i].p(changed, state, comments, comments[i], i); + each_blocks[i].p(changed, each_context); } else { - each_blocks[i] = create_each_block(state, comments, comments[i], i, component); + each_blocks[i] = create_each_block(component, each_context); each_blocks[i].c(); each_blocks[i].m(text.parentNode, text); } @@ -75,14 +83,14 @@ function create_main_fragment(state, component) { } // (1:0) {{#each comments as comment, i}} -function create_each_block(state, comments, comment, i, component) { - var div, strong, text, text_1, span, text_2_value = comment.author, text_2, text_3, text_4_value = state.elapsed(comment.time, state.time), text_4, text_5, text_6, raw_value = comment.html, raw_before; +function create_each_block(component, state) { + var div, strong, text, text_1, span, text_2_value = state.comment.author, text_2, text_3, text_4_value = state.elapsed(state.comment.time, state.time), text_4, text_5, text_6, raw_value = state.comment.html, raw_before; return { c: function create() { div = createElement("div"); strong = createElement("strong"); - text = createText(i); + text = createText(state.i); text_1 = createText("\n\n\t\t"); span = createElement("span"); text_2 = createText(text_2_value); @@ -114,16 +122,16 @@ function create_each_block(state, comments, comment, i, component) { raw_before.insertAdjacentHTML("afterend", raw_value); }, - p: function update(changed, state, comments, comment, i) { - if ((changed.comments) && text_2_value !== (text_2_value = comment.author)) { + p: function update(changed, state) { + if ((changed.comments) && text_2_value !== (text_2_value = state.comment.author)) { text_2.data = text_2_value; } - if ((changed.elapsed || changed.comments || changed.time) && text_4_value !== (text_4_value = state.elapsed(comment.time, state.time))) { + if ((changed.elapsed || changed.comments || changed.time) && text_4_value !== (text_4_value = state.elapsed(state.comment.time, state.time))) { text_4.data = text_4_value; } - if ((changed.comments) && raw_value !== (raw_value = comment.html)) { + if ((changed.comments) && raw_value !== (raw_value = state.comment.html)) { detachAfter(raw_before); raw_before.insertAdjacentHTML("afterend", raw_value); } @@ -143,7 +151,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/event-handlers-custom/expected-bundle.js b/test/js/samples/event-handlers-custom/expected-bundle.js index 43db3035a3..252257d9e2 100644 --- a/test/js/samples/event-handlers-custom/expected-bundle.js +++ b/test/js/samples/event-handlers-custom/expected-bundle.js @@ -191,7 +191,7 @@ var methods = { } }; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var button, foo_handler; return { @@ -228,7 +228,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/event-handlers-custom/expected.js b/test/js/samples/event-handlers-custom/expected.js index f7862efb62..785fefbeef 100644 --- a/test/js/samples/event-handlers-custom/expected.js +++ b/test/js/samples/event-handlers-custom/expected.js @@ -11,7 +11,7 @@ var methods = { } }; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var button, foo_handler; return { @@ -48,7 +48,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/head-no-whitespace/expected-bundle.js b/test/js/samples/head-no-whitespace/expected-bundle.js index 91f894bc30..60a9e85bf0 100644 --- a/test/js/samples/head-no-whitespace/expected-bundle.js +++ b/test/js/samples/head-no-whitespace/expected-bundle.js @@ -181,7 +181,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var meta, meta_1; return { @@ -218,7 +218,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/head-no-whitespace/expected.js b/test/js/samples/head-no-whitespace/expected.js index 32a1a5da88..9ba161c69d 100644 --- a/test/js/samples/head-no-whitespace/expected.js +++ b/test/js/samples/head-no-whitespace/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { appendNode, assign, createElement, detachNode, init, noop, proto } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var meta, meta_1; return { @@ -38,7 +38,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/if-block-no-update/expected-bundle.js b/test/js/samples/if-block-no-update/expected-bundle.js index b309a25e96..6caf1e5853 100644 --- a/test/js/samples/if-block-no-update/expected-bundle.js +++ b/test/js/samples/if-block-no-update/expected-bundle.js @@ -185,11 +185,11 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var if_block_anchor; var current_block_type = select_block_type(state); - var if_block = current_block_type(state, component); + var if_block = current_block_type(component, state); return { c: function create() { @@ -206,7 +206,7 @@ function create_main_fragment(state, component) { if (current_block_type !== (current_block_type = select_block_type(state))) { if_block.u(); if_block.d(); - if_block = current_block_type(state, component); + if_block = current_block_type(component, state); if_block.c(); if_block.m(if_block_anchor.parentNode, if_block_anchor); } @@ -224,7 +224,7 @@ function create_main_fragment(state, component) { } // (1:0) {{#if foo}} -function create_if_block(state, component) { +function create_if_block(component, state) { var p; return { @@ -246,7 +246,7 @@ function create_if_block(state, component) { } // (3:0) {{else}} -function create_if_block_1(state, component) { +function create_if_block_1(component, state) { var p; return { @@ -276,7 +276,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/if-block-no-update/expected.js b/test/js/samples/if-block-no-update/expected.js index 8f030e4e53..a6e7f09d8d 100644 --- a/test/js/samples/if-block-no-update/expected.js +++ b/test/js/samples/if-block-no-update/expected.js @@ -1,11 +1,11 @@ /* generated by Svelte vX.Y.Z */ import { assign, createComment, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var if_block_anchor; var current_block_type = select_block_type(state); - var if_block = current_block_type(state, component); + var if_block = current_block_type(component, state); return { c: function create() { @@ -22,7 +22,7 @@ function create_main_fragment(state, component) { if (current_block_type !== (current_block_type = select_block_type(state))) { if_block.u(); if_block.d(); - if_block = current_block_type(state, component); + if_block = current_block_type(component, state); if_block.c(); if_block.m(if_block_anchor.parentNode, if_block_anchor); } @@ -40,7 +40,7 @@ function create_main_fragment(state, component) { } // (1:0) {{#if foo}} -function create_if_block(state, component) { +function create_if_block(component, state) { var p; return { @@ -62,7 +62,7 @@ function create_if_block(state, component) { } // (3:0) {{else}} -function create_if_block_1(state, component) { +function create_if_block_1(component, state) { var p; return { @@ -92,7 +92,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/if-block-simple/expected-bundle.js b/test/js/samples/if-block-simple/expected-bundle.js index 5084639f4b..78b31ecb3d 100644 --- a/test/js/samples/if-block-simple/expected-bundle.js +++ b/test/js/samples/if-block-simple/expected-bundle.js @@ -185,10 +185,10 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var if_block_anchor; - var if_block = (state.foo) && create_if_block(state, component); + var if_block = (state.foo) && create_if_block(component, state); return { c: function create() { @@ -204,7 +204,7 @@ function create_main_fragment(state, component) { p: function update(changed, state) { if (state.foo) { if (!if_block) { - if_block = create_if_block(state, component); + if_block = create_if_block(component, state); if_block.c(); if_block.m(if_block_anchor.parentNode, if_block_anchor); } @@ -227,7 +227,7 @@ function create_main_fragment(state, component) { } // (1:0) {{#if foo}} -function create_if_block(state, component) { +function create_if_block(component, state) { var p; return { @@ -252,7 +252,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/if-block-simple/expected.js b/test/js/samples/if-block-simple/expected.js index 0b9fbece70..4c999546c0 100644 --- a/test/js/samples/if-block-simple/expected.js +++ b/test/js/samples/if-block-simple/expected.js @@ -1,10 +1,10 @@ /* generated by Svelte vX.Y.Z */ import { assign, createComment, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var if_block_anchor; - var if_block = (state.foo) && create_if_block(state, component); + var if_block = (state.foo) && create_if_block(component, state); return { c: function create() { @@ -20,7 +20,7 @@ function create_main_fragment(state, component) { p: function update(changed, state) { if (state.foo) { if (!if_block) { - if_block = create_if_block(state, component); + if_block = create_if_block(component, state); if_block.c(); if_block.m(if_block_anchor.parentNode, if_block_anchor); } @@ -43,7 +43,7 @@ function create_main_fragment(state, component) { } // (1:0) {{#if foo}} -function create_if_block(state, component) { +function create_if_block(component, state) { var p; return { @@ -68,7 +68,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js index 753d61db71..b40b46e4c5 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js @@ -185,7 +185,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div; return { @@ -225,7 +225,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/inline-style-optimized-multiple/expected.js b/test/js/samples/inline-style-optimized-multiple/expected.js index 77173460e2..ce3ffda243 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected.js +++ b/test/js/samples/inline-style-optimized-multiple/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div; return { @@ -41,7 +41,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/inline-style-optimized-url/expected-bundle.js b/test/js/samples/inline-style-optimized-url/expected-bundle.js index cf360881b1..ce83c0473c 100644 --- a/test/js/samples/inline-style-optimized-url/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-url/expected-bundle.js @@ -185,7 +185,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div; return { @@ -220,7 +220,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/inline-style-optimized-url/expected.js b/test/js/samples/inline-style-optimized-url/expected.js index bed00356f6..376cad092a 100644 --- a/test/js/samples/inline-style-optimized-url/expected.js +++ b/test/js/samples/inline-style-optimized-url/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div; return { @@ -36,7 +36,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/inline-style-optimized/expected-bundle.js b/test/js/samples/inline-style-optimized/expected-bundle.js index 38024df4ff..60d3574d4f 100644 --- a/test/js/samples/inline-style-optimized/expected-bundle.js +++ b/test/js/samples/inline-style-optimized/expected-bundle.js @@ -185,7 +185,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div; return { @@ -220,7 +220,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/inline-style-optimized/expected.js b/test/js/samples/inline-style-optimized/expected.js index 7f873e296c..74c4b6f417 100644 --- a/test/js/samples/inline-style-optimized/expected.js +++ b/test/js/samples/inline-style-optimized/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div; return { @@ -36,7 +36,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/inline-style-unoptimized/expected-bundle.js b/test/js/samples/inline-style-unoptimized/expected-bundle.js index 320c733a43..368d9aeb15 100644 --- a/test/js/samples/inline-style-unoptimized/expected-bundle.js +++ b/test/js/samples/inline-style-unoptimized/expected-bundle.js @@ -185,7 +185,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div, text, div_1, div_1_style_value; return { @@ -231,7 +231,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/inline-style-unoptimized/expected.js b/test/js/samples/inline-style-unoptimized/expected.js index 4c4d38d2bf..04e1226f6d 100644 --- a/test/js/samples/inline-style-unoptimized/expected.js +++ b/test/js/samples/inline-style-unoptimized/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div, text, div_1, div_1_style_value; return { @@ -47,7 +47,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/input-without-blowback-guard/expected-bundle.js b/test/js/samples/input-without-blowback-guard/expected-bundle.js index eacaca85a6..dcb3d2e061 100644 --- a/test/js/samples/input-without-blowback-guard/expected-bundle.js +++ b/test/js/samples/input-without-blowback-guard/expected-bundle.js @@ -189,7 +189,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var input; function input_change_handler() { @@ -231,7 +231,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/input-without-blowback-guard/expected.js b/test/js/samples/input-without-blowback-guard/expected.js index 03f27ab6d9..d313c7c01d 100644 --- a/test/js/samples/input-without-blowback-guard/expected.js +++ b/test/js/samples/input-without-blowback-guard/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { addListener, assign, createElement, detachNode, init, insertNode, proto, removeListener } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var input; function input_change_handler() { @@ -43,7 +43,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/legacy-input-type/expected-bundle.js b/test/js/samples/legacy-input-type/expected-bundle.js index 7b730f08d9..b0174e2105 100644 --- a/test/js/samples/legacy-input-type/expected-bundle.js +++ b/test/js/samples/legacy-input-type/expected-bundle.js @@ -187,7 +187,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var input; return { @@ -218,7 +218,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/legacy-input-type/expected.js b/test/js/samples/legacy-input-type/expected.js index 719f550044..5236b758dc 100644 --- a/test/js/samples/legacy-input-type/expected.js +++ b/test/js/samples/legacy-input-type/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { assign, createElement, detachNode, init, insertNode, noop, proto, setInputType } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var input; return { @@ -32,7 +32,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/legacy-quote-class/expected-bundle.js b/test/js/samples/legacy-quote-class/expected-bundle.js index 499a82c46e..3956acd874 100644 --- a/test/js/samples/legacy-quote-class/expected-bundle.js +++ b/test/js/samples/legacy-quote-class/expected-bundle.js @@ -204,7 +204,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div; return { @@ -243,7 +243,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { var nodes = children(options.target); diff --git a/test/js/samples/legacy-quote-class/expected.js b/test/js/samples/legacy-quote-class/expected.js index 0b49103c47..76490fd6f9 100644 --- a/test/js/samples/legacy-quote-class/expected.js +++ b/test/js/samples/legacy-quote-class/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { assign, children, claimElement, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div; return { @@ -40,7 +40,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { var nodes = children(options.target); diff --git a/test/js/samples/media-bindings/expected-bundle.js b/test/js/samples/media-bindings/expected-bundle.js index bbd56d8123..75e852e835 100644 --- a/test/js/samples/media-bindings/expected-bundle.js +++ b/test/js/samples/media-bindings/expected-bundle.js @@ -197,7 +197,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var audio, audio_is_paused = true, audio_updating = false, audio_animationframe; function audio_timeupdate_handler() { @@ -289,7 +289,7 @@ function SvelteComponent(options) { this._beforecreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/media-bindings/expected.js b/test/js/samples/media-bindings/expected.js index ae80f288f6..527e233cfd 100644 --- a/test/js/samples/media-bindings/expected.js +++ b/test/js/samples/media-bindings/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { addListener, assign, callAll, createElement, detachNode, init, insertNode, proto, removeListener, timeRangesToArray } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var audio, audio_is_paused = true, audio_updating = false, audio_animationframe; function audio_timeupdate_handler() { @@ -93,7 +93,7 @@ function SvelteComponent(options) { this._beforecreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/non-imported-component/expected-bundle.js b/test/js/samples/non-imported-component/expected-bundle.js index f8e0722db7..1b85f2f9d9 100644 --- a/test/js/samples/non-imported-component/expected-bundle.js +++ b/test/js/samples/non-imported-component/expected-bundle.js @@ -183,7 +183,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var text; var imported = new Imported({ @@ -232,7 +232,7 @@ function SvelteComponent(options) { this._aftercreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/non-imported-component/expected.js b/test/js/samples/non-imported-component/expected.js index e7fc2c7255..089e8db6e5 100644 --- a/test/js/samples/non-imported-component/expected.js +++ b/test/js/samples/non-imported-component/expected.js @@ -4,7 +4,7 @@ import Imported from 'Imported.html'; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var text; var imported = new Imported({ @@ -53,7 +53,7 @@ function SvelteComponent(options) { this._aftercreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js index e53dd0de5e..9f59cc8a9a 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js @@ -173,7 +173,7 @@ function oncreate() {} function ondestroy() {} -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { return { c: noop, @@ -200,7 +200,7 @@ function SvelteComponent(options) { this._oncreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); this.root._oncreate.push(_oncreate); diff --git a/test/js/samples/onrender-onteardown-rewritten/expected.js b/test/js/samples/onrender-onteardown-rewritten/expected.js index 51763c0b00..83e8d6200f 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected.js @@ -5,7 +5,7 @@ function oncreate() {}; function ondestroy() {}; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { return { c: noop, @@ -32,7 +32,7 @@ function SvelteComponent(options) { this._oncreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); this.root._oncreate.push(_oncreate); diff --git a/test/js/samples/setup-method/expected-bundle.js b/test/js/samples/setup-method/expected-bundle.js index 58ee3a67a0..988103bc32 100644 --- a/test/js/samples/setup-method/expected-bundle.js +++ b/test/js/samples/setup-method/expected-bundle.js @@ -185,7 +185,7 @@ function setup(Component) { Component.prototype.foo( 'baz' ); } -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { return { c: noop, @@ -204,7 +204,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/setup-method/expected.js b/test/js/samples/setup-method/expected.js index 092a32ed3b..e4778f5826 100644 --- a/test/js/samples/setup-method/expected.js +++ b/test/js/samples/setup-method/expected.js @@ -17,7 +17,7 @@ function setup(Component) { Component.prototype.foo( 'baz' ); } -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { return { c: noop, @@ -36,7 +36,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/svg-title/expected-bundle.js b/test/js/samples/svg-title/expected-bundle.js index 12f06bcf92..2ded30910d 100644 --- a/test/js/samples/svg-title/expected-bundle.js +++ b/test/js/samples/svg-title/expected-bundle.js @@ -189,7 +189,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var svg, title, text; return { @@ -219,7 +219,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/svg-title/expected.js b/test/js/samples/svg-title/expected.js index 39ccf227c0..059d1d804d 100644 --- a/test/js/samples/svg-title/expected.js +++ b/test/js/samples/svg-title/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { appendNode, assign, createSvgElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var svg, title, text; return { @@ -31,7 +31,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/title/expected-bundle.js b/test/js/samples/title/expected-bundle.js index 6eb3471b5b..f52f89a71a 100644 --- a/test/js/samples/title/expected-bundle.js +++ b/test/js/samples/title/expected-bundle.js @@ -169,7 +169,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var title_value; document.title = title_value = "a " + state.custom + " title"; @@ -195,7 +195,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/title/expected.js b/test/js/samples/title/expected.js index 6aa903d25e..d5546cd4c2 100644 --- a/test/js/samples/title/expected.js +++ b/test/js/samples/title/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { assign, init, noop, proto } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var title_value; document.title = title_value = "a " + state.custom + " title"; @@ -27,7 +27,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/use-elements-as-anchors/expected-bundle.js b/test/js/samples/use-elements-as-anchors/expected-bundle.js index e2ee1e61fb..6ece6bffa7 100644 --- a/test/js/samples/use-elements-as-anchors/expected-bundle.js +++ b/test/js/samples/use-elements-as-anchors/expected-bundle.js @@ -193,18 +193,18 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div, text, p, text_2, text_3, text_4, p_1, text_6, text_8, if_block_4_anchor; - var if_block = (state.a) && create_if_block(state, component); + var if_block = (state.a) && create_if_block(component, state); - var if_block_1 = (state.b) && create_if_block_1(state, component); + var if_block_1 = (state.b) && create_if_block_1(component, state); - var if_block_2 = (state.c) && create_if_block_2(state, component); + var if_block_2 = (state.c) && create_if_block_2(component, state); - var if_block_3 = (state.d) && create_if_block_3(state, component); + var if_block_3 = (state.d) && create_if_block_3(component, state); - var if_block_4 = (state.e) && create_if_block_4(state, component); + var if_block_4 = (state.e) && create_if_block_4(component, state); return { c: function create() { @@ -248,7 +248,7 @@ function create_main_fragment(state, component) { p: function update(changed, state) { if (state.a) { if (!if_block) { - if_block = create_if_block(state, component); + if_block = create_if_block(component, state); if_block.c(); if_block.m(div, text); } @@ -260,7 +260,7 @@ function create_main_fragment(state, component) { if (state.b) { if (!if_block_1) { - if_block_1 = create_if_block_1(state, component); + if_block_1 = create_if_block_1(component, state); if_block_1.c(); if_block_1.m(div, text_3); } @@ -272,7 +272,7 @@ function create_main_fragment(state, component) { if (state.c) { if (!if_block_2) { - if_block_2 = create_if_block_2(state, component); + if_block_2 = create_if_block_2(component, state); if_block_2.c(); if_block_2.m(div, text_4); } @@ -284,7 +284,7 @@ function create_main_fragment(state, component) { if (state.d) { if (!if_block_3) { - if_block_3 = create_if_block_3(state, component); + if_block_3 = create_if_block_3(component, state); if_block_3.c(); if_block_3.m(div, null); } @@ -296,7 +296,7 @@ function create_main_fragment(state, component) { if (state.e) { if (!if_block_4) { - if_block_4 = create_if_block_4(state, component); + if_block_4 = create_if_block_4(component, state); if_block_4.c(); if_block_4.m(if_block_4_anchor.parentNode, if_block_4_anchor); } @@ -329,7 +329,7 @@ function create_main_fragment(state, component) { } // (2:1) {{#if a}} -function create_if_block(state, component) { +function create_if_block(component, state) { var p; return { @@ -351,7 +351,7 @@ function create_if_block(state, component) { } // (8:1) {{#if b}} -function create_if_block_1(state, component) { +function create_if_block_1(component, state) { var p; return { @@ -373,7 +373,7 @@ function create_if_block_1(state, component) { } // (12:1) {{#if c}} -function create_if_block_2(state, component) { +function create_if_block_2(component, state) { var p; return { @@ -395,7 +395,7 @@ function create_if_block_2(state, component) { } // (18:1) {{#if d}} -function create_if_block_3(state, component) { +function create_if_block_3(component, state) { var p; return { @@ -417,7 +417,7 @@ function create_if_block_3(state, component) { } // (25:0) {{#if e}} -function create_if_block_4(state, component) { +function create_if_block_4(component, state) { var p; return { @@ -442,7 +442,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/use-elements-as-anchors/expected.js b/test/js/samples/use-elements-as-anchors/expected.js index d2609c45b0..0ff85d63b0 100644 --- a/test/js/samples/use-elements-as-anchors/expected.js +++ b/test/js/samples/use-elements-as-anchors/expected.js @@ -1,18 +1,18 @@ /* generated by Svelte vX.Y.Z */ import { appendNode, assign, createComment, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var div, text, p, text_2, text_3, text_4, p_1, text_6, text_8, if_block_4_anchor; - var if_block = (state.a) && create_if_block(state, component); + var if_block = (state.a) && create_if_block(component, state); - var if_block_1 = (state.b) && create_if_block_1(state, component); + var if_block_1 = (state.b) && create_if_block_1(component, state); - var if_block_2 = (state.c) && create_if_block_2(state, component); + var if_block_2 = (state.c) && create_if_block_2(component, state); - var if_block_3 = (state.d) && create_if_block_3(state, component); + var if_block_3 = (state.d) && create_if_block_3(component, state); - var if_block_4 = (state.e) && create_if_block_4(state, component); + var if_block_4 = (state.e) && create_if_block_4(component, state); return { c: function create() { @@ -56,7 +56,7 @@ function create_main_fragment(state, component) { p: function update(changed, state) { if (state.a) { if (!if_block) { - if_block = create_if_block(state, component); + if_block = create_if_block(component, state); if_block.c(); if_block.m(div, text); } @@ -68,7 +68,7 @@ function create_main_fragment(state, component) { if (state.b) { if (!if_block_1) { - if_block_1 = create_if_block_1(state, component); + if_block_1 = create_if_block_1(component, state); if_block_1.c(); if_block_1.m(div, text_3); } @@ -80,7 +80,7 @@ function create_main_fragment(state, component) { if (state.c) { if (!if_block_2) { - if_block_2 = create_if_block_2(state, component); + if_block_2 = create_if_block_2(component, state); if_block_2.c(); if_block_2.m(div, text_4); } @@ -92,7 +92,7 @@ function create_main_fragment(state, component) { if (state.d) { if (!if_block_3) { - if_block_3 = create_if_block_3(state, component); + if_block_3 = create_if_block_3(component, state); if_block_3.c(); if_block_3.m(div, null); } @@ -104,7 +104,7 @@ function create_main_fragment(state, component) { if (state.e) { if (!if_block_4) { - if_block_4 = create_if_block_4(state, component); + if_block_4 = create_if_block_4(component, state); if_block_4.c(); if_block_4.m(if_block_4_anchor.parentNode, if_block_4_anchor); } @@ -137,7 +137,7 @@ function create_main_fragment(state, component) { } // (2:1) {{#if a}} -function create_if_block(state, component) { +function create_if_block(component, state) { var p; return { @@ -159,7 +159,7 @@ function create_if_block(state, component) { } // (8:1) {{#if b}} -function create_if_block_1(state, component) { +function create_if_block_1(component, state) { var p; return { @@ -181,7 +181,7 @@ function create_if_block_1(state, component) { } // (12:1) {{#if c}} -function create_if_block_2(state, component) { +function create_if_block_2(component, state) { var p; return { @@ -203,7 +203,7 @@ function create_if_block_2(state, component) { } // (18:1) {{#if d}} -function create_if_block_3(state, component) { +function create_if_block_3(component, state) { var p; return { @@ -225,7 +225,7 @@ function create_if_block_3(state, component) { } // (25:0) {{#if e}} -function create_if_block_4(state, component) { +function create_if_block_4(component, state) { var p; return { @@ -250,7 +250,7 @@ function SvelteComponent(options) { init(this, options); this._state = assign({}, options.data); - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/window-binding-scroll/expected-bundle.js b/test/js/samples/window-binding-scroll/expected-bundle.js index a8a932c8b7..b310a6a839 100644 --- a/test/js/samples/window-binding-scroll/expected-bundle.js +++ b/test/js/samples/window-binding-scroll/expected-bundle.js @@ -189,7 +189,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var window_updating = false, clear_window_updating = function() { window_updating = false; }, window_updating_timeout, p, text, text_1; function onwindowscroll(event) { @@ -244,7 +244,7 @@ function SvelteComponent(options) { this._state = assign({}, options.data); this._state.y = window.scrollY; - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/window-binding-scroll/expected.js b/test/js/samples/window-binding-scroll/expected.js index 70eae003d7..aa265f4599 100644 --- a/test/js/samples/window-binding-scroll/expected.js +++ b/test/js/samples/window-binding-scroll/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { appendNode, assign, createElement, createText, detachNode, init, insertNode, proto } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var window_updating = false, clear_window_updating = function() { window_updating = false; }, window_updating_timeout, p, text, text_1; function onwindowscroll(event) { @@ -56,7 +56,7 @@ function SvelteComponent(options) { this._state = assign({}, options.data); this._state.y = window.scrollY; - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); From a0aeb98685ff820b60191730f149d7550a6da0cf Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 10:00:23 -0500 Subject: [PATCH 04/28] various fixes --- src/generators/dom/Block.ts | 2 +- src/generators/nodes/AwaitBlock.ts | 16 ++++++++-------- src/generators/nodes/Component.ts | 4 ++-- src/generators/nodes/EachBlock.ts | 25 ++++++++++++++++--------- src/generators/nodes/Element.ts | 4 ++-- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/generators/dom/Block.ts b/src/generators/dom/Block.ts index 2a50b3cfdb..15ee602ee2 100644 --- a/src/generators/dom/Block.ts +++ b/src/generators/dom/Block.ts @@ -326,7 +326,7 @@ export default class Block { return deindent` ${this.comment && `// ${escape(this.comment)}`} - function ${this.name}(#component, state${this.key ? `, ${localKey}` : ''}) { + function ${this.name}(#component${this.key ? `, ${localKey}` : ''}, state) { ${this.variables.size > 0 && `var ${Array.from(this.variables.keys()) .map(key => { diff --git a/src/generators/nodes/AwaitBlock.ts b/src/generators/nodes/AwaitBlock.ts index c5cce65758..5fa04d2c74 100644 --- a/src/generators/nodes/AwaitBlock.ts +++ b/src/generators/nodes/AwaitBlock.ts @@ -35,7 +35,7 @@ export default class AwaitBlock extends Node { ].forEach(([status, arg]) => { const child = this[status]; - const context = block.getUniqueName(arg || '_'); // TODO can we remove the extra param from pending blocks? + const context = arg || '_'; const contexts = new Map(block.contexts); contexts.set(arg, context); @@ -103,11 +103,11 @@ export default class AwaitBlock extends Node { // but it's probably not worth it block.builders.init.addBlock(deindent` - function ${replace_await_block}(${token}, type, ${value}, state) { + function ${replace_await_block}(${token}, type, state) { if (${token} !== ${await_token}) return; var ${old_block} = ${await_block}; - ${await_block} = (${await_block_type} = type)(state, ${resolved} = ${value}, #component); + ${await_block} = (${await_block_type} = type)(#component, state); if (${old_block}) { ${old_block}.u(); @@ -125,21 +125,21 @@ export default class AwaitBlock extends Node { if (@isPromise(${promise})) { ${promise}.then(function(${value}) { var state = #component.get(); - ${replace_await_block}(${token}, ${create_then_block}, ${value}, state); + ${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, { ${this.then.block.context}: ${value} })); }, function (${error}) { var state = #component.get(); - ${replace_await_block}(${token}, ${create_catch_block}, ${error}, state); + ${replace_await_block}(${token}, ${create_catch_block}, @assign({}, state, { ${this.catch.block.context}: ${error} })); }); // if we previously had a then/catch block, destroy it if (${await_block_type} !== ${create_pending_block}) { - ${replace_await_block}(${token}, ${create_pending_block}, null, state); + ${replace_await_block}(${token}, ${create_pending_block}, state); return true; } } else { ${resolved} = ${promise}; if (${await_block_type} !== ${create_then_block}) { - ${replace_await_block}(${token}, ${create_then_block}, ${resolved}, state); + ${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, { ${this.then.block.context}: ${resolved} })); return true; } } @@ -182,7 +182,7 @@ export default class AwaitBlock extends Node { if (${conditions.join(' && ')}) { // nothing } else { - ${await_block}.p(changed, state, ${resolved}); + ${await_block}.p(changed, state); } `); } else { diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 83e88f2bda..8039cc2a26 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -417,7 +417,7 @@ export default class Component extends Node { const listName = block.listNames.get(contextName); const indexName = block.indexNames.get(contextName); - return `${listName}: ${listName},\n${indexName}: ${indexName}`; + return `${listName}: state.${listName},\n${indexName}: state.${indexName}`; }) .join(',\n'); @@ -428,7 +428,7 @@ export default class Component extends Node { const listName = block.listNames.get(contextName); const indexName = block.indexNames.get(contextName); - return `${name_context}.${listName} = ${listName};\n${name_context}.${indexName} = ${indexName};`; + return `${name_context}.${listName} = state.${listName};\n${name_context}.${indexName} = state.${indexName};`; }) .join('\n'); diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index 9a094affe4..5e6669254a 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -35,12 +35,11 @@ export default class EachBlock extends Node { block.addDependencies(dependencies); const indexNames = new Map(block.indexNames); - const indexName = - this.index || block.getUniqueName(`${this.context}_index`); + const indexName = this.index || `${this.context}_index`; indexNames.set(this.context, indexName); const listNames = new Map(block.listNames); - const listName = block.getUniqueName( + const listName = ( (this.expression.type === 'MemberExpression' && !this.expression.computed) ? this.expression.property.name : this.expression.type === 'Identifier' ? this.expression.name : `each_value` @@ -50,9 +49,9 @@ export default class EachBlock extends Node { const contextTypes = new Map(block.contextTypes); contextTypes.set(this.context, 'each'); - const context = block.getUniqueName(this.context); + const context = this.context; const contexts = new Map(block.contexts); - contexts.set(this.context, context); + contexts.set(this.context, context); // TODO this is now redundant const indexes = new Map(block.indexes); if (this.index) indexes.set(this.index, this.context); @@ -272,7 +271,10 @@ export default class EachBlock extends Node { block.builders.init.addBlock(deindent` for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) { var ${key} = ${each_block_value}[#i].${this.key}; - var ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, state, ${key}); + var ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, @assign({}, state, { + ${this.context}: ${each_block_value}[#i], + ${this.block.indexName}: #i + })); if (${last}) ${last}.next = ${iteration}; ${iteration}.last = ${last}; @@ -376,8 +378,13 @@ export default class EachBlock extends Node { var ${key} = ${each_block_value}[#i].${this.key}; var ${iteration} = ${lookup}[${key}]; + var ${this.each_context} = @assign({}, state, { + ${this.context}: ${each_block_value}[#i], + ${this.block.indexName}: #i + }); + ${dynamic && - `if (${iteration}) ${iteration}.p(changed, state);`} + `if (${iteration}) ${iteration}.p(changed, ${this.each_context});`} if (${expected}) { if (${key} === ${expected}.key) { @@ -398,7 +405,7 @@ export default class EachBlock extends Node { if (!${expected}) ${iteration}.m(${updateMountNode}, ${anchor}); } else { // key is being inserted - ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, state, ${key}); + ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, ${this.each_context}); ${iteration}.c(); ${iteration}.${mountOrIntro}(${updateMountNode}, ${expected}.first); @@ -413,7 +420,7 @@ export default class EachBlock extends Node { ${iteration}.next = null; ${iteration}.m(${updateMountNode}, ${anchor}); } else { - ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, state, ${key}); + ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, ${this.each_context}); ${iteration}.c(); ${iteration}.${mountOrIntro}(${updateMountNode}, ${anchor}); } diff --git a/src/generators/nodes/Element.ts b/src/generators/nodes/Element.ts index a512b21d94..13f8ccff5c 100644 --- a/src/generators/nodes/Element.ts +++ b/src/generators/nodes/Element.ts @@ -386,10 +386,10 @@ export default class Element extends Node { const indexName = block.indexNames.get(contextName); initialProps.push( - `${listName}: ${listName},\n${indexName}: ${indexName}` + `${listName}: state.${listName},\n${indexName}: state.${indexName}` ); updates.push( - `${name}._svelte.${listName} = ${listName};\n${name}._svelte.${indexName} = ${indexName};` + `${name}._svelte.${listName} = state.${listName};\n${name}._svelte.${indexName} = state.${indexName};` ); }); From c3a08788e66ec7076a7e138f5b0278283965b271 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 10:19:52 -0500 Subject: [PATCH 05/28] get destructuring working --- src/generators/nodes/EachBlock.ts | 24 ++++++++++++------- .../visitors/EachBlock.ts | 4 +++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index 5e6669254a..7133ce8eac 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -83,6 +83,18 @@ export default class EachBlock extends Node { listNames }); + this.contextProps = [ + `${context}: ${listName}[#i]`, + `${indexName}: #i` + ]; + + if (this.destructuredContexts) { + for (let i = 0; i < this.destructuredContexts.length; i += 1) { + contexts.set(this.destructuredContexts[i], `${context}[${i}]`); + this.contextProps.push(`${this.destructuredContexts[i]}: ${listName}[#i][${i}]`); + } + } + this.generator.blocks.push(this.block); this.initChildren(this.block, stripWhitespace, nextSibling); block.addDependencies(this.block.dependencies); @@ -272,8 +284,7 @@ export default class EachBlock extends Node { for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) { var ${key} = ${each_block_value}[#i].${this.key}; var ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, @assign({}, state, { - ${this.context}: ${each_block_value}[#i], - ${this.block.indexName}: #i + ${this.contextProps.join(',\n')} })); if (${last}) ${last}.next = ${iteration}; @@ -379,8 +390,7 @@ export default class EachBlock extends Node { var ${iteration} = ${lookup}[${key}]; var ${this.each_context} = @assign({}, state, { - ${this.context}: ${each_block_value}[#i], - ${this.block.indexName}: #i + ${this.contextProps.join(',\n')} }); ${dynamic && @@ -477,8 +487,7 @@ export default class EachBlock extends Node { for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) { ${iterations}[#i] = ${create_each_block}(#component, @assign({}, state, { - ${this.context}: ${each_block_value}[#i], - ${this.block.indexName}: #i + ${this.contextProps.join(',\n')} })); } `); @@ -576,8 +585,7 @@ export default class EachBlock extends Node { if (${condition}) { for (var #i = ${start}; #i < ${each_block_value}.${length}; #i += 1) { var ${this.each_context} = @assign({}, state, { - ${this.context}: ${each_block_value}[#i], - ${this.block.indexName}: #i + ${this.contextProps.join(',\n')} }); ${forLoopBody} diff --git a/src/generators/server-side-rendering/visitors/EachBlock.ts b/src/generators/server-side-rendering/visitors/EachBlock.ts index 59bd5fed0b..9a5ec300ca 100644 --- a/src/generators/server-side-rendering/visitors/EachBlock.ts +++ b/src/generators/server-side-rendering/visitors/EachBlock.ts @@ -11,7 +11,9 @@ export default function visitEachBlock( block.contextualise(node.expression); const { dependencies, snippet } = node.metadata; - const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}${snippet}.map(${node.index ? `(${node.context}, ${node.index})` : node.context} => \``; + const context = node.destructuredContexts ? `[${node.destructuredContexts.join(', ')}]` : node.context; + + const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}${snippet}.map(${node.index ? `(${context}, ${node.index})` : `(${context})`} => \``; generator.append(open); // TODO should this be the generator's job? It's duplicated between From fcae19b5d834a7d3b6631172a9d46304217f611c Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 10:32:02 -0500 Subject: [PATCH 06/28] fix --- src/generators/nodes/Component.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 8039cc2a26..661bfd7d15 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -527,8 +527,8 @@ function mungeBinding(binding: Node, block: Block): Binding { let prop; if (contextual) { - obj = block.listNames.get(name); - prop = block.indexNames.get(name); + obj = `state.${block.listNames.get(name)}`; + prop = `state.${block.indexNames.get(name)}`; } else if (binding.value.type === 'MemberExpression') { prop = `[✂${binding.value.property.start}-${binding.value.property.end}✂]`; if (!binding.value.computed) prop = `'${prop}'`; From 242f08e328ce705d01ec908a8ea89bf1944740df Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 11:36:03 -0500 Subject: [PATCH 07/28] temporary hack around context issue --- src/generators/nodes/Component.ts | 82 ++++++++++++++++--------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 1fc249e960..c3214578a7 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -93,7 +93,7 @@ export default class Component extends Node { const bindings = this.attributes .filter(a => a.type === 'Binding') - .map(a => mungeBinding(a, block)); + .map(a => mungeBinding(a, block, name_context)); const eventHandlers = this.attributes .filter((a: Node) => a.type === 'EventHandler') @@ -220,8 +220,10 @@ export default class Component extends Node { componentInitProperties.push(`data: ${name_initial_data}`); + block.addVariable('__state__TODO', 'state'); + block.builders.update.addLine(`__state__TODO = state`); const initialisers = [ - 'state = #component.get()', + 'state = __state__TODO', hasLocalBindings && 'newState = {}', hasStoreBindings && 'newStoreState = {}', ].filter(Boolean).join(', '); @@ -246,6 +248,41 @@ export default class Component extends Node { } } + // maintain component context + if (allContexts.size) { + const contexts = Array.from(allContexts); + + const initialProps = contexts + .map(contextName => { + if (contextName === 'state') return `state: state`; + + const listName = block.listNames.get(contextName); + const indexName = block.indexNames.get(contextName); + + return `${listName}: state.${listName},\n${indexName}: state.${indexName}`; + }) + .join(',\n'); + + const updates = contexts + .map(contextName => { + if (contextName === 'state') return `${name_context}.state = state;`; + + const listName = block.listNames.get(contextName); + const indexName = block.indexNames.get(contextName); + + return `${name_context}.${listName} = state.${listName};\n${name_context}.${indexName} = state.${indexName};`; + }) + .join('\n'); + + block.builders.init.addBlock(deindent` + var ${name_context} = { + ${initialProps} + }; + `); + + block.builders.update.addBlock(updates); + } + const isDynamicComponent = this.name === ':Component'; const switch_vars = isDynamicComponent && { @@ -391,41 +428,6 @@ export default class Component extends Node { ${ref && `if (#component.refs.${ref.name} === ${name}) #component.refs.${ref.name} = null;`} `); } - - // maintain component context - if (allContexts.size) { - const contexts = Array.from(allContexts); - - const initialProps = contexts - .map(contextName => { - if (contextName === 'state') return `state: state`; - - const listName = block.listNames.get(contextName); - const indexName = block.indexNames.get(contextName); - - return `${listName}: state.${listName},\n${indexName}: state.${indexName}`; - }) - .join(',\n'); - - const updates = contexts - .map(contextName => { - if (contextName === 'state') return `${name_context}.state = state;`; - - const listName = block.listNames.get(contextName); - const indexName = block.indexNames.get(contextName); - - return `${name_context}.${listName} = state.${listName};\n${name_context}.${indexName} = state.${indexName};`; - }) - .join('\n'); - - block.builders.init.addBlock(deindent` - var ${name_context} = { - ${initialProps} - }; - `); - - block.builders.update.addBlock(updates); - } } } @@ -502,7 +504,7 @@ function mungeAttribute(attribute: Node, block: Block): Attribute { }; } -function mungeBinding(binding: Node, block: Block): Binding { +function mungeBinding(binding: Node, block: Block, name_context: string): Binding { const { name } = getObject(binding.value); const { contexts } = block.contextualise(binding.value); const { dependencies, snippet } = binding.metadata; @@ -513,8 +515,8 @@ function mungeBinding(binding: Node, block: Block): Binding { let prop; if (contextual) { - obj = `state.${block.listNames.get(name)}`; - prop = `state.${block.indexNames.get(name)}`; + obj = `${name_context}.${block.listNames.get(name)}`; + prop = `${name_context}.${block.indexNames.get(name)}`; } else if (binding.value.type === 'MemberExpression') { prop = `[✂${binding.value.property.start}-${binding.value.property.end}✂]`; if (!binding.value.computed) prop = `'${prop}'`; From b94f63ed704427951c90fd18dddf40a4b9ce0308 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 15:02:25 -0500 Subject: [PATCH 08/28] everything working except some destructuring stuff --- src/generators/Generator.ts | 42 +++++----- src/generators/dom/Block.ts | 25 +++++- src/generators/nodes/AwaitBlock.ts | 12 +-- src/generators/nodes/EachBlock.ts | 80 ++++++++----------- .../expected-bundle.js | 10 ++- .../each-block-changed-check/expected.js | 10 ++- 6 files changed, 100 insertions(+), 79 deletions(-) diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 5570fe0354..2463c33325 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -242,26 +242,30 @@ export default class Generator { if (name === 'event' && isEventHandler) { // noop } else if (contexts.has(name)) { - // const contextName = contexts.get(name); - // if (contextName !== name) { - // // this is true for 'reserved' names like `state` and `component`, - // // also destructured contexts - // code.overwrite( - // node.start, - // node.start + name.length, - // contextName, - // { storeName: true, contentOnly: false } - // ); - - // const destructuredName = contextName.replace(/\[\d+\]/, ''); - // if (destructuredName !== contextName) { - // // so that hoisting the context works correctly - // usedContexts.add(destructuredName); - // } + // if (self.constructor.name === 'DomGenerator') { // TODO filthy, temporary hack + const contextName = contexts.get(name); + if (contextName !== name) { + // this is true for 'reserved' names like `state` and `component`, + // also destructured contexts + + code.overwrite( + node.start, + node.start + name.length, + contextName, + { storeName: true, contentOnly: false } + ); + + const destructuredName = contextName.replace(/\[\d+\]/, ''); + if (destructuredName !== contextName) { + // so that hoisting the context works correctly + usedContexts.add(destructuredName); + } + } + + // TODO filthy, temporary hack + // if (!isEventHandler) code.prependRight(node.start, `state.`); // } - // TODO filthy, temporary hack - if (self.constructor.name === 'DomGenerator') code.prependRight(node.start, `state.`); usedContexts.add(name); } else if (helpers.has(name)) { let object = node; @@ -270,7 +274,7 @@ export default class Generator { const alias = self.templateVars.get(`helpers-${name}`); if (alias !== name) code.overwrite(object.start, object.end, alias); } else if (indexes.has(name)) { - if (self.constructor.name === 'DomGenerator') code.prependRight(node.start, `state.`); + if (self.constructor.name === 'DomGenerator' && !isEventHandler) code.prependRight(node.start, `state.`); const context = indexes.get(name); usedContexts.add(context); // TODO is this right? diff --git a/src/generators/dom/Block.ts b/src/generators/dom/Block.ts index 15ee602ee2..e1ed62c242 100644 --- a/src/generators/dom/Block.ts +++ b/src/generators/dom/Block.ts @@ -114,7 +114,12 @@ export default class Block { this.aliases = new Map(); this.variables = new Map(); - this.getUniqueName = this.generator.getUniqueNameMaker([]); // TODO this is wrong... we probably don't need this any more + // this.getUniqueName = this.generator.getUniqueNameMaker([]); // TODO this is wrong... we probably don't need this any more + + const getUniqueName = this.generator.getUniqueNameMaker([]); // TODO this is wrong... we probably don't need this any more + this.getUniqueName = name => { + return getUniqueName(name); + } this.hasUpdateMethod = false; // determined later } @@ -189,6 +194,19 @@ export default class Block { this.builders.mount.addLine(`${this.autofocus}.focus();`); } + // TODO `this.contexts` is possibly redundant post-#1122 + const initializers = []; + const updaters = []; + this.contexts.forEach((alias, name) => { + // TODO only the ones that are actually used in this block... + const assignment = `${alias} = state.${name}`; + + initializers.push(assignment); + updaters.push(`${assignment};`); + + this.hasUpdateMethod = true; + }); + // minor hack – we need to ensure that any {{{triples}}} are detached first this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString()); @@ -248,11 +266,12 @@ export default class Block { } if (this.hasUpdateMethod) { - if (this.builders.update.isEmpty()) { + if (this.builders.update.isEmpty() && updaters.length === 0) { properties.addBlock(`p: @noop,`); } else { properties.addBlock(deindent` p: function update(changed, state) { + ${updaters} ${this.builders.update} }, `); @@ -327,6 +346,8 @@ export default class Block { return deindent` ${this.comment && `// ${escape(this.comment)}`} function ${this.name}(#component${this.key ? `, ${localKey}` : ''}, state) { + ${initializers.length > 0 && + `var ${initializers.join(', ')};`} ${this.variables.size > 0 && `var ${Array.from(this.variables.keys()) .map(key => { diff --git a/src/generators/nodes/AwaitBlock.ts b/src/generators/nodes/AwaitBlock.ts index 5fa04d2c74..168109614f 100644 --- a/src/generators/nodes/AwaitBlock.ts +++ b/src/generators/nodes/AwaitBlock.ts @@ -125,10 +125,12 @@ export default class AwaitBlock extends Node { if (@isPromise(${promise})) { ${promise}.then(function(${value}) { var state = #component.get(); - ${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, { ${this.then.block.context}: ${value} })); + ${resolved} = { ${this.then.block.context}: ${value} }; + ${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, ${resolved})); }, function (${error}) { var state = #component.get(); - ${replace_await_block}(${token}, ${create_catch_block}, @assign({}, state, { ${this.catch.block.context}: ${error} })); + ${resolved} = { ${this.catch.block.context}: ${error} }; + ${replace_await_block}(${token}, ${create_catch_block}, @assign({}, state, ${resolved})); }); // if we previously had a then/catch block, destroy it @@ -137,9 +139,9 @@ export default class AwaitBlock extends Node { return true; } } else { - ${resolved} = ${promise}; + ${resolved} = { ${this.then.block.context}: ${promise} }; if (${await_block_type} !== ${create_then_block}) { - ${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, { ${this.then.block.context}: ${resolved} })); + ${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, ${resolved})); return true; } } @@ -182,7 +184,7 @@ export default class AwaitBlock extends Node { if (${conditions.join(' && ')}) { // nothing } else { - ${await_block}.p(changed, state); + ${await_block}.p(changed, @assign({}, state, ${resolved})); } `); } else { diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index 7133ce8eac..4d3479d26d 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -34,67 +34,57 @@ export default class EachBlock extends Node { const { dependencies } = this.metadata; block.addDependencies(dependencies); - const indexNames = new Map(block.indexNames); - const indexName = this.index || `${this.context}_index`; - indexNames.set(this.context, indexName); - - const listNames = new Map(block.listNames); - const listName = ( - (this.expression.type === 'MemberExpression' && !this.expression.computed) ? this.expression.property.name : - this.expression.type === 'Identifier' ? this.expression.name : - `each_value` - ); - listNames.set(this.context, listName); - - const contextTypes = new Map(block.contextTypes); - contextTypes.set(this.context, 'each'); - - const context = this.context; - const contexts = new Map(block.contexts); - contexts.set(this.context, context); // TODO this is now redundant - - const indexes = new Map(block.indexes); - if (this.index) indexes.set(this.index, this.context); - - const changeableIndexes = new Map(block.changeableIndexes); - if (this.index) changeableIndexes.set(this.index, this.key); - - if (this.destructuredContexts) { - for (let i = 0; i < this.destructuredContexts.length; i += 1) { - contexts.set(this.destructuredContexts[i], `${context}[${i}]`); - } - } - this.block = block.child({ comment: createDebuggingComment(this, this.generator), name: this.generator.getUniqueName('create_each_block'), context: this.context, key: this.key, - contexts, - contextTypes, - indexes, - changeableIndexes, + contexts: new Map(block.contexts), + contextTypes: new Map(block.contextTypes), + indexes: new Map(block.indexes), + changeableIndexes: new Map(block.changeableIndexes), - listName, - indexName, + listName: ( + (this.expression.type === 'MemberExpression' && !this.expression.computed) ? this.expression.property.name : + this.expression.type === 'Identifier' ? this.expression.name : + `each_value` + ), + indexName: this.index || `${this.context}_index`, - indexNames, - listNames + indexNames: new Map(block.indexNames), + listNames: new Map(block.listNames) }); - this.contextProps = [ - `${context}: ${listName}[#i]`, - `${indexName}: #i` - ]; + this.block.contextTypes.set(this.context, 'each'); + this.block.indexNames.set(this.context, this.block.indexName); + this.block.listNames.set(this.context, this.block.listName); + if (this.index) { + this.block.indexes.set(this.index, this.context); + this.block.changeableIndexes.set(this.index, this.key) + } + + const context = this.block.getUniqueName(this.context); + this.block.contexts.set(this.context, context); // TODO this is now redundant? if (this.destructuredContexts) { for (let i = 0; i < this.destructuredContexts.length; i += 1) { - contexts.set(this.destructuredContexts[i], `${context}[${i}]`); - this.contextProps.push(`${this.destructuredContexts[i]}: ${listName}[#i][${i}]`); + this.block.contexts.set(this.destructuredContexts[i], `${context}[${i}]`); } } + this.contextProps = [ + `${this.context}: ${this.block.listName}[#i]`, + `${this.block.indexName}: #i` + ]; + + // if (this.destructuredContexts) { + // for (let i = 0; i < this.destructuredContexts.length; i += 1) { + // contexts.set(this.destructuredContexts[i], `${context}[${i}]`); + // this.contextProps.push(`${this.destructuredContexts[i]}: ${this.block.listName}[#i][${i}]`); + // } + // } + this.generator.blocks.push(this.block); this.initChildren(this.block, stripWhitespace, nextSibling); block.addDependencies(this.block.dependencies); diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js index f90627f266..866f90aa55 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -284,7 +284,8 @@ function create_main_fragment(component, state) { // (1:0) {{#each comments as comment, i}} function create_each_block(component, state) { - var div, strong, text, text_1, span, text_2_value = state.comment.author, text_2, text_3, text_4_value = state.elapsed(state.comment.time, state.time), text_4, text_5, text_6, raw_value = state.comment.html, raw_before; + var comment = state.comment; + var div, strong, text, text_1, span, text_2_value = comment.author, text_2, text_3, text_4_value = state.elapsed(comment.time, state.time), text_4, text_5, text_6, raw_value = comment.html, raw_before; return { c: function create() { @@ -323,15 +324,16 @@ function create_each_block(component, state) { }, p: function update(changed, state) { - if ((changed.comments) && text_2_value !== (text_2_value = state.comment.author)) { + comment = state.comment; + if ((changed.comments) && text_2_value !== (text_2_value = comment.author)) { text_2.data = text_2_value; } - if ((changed.elapsed || changed.comments || changed.time) && text_4_value !== (text_4_value = state.elapsed(state.comment.time, state.time))) { + if ((changed.elapsed || changed.comments || changed.time) && text_4_value !== (text_4_value = state.elapsed(comment.time, state.time))) { text_4.data = text_4_value; } - if ((changed.comments) && raw_value !== (raw_value = state.comment.html)) { + if ((changed.comments) && raw_value !== (raw_value = comment.html)) { detachAfter(raw_before); raw_before.insertAdjacentHTML("afterend", raw_value); } diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index 414fcb3f06..6ea50f4ae4 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -84,7 +84,8 @@ function create_main_fragment(component, state) { // (1:0) {{#each comments as comment, i}} function create_each_block(component, state) { - var div, strong, text, text_1, span, text_2_value = state.comment.author, text_2, text_3, text_4_value = state.elapsed(state.comment.time, state.time), text_4, text_5, text_6, raw_value = state.comment.html, raw_before; + var comment = state.comment; + var div, strong, text, text_1, span, text_2_value = comment.author, text_2, text_3, text_4_value = state.elapsed(comment.time, state.time), text_4, text_5, text_6, raw_value = comment.html, raw_before; return { c: function create() { @@ -123,15 +124,16 @@ function create_each_block(component, state) { }, p: function update(changed, state) { - if ((changed.comments) && text_2_value !== (text_2_value = state.comment.author)) { + comment = state.comment; + if ((changed.comments) && text_2_value !== (text_2_value = comment.author)) { text_2.data = text_2_value; } - if ((changed.elapsed || changed.comments || changed.time) && text_4_value !== (text_4_value = state.elapsed(state.comment.time, state.time))) { + if ((changed.elapsed || changed.comments || changed.time) && text_4_value !== (text_4_value = state.elapsed(comment.time, state.time))) { text_4.data = text_4_value; } - if ((changed.comments) && raw_value !== (raw_value = state.comment.html)) { + if ((changed.comments) && raw_value !== (raw_value = comment.html)) { detachAfter(raw_before); raw_before.insertAdjacentHTML("afterend", raw_value); } From 71f2c350ec85d3ee4e06ecda9272d4c327420ee0 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 15:22:24 -0500 Subject: [PATCH 09/28] remove _context stuff --- src/generators/dom/Block.ts | 10 +++++ src/generators/nodes/Component.ts | 67 ++++--------------------------- 2 files changed, 18 insertions(+), 59 deletions(-) diff --git a/src/generators/dom/Block.ts b/src/generators/dom/Block.ts index e1ed62c242..5c5e2bf570 100644 --- a/src/generators/dom/Block.ts +++ b/src/generators/dom/Block.ts @@ -207,6 +207,16 @@ export default class Block { this.hasUpdateMethod = true; }); + this.indexNames.forEach((alias, name) => { + // TODO only the ones that are actually used in this block... + const assignment = `${alias} = state.${alias}`; // TODO this is wrong!!! + + initializers.push(assignment); + updaters.push(`${assignment};`); + + this.hasUpdateMethod = true; + }); + // minor hack – we need to ensure that any {{{triples}}} are detached first this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString()); diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index c3214578a7..97893dea12 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -81,7 +81,6 @@ export default class Component extends Node { const allContexts = new Set(); const statements: string[] = []; - const name_context = block.getUniqueName(`${name}_context`); let name_updating: string; let name_initial_data: string; @@ -93,11 +92,11 @@ export default class Component extends Node { const bindings = this.attributes .filter(a => a.type === 'Binding') - .map(a => mungeBinding(a, block, name_context)); + .map(a => mungeBinding(a, block)); const eventHandlers = this.attributes .filter((a: Node) => a.type === 'EventHandler') - .map(a => mungeEventHandler(generator, this, a, block, name_context, allContexts)); + .map(a => mungeEventHandler(generator, this, a, block, allContexts)); const ref = this.attributes.find((a: Node) => a.type === 'Ref'); if (ref) generator.usesRefs = true; @@ -156,8 +155,8 @@ export default class Component extends Node { const tail = binding.value.type === 'MemberExpression' ? getTailSnippet(binding.value) : ''; setFromChild = deindent` - var list = ${name_context}.${block.listNames.get(key)}; - var index = ${name_context}.${block.indexNames.get(key)}; + var list = state.${block.listNames.get(key)}; + var index = ${block.indexNames.get(key)}; list[index]${tail} = childState.${binding.name}; ${binding.dependencies @@ -248,41 +247,6 @@ export default class Component extends Node { } } - // maintain component context - if (allContexts.size) { - const contexts = Array.from(allContexts); - - const initialProps = contexts - .map(contextName => { - if (contextName === 'state') return `state: state`; - - const listName = block.listNames.get(contextName); - const indexName = block.indexNames.get(contextName); - - return `${listName}: state.${listName},\n${indexName}: state.${indexName}`; - }) - .join(',\n'); - - const updates = contexts - .map(contextName => { - if (contextName === 'state') return `${name_context}.state = state;`; - - const listName = block.listNames.get(contextName); - const indexName = block.indexNames.get(contextName); - - return `${name_context}.${listName} = state.${listName};\n${name_context}.${indexName} = state.${indexName};`; - }) - .join('\n'); - - block.builders.init.addBlock(deindent` - var ${name_context} = { - ${initialProps} - }; - `); - - block.builders.update.addBlock(updates); - } - const isDynamicComponent = this.name === ':Component'; const switch_vars = isDynamicComponent && { @@ -504,7 +468,7 @@ function mungeAttribute(attribute: Node, block: Block): Attribute { }; } -function mungeBinding(binding: Node, block: Block, name_context: string): Binding { +function mungeBinding(binding: Node, block: Block): Binding { const { name } = getObject(binding.value); const { contexts } = block.contextualise(binding.value); const { dependencies, snippet } = binding.metadata; @@ -515,8 +479,8 @@ function mungeBinding(binding: Node, block: Block, name_context: string): Bindin let prop; if (contextual) { - obj = `${name_context}.${block.listNames.get(name)}`; - prop = `${name_context}.${block.indexNames.get(name)}`; + obj = `state.${block.listNames.get(name)}`; + prop = `${block.indexNames.get(name)}`; } else if (binding.value.type === 'MemberExpression') { prop = `[✂${binding.value.property.start}-${binding.value.property.end}✂]`; if (!binding.value.computed) prop = `'${prop}'`; @@ -537,7 +501,7 @@ function mungeBinding(binding: Node, block: Block, name_context: string): Bindin }; } -function mungeEventHandler(generator: DomGenerator, node: Node, handler: Node, block: Block, name_context: string, allContexts: Set) { +function mungeEventHandler(generator: DomGenerator, node: Node, handler: Node, block: Block, allContexts: Set) { let body; if (handler.expression) { @@ -547,30 +511,15 @@ function mungeEventHandler(generator: DomGenerator, node: Node, handler: Node, b `${block.alias('component')}.` ); - const usedContexts: string[] = []; - handler.expression.arguments.forEach((arg: Node) => { const { contexts } = block.contextualise(arg, null, true); contexts.forEach(context => { - if (!~usedContexts.indexOf(context)) usedContexts.push(context); allContexts.add(context); }); }); - // TODO hoist event handlers? can do `this.__component.method(...)` - const declarations = usedContexts.map(name => { - if (name === 'state') return `var state = ${name_context}.state;`; - - const listName = block.listNames.get(name); - const indexName = block.indexNames.get(name); - - return `var ${listName} = ${name_context}.${listName}, ${indexName} = ${name_context}.${indexName}, ${name} = ${listName}[${indexName}]`; - }); - body = deindent` - ${declarations} - [✂${handler.expression.start}-${handler.expression.end}✂]; `; } else { From 7baba8bf8cb8083f1399a1b6446edc94cef0edd8 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 15:55:18 -0500 Subject: [PATCH 10/28] all tests passing --- src/generators/dom/Block.ts | 7 +------ src/generators/nodes/EachBlock.ts | 14 +++++++------- .../server-side-rendering/visitors/EachBlock.ts | 4 +--- .../each-block-changed-check/expected-bundle.js | 3 ++- .../samples/each-block-changed-check/expected.js | 3 ++- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/generators/dom/Block.ts b/src/generators/dom/Block.ts index 5c5e2bf570..07fb763f82 100644 --- a/src/generators/dom/Block.ts +++ b/src/generators/dom/Block.ts @@ -114,12 +114,7 @@ export default class Block { this.aliases = new Map(); this.variables = new Map(); - // this.getUniqueName = this.generator.getUniqueNameMaker([]); // TODO this is wrong... we probably don't need this any more - - const getUniqueName = this.generator.getUniqueNameMaker([]); // TODO this is wrong... we probably don't need this any more - this.getUniqueName = name => { - return getUniqueName(name); - } + this.getUniqueName = this.generator.getUniqueNameMaker([]); // TODO this is wrong... we probably don't need this any more this.hasUpdateMethod = false; // determined later } diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index 4d3479d26d..11a8a0c632 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -69,7 +69,8 @@ export default class EachBlock extends Node { if (this.destructuredContexts) { for (let i = 0; i < this.destructuredContexts.length; i += 1) { - this.block.contexts.set(this.destructuredContexts[i], `${context}[${i}]`); + const context = this.block.getUniqueName(this.destructuredContexts[i]); + this.block.contexts.set(this.destructuredContexts[i], context); } } @@ -78,12 +79,11 @@ export default class EachBlock extends Node { `${this.block.indexName}: #i` ]; - // if (this.destructuredContexts) { - // for (let i = 0; i < this.destructuredContexts.length; i += 1) { - // contexts.set(this.destructuredContexts[i], `${context}[${i}]`); - // this.contextProps.push(`${this.destructuredContexts[i]}: ${this.block.listName}[#i][${i}]`); - // } - // } + if (this.destructuredContexts) { + for (let i = 0; i < this.destructuredContexts.length; i += 1) { + this.contextProps.push(`${this.destructuredContexts[i]}: ${this.block.listName}[#i][${i}]`); + } + } this.generator.blocks.push(this.block); this.initChildren(this.block, stripWhitespace, nextSibling); diff --git a/src/generators/server-side-rendering/visitors/EachBlock.ts b/src/generators/server-side-rendering/visitors/EachBlock.ts index 9a5ec300ca..491a9fbc1c 100644 --- a/src/generators/server-side-rendering/visitors/EachBlock.ts +++ b/src/generators/server-side-rendering/visitors/EachBlock.ts @@ -11,9 +11,7 @@ export default function visitEachBlock( block.contextualise(node.expression); const { dependencies, snippet } = node.metadata; - const context = node.destructuredContexts ? `[${node.destructuredContexts.join(', ')}]` : node.context; - - const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}${snippet}.map(${node.index ? `(${context}, ${node.index})` : `(${context})`} => \``; + const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}${snippet}.map(${node.index ? `(${node.context}, ${node.index})` : `(${node.context})`} => \``; generator.append(open); // TODO should this be the generator's job? It's duplicated between diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js index 866f90aa55..3b76fb9ad7 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -284,7 +284,7 @@ function create_main_fragment(component, state) { // (1:0) {{#each comments as comment, i}} function create_each_block(component, state) { - var comment = state.comment; + var comment = state.comment, i = state.i; var div, strong, text, text_1, span, text_2_value = comment.author, text_2, text_3, text_4_value = state.elapsed(comment.time, state.time), text_4, text_5, text_6, raw_value = comment.html, raw_before; return { @@ -325,6 +325,7 @@ function create_each_block(component, state) { p: function update(changed, state) { comment = state.comment; + i = state.i; if ((changed.comments) && text_2_value !== (text_2_value = comment.author)) { text_2.data = text_2_value; } diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index 6ea50f4ae4..c86a3a2edf 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -84,7 +84,7 @@ function create_main_fragment(component, state) { // (1:0) {{#each comments as comment, i}} function create_each_block(component, state) { - var comment = state.comment; + var comment = state.comment, i = state.i; var div, strong, text, text_1, span, text_2_value = comment.author, text_2, text_3, text_4_value = state.elapsed(comment.time, state.time), text_4, text_5, text_6, raw_value = comment.html, raw_before; return { @@ -125,6 +125,7 @@ function create_each_block(component, state) { p: function update(changed, state) { comment = state.comment; + i = state.i; if ((changed.comments) && text_2_value !== (text_2_value = comment.author)) { text_2.data = text_2_value; } From 27f6d560b5a790d6a38e90cca2170dd85ddbb20e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 16:04:15 -0500 Subject: [PATCH 11/28] remove some unused code etc --- src/generators/Generator.ts | 45 ++++++++----------- src/generators/dom/Block.ts | 2 +- src/generators/dom/index.ts | 4 +- .../expected-bundle.js | 2 +- .../each-block-changed-check/expected.js | 2 +- 5 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 2463c33325..d5c779a5f4 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -242,29 +242,24 @@ export default class Generator { if (name === 'event' && isEventHandler) { // noop } else if (contexts.has(name)) { - // if (self.constructor.name === 'DomGenerator') { // TODO filthy, temporary hack - const contextName = contexts.get(name); - if (contextName !== name) { - // this is true for 'reserved' names like `state` and `component`, - // also destructured contexts - - code.overwrite( - node.start, - node.start + name.length, - contextName, - { storeName: true, contentOnly: false } - ); - - const destructuredName = contextName.replace(/\[\d+\]/, ''); - if (destructuredName !== contextName) { - // so that hoisting the context works correctly - usedContexts.add(destructuredName); - } + const contextName = contexts.get(name); + if (contextName !== name) { + // this is true for 'reserved' names like `state` and `component`, + // also destructured contexts + + code.overwrite( + node.start, + node.start + name.length, + contextName, + { storeName: true, contentOnly: false } + ); + + const destructuredName = contextName.replace(/\[\d+\]/, ''); + if (destructuredName !== contextName) { + // so that hoisting the context works correctly + usedContexts.add(destructuredName); } - - // TODO filthy, temporary hack - // if (!isEventHandler) code.prependRight(node.start, `state.`); - // } + } usedContexts.add(name); } else if (helpers.has(name)) { @@ -274,8 +269,6 @@ export default class Generator { const alias = self.templateVars.get(`helpers-${name}`); if (alias !== name) code.overwrite(object.start, object.end, alias); } else if (indexes.has(name)) { - if (self.constructor.name === 'DomGenerator' && !isEventHandler) code.prependRight(node.start, `state.`); - const context = indexes.get(name); usedContexts.add(context); // TODO is this right? usedIndexes.add(name); @@ -381,8 +374,8 @@ export default class Generator { return alias; } - getUniqueNameMaker(params: string[]) { - const localUsedNames = new Set(params); + getUniqueNameMaker() { + const localUsedNames = new Set(); function add(name: string) { localUsedNames.add(name); diff --git a/src/generators/dom/Block.ts b/src/generators/dom/Block.ts index 07fb763f82..0d786752dc 100644 --- a/src/generators/dom/Block.ts +++ b/src/generators/dom/Block.ts @@ -114,7 +114,7 @@ export default class Block { this.aliases = new Map(); this.variables = new Map(); - this.getUniqueName = this.generator.getUniqueNameMaker([]); // TODO this is wrong... we probably don't need this any more + this.getUniqueName = this.generator.getUniqueNameMaker(); this.hasUpdateMethod = false; // determined later } diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 090dffbf3e..2f88185fff 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -49,8 +49,8 @@ export class DomGenerator extends Generator { this.metaBindings = []; } - getUniqueNameMaker(params: string[]) { - const localUsedNames = new Set(params); // TODO is this ever called with params? + getUniqueNameMaker() { + const localUsedNames = new Set(); function add(name: string) { localUsedNames.add(name); diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js index 3b76fb9ad7..cc8a394ac1 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -291,7 +291,7 @@ function create_each_block(component, state) { c: function create() { div = createElement("div"); strong = createElement("strong"); - text = createText(state.i); + text = createText(i); text_1 = createText("\n\n\t\t"); span = createElement("span"); text_2 = createText(text_2_value); diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index c86a3a2edf..2eb66567a6 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -91,7 +91,7 @@ function create_each_block(component, state) { c: function create() { div = createElement("div"); strong = createElement("strong"); - text = createText(state.i); + text = createText(i); text_1 = createText("\n\n\t\t"); span = createElement("span"); text_2 = createText(text_2_value); From ec488fe3470c5e3f0f89e15fc5f30c2ab6c5a078 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 16:20:29 -0500 Subject: [PATCH 12/28] dont create context for pending block --- src/generators/Generator.ts | 1 - src/generators/nodes/AwaitBlock.ts | 40 +++++++++++++++++------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index d5c779a5f4..cac7858c19 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -246,7 +246,6 @@ export default class Generator { if (contextName !== name) { // this is true for 'reserved' names like `state` and `component`, // also destructured contexts - code.overwrite( node.start, node.start + name.length, diff --git a/src/generators/nodes/AwaitBlock.ts b/src/generators/nodes/AwaitBlock.ts index 168109614f..8cfa0a49e4 100644 --- a/src/generators/nodes/AwaitBlock.ts +++ b/src/generators/nodes/AwaitBlock.ts @@ -35,21 +35,19 @@ export default class AwaitBlock extends Node { ].forEach(([status, arg]) => { const child = this[status]; - const context = arg || '_'; - const contexts = new Map(block.contexts); - contexts.set(arg, context); - - const contextTypes = new Map(block.contextTypes); - contextTypes.set(arg, status); - child.block = block.child({ comment: createDebuggingComment(child, this.generator), name: this.generator.getUniqueName(`create_${status}_block`), - context, - contexts, - contextTypes + contexts: new Map(block.contexts), + contextTypes: new Map(block.contextTypes) }); + if (arg) { + child.block.context = arg; + child.block.contexts.set(arg, arg); // TODO should be using getUniqueName + child.block.contextTypes.set(arg, status); + } + child.initChildren(child.block, stripWhitespace, nextSibling); this.generator.blocks.push(child.block); @@ -107,7 +105,7 @@ export default class AwaitBlock extends Node { if (${token} !== ${await_token}) return; var ${old_block} = ${await_block}; - ${await_block} = (${await_block_type} = type)(#component, state); + ${await_block} = type && (${await_block_type} = type)(#component, state); if (${old_block}) { ${old_block}.u(); @@ -124,13 +122,21 @@ export default class AwaitBlock extends Node { if (@isPromise(${promise})) { ${promise}.then(function(${value}) { - var state = #component.get(); - ${resolved} = { ${this.then.block.context}: ${value} }; - ${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, ${resolved})); + ${this.then.block.context ? deindent` + var state = #component.get(); + ${resolved} = { ${this.then.block.context}: ${value} }; + ${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, ${resolved})); + ` : deindent` + ${replace_await_block}(${token}, null, null); + `} }, function (${error}) { - var state = #component.get(); - ${resolved} = { ${this.catch.block.context}: ${error} }; - ${replace_await_block}(${token}, ${create_catch_block}, @assign({}, state, ${resolved})); + ${this.catch.block.context ? deindent` + var state = #component.get(); + ${resolved} = { ${this.catch.block.context}: ${error} }; + ${replace_await_block}(${token}, ${create_catch_block}, @assign({}, state, ${resolved})); + ` : deindent` + ${replace_await_block}(${token}, null, null); + `} }); // if we previously had a then/catch block, destroy it From 9dcf59868d220912f7e4e8c1eee9725d54e2ef60 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 16:22:42 -0500 Subject: [PATCH 13/28] revert to component.get() in _bind --- src/generators/nodes/Component.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 97893dea12..ee5561ad72 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -219,10 +219,8 @@ export default class Component extends Node { componentInitProperties.push(`data: ${name_initial_data}`); - block.addVariable('__state__TODO', 'state'); - block.builders.update.addLine(`__state__TODO = state`); const initialisers = [ - 'state = __state__TODO', + 'state = #component.get()', hasLocalBindings && 'newState = {}', hasStoreBindings && 'newStoreState = {}', ].filter(Boolean).join(', '); From 7d44b181f23ff19dfa9e8af12c7f0e6e27b39100 Mon Sep 17 00:00:00 2001 From: Gareth Oates Date: Fri, 16 Feb 2018 10:58:18 +0100 Subject: [PATCH 14/28] this._changeHandlers was undefined when calling cancel() --- store.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/store.js b/store.js index 63a99cca05..6923455113 100644 --- a/store.js +++ b/store.js @@ -115,7 +115,7 @@ assign(Store.prototype, { cancel: function() { var index = this._changeHandlers.indexOf(callback); if (~index) this._changeHandlers.splice(index, 1); - } + }.bind(this) }; }, @@ -163,4 +163,4 @@ assign(Store.prototype, { } }); -export { Store }; \ No newline at end of file +export { Store }; From 389b5c9ff98d23df161564a91ed842b09e23db1e Mon Sep 17 00:00:00 2001 From: Gareth Oates Date: Sun, 18 Feb 2018 14:19:46 +0100 Subject: [PATCH 15/28] There was no test to determine if onchange cancel threw an error --- test/store/index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/store/index.js b/test/store/index.js index e3c024e4d5..fd84322d35 100644 --- a/test/store/index.js +++ b/test/store/index.js @@ -113,6 +113,15 @@ describe('store', () => { }); }); + it('allows user to cancel state change callback', () => { + const store = new Store(); + const handler = store.onchange(() => {}); + + assert.doesNotThrow(() => { + handler.cancel(); + }, TypeError, 'this._changeHandlers is undefined'); + }); + describe('computed', () => { it('computes a property based on data', () => { const store = new Store({ From 6e31efce11499ed9088e486eb5f2699c8ff89624 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Feb 2018 10:21:51 -0500 Subject: [PATCH 16/28] fix capitalize warning --- src/validate/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/validate/index.ts b/src/validate/index.ts index da603a437b..f79cb1a55c 100644 --- a/src/validate/index.ts +++ b/src/validate/index.ts @@ -101,7 +101,7 @@ export default function validate( throw error; } - if (name && !/^[A-Z]/.test(name)) { + if (name && /^[a-z]/.test(name)) { const message = `options.name should be capitalised`; onwarn({ message, From 8c1f09db1ff30f7309c85a188c6827e85dca7c94 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Wed, 21 Feb 2018 18:50:54 -0500 Subject: [PATCH 17/28] upgrade acorn and parse as ES2018 --- package.json | 2 +- src/parse/read/directives.ts | 6 ++++-- src/parse/read/expression.ts | 1 + src/parse/read/script.ts | 2 +- yarn.lock | 6 +++++- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 7d1d9e3b33..e0a90fd46e 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "devDependencies": { "@types/mocha": "^2.2.41", "@types/node": "^8.0.17", - "acorn": "^5.1.1", + "acorn": "^5.4.1", "acorn-dynamic-import": "^2.0.2", "chalk": "^2.0.1", "codecov": "^2.2.0", diff --git a/src/parse/read/directives.ts b/src/parse/read/directives.ts index 2a27f37812..99744e0244 100644 --- a/src/parse/read/directives.ts +++ b/src/parse/read/directives.ts @@ -31,7 +31,9 @@ function readExpression(parser: Parser, start: number, quoteMark: string|null) { } } - const expression = parseExpressionAt(repeat(' ', start) + str, start); + const expression = parseExpressionAt(repeat(' ', start) + str, start, { + ecmaVersion: 9, + }); parser.index = expression.end; parser.allowWhitespace(); @@ -102,7 +104,7 @@ export function readBindingDirective( } const source = repeat(' ', a) + parser.template.slice(a, b); - value = parseExpressionAt(source, a); + value = parseExpressionAt(source, a, { ecmaVersion: 9 }); if (value.type !== 'Identifier' && value.type !== 'MemberExpression') { parser.error(`Cannot bind to rvalue`, value.start); diff --git a/src/parse/read/expression.ts b/src/parse/read/expression.ts index 47e1d706df..c8c3054274 100644 --- a/src/parse/read/expression.ts +++ b/src/parse/read/expression.ts @@ -32,6 +32,7 @@ export default function readExpression(parser: Parser) { try { const node = parseExpressionAt(parser.template, parser.index, { + ecmaVersion: 9, preserveParens: true, }); parser.index = node.end; diff --git a/src/parse/read/script.ts b/src/parse/read/script.ts index d88c0a822d..4f28200b43 100644 --- a/src/parse/read/script.ts +++ b/src/parse/read/script.ts @@ -22,7 +22,7 @@ export default function readScript(parser: Parser, start: number, attributes: No try { ast = acorn.parse(source, { - ecmaVersion: 8, + ecmaVersion: 9, sourceType: 'module', plugins: { dynamicImport: true diff --git a/yarn.lock b/yarn.lock index e79897d0af..65ad9e5307 100644 --- a/yarn.lock +++ b/yarn.lock @@ -58,10 +58,14 @@ acorn@^4.0.3: version "4.0.13" resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.13.tgz#105495ae5361d697bd195c825192e1ad7f253787" -acorn@^5.0.0, acorn@^5.1.1, acorn@^5.2.1, acorn@^5.3.0: +acorn@^5.0.0, acorn@^5.2.1, acorn@^5.3.0: version "5.3.0" resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.3.0.tgz#7446d39459c54fb49a80e6ee6478149b940ec822" +acorn@^5.4.1: + version "5.4.1" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.4.1.tgz#fdc58d9d17f4a4e98d102ded826a9b9759125102" + acorn@~5.1.1: version "5.1.2" resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.1.2.tgz#911cb53e036807cf0fa778dc5d370fbd864246d7" From fdd9adab4d60ad478726ab9de564d2ea0c875a89 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Feb 2018 08:40:02 -0500 Subject: [PATCH 18/28] add test for #1179 --- test/validator/index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/validator/index.js b/test/validator/index.js index 0a132a7c9e..6fa792dbde 100644 --- a/test/validator/index.js +++ b/test/validator/index.js @@ -88,4 +88,19 @@ describe("validate", () => { } ]); }); + + it("does not warn if options.name begins with non-alphabetic character", () => { + const warnings = []; + svelte.compile("
", { + name: "_", + onwarn(warning) { + warnings.push({ + message: warning.message, + pos: warning.pos, + loc: warning.loc + }); + } + }); + assert.deepEqual(warnings, []); + }); }); From cde5fa12255ee4835447532d04e431c5c93e6a05 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Feb 2018 08:40:56 -0500 Subject: [PATCH 19/28] this was apparently unnecessary --- rollup.config.js | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 208c95df84..1f10cb6a0c 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -7,28 +7,11 @@ import typescript from 'rollup-plugin-typescript'; import buble from 'rollup-plugin-buble'; import pkg from './package.json'; -const src = path.resolve('src'); - export default [ /* compiler/svelte.js */ { input: 'src/index.ts', plugins: [ - { - resolveId(importee, importer) { - // bit of a hack — TypeScript only really works if it can resolve imports, - // but they misguidedly chose to reject imports with file extensions. This - // means we need to resolve them here - if ( - importer && - importer.startsWith(src) && - importee[0] === '.' && - path.extname(importee) === '' - ) { - return path.resolve(path.dirname(importer), `${importee}.ts`); - } - } - }, replace({ __VERSION__: pkg.version }), From 957179fc7fc719d29b6203b2caff66b8c26e229f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Feb 2018 08:47:49 -0500 Subject: [PATCH 20/28] use local `this` variable instead of .bind --- store.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/store.js b/store.js index 6923455113..9f0661811b 100644 --- a/store.js +++ b/store.js @@ -111,11 +111,14 @@ assign(Store.prototype, { onchange: function(callback) { this._changeHandlers.push(callback); + + var store = this; + return { cancel: function() { - var index = this._changeHandlers.indexOf(callback); - if (~index) this._changeHandlers.splice(index, 1); - }.bind(this) + var index = store._changeHandlers.indexOf(callback); + if (~index) store._changeHandlers.splice(index, 1); + } }; }, From af5a73cc117899348530f537a122040abf535697 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Feb 2018 08:57:01 -0500 Subject: [PATCH 21/28] update tests --- test/js/samples/component-static-immutable/expected-bundle.js | 4 ++-- test/js/samples/component-static-immutable/expected.js | 4 ++-- .../js/samples/component-static-immutable2/expected-bundle.js | 4 ++-- test/js/samples/component-static-immutable2/expected.js | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/js/samples/component-static-immutable/expected-bundle.js b/test/js/samples/component-static-immutable/expected-bundle.js index 18ca967e92..b188229ec7 100644 --- a/test/js/samples/component-static-immutable/expected-bundle.js +++ b/test/js/samples/component-static-immutable/expected-bundle.js @@ -176,7 +176,7 @@ var proto = { /* generated by Svelte vX.Y.Z */ var Nested = window.Nested; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var nested = new Nested({ root: component.root, @@ -214,7 +214,7 @@ function SvelteComponent(options) { this._aftercreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/component-static-immutable/expected.js b/test/js/samples/component-static-immutable/expected.js index 028dc9e64c..647b068179 100644 --- a/test/js/samples/component-static-immutable/expected.js +++ b/test/js/samples/component-static-immutable/expected.js @@ -3,7 +3,7 @@ import { _differsImmutable, assign, callAll, init, noop, proto } from "svelte/sh var Nested = window.Nested; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var nested = new Nested({ root: component.root, @@ -41,7 +41,7 @@ function SvelteComponent(options) { this._aftercreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/component-static-immutable2/expected-bundle.js b/test/js/samples/component-static-immutable2/expected-bundle.js index 18ca967e92..b188229ec7 100644 --- a/test/js/samples/component-static-immutable2/expected-bundle.js +++ b/test/js/samples/component-static-immutable2/expected-bundle.js @@ -176,7 +176,7 @@ var proto = { /* generated by Svelte vX.Y.Z */ var Nested = window.Nested; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var nested = new Nested({ root: component.root, @@ -214,7 +214,7 @@ function SvelteComponent(options) { this._aftercreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/component-static-immutable2/expected.js b/test/js/samples/component-static-immutable2/expected.js index 028dc9e64c..647b068179 100644 --- a/test/js/samples/component-static-immutable2/expected.js +++ b/test/js/samples/component-static-immutable2/expected.js @@ -3,7 +3,7 @@ import { _differsImmutable, assign, callAll, init, noop, proto } from "svelte/sh var Nested = window.Nested; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var nested = new Nested({ root: component.root, @@ -41,7 +41,7 @@ function SvelteComponent(options) { this._aftercreate = []; } - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); From 12715efbc1ff78f8e6f8dff069c427d8b706a754 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Feb 2018 09:09:32 -0500 Subject: [PATCH 22/28] -> v1.55.1 --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bbc230046..46357f6d6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Svelte changelog +## 1.55.1 + +* Fix cancellation of store `onchange` handlers ([#1177](https://github.com/sveltejs/svelte/issues/1177)) +* Write `["default"]` instead of `.default` in legacy mode ([#1166](https://github.com/sveltejs/svelte/issues/1166)) +* Upgrade Acorn ([#1182](https://github.com/sveltejs/svelte/pull/1182)) +* Don't warn about capitalisation if `options.name` begins with non-alphabetical character ([#1179](https://github.com/sveltejs/svelte/pull/1179)) + ## 1.55.0 * Add `immutable` compiler option for Svelte and runtime option for `Store` ([#1146](https://github.com/sveltejs/svelte/issues/1146)) diff --git a/package.json b/package.json index e0a90fd46e..0b8ff4284d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "svelte", - "version": "1.55.0", + "version": "1.55.1", "description": "The magical disappearing UI framework", "main": "compiler/svelte.js", "files": [ From 036277df648405092ed72c32f9ad91fcd586e70d Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Feb 2018 09:11:10 -0500 Subject: [PATCH 23/28] update tests --- test/js/samples/legacy-default/expected-bundle.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/js/samples/legacy-default/expected-bundle.js b/test/js/samples/legacy-default/expected-bundle.js index 3804eeb7df..f2fa173262 100644 --- a/test/js/samples/legacy-default/expected-bundle.js +++ b/test/js/samples/legacy-default/expected-bundle.js @@ -61,8 +61,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +function _differs(a, b) { + return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } function dispatchObservers(component, group, changed, newState, oldState) { @@ -161,7 +161,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -199,7 +199,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ From c2bb5499019a60162efcf778ad5107819758a6de Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 24 Feb 2018 12:15:06 -0500 Subject: [PATCH 24/28] wrap updates in conditional - fixes #1144 --- src/generators/nodes/Slot.ts | 2 ++ .../samples/component-slot-dynamic/Nested.html | 13 +++++++++++++ .../samples/component-slot-dynamic/_config.js | 9 +++++++++ .../samples/component-slot-dynamic/main.html | 13 +++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 test/runtime/samples/component-slot-dynamic/Nested.html create mode 100644 test/runtime/samples/component-slot-dynamic/_config.js create mode 100644 test/runtime/samples/component-slot-dynamic/main.html diff --git a/src/generators/nodes/Slot.ts b/src/generators/nodes/Slot.ts index 4bcd034a55..2a2251448d 100644 --- a/src/generators/nodes/Slot.ts +++ b/src/generators/nodes/Slot.ts @@ -57,6 +57,7 @@ export default class Slot extends Element { block.builders.create.pushCondition(`!${content_name}`); block.builders.hydrate.pushCondition(`!${content_name}`); block.builders.mount.pushCondition(`!${content_name}`); + block.builders.update.pushCondition(`!${content_name}`); block.builders.unmount.pushCondition(`!${content_name}`); block.builders.destroy.pushCondition(`!${content_name}`); @@ -67,6 +68,7 @@ export default class Slot extends Element { block.builders.create.popCondition(); block.builders.hydrate.popCondition(); block.builders.mount.popCondition(); + block.builders.update.popCondition(); block.builders.unmount.popCondition(); block.builders.destroy.popCondition(); diff --git a/test/runtime/samples/component-slot-dynamic/Nested.html b/test/runtime/samples/component-slot-dynamic/Nested.html new file mode 100644 index 0000000000..c7e0ccfad3 --- /dev/null +++ b/test/runtime/samples/component-slot-dynamic/Nested.html @@ -0,0 +1,13 @@ + + {{foo}} + + + diff --git a/test/runtime/samples/component-slot-dynamic/_config.js b/test/runtime/samples/component-slot-dynamic/_config.js new file mode 100644 index 0000000000..17f22b2830 --- /dev/null +++ b/test/runtime/samples/component-slot-dynamic/_config.js @@ -0,0 +1,9 @@ +export default { + html: ` +

override default slot

+ `, + + test(assert, component) { + component.refs.nested.set({ foo: 'b' }); + } +}; diff --git a/test/runtime/samples/component-slot-dynamic/main.html b/test/runtime/samples/component-slot-dynamic/main.html new file mode 100644 index 0000000000..348754d88c --- /dev/null +++ b/test/runtime/samples/component-slot-dynamic/main.html @@ -0,0 +1,13 @@ + +

override default slot

+
+ + From cb8071acd456e916b46bb42916383f3f0a8a0c4e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 24 Feb 2018 13:08:28 -0500 Subject: [PATCH 25/28] allow observing $foo in dev mode - #1181 --- src/shared/index.js | 2 +- .../samples/store-observe-dollar/_config.js | 25 +++++++++++++++++++ .../samples/store-observe-dollar/main.html | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/store-observe-dollar/_config.js create mode 100644 test/runtime/samples/store-observe-dollar/main.html diff --git a/src/shared/index.js b/src/shared/index.js index cf953e4bf1..d9d6475e64 100644 --- a/src/shared/index.js +++ b/src/shared/index.js @@ -100,7 +100,7 @@ export function observe(key, callback, options) { } export function observeDev(key, callback, options) { - var c = (key = '' + key).search(/[^\w]/); + var c = (key = '' + key).search(/[.[]/); if (c > -1) { var message = 'The first argument to component.observe(...) must be the name of a top-level property'; diff --git a/test/runtime/samples/store-observe-dollar/_config.js b/test/runtime/samples/store-observe-dollar/_config.js new file mode 100644 index 0000000000..1861b937d9 --- /dev/null +++ b/test/runtime/samples/store-observe-dollar/_config.js @@ -0,0 +1,25 @@ +import { Store } from '../../../../store.js'; + +const store = new Store({ + name: 'world' +}); + +export default { + store, + + html: `

Hello world!

`, + + dev: true, + + test(assert, component) { + const names = []; + + component.observe('$name', name => { + names.push(name); + }); + + store.set({ name: 'everybody' }); + + assert.deepEqual(names, ['world', 'everybody']); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/store-observe-dollar/main.html b/test/runtime/samples/store-observe-dollar/main.html new file mode 100644 index 0000000000..28154934b8 --- /dev/null +++ b/test/runtime/samples/store-observe-dollar/main.html @@ -0,0 +1 @@ +

Hello {{$name}}!

\ No newline at end of file From 602f13ae6bb1313b279a9897a8dc21bb1ade73da Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 24 Feb 2018 13:25:27 -0500 Subject: [PATCH 26/28] fix out of date test --- test/js/samples/legacy-default/expected-bundle.js | 4 ++-- test/js/samples/legacy-default/expected.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/js/samples/legacy-default/expected-bundle.js b/test/js/samples/legacy-default/expected-bundle.js index f2fa173262..023ac9b1bc 100644 --- a/test/js/samples/legacy-default/expected-bundle.js +++ b/test/js/samples/legacy-default/expected-bundle.js @@ -204,7 +204,7 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var text, p, text_1, text_2, text_3, slot_content_default = component._slotted["default"], slot_content_default_before, slot_content_default_after; var foo = new Foo({ @@ -270,7 +270,7 @@ function SvelteComponent(options) { this.slots = {}; - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/legacy-default/expected.js b/test/js/samples/legacy-default/expected.js index 27cdd1896f..6c90bb8f13 100644 --- a/test/js/samples/legacy-default/expected.js +++ b/test/js/samples/legacy-default/expected.js @@ -1,7 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { appendNode, assign, callAll, createComment, createElement, createFragment, createText, detachNode, init, insertNode, noop, proto, reinsertBetween } from "svelte/shared.js"; -function create_main_fragment(state, component) { +function create_main_fragment(component, state) { var text, p, text_1, text_2, text_3, slot_content_default = component._slotted["default"], slot_content_default_before, slot_content_default_after; var foo = new Foo({ @@ -67,7 +67,7 @@ function SvelteComponent(options) { this.slots = {}; - this._fragment = create_main_fragment(this._state, this); + this._fragment = create_main_fragment(this, this._state); if (options.target) { this._fragment.c(); From f3d0ef38ad3ca5d876530f6a14a28a29c3d01bb4 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 24 Feb 2018 13:40:19 -0500 Subject: [PATCH 27/28] -> v1.56.0 --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46357f6d6e..7d0597de60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Svelte changelog +## 1.56.0 + +* Internal refactor ([#1122](https://github.com/sveltejs/svelte/issues/1122)) +* Use correct context for component events ([#1184](https://github.com/sveltejs/svelte/issues/1184)) +* Allow observing `$foo` in dev mode ([#1181](https://github.com/sveltejs/svelte/issues/1181)) +* Handle dynamic data in default slot ([#1144](https://github.com/sveltejs/svelte/issues/1144)) + ## 1.55.1 * Fix cancellation of store `onchange` handlers ([#1177](https://github.com/sveltejs/svelte/issues/1177)) diff --git a/package.json b/package.json index 0b8ff4284d..c08645b16d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "svelte", - "version": "1.55.1", + "version": "1.56.0", "description": "The magical disappearing UI framework", "main": "compiler/svelte.js", "files": [ From 08c54eec111ca5e116a0c2501632c50ad0b82413 Mon Sep 17 00:00:00 2001 From: Gareth Oates Date: Sat, 24 Feb 2018 21:26:04 +0100 Subject: [PATCH 28/28] Shameless plug of my svelte store package --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0a948d99cc..95b352e16a 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ This is the Svelte compiler, which is primarily intended for authors of tooling * [svelte-hot-loader](https://github.com/ekhaled/svelte-hot-loader) – Webpack loader addon to support HMR * [meteor-svelte](https://github.com/klaussner/meteor-svelte) – Meteor build plugin * [sveltejs-brunch](https://github.com/StarpTech/sveltejs-brunch) – Brunch build plugin +* [svelte-dev-store](https://github.com/GarethOates/svelte-dev-store) - Use Redux tools to visualise Svelte store * More to come!