forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
add052f
commit eab3bd8
Showing
5 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Enforce event handler naming conventions in JSX (jsx-handler-names) | ||
|
||
Ensures that any component or prop methods used to handle events are correctly prefixed. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
<MyComponent handleChange={this.handleChange} /> | ||
``` | ||
|
||
```js | ||
<MyComponent onChange={this.componentChanged} /> | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
<MyComponent onChange={this.handleChange} /> | ||
``` | ||
|
||
```js | ||
<MyComponent onChange={this.props.onFoo} /> | ||
``` | ||
|
||
## Rule Options | ||
|
||
```js | ||
... | ||
"jsx-handler-names": [<enabled>, { | ||
"eventHandlerPrefix": <eventHandlerPrefix>, | ||
"eventHandlerPropPrefix": <eventHandlerPropPrefix> | ||
}] | ||
... | ||
``` | ||
|
||
* `eventHandlerPrefix`: Prefix for component methods used as event handlers. Defaults to `handle` | ||
* `eventHandlerPropPrefix`: Prefix for props that are used as event handlers. Defaults to `on` | ||
|
||
## When Not To Use It | ||
|
||
If you are not using JSX, or if you don't want to enforce specific naming conventions for event handlers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @fileoverview Enforce event handler naming conventions in JSX | ||
* @author Jake Marsh | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
var configuration = context.options[0] || {}; | ||
var eventHandlerPrefix = configuration.eventHandlerPrefix || 'handle'; | ||
var eventHandlerPropPrefix = configuration.eventHandlerPropPrefix || 'on'; | ||
|
||
var EVENT_HANDLER_REGEX = new RegExp('^((this\.props\.' + eventHandlerPropPrefix + ')' | ||
+ '|((.*\.)?' + eventHandlerPrefix + ')).+$'); | ||
var PROP_EVENT_HANDLER_REGEX = new RegExp('^' + eventHandlerPropPrefix + '.+$'); | ||
|
||
/** | ||
* Get full prop value for a handler, i.e. `this.props.<name>` | ||
* @param {Object} node.value.expression for JSXAttribute | ||
* @return {String} Full prop value | ||
*/ | ||
function rebuildPropValue(valueNode) { | ||
var valueNodeObject = valueNode.object; | ||
var subObjectType = valueNodeObject.object ? valueNodeObject.object.type : ''; | ||
var propertyName = valueNodeObject.property ? valueNodeObject.property.name : ''; | ||
var propValue = valueNode.property ? valueNode.property.name : ''; | ||
|
||
if (propertyName.length) { | ||
propValue = propertyName + '.' + propValue; | ||
} | ||
|
||
if (subObjectType === 'ThisExpression') { | ||
propValue = 'this.' + propValue; | ||
} | ||
|
||
return propValue; | ||
} | ||
|
||
return { | ||
JSXAttribute: function(node) { | ||
if (!node.value || !node.value.expression || !node.value.expression.object) { | ||
return; | ||
} | ||
|
||
var propKey = typeof node.name === 'object' ? node.name.name : node.name; | ||
var propValue = rebuildPropValue(node.value.expression); | ||
|
||
var propIsEventHandler = PROP_EVENT_HANDLER_REGEX.test(propKey); | ||
var propFnIsNamedCorrectly = EVENT_HANDLER_REGEX.test(propValue); | ||
var eventName; | ||
|
||
if (propIsEventHandler && !propFnIsNamedCorrectly) { | ||
eventName = propKey.split(eventHandlerPropPrefix)[1]; | ||
context.report( | ||
node, | ||
'Handler function for ' + propKey + ' prop key must be named ' + eventHandlerPrefix + eventName | ||
); | ||
} else if (propFnIsNamedCorrectly && !propIsEventHandler) { | ||
eventName = propValue.split(eventHandlerPrefix)[1]; | ||
context.report( | ||
node, | ||
'Prop key for ' + propValue + ' must be named ' + eventHandlerPropPrefix + eventName | ||
); | ||
} | ||
} | ||
}; | ||
|
||
}; | ||
|
||
module.exports.schema = [{ | ||
type: 'object', | ||
properties: { | ||
eventHandlerPrefix: { | ||
type: 'string' | ||
}, | ||
eventHandlerPropPrefix: { | ||
type: 'string' | ||
} | ||
}, | ||
additionalProperties: false | ||
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* @fileoverview Tests for jsx-handler-names | ||
* @author Jake Marsh | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/jsx-handler-names'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('jsx-handler-names', rule, { | ||
valid: [{ | ||
code: [ | ||
'<TestComponent onChange={this.handleChange} />' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}, { | ||
code: [ | ||
'<TestComponent onChange={this.props.onChange} />' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}, { | ||
code: [ | ||
'<TestComponent onChange={this.props.onFoo} />' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}, { | ||
code: [ | ||
'<TestComponent isSelected={this.props.isSelected} />' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}, { | ||
code: [ | ||
'<TestComponent shouldDisplay={this.state.shouldDisplay} />' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}], | ||
|
||
invalid: [{ | ||
code: [ | ||
'<TestComponent onChange={this.doSomethingOnChange} />' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
}, | ||
errors: [{message: 'Handler function for onChange prop key must be named handleChange'}] | ||
}, { | ||
code: [ | ||
'<TestComponent handleChange={this.handleChange} />' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
}, | ||
errors: [{message: 'Prop key for handleChange must be named onChange'}] | ||
}, { | ||
code: [ | ||
'<TestComponent onChange={this.onChange} />' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
}, | ||
errors: [{message: 'Handler function for onChange prop key must be named handleChange'}] | ||
}] | ||
}); |