almost all tests passing

pull/922/head
Rich Harris 8 years ago
parent 85f98704a3
commit 1f43e547f7

@ -7,21 +7,10 @@ import getTailSnippet from '../../../utils/getTailSnippet';
import getObject from '../../../utils/getObject'; import getObject from '../../../utils/getObject';
import getExpressionPrecedence from '../../../utils/getExpressionPrecedence'; import getExpressionPrecedence from '../../../utils/getExpressionPrecedence';
import { stringify } from '../../../utils/stringify'; import { stringify } from '../../../utils/stringify';
import stringifyProps from '../../../utils/stringifyProps';
import { Node } from '../../../interfaces'; import { Node } from '../../../interfaces';
import { State } from '../interfaces'; import { State } from '../interfaces';
function stringifyProps(props: string[]) {
if (!props.length) return '{}';
const joined = props.join(', ');
if (joined.length > 40) {
// make larger data objects readable
return `{\n\t${props.join(',\n\t')}\n}`;
}
return `{ ${joined} }`;
}
interface Attribute { interface Attribute {
name: string; name: string;
value: any; value: any;

@ -7,6 +7,7 @@ import { Node } from '../../../../interfaces';
import { State } from '../../interfaces'; import { State } from '../../interfaces';
import getObject from '../../../../utils/getObject'; import getObject from '../../../../utils/getObject';
import getTailSnippet from '../../../../utils/getTailSnippet'; import getTailSnippet from '../../../../utils/getTailSnippet';
import stringifyProps from '../../../../utils/stringifyProps';
import visitBinding from './Binding'; import visitBinding from './Binding';
import { generateRule } from '../../../../shared/index'; import { generateRule } from '../../../../shared/index';
import flatten from '../../../../utils/flattenReference'; import flatten from '../../../../utils/flattenReference';
@ -15,20 +16,6 @@ interface Binding {
name: string; name: string;
} }
const types: Record<string, (
generator: DomGenerator,
block: Block,
state: State,
node: Node,
binding: Node[]
) => void> = {
input: addInputBinding,
textarea: addInputBinding,
select: addSelectBinding,
audio: addMediaBinding,
video: addMediaBinding
};
const readOnlyMediaAttributes = new Set([ const readOnlyMediaAttributes = new Set([
'duration', 'duration',
'buffered', 'buffered',
@ -179,10 +166,26 @@ export default function addBindings(
const lock = needsLock ? block.getUniqueName(`${node.var}_updating`) : null; const lock = needsLock ? block.getUniqueName(`${node.var}_updating`) : null;
if (needsLock) block.addVariable(lock, 'false'); if (needsLock) block.addVariable(lock, 'false');
const usesContext = group.bindings.some(binding => binding.setter.usesContext);
const usesState = group.bindings.some(binding => binding.setter.usesState);
const mutations = group.bindings.map(binding => binding.setter.mutation).filter(Boolean).join('\n');
const updates = new Set();
group.bindings.forEach(binding => {
binding.setter.updates.forEach(update => {
updates.add(update);
});
});
const props = Array.from(updates).join(', '); // TODO use stringifyProps once indenting is fixed
block.builders.init.addBlock(deindent` block.builders.init.addBlock(deindent`
function ${handler}() { function ${handler}() {
${usesContext && `var context = ${node.var}._svelte;`}
${usesState && `var state = #component.get();`}
${needsLock && `${lock} = true;`} ${needsLock && `${lock} = true;`}
${group.bindings.map(binding => binding.setter)} ${mutations.length > 0 && mutations}
#component.set({ ${props} });
${needsLock && `${lock} = false;`} ${needsLock && `${lock} = false;`}
} }
`); `);
@ -256,24 +259,20 @@ function getSetter(
dependencies: string[], dependencies: string[],
value: string, value: string,
) { ) {
const tail = attribute.value.type === 'MemberExpression'
? getTailSnippet(attribute.value)
: '';
if (block.contexts.has(name)) { if (block.contexts.has(name)) {
const prop = dependencies[0]; const tail = attribute.value.type === 'MemberExpression'
const computed = isComputed(attribute.value); ? getTailSnippet(attribute.value)
: '';
return deindent`
var list = ${node.var}._svelte.${block.listNames.get(name)}; const list = `context.${block.listNames.get(name)}`;
var index = ${node.var}._svelte.${block.indexNames.get(name)}; const index = `context.${block.indexNames.get(name)}`;
${computed && `var state = #component.get();`}
list[index]${tail} = ${value}; return {
usesContext: true,
${computed usesState: true,
? `#component.set({${dependencies.map((prop: string) => `${prop}: state.${prop}`).join(', ')} });` mutation: `${list}[${index}]${tail} = ${value};`,
: `#component.set({${dependencies.map((prop: string) => `${prop}: #component.get('${prop}')`).join(', ')} });`} updates: dependencies.map(prop => `${prop}: state.${prop}`)
`; };
} }
if (attribute.value.type === 'MemberExpression') { if (attribute.value.type === 'MemberExpression') {
@ -286,14 +285,20 @@ function getSetter(
// that we don't have to do the `.some()` here // that we don't have to do the `.some()` here
dependencies = dependencies.filter(prop => !generator.computations.some(computation => computation.key === prop)); dependencies = dependencies.filter(prop => !generator.computations.some(computation => computation.key === prop));
return deindent` return {
var state = #component.get(); usesContext: false,
${snippet} = ${value}; usesState: true,
#component.set({ ${dependencies.map((prop: string) => `${prop}: state.${prop}`).join(', ')} }); mutation: `${snippet} = ${value}`,
`; updates: dependencies.map((prop: string) => `${prop}: state.${prop}`)
};
} }
return `#component.set({ ${name}: ${value} });`; return {
usesContext: false,
usesState: false,
mutation: null,
updates: [`${name}: ${value}`]
};
} }
function getBindingValue( function getBindingValue(

@ -0,0 +1,11 @@
export default function stringifyProps(props: string[]) {
if (!props.length) return '{}';
const joined = props.join(', ');
if (joined.length > 40) {
// make larger data objects readable
return `{\n\t${props.join(',\n\t')}\n}`;
}
return `{ ${joined} }`;
}

@ -24,7 +24,7 @@ export default {
assert.equal(component.get('indeterminate'), false); assert.equal(component.get('indeterminate'), false);
assert.equal(component.get('checked'), true); assert.equal(component.get('checked'), true);
assert.equal(target.innerHTML, ` assert.htmlEqual(target.innerHTML, `
<input type="checkbox"> <input type="checkbox">
<p>checked? true</p> <p>checked? true</p>
<p>indeterminate? false</p> <p>indeterminate? false</p>
@ -33,7 +33,7 @@ export default {
component.set({ indeterminate: true }); component.set({ indeterminate: true });
assert.equal(input.indeterminate, true); assert.equal(input.indeterminate, true);
assert.equal(input.checked, true); assert.equal(input.checked, true);
assert.equal(target.innerHTML, ` assert.htmlEqual(target.innerHTML, `
<input type="checkbox"> <input type="checkbox">
<p>checked? true</p> <p>checked? true</p>
<p>indeterminate? true</p> <p>indeterminate? true</p>

Loading…
Cancel
Save