Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-traverse

Package Overview
Dependencies
Maintainers
3
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-traverse - npm Package Compare versions

Comparing version 7.0.0-alpha.9 to 7.0.0-alpha.10

20

lib/path/ancestry.js

@@ -14,3 +14,2 @@ "use strict";

exports.inType = inType;
exports.inShadow = inShadow;

@@ -215,21 +214,2 @@ var _babelTypes = require("babel-types");

return false;
}
function inShadow(key) {
var parentFn = this.isFunction() ? this : this.findParent(function (p) {
return p.isFunction();
});
if (!parentFn) return;
if (parentFn.isFunctionExpression() || parentFn.isFunctionDeclaration()) {
var shadow = parentFn.node.shadow;
if (shadow && (!key || shadow[key] !== false)) {
return parentFn;
}
} else if (parentFn.isArrowFunctionExpression()) {
return parentFn;
}
return null;
}

355

lib/path/conversion.js

@@ -7,2 +7,4 @@ "use strict";

exports.arrowFunctionToShadowed = arrowFunctionToShadowed;
exports.unwrapFunctionEnvironment = unwrapFunctionEnvironment;
exports.arrowFunctionToExpression = arrowFunctionToExpression;

@@ -13,2 +15,8 @@ var _babelTypes = require("babel-types");

var _babelHelperFunctionName = require("babel-helper-function-name");
var _babelHelperFunctionName2 = _interopRequireDefault(_babelHelperFunctionName);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

@@ -42,9 +50,348 @@

