canonical
Version:
Canonical code style linter and formatter for JavaScript, SCSS and CSS.
52 lines (47 loc) • 1.36 kB
JavaScript
/**
* @fileoverview Prevents usage of Function.prototype.bind and arrow functions
* in React component definition.
* @author Daniel Lo Nigro <dan.cx>
*/
;
// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------
module.exports = function(context) {
var configuration = context.options[0] || {};
return {
JSXAttribute: function(node) {
if (!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'
}
},
additionalProperties: false
}];