Skip to content

Commit 8093565

Browse files
committed
[New] jsx-handler-names: add checkLocalVariables option
1 parent 5be539b commit 8093565

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

docs/rules/jsx-handler-names.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ The following patterns are **not** considered warnings:
3030
...
3131
"react/jsx-handler-names": [<enabled>, {
3232
"eventHandlerPrefix": <eventHandlerPrefix>,
33-
"eventHandlerPropPrefix": <eventHandlerPropPrefix>
33+
"eventHandlerPropPrefix": <eventHandlerPropPrefix>,
34+
"checkLocalVariables": <boolean>
3435
}]
3536
...
3637
```
3738

3839
* `eventHandlerPrefix`: Prefix for component methods used as event handlers. Defaults to `handle`
3940
* `eventHandlerPropPrefix`: Prefix for props that are used as event handlers. Defaults to `on`
41+
* `checkLocalVariables`: Determines whether event handlers stored as local variables are checked. Defaults to `false`
4042

4143
## When Not To Use It
4244

lib/rules/jsx-handler-names.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ module.exports = {
2121
},
2222

2323
schema: [{
24-
oneOf: [
24+
anyOf: [
2525
{
2626
type: 'object',
2727
properties: {
2828
eventHandlerPrefix: {type: 'string'},
29-
eventHandlerPropPrefix: {type: 'string'}
29+
eventHandlerPropPrefix: {type: 'string'},
30+
checkLocalVariables: {type: 'boolean'}
3031
},
3132
additionalProperties: false
3233
}, {
@@ -36,7 +37,8 @@ module.exports = {
3637
eventHandlerPropPrefix: {
3738
type: 'boolean',
3839
enum: [false]
39-
}
40+
},
41+
checkLocalVariables: {type: 'boolean'}
4042
},
4143
additionalProperties: false
4244
}, {
@@ -46,7 +48,14 @@ module.exports = {
4648
type: 'boolean',
4749
enum: [false]
4850
},
49-
eventHandlerPropPrefix: {type: 'string'}
51+
eventHandlerPropPrefix: {type: 'string'},
52+
checkLocalVariables: {type: 'boolean'}
53+
},
54+
additionalProperties: false
55+
}, {
56+
type: 'object',
57+
properties: {
58+
checkLocalVariables: {type: 'boolean'}
5059
},
5160
additionalProperties: false
5261
}
@@ -75,9 +84,11 @@ module.exports = {
7584
null :
7685
new RegExp(`^(${eventHandlerPropPrefix}[A-Z].*|ref)$`);
7786

87+
const checkLocal = !!configuration.checkLocalVariables;
88+
7889
return {
7990
JSXAttribute(node) {
80-
if (!node.value || !node.value.expression || !node.value.expression.object) {
91+
if (!node.value || !node.value.expression || (!checkLocal && !node.value.expression.object)) {
8192
return;
8293
}
8394

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

+30
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ ruleTester.run('jsx-handler-names', rule, {
3232
code: '<TestComponent onChange={this.handleChange} />'
3333
}, {
3434
code: '<TestComponent onChange={this.props.onChange} />'
35+
}, {
36+
code: '<TestComponent onChange={handleChange} />',
37+
options: [{
38+
checkLocalVariables: true
39+
}]
40+
}, {
41+
code: '<TestComponent onChange={takeCareOfChange} />',
42+
options: [{
43+
checkLocalVariables: false
44+
}]
3545
}, {
3646
code: '<TestComponent onChange={this.props.onFoo} />'
3747
}, {
@@ -99,12 +109,32 @@ ruleTester.run('jsx-handler-names', rule, {
99109
}, {
100110
code: '<TestComponent onChange={this.handlerChange} />',
101111
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}]
112+
}, {
113+
code: '<TestComponent onChange={takeCareOfChange} />',
114+
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}],
115+
options: [{
116+
checkLocalVariables: true
117+
}]
102118
}, {
103119
code: '<TestComponent only={this.handleChange} />',
104120
errors: [{message: 'Prop key for handleChange must begin with \'on\''}]
105121
}, {
106122
code: '<TestComponent handleChange={this.handleChange} />',
107123
errors: [{message: 'Prop key for handleChange must begin with \'on\''}]
124+
}, {
125+
code: '<TestComponent whenChange={handleChange} />',
126+
errors: [{message: 'Prop key for handleChange must begin with \'on\''}],
127+
options: [{
128+
checkLocalVariables: true
129+
}]
130+
}, {
131+
code: '<TestComponent onChange={handleChange} />',
132+
errors: [{message: 'Prop key for handleChange must begin with \'when\''}],
133+
options: [{
134+
checkLocalVariables: true,
135+
eventHandlerPrefix: 'handle',
136+
eventHandlerPropPrefix: 'when'
137+
}]
108138
}, {
109139
code: '<TestComponent onChange={this.onChange} />',
110140
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}]

0 commit comments

Comments
 (0)