expose parentPath
This commit is contained in:
parent
a15f218e9b
commit
3361b81658
@ -8,8 +8,9 @@ var TraversalPath = require("./path");
|
||||
var flatten = require("lodash/array/flatten");
|
||||
var compact = require("lodash/array/compact");
|
||||
|
||||
function TraversalContext(scope, opts, state) {
|
||||
function TraversalContext(scope, opts, state, parentPath) {
|
||||
this.shouldFlatten = false;
|
||||
this.parentPath = parentPath;
|
||||
|
||||
this.scope = scope;
|
||||
this.state = state;
|
||||
|
||||
@ -29,11 +29,11 @@ function traverse(parent, opts, scope, state) {
|
||||
}
|
||||
}
|
||||
|
||||
traverse.node = function (node, opts, scope, state) {
|
||||
traverse.node = function (node, opts, scope, state, parentPath) {
|
||||
var keys = t.VISITOR_KEYS[node.type];
|
||||
if (!keys) return;
|
||||
|
||||
var context = new TraversalContext(scope, opts, state);
|
||||
var context = new TraversalContext(scope, opts, state, parentPath);
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
if (context.visit(node, keys[i])) {
|
||||
return;
|
||||
|
||||
@ -14,16 +14,17 @@ function TraversalPath(context, parent, obj, key) {
|
||||
this.shouldSkip = false;
|
||||
this.shouldStop = false;
|
||||
|
||||
this.context = context;
|
||||
this.state = this.context.state;
|
||||
this.opts = this.context.opts;
|
||||
this.parentPath = context.parentPath;
|
||||
this.context = context;
|
||||
this.state = this.context.state;
|
||||
this.opts = this.context.opts;
|
||||
|
||||
this.key = key;
|
||||
this.obj = obj;
|
||||
|
||||
this.parent = parent;
|
||||
this.scope = TraversalPath.getScope(this.getNode(), parent, context.scope);
|
||||
this.state = context.state;
|
||||
this.parent = parent;
|
||||
this.scope = TraversalPath.getScope(this.getNode(), parent, context.scope);
|
||||
this.state = context.state;
|
||||
}
|
||||
|
||||
TraversalPath.prototype.remove = function () {
|
||||
@ -70,7 +71,7 @@ TraversalPath.prototype.getNode = function () {
|
||||
return this.obj[this.key];
|
||||
};
|
||||
|
||||
TraversalPath.prototype.replaceNode = function (replacement, scope) {
|
||||
TraversalPath.prototype.replaceNode = function (replacement) {
|
||||
var isArray = Array.isArray(replacement);
|
||||
|
||||
// inherit comments from original node to the first replacement node
|
||||
@ -85,10 +86,10 @@ TraversalPath.prototype.replaceNode = function (replacement, scope) {
|
||||
if (file) {
|
||||
if (isArray) {
|
||||
for (var i = 0; i < replacement.length; i++) {
|
||||
file.checkNode(replacement[i], scope);
|
||||
file.checkNode(replacement[i], this.scope);
|
||||
}
|
||||
} else {
|
||||
file.checkNode(replacement, scope);
|
||||
file.checkNode(replacement, this.scope);
|
||||
}
|
||||
}
|
||||
|
||||
@ -144,10 +145,10 @@ TraversalPath.prototype.visit = function () {
|
||||
// traverse over these replacement nodes we purposely don't call exitNode
|
||||
// as the original node has been destroyed
|
||||
for (var i = 0; i < node.length; i++) {
|
||||
traverse.node(node[i], opts, this.scope, this.state);
|
||||
traverse.node(node[i], opts, this.scope, this.state, this);
|
||||
}
|
||||
} else {
|
||||
traverse.node(node, opts, this.scope, this.state);
|
||||
traverse.node(node, opts, this.scope, this.state, this);
|
||||
this.call("exit");
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user