this.arrowFunctionToExpression();
}
function unwrapFunctionEnvironment() {
if (!this.isArrowFunctionExpression() && !this.isFunctionExpression() && !this.isFunctionDeclaration()) {
throw this.buildCodeFrameError("Can only unwrap the environment of a function.");
}
hoistFunctionEnvironment(this);
}
function arrowFunctionToExpression() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$allowInsertArrow = _ref.allowInsertArrow,
allowInsertArrow = _ref$allowInsertArrow === undefined ? true : _ref$allowInsertArrow,
_ref$specCompliant = _ref.specCompliant,
specCompliant = _ref$specCompliant === undefined ? false : _ref$specCompliant;
if (!this.isArrowFunctionExpression()) {
throw this.buildCodeFrameError("Cannot convert non-arrow function to a function expression.");
}
var thisBinding = hoistFunctionEnvironment(this, specCompliant, allowInsertArrow);
this.ensureBlock();
this.node.type = "FunctionExpression";
if (specCompliant) {
var checkBinding = thisBinding ? null : this.parentPath.scope.generateUidIdentifier("arrowCheckId");
if (checkBinding) this.parentPath.scope.push({ id: checkBinding, init: t.objectExpression([]) });
var node = this.node;
this.get("body").unshiftContainer("body", t.expressionStatement(t.callExpression(this.hub.file.addHelper("newArrowCheck"), [t.thisExpression(), checkBinding ? t.identifier(checkBinding.name) : t.identifier(thisBinding)])));
node.expression = false;
node.type = "FunctionExpression";
node.shadow = node.shadow || true;
this.replaceWith(t.callExpression(t.memberExpression((0, _babelHelperFunctionName2.default)(this) || this.node, t.identifier("bind")), [checkBinding ? t.identifier(checkBinding.name) : t.thisExpression()]));
}
}
function hoistFunctionEnvironment(fnPath) {
var specCompliant = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var allowInsertArrow = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var thisEnvFn = fnPath.findParent(function (p) {
return p.isFunction() && !p.isArrowFunctionExpression() || p.isProgram() || p.isClassProperty();
});
var inConstructor = thisEnvFn && thisEnvFn.node.kind === "constructor";
if (thisEnvFn.isClassProperty()) {
throw fnPath.buildCodeFrameError("Unable to transform arrow inside class property");
}
var _getScopeInformation = getScopeInformation(fnPath),
thisPaths = _getScopeInformation.thisPaths,
argumentsPaths = _getScopeInformation.argumentsPaths,
newTargetPaths = _getScopeInformation.newTargetPaths,
superProps = _getScopeInformation.superProps,
superCalls = _getScopeInformation.superCalls;
if (inConstructor && superCalls.length > 0) {
if (!allowInsertArrow) {
throw superCalls[0].buildCodeFrameError("Unable to handle nested super() usage in arrow");
}
var allSuperCalls = [];
thisEnvFn.traverse({
Function: function Function(child) {
if (child.isArrowFunctionExpression() || child.isClassProperty() || child === fnPath) return;
child.skip();
},
CallExpression: function CallExpression(child) {
if (!child.get("callee").isSuper()) return;
allSuperCalls.push(child);
}
});
var superBinding = getSuperBinding(thisEnvFn);
allSuperCalls.forEach(function (superCall) {
return superCall.get("callee").replaceWith(t.identifier(superBinding));
});
}
var thisBinding = void 0;
if (thisPaths.length > 0 || specCompliant) {
thisBinding = getThisBinding(thisEnvFn, inConstructor);
if (!specCompliant || inConstructor && hasSuperClass(thisEnvFn)) {
thisPaths.forEach(function (thisChild) {
thisChild.replaceWith(thisChild.isJSX() ? t.jSXIdentifier(thisBinding) : t.identifier(thisBinding));
});
if (specCompliant) thisBinding = null;
}
}
if (argumentsPaths.length > 0) {
var argumentsBinding = getBinding(thisEnvFn, "arguments", function () {
return t.identifier("arguments");
});
argumentsPaths.forEach(function (argumentsChild) {
argumentsChild.replaceWith(t.identifier(argumentsBinding));
});
}
if (newTargetPaths.length > 0) {
var newTargetBinding = getBinding(thisEnvFn, "newtarget", function () {
return t.metaProperty(t.identifier("new"), t.identifier("target"));
});
newTargetPaths.forEach(function (argumentsChild) {
argumentsChild.replaceWith(t.identifier(newTargetBinding));
});
}
if (superProps.length > 0) {
if (!allowInsertArrow) {
throw superProps[0].buildCodeFrameError("Unable to handle nested super.prop usage");
}
var flatSuperProps = superProps.reduce(function (acc, superProp) {
return acc.concat(standardizeSuperProperty(superProp));
}, []);
flatSuperProps.forEach(function (superProp) {
var key = superProp.node.computed ? "" : superProp.get("property").node.name;
if (superProp.parentPath.isCallExpression({ callee: superProp.node })) {
var _superBinding = getSuperPropCallBinding(thisEnvFn, key);
if (superProp.node.computed) {
var prop = superProp.get("property").node;
superProp.replaceWith(t.identifier(_superBinding));
superProp.parentPath.node.arguments.unshift(prop);
} else {
superProp.replaceWith(t.identifier(_superBinding));
}
} else {
var isAssignment = superProp.parentPath.isAssignmentExpression({ left: superProp.node });
var _superBinding2 = getSuperPropBinding(thisEnvFn, isAssignment, key);
var args = [];
if (superProp.node.computed) {
args.push(superProp.get("property").node);
}
if (isAssignment) {
var value = superProp.parentPath.node.right;
args.push(value);
superProp.parentPath.replaceWith(t.callExpression(t.identifier(_superBinding2), args));
} else {
superProp.replaceWith(t.callExpression(t.identifier(_superBinding2), args));
}
}
});
}
return thisBinding;
}
function standardizeSuperProperty(superProp) {
if (superProp.parentPath.isAssignmentExpression() && superProp.parentPath.node.operator !== "=") {
var assignmentPath = superProp.parentPath;
var op = assignmentPath.node.operator.slice(0, -1);
var value = assignmentPath.node.right;
assignmentPath.node.operator = "=";
if (superProp.node.computed) {
var tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
assignmentPath.get("left").replaceWith(t.memberExpression(superProp.node.object, t.assignmentExpression("=", tmp, superProp.node.property), true));
assignmentPath.get("right").replaceWith(t.binaryExpression(op, t.memberExpression(superProp.node.object, t.identifier(tmp.name), true), value));
} else {
assignmentPath.get("left").replaceWith(t.memberExpression(superProp.node.object, superProp.node.property));
assignmentPath.get("right").replaceWith(t.binaryExpression(op, t.memberExpression(superProp.node.object, t.identifier(superProp.node.property.name)), value));
}
return [assignmentPath.get("left"), assignmentPath.get("right").get("left")];
} else if (superProp.parentPath.isUpdateExpression()) {
var updateExpr = superProp.parentPath;
var _tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
var computedKey = superProp.node.computed ? superProp.scope.generateDeclaredUidIdentifier("prop") : null;
var parts = [t.assignmentExpression("=", _tmp, t.memberExpression(superProp.node.object, computedKey ? t.assignmentExpression("=", computedKey, superProp.node.property) : superProp.node.property, superProp.node.computed)), t.assignmentExpression("=", t.memberExpression(superProp.node.object, computedKey ? t.identifier(computedKey.name) : superProp.node.property, superProp.node.computed), t.binaryExpression("+", t.identifier(_tmp.name), t.numericLiteral(1)))];
if (!superProp.parentPath.node.prefix) {
parts.push(t.identifier(_tmp.name));
}
updateExpr.replaceWith(t.sequenceExpression(parts));
var left = updateExpr.get("expressions.0.right");
var right = updateExpr.get("expressions.1.left");
return [left, right];
}
return [superProp];
}
function hasSuperClass(thisEnvFn) {
return thisEnvFn.isClassMethod() && !!thisEnvFn.parentPath.parentPath.node.superClass;
}
function getThisBinding(thisEnvFn, inConstructor) {
var _this = this;
return getBinding(thisEnvFn, "this", function (thisBinding) {
if (!inConstructor || !hasSuperClass(thisEnvFn)) return t.thisExpression();
var supers = new WeakSet();
thisEnvFn.traverse({
Function: function Function(child) {
if (child.isArrowFunctionExpression() || child.isClassProperty() || child === _this) return;
child.skip();
},
CallExpression: function CallExpression(child) {
if (!child.get("callee").isSuper()) return;
if (supers.has(child.node)) return;
supers.add(child.node);
child.replaceWith(t.assignmentExpression("=", t.identifier(thisBinding), child.node));
}
});
});
}
function getSuperBinding(thisEnvFn) {
return getBinding(thisEnvFn, "supercall", function () {
var argsBinding = thisEnvFn.scope.generateUidIdentifier("args");
return t.arrowFunctionExpression([t.restElement(argsBinding)], t.callExpression(t.super(), [t.spreadElement(t.identifier(argsBinding.name))]));
});
}
function getSuperPropCallBinding(thisEnvFn, propName) {
return getBinding(thisEnvFn, "superprop_call:" + (propName || ""), function () {
var argsBinding = thisEnvFn.scope.generateUidIdentifier("args");
var argsList = [t.restElement(argsBinding)];
var fnBody = void 0;
if (propName) {
fnBody = t.callExpression(t.memberExpression(t.super(), t.identifier(propName)), [t.spreadElement(t.identifier(argsBinding.name))]);
} else {
var method = thisEnvFn.scope.generateUidIdentifier("prop");
argsList.unshift(method);
fnBody = t.callExpression(t.memberExpression(t.super(), t.identifier(method.name), true), [t.spreadElement(t.identifier(argsBinding.name))]);
}
return t.arrowFunctionExpression(argsList, fnBody);
});
}
function getSuperPropBinding(thisEnvFn, isAssignment, propName) {
var op = isAssignment ? "set" : "get";
return getBinding(thisEnvFn, "superprop_" + op + ":" + (propName || ""), function () {
var argsList = [];
var fnBody = void 0;
if (propName) {
fnBody = t.memberExpression(t.super(), t.identifier(propName));
} else {
var method = thisEnvFn.scope.generateUidIdentifier("prop");
argsList.unshift(method);
fnBody = t.memberExpression(t.super(), t.identifier(method.name), true);
}
if (isAssignment) {
var valueIdent = thisEnvFn.scope.generateUidIdentifier("value");
argsList.push(valueIdent);
fnBody = t.assignmentExpression("=", fnBody, t.identifier(valueIdent.name));
}
return t.arrowFunctionExpression(argsList, fnBody);
});
}
function getBinding(thisEnvFn, key, init) {
var cacheKey = "binding:" + key;
var data = thisEnvFn.getData(cacheKey);
if (!data) {
var id = thisEnvFn.scope.generateUidIdentifier(key);
data = id.name;
thisEnvFn.setData(cacheKey, data);
thisEnvFn.scope.push({
id: id,
init: init(data)
});
}
return data;
}
function getScopeInformation(fnPath) {
var thisPaths = [];
var argumentsPaths = [];
var newTargetPaths = [];
var superProps = [];
var superCalls = [];
fnPath.traverse({
Function: function Function(child) {
if (child.isArrowFunctionExpression() || child.isClassProperty()) return;
child.skip();
},
ThisExpression: function ThisExpression(child) {
thisPaths.push(child);
},
JSXIdentifier: function JSXIdentifier(child) {
if (child.node.name !== "this") return;
if (!child.parentPath.isJSXMemberExpression({ object: child.node }) && !child.parentPath.isJSXOpeningElement({ name: child.node })) return;
thisPaths.push(child);
},
CallExpression: function CallExpression(child) {
if (child.get("callee").isSuper()) superCalls.push(child);
},
MemberExpression: function MemberExpression(child) {
if (child.get("object").isSuper()) superProps.push(child);
},
ReferencedIdentifier: function ReferencedIdentifier(child) {
if (child.node.name !== "arguments") return;
argumentsPaths.push(child);
},
MetaProperty: function MetaProperty(child) {
if (!child.get("meta").isIdentifier({ name: "new" })) return;
if (!child.get("property").isIdentifier({ name: "target" })) return;
newTargetPaths.push(child);
}
});
return {
thisPaths: thisPaths,
argumentsPaths: argumentsPaths,
newTargetPaths: newTargetPaths,
superProps: superProps,
superCalls: superCalls
};
}

