@babel/helper-replace-supers
Advanced tools
Comparing version 7.0.0-beta.44 to 7.0.0-beta.45
333
lib/index.js
@@ -6,4 +6,24 @@ "use strict"; | ||
}); | ||
exports.default = void 0; | ||
exports.default = exports.environmentVisitor = void 0; | ||
function _traverse() { | ||
var data = _interopRequireDefault(require("@babel/traverse")); | ||
_traverse = function _traverse() { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _helperMemberExpressionToFunctions() { | ||
var data = _interopRequireDefault(require("@babel/helper-member-expression-to-functions")); | ||
_helperMemberExpressionToFunctions = function _helperMemberExpressionToFunctions() { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _helperOptimiseCallExpression() { | ||
@@ -33,208 +53,187 @@ var data = _interopRequireDefault(require("@babel/helper-optimise-call-expression")); | ||
var HARDCORE_THIS_REF = new WeakSet(); | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
function isIllegalBareSuper(node, parent) { | ||
if (!t().isSuper(node)) return false; | ||
if (t().isMemberExpression(parent, { | ||
computed: false | ||
})) return false; | ||
if (t().isCallExpression(parent, { | ||
callee: node | ||
})) return false; | ||
return true; | ||
} | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function isMemberExpressionSuper(node) { | ||
return t().isMemberExpression(node) && t().isSuper(node.object); | ||
} | ||
function getPrototypeOfExpression(objectRef, isStatic) { | ||
function getPrototypeOfExpression(objectRef, isStatic, file) { | ||
objectRef = t().cloneNode(objectRef); | ||
var targetRef = isStatic ? objectRef : t().memberExpression(objectRef, t().identifier("prototype")); | ||
return t().logicalExpression("||", t().memberExpression(targetRef, t().identifier("__proto__")), t().callExpression(t().memberExpression(t().identifier("Object"), t().identifier("getPrototypeOf")), [t().cloneNode(targetRef)])); | ||
return t().callExpression(file.addHelper("getPrototypeOf"), [targetRef]); | ||
} | ||
var visitor = { | ||
Function: function Function(path) { | ||
if (!path.isArrowFunctionExpression()) path.skip(); | ||
}, | ||
ClassProperty: function ClassProperty(path) { | ||
if (!path.node.static) path.skip(); | ||
}, | ||
ReturnStatement: function ReturnStatement(path, state) { | ||
if (!path.getFunctionParent().isArrowFunctionExpression()) { | ||
state.returns.push(path); | ||
} | ||
}, | ||
ThisExpression: function ThisExpression(path, state) { | ||
if (!HARDCORE_THIS_REF.has(path.node)) { | ||
state.thises.push(path); | ||
} | ||
}, | ||
enter: function enter(path, state) { | ||
var callback = state.specHandle; | ||
if (state.isLoose) callback = state.looseHandle; | ||
var isBareSuper = path.isCallExpression() && path.get("callee").isSuper(); | ||
var result = callback.call(state, path); | ||
function skipAllButComputedKey(path) { | ||
if (!path.node.computed) { | ||
path.skip(); | ||
return; | ||
} | ||
if (result) { | ||
state.hasSuper = true; | ||
} | ||
var keys = t().VISITOR_KEYS[path.type]; | ||
if (isBareSuper) { | ||
state.bareSupers.push(path); | ||
} | ||
for (var _iterator = keys, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (result === true) { | ||
path.requeue(); | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
if (result !== true && result) { | ||
if (Array.isArray(result)) { | ||
path.replaceWithMultiple(result); | ||
} else { | ||
path.replaceWith(result); | ||
} | ||
} | ||
var key = _ref; | ||
if (key !== "key") path.skipKey(key); | ||
} | ||
} | ||
var environmentVisitor = { | ||
Function: function Function(path) { | ||
if (path.isMethod()) return; | ||
if (path.isArrowFunctionExpression()) return; | ||
path.skip(); | ||
}, | ||
Method: function Method(path) { | ||
skipAllButComputedKey(path); | ||
}, | ||
"ClassProperty|ClassPrivateProperty": function ClassPropertyClassPrivateProperty(path) { | ||
if (path.node.static) return; | ||
skipAllButComputedKey(path); | ||
} | ||
}; | ||
exports.environmentVisitor = environmentVisitor; | ||
var ReplaceSupers = function () { | ||
function ReplaceSupers(opts, inClass) { | ||
if (inClass === void 0) { | ||
inClass = false; | ||
} | ||
this.forceSuperMemoisation = opts.forceSuperMemoisation; | ||
this.methodPath = opts.methodPath; | ||
this.methodNode = opts.methodNode; | ||
this.superRef = opts.superRef; | ||
this.isStatic = opts.isStatic; | ||
this.hasSuper = false; | ||
this.inClass = inClass; | ||
this.inConstructor = opts.inConstructor; | ||
this.isLoose = opts.isLoose; | ||
this.scope = this.methodPath.scope; | ||
this.file = opts.file; | ||
this.opts = opts; | ||
this.bareSupers = []; | ||
this.returns = []; | ||
this.thises = []; | ||
var visitor = _traverse().default.visitors.merge([environmentVisitor, { | ||
Super: function Super(path, state) { | ||
var node = path.node, | ||
parentPath = path.parentPath; | ||
if (!parentPath.isMemberExpression({ | ||
object: node | ||
})) return; | ||
state.handle(parentPath); | ||
} | ||
}]); | ||
var _proto = ReplaceSupers.prototype; | ||
var memoized = new WeakMap(); | ||
var specHandlers = { | ||
memoize: function memoize(superMember) { | ||
var scope = superMember.scope, | ||
node = superMember.node; | ||
var computed = node.computed, | ||
property = node.property; | ||
_proto.getObjectRef = function getObjectRef() { | ||
return t().cloneNode(this.opts.objectRef || this.opts.getObjectRef()); | ||
}; | ||
if (!computed) { | ||
return; | ||
} | ||
_proto.setSuperProperty = function setSuperProperty(property, value, isComputed) { | ||
return t().callExpression(this.file.addHelper("set"), [getPrototypeOfExpression(this.getObjectRef(), this.isStatic), isComputed ? property : t().stringLiteral(property.name), value, t().thisExpression()]); | ||
}; | ||
var memo = scope.maybeGenerateMemoised(property); | ||
_proto.getSuperProperty = function getSuperProperty(property, isComputed) { | ||
if (!memo) { | ||
return; | ||
} | ||
memoized.set(property, memo); | ||
}, | ||
get: function get(superMember) { | ||
var _superMember$node = superMember.node, | ||
computed = _superMember$node.computed, | ||
property = _superMember$node.property; | ||
var thisExpr = t().thisExpression(); | ||
var prop; | ||
if (this.inConstructor) { | ||
thisExpr = t().callExpression(this.file.addHelper("assertThisInitialized"), [thisExpr]); | ||
if (computed && memoized.has(property)) { | ||
prop = t().cloneNode(memoized.get(property)); | ||
} else { | ||
prop = computed ? property : t().stringLiteral(property.name); | ||
} | ||
return t().callExpression(this.file.addHelper("get"), [getPrototypeOfExpression(this.getObjectRef(), this.isStatic), isComputed ? property : t().stringLiteral(property.name), thisExpr]); | ||
}; | ||
return t().callExpression(this.file.addHelper("get"), [getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file), prop, thisExpr]); | ||
}, | ||
set: function set(superMember, value) { | ||
var _superMember$node2 = superMember.node, | ||
computed = _superMember$node2.computed, | ||
property = _superMember$node2.property; | ||
var prop; | ||
_proto.replace = function replace() { | ||
this.methodPath.traverse(visitor, this); | ||
}; | ||
if (computed && memoized.has(property)) { | ||
prop = t().assignmentExpression("=", t().cloneNode(memoized.get(property)), property); | ||
} else { | ||
prop = computed ? property : t().stringLiteral(property.name); | ||
} | ||
_proto.getLooseSuperProperty = function getLooseSuperProperty(id, parent) { | ||
var methodNode = this.methodNode; | ||
var superRef = this.superRef || t().identifier("Function"); | ||
return t().callExpression(this.file.addHelper("set"), [getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file), prop, value, t().thisExpression(), t().booleanLiteral(superMember.isInStrictMode())]); | ||
}, | ||
call: function call(superMember, args) { | ||
return (0, _helperOptimiseCallExpression().default)(this.get(superMember), t().thisExpression(), args); | ||
} | ||
}; | ||
var looseHandlers = { | ||
memoize: specHandlers.memoize, | ||
call: specHandlers.call, | ||
get: function get(superMember) { | ||
var isStatic = this.isStatic, | ||
superRef = this.superRef; | ||
var _superMember$node3 = superMember.node, | ||
property = _superMember$node3.property, | ||
computed = _superMember$node3.computed; | ||
var object; | ||
if (parent.property === id) { | ||
return; | ||
} else if (t().isCallExpression(parent, { | ||
callee: id | ||
})) { | ||
return; | ||
} else if (t().isMemberExpression(parent) && !methodNode.static) { | ||
return t().memberExpression(t().cloneNode(superRef), t().identifier("prototype")); | ||
if (isStatic) { | ||
object = superRef ? t().cloneNode(superRef) : t().memberExpression(t().identifier("Function"), t().identifier("prototype")); | ||
} else { | ||
return t().cloneNode(superRef); | ||
object = superRef ? t().memberExpression(t().cloneNode(superRef), t().identifier("prototype")) : t().memberExpression(t().identifier("Object"), t().identifier("prototype")); | ||
} | ||
}; | ||
_proto.looseHandle = function looseHandle(path) { | ||
var node = path.node; | ||
var prop; | ||
if (path.isSuper()) { | ||
return this.getLooseSuperProperty(node, path.parent); | ||
} else if (path.isCallExpression()) { | ||
var callee = node.callee; | ||
if (!t().isMemberExpression(callee)) return; | ||
if (!t().isSuper(callee.object)) return; | ||
t().appendToMemberExpression(callee, t().identifier("call")); | ||
node.arguments.unshift(t().thisExpression()); | ||
return true; | ||
} | ||
}; | ||
_proto.specHandleAssignmentExpression = function specHandleAssignmentExpression(ref, path, node) { | ||
if (node.operator === "=") { | ||
return this.setSuperProperty(node.left.property, node.right, node.left.computed); | ||
if (computed && memoized.has(property)) { | ||
prop = t().cloneNode(memoized.get(property)); | ||
} else { | ||
ref = ref || path.scope.generateUidIdentifier("ref"); | ||
return [t().variableDeclaration("var", [t().variableDeclarator(t().cloneNode(ref), t().cloneNode(node.left))]), t().expressionStatement(t().assignmentExpression("=", node.left, t().binaryExpression(node.operator.slice(0, -1), t().cloneNode(ref), node.right)))]; | ||
prop = property; | ||
} | ||
}; | ||
_proto.specHandle = function specHandle(path) { | ||
var property; | ||
var computed; | ||
var args; | ||
var parent = path.parent; | ||
var node = path.node; | ||
return t().memberExpression(object, prop, computed); | ||
}, | ||
set: function set(superMember, value) { | ||
var _superMember$node4 = superMember.node, | ||
property = _superMember$node4.property, | ||
computed = _superMember$node4.computed; | ||
var prop; | ||
if (isIllegalBareSuper(node, parent)) { | ||
throw path.buildCodeFrameError("Illegal use of bare super"); | ||
if (computed && memoized.has(property)) { | ||
prop = t().assignmentExpression("=", t().cloneNode(memoized.get(property)), property); | ||
} else { | ||
prop = property; | ||
} | ||
if (t().isCallExpression(node)) { | ||
var callee = node.callee; | ||
return t().assignmentExpression("=", t().memberExpression(t().thisExpression(), prop, computed), value); | ||
} | ||
}; | ||
if (t().isSuper(callee)) { | ||
return; | ||
} else if (isMemberExpressionSuper(callee)) { | ||
property = callee.property; | ||
computed = callee.computed; | ||
args = node.arguments; | ||
} | ||
} else if (t().isMemberExpression(node) && t().isSuper(node.object)) { | ||
property = node.property; | ||
computed = node.computed; | ||
} else if (t().isUpdateExpression(node) && isMemberExpressionSuper(node.argument)) { | ||
var binary = t().assignmentExpression(node.operator[0] + "=", node.argument, t().numericLiteral(1)); | ||
var ReplaceSupers = function () { | ||
function ReplaceSupers(opts) { | ||
var path = opts.methodPath; | ||
this.methodPath = path; | ||
this.isStatic = path.isClassMethod({ | ||
static: true | ||
}) || path.isObjectMethod(); | ||
this.file = opts.file; | ||
this.superRef = opts.superRef; | ||
this.isLoose = opts.isLoose; | ||
this.opts = opts; | ||
} | ||
if (node.prefix) { | ||
return this.specHandleAssignmentExpression(null, path, binary); | ||
} else { | ||
var ref = path.scope.generateUidIdentifier("ref"); | ||
return this.specHandleAssignmentExpression(ref, path, binary).concat(t().expressionStatement(ref)); | ||
} | ||
} else if (t().isAssignmentExpression(node) && isMemberExpressionSuper(node.left)) { | ||
return this.specHandleAssignmentExpression(null, path, node); | ||
} | ||
var _proto = ReplaceSupers.prototype; | ||
if (!property) return; | ||
var superProperty = this.getSuperProperty(property, computed); | ||
if (args) { | ||
return this.optimiseCall(superProperty, args); | ||
} else { | ||
return superProperty; | ||
} | ||
_proto.getObjectRef = function getObjectRef() { | ||
return t().cloneNode(this.opts.objectRef || this.opts.getObjectRef()); | ||
}; | ||
_proto.optimiseCall = function optimiseCall(callee, args) { | ||
var thisNode = t().thisExpression(); | ||
HARDCORE_THIS_REF.add(thisNode); | ||
return (0, _helperOptimiseCallExpression().default)(callee, thisNode, args); | ||
_proto.replace = function replace() { | ||
var handler = this.isLoose ? looseHandlers : specHandlers; | ||
(0, _helperMemberExpressionToFunctions().default)(this.methodPath, visitor, _objectSpread({ | ||
file: this.file, | ||
isStatic: this.isStatic, | ||
getObjectRef: this.getObjectRef.bind(this), | ||
superRef: this.superRef | ||
}, handler)); | ||
}; | ||
@@ -241,0 +240,0 @@ |
{ | ||
"name": "@babel/helper-replace-supers", | ||
"version": "7.0.0-beta.44", | ||
"version": "7.0.0-beta.45", | ||
"description": "Helper function to replace supers", | ||
@@ -9,7 +9,7 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-helper-replace-supers", | ||
"dependencies": { | ||
"@babel/helper-optimise-call-expression": "7.0.0-beta.44", | ||
"@babel/template": "7.0.0-beta.44", | ||
"@babel/traverse": "7.0.0-beta.44", | ||
"@babel/types": "7.0.0-beta.44" | ||
"@babel/helper-member-expression-to-functions": "7.0.0-beta.45", | ||
"@babel/helper-optimise-call-expression": "7.0.0-beta.45", | ||
"@babel/traverse": "7.0.0-beta.45", | ||
"@babel/types": "7.0.0-beta.45" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8512
197
1
+ Added@babel/helper-member-expression-to-functions@7.0.0-beta.45
+ Added@babel/code-frame@7.0.0-beta.45(transitive)
+ Added@babel/generator@7.0.0-beta.45(transitive)
+ Added@babel/helper-function-name@7.0.0-beta.45(transitive)
+ Added@babel/helper-get-function-arity@7.0.0-beta.45(transitive)
+ Added@babel/helper-member-expression-to-functions@7.0.0-beta.45(transitive)
+ Added@babel/helper-optimise-call-expression@7.0.0-beta.45(transitive)
+ Added@babel/helper-split-export-declaration@7.0.0-beta.45(transitive)
+ Added@babel/highlight@7.0.0-beta.45(transitive)
+ Added@babel/template@7.0.0-beta.45(transitive)
+ Added@babel/traverse@7.0.0-beta.45(transitive)
+ Added@babel/types@7.0.0-beta.45(transitive)
+ Addedbabylon@7.0.0-beta.45(transitive)
- Removed@babel/template@7.0.0-beta.44
- Removed@babel/code-frame@7.0.0-beta.44(transitive)
- Removed@babel/generator@7.0.0-beta.44(transitive)
- Removed@babel/helper-function-name@7.0.0-beta.44(transitive)
- Removed@babel/helper-get-function-arity@7.0.0-beta.44(transitive)
- Removed@babel/helper-optimise-call-expression@7.0.0-beta.44(transitive)
- Removed@babel/helper-split-export-declaration@7.0.0-beta.44(transitive)
- Removed@babel/highlight@7.0.0-beta.44(transitive)
- Removed@babel/template@7.0.0-beta.44(transitive)
- Removed@babel/traverse@7.0.0-beta.44(transitive)
- Removed@babel/types@7.0.0-beta.44(transitive)
- Removedbabylon@7.0.0-beta.44(transitive)
Updated@babel/types@7.0.0-beta.45