Skip to content

Commit 889461b

Browse files
committed
Fix jsx-handler-names for stateless components (fixes #346)
1 parent be3b7f3 commit 889461b

File tree

2 files changed

+9
-24
lines changed

2 files changed

+9
-24
lines changed

lib/rules/jsx-handler-names.js

+2-24
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,18 @@ module.exports = function(context) {
1414
var eventHandlerPrefix = configuration.eventHandlerPrefix || 'handle';
1515
var eventHandlerPropPrefix = configuration.eventHandlerPropPrefix || 'on';
1616

17-
var EVENT_HANDLER_REGEX = new RegExp('^((this\.props\.' + eventHandlerPropPrefix + ')'
17+
var EVENT_HANDLER_REGEX = new RegExp('^((props\.' + eventHandlerPropPrefix + ')'
1818
+ '|((.*\.)?' + eventHandlerPrefix + ')).+$');
1919
var PROP_EVENT_HANDLER_REGEX = new RegExp('^' + eventHandlerPropPrefix + '.+$');
2020

21-
/**
22-
* Get full prop value for a handler, i.e. `this.props.<name>`
23-
* @param {Object} node.value.expression for JSXAttribute
24-
* @return {String} Full prop value
25-
*/
26-
function rebuildPropValue(valueNode) {
27-
var valueNodeObject = valueNode.object;
28-
var subObjectType = valueNodeObject.object ? valueNodeObject.object.type : '';
29-
var propertyName = valueNodeObject.property && valueNodeObject.property.name ? valueNodeObject.property.name : '';
30-
var propValue = valueNode.property && valueNode.property.name ? valueNode.property.name : '';
31-
32-
if (propertyName.length) {
33-
propValue = propertyName + '.' + propValue;
34-
}
35-
36-
if (subObjectType === 'ThisExpression') {
37-
propValue = 'this.' + propValue;
38-
}
39-
40-
return propValue;
41-
}
42-
4321
return {
4422
JSXAttribute: function(node) {
4523
if (!node.value || !node.value.expression || !node.value.expression.object) {
4624
return;
4725
}
4826

4927
var propKey = typeof node.name === 'object' ? node.name.name : node.name;
50-
var propValue = rebuildPropValue(node.value.expression);
28+
var propValue = context.getSource(node.value.expression).replace(/^this\./, '');
5129

5230
var propIsEventHandler = PROP_EVENT_HANDLER_REGEX.test(propKey);
5331
var propFnIsNamedCorrectly = EVENT_HANDLER_REGEX.test(propValue);

tests/lib/rules/jsx-handler-names.js

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ ruleTester.run('jsx-handler-names', rule, {
5959
ecmaFeatures: {
6060
jsx: true
6161
}
62+
}, {
63+
code: [
64+
'<TestComponent onChange={props.onChange} />'
65+
].join('\n'),
66+
ecmaFeatures: {
67+
jsx: true
68+
}
6269
}],
6370

6471
invalid: [{

0 commit comments

Comments
 (0)