Socket
Socket
Sign inDemoInstall

babel-plugin-autobind-class-methods

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.2.0 to 5.0.1

81

lib/index.js

@@ -6,72 +6,7 @@ '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.isClassDeclaration = isClassDeclaration;
this.classBody = path.get('body');
this.selfIndentifier = isClassDeclaration ? path.node.id : this.types.thisExpression();
this.currentPath = path.parentPath.isExportDeclaration() ? path.parentPath : path;
}
_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.currentPath.insertAfter(boundMethodExpressions) : this.classBody.pushContainer('body', boundMethodExpressions);
}
}]);
return Autobind;
}();
exports.default = function (_ref) {

@@ -81,4 +16,10 @@ var types = _ref.types;

visitor: {
Class: function Class(path, state) {
new Autobind(types, path, path.isClassDeclaration()).run();
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);
}));
}

@@ -85,0 +26,0 @@ }

2

package.json
{
"name": "babel-plugin-autobind-class-methods",
"version": "4.2.0",
"version": "5.0.1",
"description": "Babel plugin that binds every class method to \"this\" with hot reload support",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc