fix: keep previous node ref to make method calls

This commit is contained in:
cha0s 2019-05-26 12:01:33 -05:00
parent 39e73955a3
commit 372f0b9a3b

View File

@ -62,14 +62,17 @@ class Context extends Map {
if ('key' !== first.type) {
throw new TypeError(`First step in a traversal must be type "key"`);
}
return rest.reduce((walk, step, index) => {
return this.takeStep(walk, (walk) => {
return fn(walk, step, index);
let previousNode = null;
return rest.reduce((node, step, index) => {
return this.takeStep(node, (node) => {
const result = fn(node, previousNode, step, index);
previousNode = node;
return result;
});
}, this.get(first.key));
}
traverseOneStep(steps, node, step) {
traverseOneStep(steps, previousNode, node, step) {
if ('undefined' === typeof node) {
const rendered = this.renderStepsUntilNow(steps, step);
debug(`"${rendered}" is traversed through but undefined`);
@ -80,17 +83,17 @@ class Context extends Map {
return node[step.key];
case 'invoke':
const args = step.args.map((arg) => arg.get(this));
return fastApply(null, node, args);
return fastApply(previousNode, node, args);
}
}
traverseRaw(traversal) {
const {steps, value} = traversal;
return this.traverseAndDo(steps, (node, step, index) => {
return this.traverseAndDo(steps, (node, previousNode, step, index) => {
const isLastStep = index === steps.length - 2;
// Traverse if we're not at the final step with a value.
if (!isLastStep || !value) {
return this.traverseOneStep(steps, node, step);
return this.traverseOneStep(steps, previousNode, node, step);
}
// Try to set the value.
switch (step.type) {