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