Skip to content

Commit

Permalink
Merge branch 'master' into gh-1122
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 11, 2018
2 parents fcae19b + b3049a6 commit dd5e0f7
Show file tree
Hide file tree
Showing 24 changed files with 226 additions and 62 deletions.
90 changes: 38 additions & 52 deletions src/generators/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,21 @@ export default class Component extends Node {
block.addVariable(name_updating, '{}');
statements.push(`var ${name_initial_data} = ${initialPropString};`);

const setParentFromChildOnChange = new CodeBuilder();
const setParentFromChildOnInit = new CodeBuilder();
let hasLocalBindings = false;
let hasStoreBindings = false;

const setStoreFromChildOnChange = new CodeBuilder();
const setStoreFromChildOnInit = new CodeBuilder();
const builder = new CodeBuilder();

bindings.forEach((binding: Binding) => {
let { name: key } = getObject(binding.value);

const isStoreProp = generator.options.store && key[0] === '$';
if (isStoreProp) key = key.slice(1);
const newState = isStoreProp ? 'newStoreState' : 'newState';

binding.contexts.forEach(context => {
allContexts.add(context);
});

let setFromChild;

if (!isStoreProp && block.contexts.has(key)) {
const prop = binding.dependencies[0];
if (block.contexts.has(key)) {
const computed = isComputed(binding.value);
const tail = binding.value.type === 'MemberExpression' ? getTailSnippet(binding.value) : '';

Expand All @@ -167,20 +161,38 @@ export default class Component extends Node {
list[index]${tail} = childState.${binding.name};
${binding.dependencies
.map((prop: string) => `${newState}.${prop} = state.${prop};`)
.join('\n')}
`;
}
.map((name: string) => {
const isStoreProp = generator.options.store && name[0] === '$';
const prop = isStoreProp ? name.slice(1) : name;
const newState = isStoreProp ? 'newStoreState' : 'newState';
else if (binding.value.type === 'MemberExpression') {
setFromChild = deindent`
${binding.snippet} = childState.${binding.name};
${binding.dependencies.map((prop: string) => `${newState}.${prop} = state.${prop};`).join('\n')}
if (isStoreProp) hasStoreBindings = true;
else hasLocalBindings = true;
return `${newState}.${prop} = state.${name};`;
})
.join('\n')}
`;
}

else {
setFromChild = `${newState}.${key} = childState.${binding.name};`;
const isStoreProp = generator.options.store && key[0] === '$';
const prop = isStoreProp ? key.slice(1) : key;
const newState = isStoreProp ? 'newStoreState' : 'newState';

if (isStoreProp) hasStoreBindings = true;
else hasLocalBindings = true;

if (binding.value.type === 'MemberExpression') {
setFromChild = deindent`
${binding.snippet} = childState.${binding.name};
${newState}.${prop} = state.${key};
`;
}

else {
setFromChild = `${newState}.${prop} = childState.${binding.name};`;
}
}

statements.push(deindent`
Expand All @@ -190,16 +202,11 @@ export default class Component extends Node {
}`
);

(isStoreProp ? setStoreFromChildOnChange : setParentFromChildOnChange).addConditional(
builder.addConditional(
`!${name_updating}.${binding.name} && changed.${binding.name}`,
setFromChild
);

(isStoreProp ? setStoreFromChildOnInit : setParentFromChildOnInit).addConditional(
`!${name_updating}.${binding.name}`,
setFromChild
);

// TODO could binding.dependencies.length ever be 0?
if (binding.dependencies.length) {
updates.push(deindent`
Expand All @@ -215,44 +222,23 @@ export default class Component extends Node {

const initialisers = [
'state = #component.get()',
!setParentFromChildOnChange.isEmpty() && 'newState = {}',
!setStoreFromChildOnChange.isEmpty() && 'newStoreState = {}',
hasLocalBindings && 'newState = {}',
hasStoreBindings && 'newStoreState = {}',
].filter(Boolean).join(', ');

componentInitProperties.push(deindent`
_bind: function(changed, childState) {
var ${initialisers};
${!setStoreFromChildOnChange.isEmpty() && deindent`
${setStoreFromChildOnChange}
${name_updating} = @assign({}, changed);
#component.store.set(newStoreState);
`}
${!setParentFromChildOnChange.isEmpty() && deindent`
${setParentFromChildOnChange}
${name_updating} = @assign({}, changed);
#component._set(newState);
`}
${builder}
${hasStoreBindings && `#component.store.set(newStoreState);`}
${hasLocalBindings && `#component._set(newState);`}
${name_updating} = {};
}
`);

// TODO can `!childState` ever be true?
beforecreate = deindent`
#component.root._beforecreate.push(function() {
var childState = ${name}.get(), ${initialisers};
if (!childState) return;
${setParentFromChildOnInit}
${!setStoreFromChildOnInit.isEmpty() && deindent`
${setStoreFromChildOnInit}
${name_updating} = { ${bindings.map((binding: Binding) => `${binding.name}: true`).join(', ')} };
#component.store.set(newStoreState);
`}
${!setParentFromChildOnInit.isEmpty() && deindent`
${setParentFromChildOnInit}
${name_updating} = { ${bindings.map((binding: Binding) => `${binding.name}: true`).join(', ')} };
#component._set(newState);
`}
${name_updating} = {};
${name}._bind({ ${bindings.map(b => `${b.name}: 1`).join(', ')} }, ${name}.get());
});
`;
} else if (initialProps.length) {
Expand Down
4 changes: 2 additions & 2 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ export class Parser {
throw new ParseError(message, this.template, index, this.filename);
}

eat(str: string, required?: boolean) {
eat(str: string, required?: boolean, message?: string) {
if (this.match(str)) {
this.index += str.length;
return true;
}

if (required) {
this.error(`Expected ${str}`);
this.error(message || `Expected ${str}`);
}

return false;
Expand Down
4 changes: 1 addition & 3 deletions src/parse/state/mustache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Parser } from '../index';
import { Node } from '../../interfaces';

function trimWhitespace(block: Node, trimBefore: boolean, trimAfter: boolean) {
if (!block.children) return; // AwaitBlock
if (!block.children || block.children.length === 0) return; // AwaitBlock

const firstChild = block.children[0];
const lastChild = block.children[block.children.length - 1];
Expand Down Expand Up @@ -74,8 +74,6 @@ export default function mustache(parser: Parser) {
}

// strip leading/trailing whitespace as necessary
if (block.children && !block.children.length) parser.error(`Empty block`, block.start);

const charBefore = parser.template[block.start - 1];
const charAfter = parser.template[parser.index];
const trimBefore = !charBefore || whitespace.test(charBefore);
Expand Down
2 changes: 1 addition & 1 deletion src/parse/state/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function tag(parser: Parser) {

if (parser.eat('!--')) {
const data = parser.readUntil(/-->/);
parser.eat('-->');
parser.eat('-->', true, 'comment was left open, expected -->');

parser.current().children.push({
start,
Expand Down
11 changes: 11 additions & 0 deletions src/validate/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const meta = new Map([
[':Head', validateHead]
]);

function isEmptyBlock(node: Node) {
if (!/Block$/.test(node.type) || !node.children) return false;
if (node.children.length > 1) return false;
const child = node.children[0];
return !child || (child.type === 'Text' && !/\S/.test(child.data));
}

export default function validateHtml(validator: Validator, html: Node) {
const refs = new Map();
const refCallees: Node[] = [];
Expand Down Expand Up @@ -58,6 +65,10 @@ export default function validateHtml(validator: Validator, html: Node) {
}
}

if (validator.options.dev && isEmptyBlock(node)) {
validator.warn('Empty block', node.start);
}

if (node.children) {
if (node.type === 'Element') elementStack.push(node);
stack.push(node);
Expand Down
5 changes: 3 additions & 2 deletions src/validate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default function validate(
stylesheet: Stylesheet,
options: CompileOptions
) {
const { onwarn, onerror, name, filename, store } = options;
const { onwarn, onerror, name, filename, store, dev } = options;

try {
if (name && !/^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test(name)) {
Expand All @@ -114,7 +114,8 @@ export default function validate(
onwarn,
name,
filename,
store
store,
dev
});

if (parsed.js) {
Expand Down
8 changes: 8 additions & 0 deletions test/parser/samples/error-comment-unclosed/error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"message": "comment was left open, expected -->",
"loc": {
"line": 1,
"column": 24
},
"pos": 24
}
1 change: 1 addition & 0 deletions test/parser/samples/error-comment-unclosed/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- an unclosed comment
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input bind:value>
42 changes: 42 additions & 0 deletions test/runtime/samples/store-component-binding-deep/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Store } from '../../../../store.js';

const store = new Store({
name: {
value: 'world'
}
});

export default {
store,

html: `
<h1>Hello world!</h1>
<input>
`,

test(assert, component, target, window) {
const input = target.querySelector('input');
const event = new window.Event('input');

const changeRecord = [];
store.onchange((state, changes) => {
changeRecord.push({ state, changes });
});

input.value = 'everybody';
input.dispatchEvent(event);

assert.equal(store.get('name').value, 'everybody');
assert.htmlEqual(target.innerHTML, `
<h1>Hello everybody!</h1>
<input>
`);

assert.deepEqual(changeRecord, [
{
state: { name: { value: 'everybody' } },
changes: { name: true }
}
]);
}
};
10 changes: 10 additions & 0 deletions test/runtime/samples/store-component-binding-deep/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Hello {{$name.value}}!</h1>
<TextInput bind:value=$name.value/>

<script>
import TextInput from './TextInput.html';

export default {
components: { TextInput }
};
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input bind:value>
30 changes: 30 additions & 0 deletions test/runtime/samples/store-component-binding-each/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Store } from '../../../../store.js';

const store = new Store({
a: ['foo', 'bar', 'baz']
});

export default {
store,

html: `
<input><input><input>
<p>foo, bar, baz</p>
`,

test(assert, component, target, window) {
const event = new window.MouseEvent('input');
const inputs = target.querySelectorAll('input');

inputs[0].value = 'blah';
inputs[0].dispatchEvent(event);

assert.deepEqual(store.get('a'), ['blah', 'bar', 'baz']);
assert.htmlEqual(target.innerHTML, `
<input><input><input>
<p>blah, bar, baz</p>
`);

component.destroy();
},
};
15 changes: 15 additions & 0 deletions test/runtime/samples/store-component-binding-each/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{#each $a as x}}
<Widget bind:value='x'/>
{{/each}}

<p>{{$a.join(', ')}}</p>

<script>
import Widget from './Widget.html';

export default {
components: {
Widget
}
};
</script>
12 changes: 12 additions & 0 deletions test/runtime/samples/store-component-binding/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export default {
const input = target.querySelector('input');
const event = new window.Event('input');

const changeRecord = [];
store.onchange((state, changes) => {
changeRecord.push({ state, changes });
});

input.value = 'everybody';
input.dispatchEvent(event);

Expand All @@ -24,5 +29,12 @@ export default {
<h1>Hello everybody!</h1>
<input>
`);

assert.deepEqual(changeRecord, [
{
state: { name: 'everybody' },
changes: { name: true }
}
]);
}
};
Loading

0 comments on commit dd5e0f7

Please sign in to comment.