@@ -95,5 +95,7 @@ "use strict";

if (binding.kind === "param") continue;
if (binding.kind === "param" || binding.path.parentKey === "params") continue;
if (this.getAttachmentParentForPath(binding.path).key > path.key) {
var bindingParentPath = this.getAttachmentParentForPath(binding.path);
if (bindingParentPath.key >= path.key) {
this.attachAfter = true;

@@ -124,2 +126,6 @@ path = binding.path;

if (path.parentPath.isExportDeclaration()) {
path = path.parentPath;
}
return path;

@@ -159,3 +165,3 @@ };

do {
if (!path.parentPath || Array.isArray(path.container) && path.isStatement() || path.isVariableDeclarator() && path.parentPath.node !== null && path.parentPath.node.declarations.length > 1) {
if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
return path;

@@ -162,0 +168,0 @@ }

@@ -166,3 +166,5 @@ "use strict";

} else if (this.isStatementOrBlock()) {
if (this.node) nodes.unshift(this.node);
if (this.node && (!this.isExpressionStatement() || this.node.expression != null)) {
nodes.unshift(this.node);
}
this._replaceWith(t.blockStatement(nodes));

@@ -169,0 +171,0 @@ } else {

@@ -201,4 +201,3 @@ "use strict";

} else {
var container = t.functionExpression(null, [], t.blockStatement(nodes));
container.shadow = true;
var container = t.arrowFunctionExpression([], t.blockStatement(nodes));

@@ -246,2 +245,4 @@ this.replaceWith(t.callExpression(container, []));

this.get("callee").arrowFunctionToExpression();
return this.node;

@@ -248,0 +249,0 @@ }

{
"name": "babel-traverse",
"version": "7.0.0-alpha.9",
"version": "7.0.0-alpha.10",
"description": "The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes",

@@ -12,5 +12,6 @@ "author": "Sebastian McKenzie <sebmck@gmail.com>",

"babel-code-frame": "7.0.0-alpha.9",
"babel-messages": "7.0.0-alpha.9",
"babel-types": "7.0.0-alpha.9",
"babylon": "7.0.0-beta.8",
"babel-helper-function-name": "7.0.0-alpha.7",
"babel-messages": "7.0.0-alpha.10",
"babel-types": "7.0.0-alpha.10",
"babylon": "7.0.0-beta.10",
"debug": "^2.2.0",

@@ -22,5 +23,5 @@ "globals": "^9.0.0",

"devDependencies": {
"babel-generator": "7.0.0-alpha.9",
"babel-helper-plugin-test-runner": "7.0.0-alpha.9"
"babel-generator": "7.0.0-alpha.10",
"babel-helper-plugin-test-runner": "7.0.0-alpha.10"
}
}
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc