Repository navigation
Implement transform-async-to-promises - #7076
MatAtBread wants to merge 24 commits into
Conversation
Implement transform-async-to-promises
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/7756/ |
|
Great thanks for the initial PR, are you ok if I make some commits? I think this has tests from the other transforms, so we should merge into the existing plugin (which I can do if it was erroring) or at least remove the old files. |
|
@hzoo - as requested, pull implementing transform-async-to-promises. As I understand it, you're intending to merge into transform-async-to-generator and used options to specify the target. I have implemented only two options:
|
|
...sure - it's your project/plugin. Like everyone I have an opinion, but I'm no Babel expert (and only a very occasional user). Feel free to ask questions, raise issues, etc. I'm certainly interested in where it goes next |
| }).program; | ||
| } | ||
|
|
||
| /* Polyfill for Object.assign(...) from: |
There was a problem hiding this comment.
Please do not use MDN code in production anywhere :-/ this code doesn’t work with symbols, and has a number of other subtle bugs.
I’d suggest object.assign on npm.
There was a problem hiding this comment.
Well we don't need this at all, can just use Object.assign because min version is Node 4, or object spread
There was a problem hiding this comment.
If you look closely, it's not polyfilling the prototype, it's just defined & called locally. In fact, it could easily be replaced by a simple for-loop. And as hzoo says, if you're only targeting Node >4, Object.assign() will work fine.
@hzoo - can I assume you're OK to maintain this for now?
There was a problem hiding this comment.
Babel 7 does target Node 4 so you can use Object.assign by itself in this PR
There was a problem hiding this comment.
@MatAtBread right, i'm saying that basically all the code on MDN is broken and should never be used for any reason beyond "loosely explaining what a thing does". Since babel requires node 4+, this can just be deleted, but the larger point about MDN code stands.
| }).program; | ||
| } | ||
|
|
||
| /* Polyfill for Object.assign(...) from: |
|
|
||
| if (options.codeGenerationOptions) { | ||
| let keys = Object.keys(options.codeGenerationOptions); | ||
| for (let i=0; i<keys.length;i++) { |
There was a problem hiding this comment.
per previous comments, this can just be replaced with Object.assign(opts, options.codeGenerationOptions);
There was a problem hiding this comment.
(IIRC we use transform-object-rest-spread in babel itself so) alternatively,
const opts = {
// Code generation options
es6target: false,
// ...
$makeThenable: "$makeThenable",
...options.codeGenerationOptions
};There was a problem hiding this comment.
If you look at the code, there's a conditional to prevent unknown keys being placed in the destination object. This is not the same as replacing it with Object.assign, which unconditionally copies all keys, irrespective of their existence in the destination.
I'm not familiar enough with the semantics of the Object spread operator to know if it is a suitable replacement for this simple loop. Is there something about for loops you don't like?
There was a problem hiding this comment.
Just the unreadability, hence this very confusion :-)
| if (options.codeGenerationOptions) { | ||
| let keys = Object.keys(options.codeGenerationOptions); | ||
| for (let i=0; i<keys.length;i++) { | ||
| if (keys[i] in opts) |
There was a problem hiding this comment.
This line seems to be indented weirdly?
There was a problem hiding this comment.
No idea why - I'd guess something put tabs in place of spaces or vice versa. I'm not able to fix this at the moment (away from my home setup with the correct tools). Feel free to edit to match your preferred style.
There was a problem hiding this comment.
...I just had a go with the github editor, but TBH I'm not familiar with how these are best merged online.
There was a problem hiding this comment.
Oops, sorry, I'll dismiss my review. I'm not personally confident in approving this, but I definitely am not blocking it further :-)
|
@hzoo - I have no idea how to finally get this pull accepted. Maybe you can help? |
| return generate(ast, {}).code; | ||
| } | ||
|
|
||
| function parse(code) { |
There was a problem hiding this comment.
I wondering why you need to parse code here? Isn't it a regular Babel plugin?
There was a problem hiding this comment.
There is an internal templating system which needs parse the templates.
|
|
||
| if (runtime) { | ||
| newAst.body.splice(0, 0, { | ||
| type: "ImportDeclaration", |
There was a problem hiding this comment.
Could you please use babel-types for that? and maybe babel-traverse to place to node at the correct location?
There was a problem hiding this comment.
Actually, I don't have the bandwidth to learn a new API or style/code conventions. I started this as a favour to @hzoo, and as far as I know the current implementation works.
@hzoo - I'm happy to invite you (or anyone else) to my babel fork, but I'm really unable to provide the time to do this to the Babel team's satisfaction.
| }).program; | ||
| } | ||
|
|
||
| const transform = require("nodent-transform").transform; |
There was a problem hiding this comment.
This answers https://github.com/babel/babel/pull/7076/files#r160679424. Could we rather have a real Babel plugin?
There was a problem hiding this comment.
Not without a lot of effort - far more than I can commit to. This was discussed initially in MatAtBread/fast-async#46 and at that point the costs of re-coding for the babel API (ignoring the stylistic requirements) would not be viable without the significant contribution of knowledgable babel contributors, hence a half-way where the 'transform' part of nodent was stripped of its dependancies was provided by me.
Fix indenting lines 44-51
|
Is anyone able to explain the CircleCI issues? They appear unrelated to the changes in the pull |
|
Hello @mAAdhaTTah and Babel Team (@nicolo-ribaudo, @hzoo)! Do you plan to continue work on this PR? What are the pending issues? Do you still see a value in this concept? If you need any help, I'm here for you! |
|
I have no plans to a work on it. It was left to @hzoo and the Babel team to decide whether to incorporate it or not |
|
#7076 (review) is a good summary of the minimum steps that need to be done to move this forward. However,
|
|
In the time that this PR has sat open the need for it has largely disappeared. Async function support is now up to above 90%. Still, thank you @MatAtBread for trying. |
|
I don’t agree that the need has disappeared at all; I’d still very much like to see this land in core. “90%” doesn’t mean anything; since everyone’s targets are different. |
|
There is no need to remove the current approach when this one would be merged. It may be an option like a loose mode is for many presets, don't you think? In that way, everybody may choose what is best for a particular use case. |
|
|
What would the use case be for preferring generators over promises though? 😕 |
|
@nicolo-ribaudo I meant an option to the preset, similar to @Dan503 I suppose compliance with specs would be the benefit of the original generator approach. Maybe there is something more, like handling more complex cases. I'm thinking about nested and conditional awaits, async generators etc. In general, what I would like to accomplish is not having to advise people dropping |
It doesn't matter if it's the default behavior or not. If it's not implemented as a Babel plugin we cannot maintain it. Nodent isn't a Babel plugin. It passes the original AST to an external compiler, which doesn't use Babel's architecture. Deleted - I was wrong1. When there is an async function, transform it into a source code string 2. Pass this string to the nodent compiler 3. Get a compiled source code from the node compiler, parse it and put it back in the original positionThe whole compilation is opaque to us, and it's not something that we can maintain. {
"presets": [
["@babel/preset-env", {
"exclude": ["async-to-generator"]
}]
},
"plugins": [
"transform-async-with-nodent"
]
}We have nothing against transpiling async to promises, but also the community is free to publish a plugin outside of preset-env.
@Dan503 This. 💯 Also, transpiling async functions to generators (not to es5) produces an output which is much more similar to the original one rather than when using promises: // input
(async () => {
const response = await fetch(url);
const asJSON = await response.json();
console.log(asJSON);
})();
// Compiled to generators
_asyncToGenerator(function* () {
const response = yield fetch(url);
const asJSON = yield response.json();
console.log(asJSON);
})();
// Compiled to promises
(() => new Promise(function ($return, $error) {
let response, asJSON;
return fetch(url).then((function ($await_1) {
response = $await_1;
return response.json().then((function ($await_2) {
asJSON = $await_2;
console.log(asJSON);
return $return();
}).$asyncbind(this, $error), $error);
}).$asyncbind(this, $error), $error);
}))();Additionally, I just noticed that nodent's output modifiers the native |
|
Actually, that's not true. It passes the parsed AST to nodent, which does all the tree transforms itself (not using the Babel utils). I'm all in favour of debate, but please base it on facts. It did considerable work on factoring it the transformer especially for this PR. It's up to you if you use it, but don't misrepresent what it does |
|
Thanks, I updated my comment. |
|
In the short term, we could link the third-party plugin in Babel's docs. |
|
Alright, I see the whole story now. If it's about maintainability and code ownership, maybe there is nothing wrong with rejecting this PR and continuing the work somewhere else? I suppose rewriting the code to Babel utils would require starting the work from scratch. It's far more than just polishing the code. If we're focused on results more than on resolving technical or operational issues, maybe we should start from releasing a stable version of @nicolo-ribaudo, @MatAtBread, what do you think? |
|
Whatever is appropriate for you. As for maintainability, it is a complex piece of code, and quite old (the original implementation is maybe 5 years old). However, it has a full test suite in the main compiler (nodent, as opposed no nodent-transform which is just the AST transformer) which covers many more test cases than babel's current set and it has been in use in production systems for many years. I've not had to do anything to it for months to meet edge cases or indeed the spec (there are compiler flags for strict and loose adherence to the spec, along with options for output to ES5, Promises, ES6, etc). If I remember correctly, the babel version defaults to strict adherence to the spec with no runtime at all and using Promises. All that having been said, like last time this came up, this seems to be a "political" or "philosophical" debate. It's not about the code or it's functionality (which is fully in line with the rest of the world), it's about whether making the option available to end users of babel should be easy (included) or hard (a comment somewhere in the docs) vs. the potential cost of maintenance. This is not something I have a view on as I'm not a babel maintainer. |
|
Matt, you're confusing me. Are you at work or having a sandwich break? 😐 |
|
Erm, both? It was lunch time in London. I'm not trying to be confusing. I'm genuinely agnostic about whether Babel include the transformer or not. Most of my day job is backend, or if it's FE it's not for IE11, and I'm not a Babel user in general. So it really is whether there's a demand in the Babel user base for an async transformer with no runtime. As an aside, when I first wrote it, it really was 10x quicker than regenerator & Promise polyfills, but now these are both implemented in major engines like V8, the point is moot as native is easily twice as quick as any userland implementations Should you decide to integrate into Babel's core product, I'm happy to advise where I can, but it's not something I have an active interest in myself |
|
I also think you're looking at the wrong plugin. Try https://www.npmjs.com/package/fast-async - that has 22k a week. I think someone built their own plugin on top of fast-sync for Babel & used the name |
|
Just a stupid joke, you use two GitHub accounts interchangeably. I suppose by accident, but I found it funny because of the names. Sorry about that. Do you refer runtime speed, right? I'm more interested in optimizing bundle size. I didn't consider potential improvement or regression in execution, but my gut feeling is Promises winning here as well, there is less code to run. On the other hand, there is some cost of creating many lambdas for Huh, I'm looking at the wrong plugin! 😱 |
|
Oh yeah! I forgot. I have a paid-for-put-food-on-the-table and other stuff. It depends if I respond from my phone or desktop! |
|
since this is showing activity again, just chiming in once more: if you're just trying to avoid using babel.config.jsmodule.exports = {
plugins: [
[
'@babel/plugin-transform-regenerator',
{
asyncGenerators: false,
generators: true,
async: false,
},
],
[
'module:fast-async',
{
spec: true,
},
],
],
presets: [
[
'@babel/preset-env',
{
exclude: [
'transform-regenerator',
'transform-async-to-generator',
],
},
],
],
}; |
|
@erykpiast - if you want the Babel-7 plugin, that I prepped for this PR, it's here: https://github.com/MatAtBread/fast-async/tree/babel-v7 |
|
@nicolo-ribaudo The comment about modifying the Function.prototype is a fair one, but you have not used the super-clean & spec options to test. Here's is the output with no runtime at all (function protoypical or otherwise). Of course, it's even bigger and more ugly, but somewhat easier to read that regenerators output. Of course, if you have a runtime that does generators, you don't need either. |
|
What's the latest story about this PR? Seems it has been open for a long time. |
|
You can use the |
No description provided.