From 3361b8165823a4315858329eb8f09bb5a9ff0e36 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 8 Feb 2015 01:27:00 +1100 Subject: [PATCH] expose parentPath --- lib/6to5/traversal/context.js | 3 ++- lib/6to5/traversal/index.js | 4 ++-- lib/6to5/traversal/path.js | 23 ++++++++++++----------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/6to5/traversal/context.js b/lib/6to5/traversal/context.js index e07b7362cd..4aa075db07 100644 --- a/lib/6to5/traversal/context.js +++ b/lib/6to5/traversal/context.js @@ -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; diff --git a/lib/6to5/traversal/index.js b/lib/6to5/traversal/index.js index 16d0934bfb..307868fa4e 100644 --- a/lib/6to5/traversal/index.js +++ b/lib/6to5/traversal/index.js @@ -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; diff --git a/lib/6to5/traversal/path.js b/lib/6to5/traversal/path.js index 19cac99043..473474038f 100644 --- a/lib/6to5/traversal/path.js +++ b/lib/6to5/traversal/path.js @@ -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"); }