Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit

Permalink
Fix evaluation ordering with method calls (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo authored Mar 12, 2020
1 parent 588bbde commit 49f7318
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 22 deletions.
30 changes: 16 additions & 14 deletions packages/regenerator-transform/src/emit.js
Original file line number Diff line number Diff line change
Expand Up @@ -995,13 +995,13 @@ Ep.explodeExpression = function(path, ignoreResult) {
let argsPath = path.get("arguments");

let newCallee;
let newArgs = [];
let newArgs;

let hasLeapingArgs = false;
argsPath.forEach(function(argPath) {
hasLeapingArgs = hasLeapingArgs ||
meta.containsLeap(argPath.node);
});
let hasLeapingArgs = argsPath.some(
argPath => meta.containsLeap(argPath.node)
);

let injectFirstArg = null;

if (t.isMemberExpression(calleePath.node)) {
if (hasLeapingArgs) {
Expand All @@ -1022,7 +1022,7 @@ Ep.explodeExpression = function(path, ignoreResult) {
? explodeViaTempVar(null, calleePath.get("property"))
: calleePath.node.property;

newArgs.unshift(newObject);
injectFirstArg = newObject;

newCallee = t.memberExpression(
t.memberExpression(
Expand Down Expand Up @@ -1057,14 +1057,16 @@ Ep.explodeExpression = function(path, ignoreResult) {
}
}

argsPath.forEach(function(argPath) {
newArgs.push(explodeViaTempVar(null, argPath));
});
if (hasLeapingArgs) {
newArgs = argsPath.map(argPath => explodeViaTempVar(null, argPath));
if (injectFirstArg) newArgs.unshift(injectFirstArg);

return finish(t.callExpression(
newCallee,
newArgs.map(arg => t.cloneDeep(arg))
));
newArgs = newArgs.map(arg => t.cloneDeep(arg));
} else {
newArgs = path.node.arguments;
}

return finish(t.callExpression(newCallee, newArgs));

case "NewExpression":
return finish(t.newExpression(
Expand Down
89 changes: 81 additions & 8 deletions test/tests.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,14 +893,11 @@ describe("yield chain", function() {
});
});

describe("call expression ordering (#244)", function test() {
function *gen() {
return (yield 1)(yield 2)(yield 3);
}

it("should be correct", function () {
var g = gen();
var order = [];
describe("call expression ordering", function test() {
it("should be correct with chained calls (#244)", function () {
var g = (function *gen() {
return (yield 1)(yield 2)(yield 3);
})();

assert.deepEqual(g.next(), { value: 1, done: false });

Expand All @@ -916,6 +913,82 @@ describe("call expression ordering (#244)", function test() {
assert.deepEqual(g.next("sent 2"), { value: 3, done: false });
assert.deepEqual(g.next("sent 3"), { value: "done", done: true });
});

describe("when the callee is a member expression", function() {
it("should allow vars assigned in the callee to be used in the args (#379)", function() {
var g = (function* fn() {
var _ref;
(_ref = yield).method(_ref);
})();

var res;
var obj = {
method(arg) {
res = arg;
}
};

g.next();
g.next(obj);

assert.strictEqual(obj, res);
});

it("should be correct when only the callee contains yield", function() {
var order = [];

function step(n) {
order.push(n);
return { method() {} }
}

var g = (function* fn() {
(yield, step(1)).method(step(2));
})();

g.next();
g.next();

assert.deepStrictEqual(order, [1, 2]);
});

it("should be correct when only the arguments contains yield", function() {
var order = [];

function step(n) {
order.push(n);
return { method() {} }
}

var g = (function* fn() {
(step(1)).method(step(2), yield, step(3));
})();

g.next();
g.next();

assert.deepStrictEqual(order, [1, 2, 3]);
});

it("should be correct when the callee and the arguments contain yield", function() {
var order = [];

function step(n) {
order.push(n);
return { method() {} }
}

var g = (function* fn() {
(yield, step(1)).method(step(2), yield, step(3));
})();

g.next();
g.next();
g.next();

assert.deepStrictEqual(order, [1, 2, 3]);
});
});
});

describe("object literal generator", function() {
Expand Down

0 comments on commit 49f7318

Please sign in to comment.