Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zenparsing async generator functions #4576

Merged
merged 2 commits into from Sep 27, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/babel-core/src/transformation/file/index.js
Expand Up @@ -414,7 +414,7 @@ export default class File extends Store {

parse(code: string) {
let parseCode = parse;
let parserOpts = this.opts.parserOpts || this.parserOpts;
let parserOpts = this.opts.parserOpts;

if (parserOpts) {
parserOpts = Object.assign({}, this.parserOpts, parserOpts);
Expand All @@ -441,7 +441,7 @@ export default class File extends Store {
}

this.log.debug("Parse start");
let ast = parseCode(code, parserOpts);
let ast = parseCode(code, parserOpts || this.parserOpts);
this.log.debug("Parse stop");
return ast;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/index.js
Expand Up @@ -122,7 +122,7 @@ function findCommonStringDelimiter(code, tokens) {
if (occurences.single > occurences.double) {
return "single";
} else {
return DEFAULT_STRING_DELIMITER;
return "double";
}
}

Expand Down
16 changes: 14 additions & 2 deletions packages/babel-helper-transform-fixture-test-runner/src/index.js
Expand Up @@ -47,14 +47,15 @@ function run(task) {

let execCode = exec.code;
let result;
let resultExec;

if (execCode) {
let execOpts = getOpts(exec);
result = babel.transform(execCode, execOpts);
execCode = result.code;

try {
runExec(execOpts, execCode);
resultExec = runExec(execOpts, execCode);
} catch (err) {
err.message = exec.loc + ": " + err.message;
err.message += codeFrame(execCode);
Expand Down Expand Up @@ -90,6 +91,10 @@ function run(task) {
chai.expect({ line: expect.line, column: expect.column }).to.deep.equal(actual);
});
}

if (execCode && resultExec) {
return resultExec;
}
}

function runExec(opts, execCode) {
Expand Down Expand Up @@ -151,7 +156,14 @@ export default function (
return throwMsg === true || err.message.indexOf(throwMsg) >= 0;
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we try to handle the promise case here too, to allow async tests to reject.

Copy link
Member Author

@hzoo hzoo Sep 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should, do you want to do that in another pr if you want to and with an example test?

} else {
runTask();
if (task.exec.code) {
let result = run(task);
if (result && typeof result.then === "function") {
return result;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically just returning the result of the runExec(execOpts, execCode); if it's a promise. We could also just always return

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the .then check? I'd have thought we could return it without worrying to let Mocha worry about whether it is a promise.

}
} else {
runTask();
}
}
});
}
Expand Down
@@ -1,11 +1,12 @@
let agf = (() => {
var ref = babelHelpers.asyncGenerator.wrap(function* () {
var _ref = babelHelpers.asyncGenerator.wrap(function* () {
this;
yield babelHelpers.asyncGenerator.await(1);
yield 2;
return 3;
});

return function agf() {
return ref.apply(this, arguments);
return _ref.apply(this, arguments);
};
})();
@@ -1,13 +1,13 @@
(() => {
var ref = babelHelpers.asyncGenerator.wrap(function* () {
var _ref = babelHelpers.asyncGenerator.wrap(function* () {
this;
yield babelHelpers.asyncGenerator.await(1);
yield 2;
return 3;
});

function agf() {
return ref.apply(this, arguments);
return _ref.apply(this, arguments);
}

return agf;
Expand Down
@@ -1,9 +1,10 @@
let g = (() => {
var ref = babelHelpers.asyncGenerator.wrap(function* () {
var _ref = babelHelpers.asyncGenerator.wrap(function* () {
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator([1, 2, 3]), babelHelpers.asyncGenerator.await);
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator(iterable), babelHelpers.asyncGenerator.await);
});

return function g() {
return ref.apply(this, arguments);
return _ref.apply(this, arguments);
};
})();
@@ -1,5 +1,5 @@
let f = (() => {
var ref = babelHelpers.asyncToGenerator(function* () {
var _ref = babelHelpers.asyncToGenerator(function* () {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this happened but some other code changed in the time between the original PR and now

var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
Expand All @@ -25,7 +25,8 @@ let f = (() => {
}
}
});

return function f() {
return ref.apply(this, arguments);
return _ref.apply(this, arguments);
};
})();
@@ -0,0 +1,26 @@
"use strict";

async function* genAnswers() {
var stream = [ Promise.resolve(4), Promise.resolve(9), Promise.resolve(12) ];
var total = 0;
for await (let val of stream) {
total += await val;
yield total;
}
}

function forEach(ai, fn) {
return ai.next().then(function (r) {
if (!r.done) {
fn(r);
return forEach(ai, fn);
}
});
}

var output = 0;
return forEach(genAnswers(), function(val) { output += val.value })
.then(function () {
assert.equal(output, 42);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert.equal(output, 42); in a promise. Needs to use the allowReturnOutsideFunction parser option

});

@@ -0,0 +1,11 @@
{
"plugins": [
"external-helpers",
"transform-async-to-generator",
"transform-async-generator-functions"
],
"presets": ["es2015"],
"parserOpts": {
"allowReturnOutsideFunction": true
}
}
@@ -1,5 +1,5 @@
let g = (() => {
var ref = babelHelpers.asyncGenerator.wrap(function* () {
var _ref = babelHelpers.asyncGenerator.wrap(function* () {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
Expand All @@ -25,7 +25,8 @@ let g = (() => {
}
}
});

return function g() {
return ref.apply(this, arguments);
return _ref.apply(this, arguments);
};
})();
@@ -1,5 +1,5 @@
let f = (() => {
var ref = babelHelpers.asyncToGenerator(function* () {
var _ref = babelHelpers.asyncToGenerator(function* () {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
Expand All @@ -25,7 +25,8 @@ let f = (() => {
}
}
});

return function f() {
return ref.apply(this, arguments);
return _ref.apply(this, arguments);
};
})();
@@ -1,7 +1,7 @@
{
"plugins": [
"external-helpers",
"transform-async-to-generator",
"transform-async-to-generator",
"transform-async-generator-functions"
]
}
@@ -1,5 +1,5 @@
let g = (() => {
var ref = babelHelpers.asyncGenerator.wrap(function* () {
var _ref = babelHelpers.asyncGenerator.wrap(function* () {
var _this = this;

(function () {
Expand All @@ -14,7 +14,8 @@ let g = (() => {
});
yield babelHelpers.asyncGenerator.await(1);
});

return function g() {
return ref.apply(this, arguments);
return _ref.apply(this, arguments);
};
})();
@@ -1,11 +1,12 @@
let g = (() => {
var ref = babelHelpers.asyncGenerator.wrap(function* (x = babelHelpers.asyncToGenerator(function* () {
var _ref = babelHelpers.asyncGenerator.wrap(function* (x = babelHelpers.asyncToGenerator(function* () {
yield 1;
})) {
yield babelHelpers.asyncGenerator.await(2);
yield 3;
});

return function g(_x) {
return ref.apply(this, arguments);
return _ref.apply(this, arguments);
};
})();
@@ -1,18 +1,20 @@
let f = (() => {
var ref = babelHelpers.asyncToGenerator(function* () {
var _ref = babelHelpers.asyncToGenerator(function* () {
let g = (() => {
var ref = babelHelpers.asyncGenerator.wrap(function* () {
var _ref2 = babelHelpers.asyncGenerator.wrap(function* () {
yield babelHelpers.asyncGenerator.await(2);
yield 3;
});

return function g() {
return ref.apply(this, arguments);
return _ref2.apply(this, arguments);
};
})();

yield 1;
});

return function f() {
return ref.apply(this, arguments);
return _ref.apply(this, arguments);
};
})();