rejigger path traversal class for es6ification
This commit is contained in:
parent
7bb98352df
commit
5889233adc
@ -21,7 +21,7 @@ var visitor = {
|
|||||||
|
|
||||||
if (t.isAssignmentExpression(parent) || t.isUpdateExpression(parent)) {
|
if (t.isAssignmentExpression(parent) || t.isUpdateExpression(parent)) {
|
||||||
if (parent._ignoreBlockScopingTDZ) return;
|
if (parent._ignoreBlockScopingTDZ) return;
|
||||||
this.parentPath.replaceNode(t.sequenceExpression([assert, parent]));
|
this.parentPath.node = t.sequenceExpression([assert, parent]);
|
||||||
} else {
|
} else {
|
||||||
return t.logicalExpression("&&", assert, node);
|
return t.logicalExpression("&&", assert, node);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,14 +2,12 @@
|
|||||||
|
|
||||||
module.exports = TraversalPath;
|
module.exports = TraversalPath;
|
||||||
|
|
||||||
/* jshint maxparams:7 */
|
|
||||||
|
|
||||||
var traverse = require("./index");
|
var traverse = require("./index");
|
||||||
var includes = require("lodash/collection/includes");
|
var includes = require("lodash/collection/includes");
|
||||||
var Scope = require("./scope");
|
var Scope = require("./scope");
|
||||||
var t = require("../types");
|
var t = require("../types");
|
||||||
|
|
||||||
function TraversalPath(context, parent, obj, key) {
|
function TraversalPath(context, parent, container, key) {
|
||||||
this.shouldRemove = false;
|
this.shouldRemove = false;
|
||||||
this.shouldSkip = false;
|
this.shouldSkip = false;
|
||||||
this.shouldStop = false;
|
this.shouldStop = false;
|
||||||
@ -19,14 +17,30 @@ function TraversalPath(context, parent, obj, key) {
|
|||||||
this.state = this.context.state;
|
this.state = this.context.state;
|
||||||
this.opts = this.context.opts;
|
this.opts = this.context.opts;
|
||||||
|
|
||||||
this.key = key;
|
this.container = container;
|
||||||
this.obj = obj;
|
this.key = key;
|
||||||
|
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.scope = TraversalPath.getScope(this.getNode(), parent, context.scope);
|
|
||||||
this.state = context.state;
|
this.state = context.state;
|
||||||
|
|
||||||
|
this.setScope();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TraversalPath.getScope = function (node, parent, scope) {
|
||||||
|
var ourScope = scope;
|
||||||
|
|
||||||
|
// we're entering a new scope so let's construct it!
|
||||||
|
if (t.isScope(node, parent)) {
|
||||||
|
ourScope = new Scope(node, parent, scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ourScope;
|
||||||
|
};
|
||||||
|
|
||||||
|
TraversalPath.prototype.setScope = function () {
|
||||||
|
this.scope = TraversalPath.getScope(this.node, this.parent, this.context.scope);
|
||||||
|
};
|
||||||
|
|
||||||
TraversalPath.prototype.remove = function () {
|
TraversalPath.prototype.remove = function () {
|
||||||
this.shouldRemove = true;
|
this.shouldRemove = true;
|
||||||
this.shouldSkip = true;
|
this.shouldSkip = true;
|
||||||
@ -45,67 +59,48 @@ TraversalPath.prototype.flatten = function () {
|
|||||||
this.context.flatten();
|
this.context.flatten();
|
||||||
};
|
};
|
||||||
|
|
||||||
TraversalPath.getScope = function (node, parent, scope) {
|
Object.defineProperty(TraversalPath.prototype, "node", {
|
||||||
var ourScope = scope;
|
get: function () {
|
||||||
|
return this.container[this.key];
|
||||||
|
},
|
||||||
|
|
||||||
// we're entering a new scope so let's construct it!
|
set: function (replacement) {
|
||||||
if (t.isScope(node, parent)) {
|
var isArray = Array.isArray(replacement);
|
||||||
ourScope = new Scope(node, parent, scope);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ourScope;
|
// inherit comments from original node to the first replacement node
|
||||||
};
|
var inheritTo = replacement;
|
||||||
|
if (isArray) inheritTo = replacement[0];
|
||||||
|
if (inheritTo) t.inheritsComments(inheritTo, this.node);
|
||||||
|
|
||||||
TraversalPath.prototype.maybeRemove = function () {
|
// replace the node
|
||||||
if (this.shouldRemove) {
|
this.container[this.key] = replacement;
|
||||||
this.setNode(null);
|
this.setScope();
|
||||||
this.flatten();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
TraversalPath.prototype.setNode = function (val) {
|
var file = this.scope && this.scope.file;
|
||||||
return this.obj[this.key] = val;
|
if (file) {
|
||||||
};
|
if (isArray) {
|
||||||
|
for (var i = 0; i < replacement.length; i++) {
|
||||||
TraversalPath.prototype.getNode = function () {
|
file.checkNode(replacement[i], this.scope);
|
||||||
return this.obj[this.key];
|
}
|
||||||
};
|
} else {
|
||||||
|
file.checkNode(replacement, this.scope);
|
||||||
TraversalPath.prototype.replaceNode = function (replacement) {
|
|
||||||
var isArray = Array.isArray(replacement);
|
|
||||||
|
|
||||||
// inherit comments from original node to the first replacement node
|
|
||||||
var inheritTo = replacement;
|
|
||||||
if (isArray) inheritTo = replacement[0];
|
|
||||||
if (inheritTo) t.inheritsComments(inheritTo, this.getNode());
|
|
||||||
|
|
||||||
// replace the node
|
|
||||||
this.setNode(replacement);
|
|
||||||
|
|
||||||
var file = this.scope && this.scope.file;
|
|
||||||
if (file) {
|
|
||||||
if (isArray) {
|
|
||||||
for (var i = 0; i < replacement.length; i++) {
|
|
||||||
file.checkNode(replacement[i], this.scope);
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
file.checkNode(replacement, this.scope);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// we're replacing a statement or block node with an array of statements so we better
|
|
||||||
// ensure that it's a block
|
|
||||||
if (isArray) {
|
|
||||||
if (includes(t.STATEMENT_OR_BLOCK_KEYS, this.key) && !t.isBlockStatement(this.obj)) {
|
|
||||||
t.ensureBlock(this.obj, this.key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.flatten();
|
// we're replacing a statement or block node with an array of statements so we better
|
||||||
|
// ensure that it's a block
|
||||||
|
if (isArray) {
|
||||||
|
if (includes(t.STATEMENT_OR_BLOCK_KEYS, this.key) && !t.isBlockStatement(this.container)) {
|
||||||
|
t.ensureBlock(this.container, this.key);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.flatten();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
TraversalPath.prototype.call = function (key) {
|
TraversalPath.prototype.call = function (key) {
|
||||||
var node = this.getNode();
|
var node = this.node;
|
||||||
if (!node) return;
|
if (!node) return;
|
||||||
|
|
||||||
var opts = this.opts;
|
var opts = this.opts;
|
||||||
@ -115,18 +110,18 @@ TraversalPath.prototype.call = function (key) {
|
|||||||
var replacement = fn.call(this, node, this.parent, this.scope, this.state);
|
var replacement = fn.call(this, node, this.parent, this.scope, this.state);
|
||||||
|
|
||||||
if (replacement) {
|
if (replacement) {
|
||||||
this.replaceNode(replacement);
|
this.node = replacement;
|
||||||
node = replacement;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.maybeRemove();
|
if (this.shouldRemove) {
|
||||||
|
this.container[this.key] = null;
|
||||||
return node;
|
this.flatten();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
TraversalPath.prototype.visit = function () {
|
TraversalPath.prototype.visit = function () {
|
||||||
var opts = this.opts;
|
var opts = this.opts;
|
||||||
var node = this.getNode();
|
var node = this.node;
|
||||||
|
|
||||||
// type is blacklisted
|
// type is blacklisted
|
||||||
if (opts.blacklist && opts.blacklist.indexOf(node.type) > -1) {
|
if (opts.blacklist && opts.blacklist.indexOf(node.type) > -1) {
|
||||||
@ -139,7 +134,7 @@ TraversalPath.prototype.visit = function () {
|
|||||||
return this.shouldStop;
|
return this.shouldStop;
|
||||||
}
|
}
|
||||||
|
|
||||||
node = this.getNode();
|
node = this.node;
|
||||||
|
|
||||||
if (Array.isArray(node)) {
|
if (Array.isArray(node)) {
|
||||||
// traverse over these replacement nodes we purposely don't call exitNode
|
// traverse over these replacement nodes we purposely don't call exitNode
|
||||||
@ -154,3 +149,7 @@ TraversalPath.prototype.visit = function () {
|
|||||||
|
|
||||||
return this.shouldStop;
|
return this.shouldStop;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TraversalPath.prototype.isReferencedIdentifier = function () {
|
||||||
|
return t.isReferencedIdentifier(this.node);
|
||||||
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user