UNPKG

babel-plugin-autobind-class-methods

Version:

Babel plugin that binds every class method to "this" with hot reload support

26 lines (23 loc) 1.15 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var IGNORED_METHODS = ['componentWillMount', 'componentDidMount', 'componentWillReceiveProps', 'shouldComponentUpdate', 'componentWillUpdate', 'componentDidUpdate', 'componentWillUnmount', 'render']; var buildBoundMethod = function buildBoundMethod(types, methodName) { return types.classProperty(types.identifier(methodName), types.callExpression(types.memberExpression(types.memberExpression(types.thisExpression(), types.identifier(methodName), false), types.identifier('bind'), false), [types.thisExpression()])); }; exports.default = function (_ref) { var types = _ref.types; return { visitor: { ClassExpression: function ClassExpression(path, state) { var classBody = path.get('body'); classBody.pushContainer('body', classBody.get('body').filter(function (node) { return node.isClassMethod({ kind: 'method' }) && IGNORED_METHODS.indexOf(node.node.key.name) == -1 && !node.node.static; }).map(function (node) { return buildBoundMethod(types, node.node.key.name); })); } } }; };