Skip to content

Commit

Permalink
Merge pull request #1414 from sveltejs/gh-1413
Browse files Browse the repository at this point in the history
Always declare spread levels
  • Loading branch information
Rich-Harris authored May 4, 2018
2 parents ae8c3ba + 5afb73c commit 2e8b956
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/compile/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Binding from './Binding';
import EventHandler from './EventHandler';
import Expression from './shared/Expression';
import { AppendTarget } from '../../interfaces';
import addToSet from '../../utils/addToSet';

export default class Component extends Node {
type: 'Component';
Expand Down Expand Up @@ -161,10 +162,16 @@ export default class Component extends Node {
const initialProps = [];
const changes = [];

const allDependencies = new Set();

this.attributes.forEach(attr => {
addToSet(allDependencies, attr.dependencies);
});

this.attributes.forEach(attr => {
const { name, dependencies } = attr;

const condition = dependencies.size > 0
const condition = dependencies.size > 0 && (dependencies.size !== allDependencies.size)
? [...dependencies].map(d => `changed.${d}`).join(' || ')
: null;

Expand All @@ -181,20 +188,22 @@ export default class Component extends Node {
}
});

block.addVariable(levels);

statements.push(deindent`
${levels} = [
block.builders.init.addBlock(deindent`
var ${levels} = [
${initialProps.join(',\n')}
];
`);

statements.push(deindent`
for (var #i = 0; #i < ${levels}.length; #i += 1) {
${name_initial_data} = @assign(${name_initial_data}, ${levels}[#i]);
}
`);

const conditions = [...allDependencies].map(dep => `changed.${dep}`).join(' || ');

updates.push(deindent`
var ${name_changes} = @getSpreadUpdate(${levels}, [
var ${name_changes} = ${allDependencies.size === 1 ? `${conditions}` : `(${conditions})`} && @getSpreadUpdate(${levels}, [
${changes.join(',\n')}
]);
`);
Expand Down Expand Up @@ -381,6 +390,12 @@ export default class Component extends Node {

const updateMountNode = this.getUpdateMountNode(anchor);

if (updates.length) {
block.builders.update.addBlock(deindent`
${updates}
`);
}

block.builders.update.addBlock(deindent`
if (${switch_value} !== (${switch_value} = ${snippet})) {
if (${name}) ${name}.destroy();
Expand Down Expand Up @@ -408,8 +423,7 @@ export default class Component extends Node {

if (updates.length) {
block.builders.update.addBlock(deindent`
else {
${updates}
else if (${switch_value}) {
${name}._set(${name_changes});
${this.bindings.length && `${name_updating} = {};`}
}
Expand Down
19 changes: 19 additions & 0 deletions test/runtime/samples/spread-component-dynamic-undefined/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default {
data: {
props: {
a: 1,
},
},

html: ``,

test(assert, component, target) {
component.set({
props: {
a: 2,
},
});

assert.htmlEqual(target.innerHTML, ``);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<svelte:component this={undefined} {...props} />

0 comments on commit 2e8b956

Please sign in to comment.