babel-plugin-autobind-class-methods
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -6,7 +6,72 @@ 'use strict'; | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
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()])); | ||
}; | ||
var Autobind = function () { | ||
function Autobind(types, path, isClassDeclaration) { | ||
_classCallCheck(this, Autobind); | ||
this.types = types; | ||
this.path = path; | ||
this.isClassDeclaration = isClassDeclaration; | ||
this.classBody = this.path.get('body'); | ||
this.selfIndentifier = isClassDeclaration ? this.path.node.id : this.types.thisExpression(); | ||
} | ||
_createClass(Autobind, [{ | ||
key: 'run', | ||
value: function run() { | ||
this.addBoundMethodExpressions(this.getBoundMethodExpressions()); | ||
} | ||
}, { | ||
key: 'getBoundMethodExpressions', | ||
value: function getBoundMethodExpressions() { | ||
return this.classBody.get('body').filter(this.classMatchesDeclarationType.bind(this)).map(this.buildBoundMethodExpression.bind(this)); | ||
} | ||
}, { | ||
key: 'classMatchesDeclarationType', | ||
value: function classMatchesDeclarationType(node) { | ||
return node.isClassMethod({ kind: 'method' }) && IGNORED_METHODS.indexOf(node.node.key.name) == -1 && this.isClassDeclaration === node.node.static; | ||
} | ||
}, { | ||
key: 'buildBoundMethodExpression', | ||
value: function buildBoundMethodExpression(node) { | ||
var methodIdentifier = this.buildMethodIdentifier(node.node.key.name); | ||
return this.isClassDeclaration ? this.buildBoundClassMethod(methodIdentifier) : this.buildBoundInstanceMethod(methodIdentifier); | ||
} | ||
}, { | ||
key: 'buildMethodIdentifier', | ||
value: function buildMethodIdentifier(methodName) { | ||
return this.types.identifier(methodName); | ||
} | ||
}, { | ||
key: 'buildBoundClassMethod', | ||
value: function buildBoundClassMethod(methodIdentifier) { | ||
return this.types.expressionStatement(this.types.assignmentExpression('=', this.types.memberExpression(this.selfIndentifier, methodIdentifier), this.buildBoundMethod(methodIdentifier))); | ||
} | ||
}, { | ||
key: 'buildBoundInstanceMethod', | ||
value: function buildBoundInstanceMethod(methodIdentifier) { | ||
return this.types.classProperty(methodIdentifier, this.buildBoundMethod(methodIdentifier)); | ||
} | ||
}, { | ||
key: 'buildBoundMethod', | ||
value: function buildBoundMethod(methodIdentifier) { | ||
return this.types.callExpression(this.types.memberExpression(this.types.memberExpression(this.selfIndentifier, methodIdentifier, false), this.types.identifier('bind'), false), [this.selfIndentifier]); | ||
} | ||
}, { | ||
key: 'addBoundMethodExpressions', | ||
value: function addBoundMethodExpressions(boundMethodExpressions) { | ||
return this.isClassDeclaration ? this.path.insertAfter(boundMethodExpressions) : this.classBody.pushContainer('body', boundMethodExpressions); | ||
} | ||
}]); | ||
return Autobind; | ||
}(); | ||
exports.default = function (_ref) { | ||
@@ -16,10 +81,4 @@ var types = _ref.types; | ||
visitor: { | ||
ClassExpression: function ClassExpression(path, state) { | ||
var classBody = path.get('body'); | ||
classBody.get('body').filter(function (node) { | ||
return node.isClassMethod({ kind: 'method' }) && IGNORED_METHODS.indexOf(node.node.key.name) == -1; | ||
}).forEach(function (node) { | ||
classBody.pushContainer('body', buildBoundMethod(types, node.node.key.name)); | ||
}); | ||
Class: function Class(path, state) { | ||
new Autobind(types, path, path.isClassDeclaration()).run(); | ||
} | ||
@@ -26,0 +85,0 @@ } |
{ | ||
"name": "babel-plugin-autobind-class-methods", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Babel plugin that binds every class method to \"this\" with hot reload support", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
99078
75
0