Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ido committed Jan 9, 2017
1 parent 696aa2b commit d47ef7e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,10 @@ $ rt <filename|glob> [<filename|glob> ...] [<args>]`,
type: 'Boolean',
default: 'false',
description: 'Remove repeating whitespace from HTML text.'
}, {
option: 'autobind',
type: 'Boolean',
default: 'false',
description: 'Automatically bind event handlers to components'
}]
});
32 changes: 21 additions & 11 deletions src/reactTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,28 @@ function generateProps(node, context) {
}

function handleEventHandler(val, context, node, key) {
const funcParts = val.split('=>');
if (funcParts.length !== 2) {
throw RTCodeError.build(context, node, `when using 'on' events, use lambda '(p1,p2)=>body' notation or use {} to return a callback function. error: [${key}='${val}']`);
}
const evtParams = funcParts[0].replace('(', '').replace(')', '').trim();
const funcBody = funcParts[1].trim();
let params = context.boundParams;
if (evtParams.trim() !== '') {
params = params.concat([evtParams.trim()]);
let handlerString;
if (_.startsWith(val, 'this.')) {
if (context.options.autobind) {
handlerString = `${val}.bind(this)`;
} else {
throw RTCodeError.build(context, node, "'this.handler' syntax allowed only when the --autobind is on, use {} to return a callback function.");
}
} else {
const funcParts = val.split('=>');
if (funcParts.length !== 2) {
throw RTCodeError.build(context, node, `when using 'on' events, use lambda '(p1,p2)=>body' notation or 'this.handler'; otherwise use {} to return a callback function. error: [${key}='${val}']`);
}
const evtParams = funcParts[0].replace('(', '').replace(')', '').trim();
const funcBody = funcParts[1].trim();
let params = context.boundParams;
if (evtParams.trim() !== '') {
params = params.concat([evtParams.trim()]);
}
const generatedFuncName = generateInjectedFunc(context, key, funcBody, params);
handlerString = genBind(generatedFuncName, context.boundParams);
}
const generatedFuncName = generateInjectedFunc(context, key, funcBody, params);
return genBind(generatedFuncName, context.boundParams);
return handlerString;
}

function genBind(func, args) {
Expand Down
2 changes: 2 additions & 0 deletions test/data/autobind.rt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div onKeyDown="this.handleKeyDown"></div>

10 changes: 10 additions & 0 deletions test/data/autobind.rt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
define([
'react',
'lodash'
], function (React, _) {
'use strict';
return function () {
return React.createElement('div', { 'onKeyDown': this.handleKeyDown.bind(this) });
};
});

4 changes: 4 additions & 0 deletions test/data/invalid/invalid-autobind.rt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div onKeyDown="this.handleKeyDown">
This should trigger an error because evaluated with no autobinding (--autobind=false)
</div>

3 changes: 2 additions & 1 deletion test/src/rt.invalid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module.exports = {
{file: 'invalid-scope.rt', issue: new RTCodeError("invalid scope part 'a in a in a'", 0, 35, 1, 1)},
{file: 'invalid-html.rt', issue: new RTCodeError('Document should have a root element', -1, -1, -1, -1)},
{file: 'invalid-exp.rt', issue: new RTCodeError("Failed to parse text '\n {z\n'", 5, 13, 1, 6)},
{file: 'invalid-lambda.rt', issue: new RTCodeError("when using 'on' events, use lambda '(p1,p2)=>body' notation or use {} to return a callback function. error: [onClick='']", 0, 23, 1, 1)},
{file: 'invalid-lambda.rt', issue: new RTCodeError("when using 'on' events, use lambda '(p1,p2)=>body' notation or 'this.handler'; otherwise use {} to return a callback function. error: [onClick='']", 0, 23, 1, 1)},
{file: 'invalid-autobind.rt', issue: new RTCodeError("'this.handler' syntax allowed only when the --autobind is on, use {} to return a callback function.", 0, 132, 1, 1)},
// {file: 'invalid-js.rt', issue: new RTCodeError('Unexpected token ILLEGAL', 0, 32, 1, 1)}, bug interduced due to scope parsing
{file: 'invalid-single-root.rt', issue: new RTCodeError('Document should have no more than a single root element', 12, 23, 2, 1)},
{file: 'invalid-repeat-1.rt', issue: new RTCodeError("rt-repeat invalid 'in' expression 'a in b in c'", 9, 44, 2, 4)},
Expand Down
8 changes: 8 additions & 0 deletions test/src/rt.valid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ module.exports = {
testFiles(t, files, options);
});

test('autobinding conversion test', t => {
const options = {
autobind: true
};
const files = ['autobind.rt'];
testFiles(t, files, options);
});

test('prop template conversion test', t => {
const options = {
propTemplates: {
Expand Down

0 comments on commit d47ef7e

Please sign in to comment.