Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autobinding handlers syntax (#109) #131

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,10 @@ $ rt <filename> [<filename> ...] [<args>]`,
type: 'String',
default: reactNativeSupport.default,
description: `'React native version to generate code for (${Object.keys(reactNativeSupport).join(', ')})'`
}, {
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 @@ -199,18 +199,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)},
{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.rt', issue: new RTCodeError('rt-repeat invalid \'in\' expression \'a in b in c\'', 0, 35, 1, 1)},
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 @@ -33,6 +33,14 @@ module.exports = {
testFiles(t, files);
});

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