Socket
Socket
Sign inDemoInstall

babel-traverse

Package Overview
Dependencies
23
Maintainers
5
Versions
78
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 7.0.0-alpha.20 to 7.0.0-beta.0

8

lib/path/index.js

@@ -148,9 +148,3 @@ "use strict";

NodePath.prototype.getScope = function getScope(scope) {
var ourScope = scope;
if (this.isScope()) {
ourScope = new _scope2.default(this, scope);
}
return ourScope;
return this.isScope() ? new _scope2.default(this) : scope;
};

@@ -157,0 +151,0 @@

59

lib/path/modification.js

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

exports.insertAfter = insertAfter;
exports._insertAfter = _insertAfter;
exports.updateSiblingKeys = updateSiblingKeys;

@@ -47,37 +46,29 @@ exports._verifyNodeList = _verifyNodeList;

if (this.node) nodes.push(this.node);
this.replaceExpressionWithStatements(nodes);
return this.replaceExpressionWithStatements(nodes);
} else if (Array.isArray(this.container)) {
return this._containerInsertBefore(nodes);
} else if (this.isStatementOrBlock()) {
if (this.node) nodes.push(this.node);
this._replaceWith(t.blockStatement(nodes));
var shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
this.replaceWith(t.blockStatement(shouldInsertCurrentNode ? [this.node] : []));
return this.unshiftContainer("body", nodes);
} else {
throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
}
return [this];
}
function _containerInsert(from, nodes) {
var _container;
this.updateSiblingKeys(from, nodes.length);
var paths = [];
(_container = this.container).splice.apply(_container, [from, 0].concat(nodes));
for (var i = 0; i < nodes.length; i++) {
var to = from + i;
var node = nodes[i];
this.container.splice(to, 0, node);
var path = this.getSibling("" + to);
paths.push(path);
if (this.context) {
var path = this.context.create(this.parent, this.container, to, this.listKey);
if (this.context.queue) path.pushContext(this.context);
paths.push(path);
} else {
paths.push(_index2.default.get({
parentPath: this.parentPath,
parent: this.parent,
container: this.container,
listKey: this.listKey,
key: to
}));
if (this.context && this.context.queue) {
path.pushContext(this.context);
}

@@ -127,10 +118,2 @@ }

function insertAfter(nodes) {
return this._insertAfter(nodes);
}
function _insertAfter(nodes, shouldRequeue) {
if (shouldRequeue === void 0) {
shouldRequeue = false;
}
this._assertUnremoved();

@@ -141,3 +124,3 @@

if (this.parentPath.isExpressionStatement() || this.parentPath.isLabeledStatement()) {
return this.parentPath._insertAfter(nodes, true);
return this.parentPath.insertAfter(nodes);
} else if (this.isNodeType("Expression") || this.parentPath.isForStatement() && this.key === "init") {

@@ -150,20 +133,12 @@ if (this.node) {

this.replaceExpressionWithStatements(nodes);
return this.replaceExpressionWithStatements(nodes);
} else if (Array.isArray(this.container)) {
return this._containerInsertAfter(nodes);
} else if (this.isStatementOrBlock()) {
if (this.node && (!this.isExpressionStatement() || this.node.expression != null)) {
nodes.unshift(this.node);
}
this._replaceWith(t.blockStatement(nodes));
if (shouldRequeue) {
this.requeue();
}
var shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
this.replaceWith(t.blockStatement(shouldInsertCurrentNode ? [this.node] : []));
return this.pushContainer("body", nodes);
} else {
throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
}
return [this];
}

@@ -170,0 +145,0 @@

@@ -66,3 +66,3 @@ "use strict";

this.node = this.container[this.key] = null;
this.insertAfter(nodes);
var paths = this.insertAfter(nodes);

@@ -74,2 +74,4 @@ if (this.node) {

}
return paths;
}

@@ -123,3 +125,3 @@

if (this.node === replacement) {
return;
return [this];
}

@@ -139,5 +141,8 @@

var nodePath = "";
if (this.isNodeType("Statement") && t.isExpression(replacement)) {
if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration()) {
replacement = t.expressionStatement(replacement);
nodePath = "expression";
}

@@ -164,2 +169,3 @@ }

this.requeue();
return [nodePath ? this.get(nodePath) : this];
}

@@ -189,49 +195,53 @@

