rejigger path traversal class for es6ification

This commit is contained in:
Sebastian McKenzie 2015-02-25 21:50:40 +11:00
parent 7bb98352df
commit 5889233adc
2 changed files with 64 additions and 65 deletions

View File

@ -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);
} }

View File

@ -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.container = container;
this.key = key; this.key = key;
this.obj = obj;
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,42 +59,22 @@ 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)) {
ourScope = new Scope(node, parent, scope);
}
return ourScope;
};
TraversalPath.prototype.maybeRemove = function () {
if (this.shouldRemove) {
this.setNode(null);
this.flatten();
}
};
TraversalPath.prototype.setNode = function (val) {
return this.obj[this.key] = val;
};
TraversalPath.prototype.getNode = function () {
return this.obj[this.key];
};
TraversalPath.prototype.replaceNode = function (replacement) {
var isArray = Array.isArray(replacement); var isArray = Array.isArray(replacement);
// inherit comments from original node to the first replacement node // inherit comments from original node to the first replacement node
var inheritTo = replacement; var inheritTo = replacement;
if (isArray) inheritTo = replacement[0]; if (isArray) inheritTo = replacement[0];
if (inheritTo) t.inheritsComments(inheritTo, this.getNode()); if (inheritTo) t.inheritsComments(inheritTo, this.node);
// replace the node // replace the node
this.setNode(replacement); this.container[this.key] = replacement;
this.setScope();
var file = this.scope && this.scope.file; var file = this.scope && this.scope.file;
if (file) { if (file) {
@ -96,16 +90,17 @@ TraversalPath.prototype.replaceNode = function (replacement) {
// we're replacing a statement or block node with an array of statements so we better // we're replacing a statement or block node with an array of statements so we better
// ensure that it's a block // ensure that it's a block
if (isArray) { if (isArray) {
if (includes(t.STATEMENT_OR_BLOCK_KEYS, this.key) && !t.isBlockStatement(this.obj)) { if (includes(t.STATEMENT_OR_BLOCK_KEYS, this.key) && !t.isBlockStatement(this.container)) {
t.ensureBlock(this.obj, this.key); t.ensureBlock(this.container, this.key);
} }
this.flatten(); 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);
};