Skip to content

Commit

Permalink
Add array spread support (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 12, 2020
1 parent 2d65f34 commit c0b6b09
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 35 deletions.
89 changes: 59 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@babel/parser": "^7.8.4",
"@babel/plugin-transform-modules-commonjs": "^7.8.3",
"@babel/plugin-transform-parameters": "^7.8.4",
"@babel/plugin-transform-spread": "^7.8.3",
"babel-check-duplicated-nodes": "^1.0.0",
"browserify": "^16.5.0",
"mocha": "^7.0.1",
Expand Down
8 changes: 7 additions & 1 deletion packages/regenerator-transform/src/emit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,13 @@ Ep.explodeExpression = function(path, ignoreResult) {
case "ArrayExpression":
return finish(t.arrayExpression(
path.get("elements").map(function(elemPath) {
return explodeViaTempVar(null, elemPath);
if (elemPath.isSpreadElement()) {
return t.spreadElement(
explodeViaTempVar(null, elemPath.get("argument"))
);
} else {
return explodeViaTempVar(null, elemPath);
}
})
));

Expand Down
5 changes: 2 additions & 3 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,10 @@ enqueue(convert, [
"./test/class.es5.js"
]);

function convertWithSpread(es6File, es5File, callback) {
function convertWithParamsTransform(es6File, es5File, callback) {
var transformOptions = {
presets:[require("regenerator-preset")],
plugins: [
require("@babel/plugin-transform-spread"),
require("@babel/plugin-transform-parameters")
],
parserOpts: {
Expand Down Expand Up @@ -200,7 +199,7 @@ function convertWithSpread(es6File, es5File, callback) {
});
}

enqueue(convertWithSpread, [
enqueue(convertWithParamsTransform, [
"./test/regression.js",
"./test/regression.es5.js"
]);
Expand Down
18 changes: 18 additions & 0 deletions test/tests.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -2815,4 +2815,22 @@ describe("expressions containing yield subexpressions", function() {
done: true
});
});

it("should work when yield is in an array spread", function() {
function *gen() {
return [0, ...(yield "foo"), 3];
}

var g = gen();

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

assert.deepEqual(g.next([1, 2]), {
value: [0, 1, 2, 3],
done: true,
});
});
});

0 comments on commit c0b6b09

Please sign in to comment.