if (toSequenceExpression) {
this.replaceWith(toSequenceExpression);
} else {
var container = t.arrowFunctionExpression([], t.blockStatement(nodes));
this.replaceWith(t.callExpression(container, []));
this.traverse(hoistVariablesVisitor);
var completionRecords = this.get("callee").getCompletionRecords();
return this.replaceWith(toSequenceExpression)[0].get("expressions");
}
for (var _iterator = completionRecords, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
var container = t.arrowFunctionExpression([], t.blockStatement(nodes));
this.replaceWith(t.callExpression(container, []));
this.traverse(hoistVariablesVisitor);
var completionRecords = this.get("callee").getCompletionRecords();
if (_isArray) {
if (_i2 >= _iterator.length) break;
_ref = _iterator[_i2++];
} else {
_i2 = _iterator.next();
if (_i2.done) break;
_ref = _i2.value;
}
for (var _iterator = completionRecords, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
var _path = _ref;
if (!_path.isExpressionStatement()) continue;
if (_isArray) {
if (_i2 >= _iterator.length) break;
_ref = _iterator[_i2++];
} else {
_i2 = _iterator.next();
if (_i2.done) break;
_ref = _i2.value;
}
var loop = _path.findParent(function (path) {
return path.isLoop();
});
var _path = _ref;
if (!_path.isExpressionStatement()) continue;
if (loop) {
var uid = loop.getData("expressionReplacementReturnUid");
var loop = _path.findParent(function (path) {
return path.isLoop();
});
if (!uid) {
var callee = this.get("callee");
uid = callee.scope.generateDeclaredUidIdentifier("ret");
callee.get("body").pushContainer("body", t.returnStatement(uid));
loop.setData("expressionReplacementReturnUid", uid);
} else {
uid = t.identifier(uid.name);
}
if (loop) {
var uid = loop.getData("expressionReplacementReturnUid");
_path.get("expression").replaceWith(t.assignmentExpression("=", uid, _path.node.expression));
if (!uid) {
var _callee = this.get("callee");
uid = _callee.scope.generateDeclaredUidIdentifier("ret");
_callee.get("body").pushContainer("body", t.returnStatement(uid));
loop.setData("expressionReplacementReturnUid", uid);
} else {
_path.replaceWith(t.returnStatement(_path.node.expression));
uid = t.identifier(uid.name);
}
_path.get("expression").replaceWith(t.assignmentExpression("=", uid, _path.node.expression));
} else {
_path.replaceWith(t.returnStatement(_path.node.expression));
}
}
this.get("callee").arrowFunctionToExpression();
return this.node;
}
var callee = this.get("callee");
callee.arrowFunctionToExpression();
return callee.get("body.body");
}

@@ -246,5 +256,6 @@

