babel-plugin-autobind-class-methods
Advanced tools
Comparing version 3.0.0 to 4.0.0
128
lib/index.js
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
var template = require('babel-template'); | ||
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 t = _ref.types; | ||
return { | ||
visitor: { | ||
ClassExpression: function ClassExpression(path, state) { | ||
var findBareSupers = { | ||
Super: function Super(path) { | ||
if (path.parentPath.isCallExpression({ callee: path.node })) { | ||
this.push(path.parentPath); | ||
} | ||
} | ||
}; | ||
var isDerived = !!path.node.superClass; | ||
var constructor = void 0; | ||
var body = path.get('body'); | ||
var methods = []; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
var types = _ref.types; | ||
return { | ||
visitor: { | ||
ClassExpression: function ClassExpression(path, state) { | ||
var classBody = path.get('body'); | ||
try { | ||
for (var _iterator = body.get('body')[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var node = _step.value; | ||
if (node.isClassMethod({ kind: 'constructor' })) { | ||
constructor = node; | ||
} else if (node.isClassMethod({ kind: 'method' })) { | ||
methods.push(node.node.key); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
var name = '_makeJsClassGreatAgain'; | ||
var uid = void 0; | ||
if (!state.file.usedHelpers[name]) { | ||
state.file.metadata.usedHelpers.push(name); | ||
uid = state.file.scope.generateUidIdentifier(name); | ||
state.file.usedHelpers[name] = uid; | ||
state.file.path.unshiftContainer('body', template('function UID(_this, funcName, params){return Object.getPrototypeOf(_this)[funcName].apply(_this, params)}')({ UID: uid })); | ||
} else { | ||
uid = state.file.usedHelpers[name]; | ||
} | ||
var newNodes = methods.map(function (method) { | ||
return template('THIS.METHOD = (...rest) => UID(THIS, \'' + method.name + '\', rest)')({ UID: uid, THIS: t.thisExpression(), METHOD: method }); | ||
}); | ||
if (!constructor) { | ||
var newConstructor = t.classMethod('constructor', t.identifier('constructor'), [], t.blockStatement([])); | ||
if (isDerived) { | ||
newConstructor.params = [t.restElement(t.identifier('args'))]; | ||
newConstructor.body.body.push(t.returnStatement(t.callExpression(t.super(), [t.spreadElement(t.identifier('args'))]))); | ||
} | ||
constructor = body.unshiftContainer('body', newConstructor)[0]; | ||
} | ||
if (isDerived) { | ||
var bareSupers = []; | ||
constructor.traverse(findBareSupers, bareSupers); | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
var _loop = function _loop() { | ||
var bareSuper = _step2.value; | ||
newNodes.forEach(function (node) { | ||
bareSuper.insertAfter(node); | ||
}); | ||
}; | ||
for (var _iterator2 = bareSupers[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
_loop(); | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
} else { | ||
newNodes.forEach(function (node) { | ||
constructor.get('body').unshiftContainer('body', node); | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
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)); | ||
}); | ||
} | ||
} | ||
}; | ||
}; |
{ | ||
"name": "babel-plugin-autobind-class-methods", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Babel plugin that binds every class method to \"this\" with hot reload support", | ||
@@ -16,8 +16,8 @@ "main": "lib/index.js", | ||
"type": "git", | ||
"url": "git+https://github.com/UgnisSoftware/babel-plugin-autobind-class-methods.git" | ||
"url": "git+https://github.com/chris-lock/babel-plugin-autobind-class-methods.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/UgnisSoftware/babel-plugin-autobind-class-methods/issues" | ||
"url": "https://github.com/chris-lock/babel-plugin-autobind-class-methods/issues" | ||
}, | ||
"homepage": "https://github.com/UgnisSoftware/babel-plugin-autobind-class-methods#readme", | ||
"homepage": "https://github.com/chris-lock/babel-plugin-autobind-class-methods#readme", | ||
"keywords": [ | ||
@@ -34,3 +34,3 @@ "Babel", | ||
], | ||
"author": "Karolis Masiulis", | ||
"author": "Karolis Masiulis & Chris Lock", | ||
"license": "MIT", | ||
@@ -37,0 +37,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
96360
7
22
1