jsx-no-bind.js
1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* @fileoverview Prevents usage of Function.prototype.bind and arrow functions
* in React component definition.
* @author Daniel Lo Nigro <dan.cx>
*/
'use strict';
// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------
module.exports = function(context) {
var configuration = context.options[0] || {};
return {
JSXAttribute: function(node) {
var isRef = configuration.ignoreRefs && node.name.name === 'ref';
if (isRef || !node.value || !node.value.expression) {
return;
}
var valueNode = node.value.expression;
if (
!configuration.allowBind &&
valueNode.type === 'CallExpression' &&
valueNode.callee.type === 'MemberExpression' &&
valueNode.callee.property.name === 'bind'
) {
context.report(node, 'JSX props should not use .bind()');
} else if (
!configuration.allowArrowFunctions &&
valueNode.type === 'ArrowFunctionExpression'
) {
context.report(node, 'JSX props should not use arrow functions');
}
}
};
};
module.exports.schema = [{
type: 'object',
properties: {
allowArrowFunctions: {
default: false,
type: 'boolean'
},
allowBind: {
default: false,
type: 'boolean'
},
ignoreRefs: {
default: false,
type: 'boolean'
}
},
additionalProperties: false
}];