this._containerInsertAfter(nodes);
var paths = this._containerInsertAfter(nodes);
return this.remove();
this.remove();
return paths;
} else {

@@ -251,0 +262,0 @@ return this.replaceWithMultiple(nodes);

@@ -7,13 +7,6 @@ "use strict";

function Binding(_ref) {
var existing = _ref.existing,
identifier = _ref.identifier,
var identifier = _ref.identifier,
scope = _ref.scope,
path = _ref.path,
kind = _ref.kind;
if (existing) {
existing.constantViolations = existing.constantViolations.concat(path);
return existing;
}
this.identifier = identifier;

@@ -20,0 +13,0 @@ this.scope = scope;

@@ -6,2 +6,4 @@ "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; }; }();
var _class, _temp;

@@ -53,28 +55,2 @@

function getCache(path, parentScope, self) {
var scopes = _cache.scope.get(path.node) || [];
for (var _iterator = scopes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var _scope = _ref;
if (_scope.parent === parentScope && _scope.path === path) return _scope;
}
scopes.push(self);
if (!_cache.scope.has(path.node)) {
_cache.scope.set(path.node, scopes);
}
}
function gatherNodeParts(node, parts) {

@@ -87,4 +63,4 @@ if (t.isModuleDeclaration(node)) {

for (var _i2 = 0; _i2 < _arr.length; _i2++) {
var specifier = _arr[_i2];
for (var _i = 0; _i < _arr.length; _i++) {
var specifier = _arr[_i];
gatherNodeParts(specifier, parts);

@@ -109,4 +85,4 @@ }

for (var _i3 = 0; _i3 < _arr2.length; _i3++) {
var prop = _arr2[_i3];
for (var _i2 = 0; _i2 < _arr2.length; _i2++) {
var prop = _arr2[_i2];
gatherNodeParts(prop.key || prop.argument, parts);

@@ -121,4 +97,4 @@ }

for (var _i4 = 0; _i4 < _arr3.length; _i4++) {
var key = _arr3[_i4];
for (var _i3 = 0; _i3 < _arr3.length; _i3++) {
var key = _arr3[_i3];
var declar = path.get(key);

@@ -166,4 +142,4 @@

for (var _i5 = 0; _i5 < _arr4.length; _i5++) {
var decl = _arr4[_i5];
for (var _i4 = 0; _i4 < _arr4.length; _i4++) {
var decl = _arr4[_i4];
var ids = t.getBindingIdentifiers(decl);

@@ -210,4 +186,4 @@

for (var _i6 = 0; _i6 < _arr5.length; _i6++) {
var bodyPath = _arr5[_i6];
for (var _i5 = 0; _i5 < _arr5.length; _i5++) {
var bodyPath = _arr5[_i5];

@@ -222,14 +198,15 @@ if (bodyPath.isFunctionDeclaration()) {

var Scope = (_temp = _class = function () {
function Scope(path, parentScope) {
if (parentScope && parentScope.block === path.node) {
return parentScope;
function Scope(path) {
var node = path.node;
var cached = _cache.scope.get(node);
if (cached && cached.path === path) {
return cached;
}
var cached = getCache(path, parentScope, this);
if (cached) return cached;
_cache.scope.set(node, this);
this.uid = uid++;
this.parent = parentScope;
this.hub = path.hub;
this.parentBlock = path.parent;
this.block = path.node;
this.block = node;
this.path = path;

@@ -442,4 +419,4 @@ this.labels = new Map();

for (var _i7 = 0; _i7 < _arr6.length; _i7++) {
var declar = _arr6[_i7];
for (var _i6 = 0; _i6 < _arr6.length; _i6++) {
var declar = _arr6[_i6];
this.registerBinding(path.node.kind, declar);

@@ -453,4 +430,4 @@ }

for (var _i8 = 0; _i8 < _arr7.length; _i8++) {
var specifier = _arr7[_i8];
for (var _i7 = 0; _i7 < _arr7.length; _i7++) {
var specifier = _arr7[_i7];
this.registerBinding("module", specifier);

@@ -496,15 +473,15 @@ }

for (var _iterator2 = declarators, _isArray2 = Array.isArray(_iterator2), _i9 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
for (var _iterator = declarators, _isArray = Array.isArray(_iterator), _i8 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray2) {
if (_i9 >= _iterator2.length) break;
_ref2 = _iterator2[_i9++];
if (_isArray) {
if (_i8 >= _iterator.length) break;
_ref = _iterator[_i8++];
} else {
_i9 = _iterator2.next();
if (_i9.done) break;
_ref2 = _i9.value;
_i8 = _iterator.next();
if (_i8.done) break;
_ref = _i8.value;
}
var _declar2 = _ref2;
var _declar2 = _ref;
this.registerBinding(kind, _declar2);

@@ -522,4 +499,4 @@ }

for (var _i10 = 0; _i10 < _arr8.length; _i10++) {
var _id3 = _arr8[_i10];
for (var _i9 = 0; _i9 < _arr8.length; _i9++) {
var _id3 = _arr8[_i9];
var local = this.getOwnBinding(_name3);

@@ -534,9 +511,13 @@

parent.references[_name3] = true;
this.bindings[_name3] = new _binding3.default({
identifier: _id3,
existing: local,
scope: this,
path: bindingPath,
kind: kind
});
if (local) {
this.registerConstantViolation(bindingPath);
} else {
this.bindings[_name3] = new _binding3.default({
identifier: _id3,
scope: this,
path: bindingPath,
kind: kind
});
}
}

@@ -593,15 +574,15 @@ }

} else if (t.isClassBody(node)) {
for (var _iterator3 = node.body, _isArray3 = Array.isArray(_iterator3), _i11 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
var _ref3;
for (var _iterator2 = node.body, _isArray2 = Array.isArray(_iterator2), _i10 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
if (_isArray3) {
if (_i11 >= _iterator3.length) break;
_ref3 = _iterator3[_i11++];
if (_isArray2) {
if (_i10 >= _iterator2.length) break;
_ref2 = _iterator2[_i10++];
} else {
_i11 = _iterator3.next();
if (_i11.done) break;
_ref3 = _i11.value;
_i10 = _iterator2.next();
if (_i10.done) break;
_ref2 = _i10.value;
}
var _method = _ref3;
var _method = _ref2;
if (!this.isPure(_method, constantsOnly)) return false;

@@ -616,4 +597,4 @@ }

for (var _i12 = 0; _i12 < _arr9.length; _i12++) {
var elem = _arr9[_i12];
for (var _i11 = 0; _i11 < _arr9.length; _i11++) {
var elem = _arr9[_i11];
if (!this.isPure(elem, constantsOnly)) return false;

@@ -626,4 +607,4 @@ }

for (var _i13 = 0; _i13 < _arr10.length; _i13++) {
var prop = _arr10[_i13];
for (var _i12 = 0; _i12 < _arr10.length; _i12++) {
var prop = _arr10[_i12];
if (!this.isPure(prop, constantsOnly)) return false;

@@ -647,4 +628,4 @@ }

for (var _i14 = 0; _i14 < _arr11.length; _i14++) {
var expression = _arr11[_i14];
for (var _i13 = 0; _i13 < _arr11.length; _i13++) {
var expression = _arr11[_i13];
if (!this.isPure(expression, constantsOnly)) return false;

@@ -704,4 +685,4 @@ }

for (var _i15 = 0; _i15 < _arr12.length; _i15++) {
var key = _arr12[_i15];
for (var _i14 = 0; _i14 < _arr12.length; _i14++) {
var key = _arr12[_i14];
var node = path.get(key);

@@ -727,15 +708,15 @@ if (node.isBlockScoped()) this.registerBinding(node.node.kind, node);

for (var _iterator4 = params, _isArray4 = Array.isArray(_iterator4), _i16 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
var _ref4;
for (var _iterator3 = params, _isArray3 = Array.isArray(_iterator3), _i15 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
var _ref3;
if (_isArray4) {
if (_i16 >= _iterator4.length) break;
_ref4 = _iterator4[_i16++];
if (_isArray3) {
if (_i15 >= _iterator3.length) break;
_ref3 = _iterator3[_i15++];
} else {
_i16 = _iterator4.next();
if (_i16.done) break;
_ref4 = _i16.value;
_i15 = _iterator3.next();
if (_i15.done) break;
_ref3 = _i15.value;
}
var _param = _ref4;
var _param = _ref3;
this.registerBinding("param", _param);

@@ -760,15 +741,15 @@ }

for (var _iterator5 = state.assignments, _isArray5 = Array.isArray(_iterator5), _i17 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
var _ref5;
for (var _iterator4 = state.assignments, _isArray4 = Array.isArray(_iterator4), _i16 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
var _ref4;
if (_isArray5) {
if (_i17 >= _iterator5.length) break;
_ref5 = _iterator5[_i17++];
if (_isArray4) {
if (_i16 >= _iterator4.length) break;
_ref4 = _iterator4[_i16++];
} else {
_i17 = _iterator5.next();
if (_i17.done) break;
_ref5 = _i17.value;
_i16 = _iterator4.next();
if (_i16.done) break;
_ref4 = _i16.value;
}
var _path3 = _ref5;
var _path3 = _ref4;

@@ -788,38 +769,38 @@ var ids = _path3.getBindingIdentifiers();

for (var _iterator6 = state.references, _isArray6 = Array.isArray(_iterator6), _i18 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
var _ref6;
for (var _iterator5 = state.references, _isArray5 = Array.isArray(_iterator5), _i17 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
var _ref5;
if (_isArray6) {
if (_i18 >= _iterator6.length) break;
_ref6 = _iterator6[_i18++];
if (_isArray5) {
if (_i17 >= _iterator5.length) break;
_ref5 = _iterator5[_i17++];
} else {
_i18 = _iterator6.next();
if (_i18.done) break;
_ref6 = _i18.value;
_i17 = _iterator5.next();
if (_i17.done) break;
_ref5 = _i17.value;
}
var _ref8 = _ref6;
var _ref7 = _ref5;
var binding = _ref8.scope.getBinding(_ref8.node.name);
var binding = _ref7.scope.getBinding(_ref7.node.name);
if (binding) {
binding.reference(_ref8);
binding.reference(_ref7);
} else {
_ref8.scope.getProgramParent().addGlobal(_ref8.node);
_ref7.scope.getProgramParent().addGlobal(_ref7.node);
}
}
for (var _iterator7 = state.constantViolations, _isArray7 = Array.isArray(_iterator7), _i19 = 0, _iterator7 = _isArray7 ? _iterator7 : _iterator7[Symbol.iterator]();;) {
var _ref7;
for (var _iterator6 = state.constantViolations, _isArray6 = Array.isArray(_iterator6), _i18 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
var _ref6;
if (_isArray7) {
if (_i19 >= _iterator7.length) break;
_ref7 = _iterator7[_i19++];
if (_isArray6) {
if (_i18 >= _iterator6.length) break;
_ref6 = _iterator6[_i18++];
} else {
_i19 = _iterator7.next();
if (_i19.done) break;
_ref7 = _i19.value;
_i18 = _iterator6.next();
if (_i18.done) break;
_ref6 = _i18.value;
}
var _path4 = _ref7;
var _path4 = _ref6;

@@ -919,4 +900,4 @@ _path4.scope.registerConstantViolation(_path4);

for (var _i20 = 0; _i20 < _arr13.length; _i20++) {
var kind = _arr13[_i20];
for (var _i19 = 0; _i19 < _arr13.length; _i19++) {
var kind = _arr13[_i19];
var scope = this;

@@ -943,3 +924,3 @@

if (_crawlCallsCount === 0 && binding && binding.path.isFlow()) {
console.warn("\n You or one of the Babel plugins you are using are using Flow declarations as bindings.\n Support for this will be removed in version 6.8. To find out the caller, grep for this\n message and change it to a `console.trace()`.\n ");
console.warn("\n You or one of the Babel plugins you are using are using Flow declarations as bindings.\n Support for this will be removed in version 7. To find out the caller, grep for this\n message and change it to a `console.trace()`.\n ");
}

@@ -1021,4 +1002,24 @@

_createClass(Scope, [{
key: "parent",
get: function get() {
var parent = this.path.findParent(function (p) {
return p.isScope();
});
return parent && parent.scope;
}
}, {
key: "parentBlock",
get: function get() {
return this.path.parent;
}
}, {
key: "hub",
get: function get() {
return this.path.hub;
}
}]);
return Scope;
}(), _class.globals = Object.keys(_globals2.default.builtin), _class.contextVariables = ["arguments", "undefined", "Infinity", "NaN"], _temp);
exports.default = Scope;
{
"name": "babel-traverse",
"version": "7.0.0-alpha.20",
"version": "7.0.0-beta.0",
"description": "The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes",

@@ -11,8 +11,8 @@ "author": "Sebastian McKenzie <sebmck@gmail.com>",

"dependencies": {
"babel-code-frame": "7.0.0-alpha.20",
"babel-helper-function-name": "7.0.0-alpha.20",
"babel-messages": "7.0.0-alpha.20",
"babel-types": "7.0.0-alpha.20",
"babel-code-frame": "7.0.0-beta.0",
"babel-helper-function-name": "7.0.0-beta.0",
"babel-messages": "7.0.0-beta.0",
"babel-types": "7.0.0-beta.0",
"babylon": "7.0.0-beta.22",
"debug": "^2.2.0",
"debug": "^3.0.1",
"globals": "^10.0.0",

@@ -23,5 +23,5 @@ "invariant": "^2.2.0",

"devDependencies": {
"babel-generator": "7.0.0-alpha.20",
"babel-helper-plugin-test-runner": "7.0.0-alpha.20"
"babel-generator": "7.0.0-beta.0",
"babel-helper-plugin-test-runner": "7.0.0-beta.0"
}
}
# babel-traverse
> babel-traverse maintains the overall tree state, and is responsible for replacing, removing, and adding nodes.
## Install
```sh
$ npm install --save babel-traverse
```
## Usage
We can use it alongside Babylon to traverse and update nodes:
```js
import * as babylon from "babylon";
import traverse from "babel-traverse";
const code = `function square(n) {
return n * n;
}`;
const ast = babylon.parse(code);
traverse(ast, {
enter(path) {
if (path.isIdentifier({ name: "n" })) {
path.node.name = "x";
}
}
});
```
[:book: **Read the full docs here**](https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#babel-traverse)
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