-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Babel very strange move comments #5512
Comments
Hey @burashka! We really appreciate you taking the time to report an issue. The collaborators If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack |
Thanks for the report, there's a few issues with comments already and probably all the same root cause of not knowing where to keep the comments https://github.com/babel/babel/issues?q=is%3Aissue+is%3Aopen+comments+label%3A%22comment+attachment%22 I think you just won't be able to rely on the fact that Babel will be able to put it make in the right place without us switching the way comments work. The normal usecase for Babel is compiled/output code so whitespace/comments aren't our focus. This is why our printer doesn't have any options for things like quotes anymore. |
Comments are challenging because they can occur anywhere. The way the current algorithm works, comments are marked as "leading" and "trailing" comments on nodes. This method works in that all comments are accounted for (i.e. no comments will be lost), but isn't very precise as far being able to calculate where the comment should go after transformation. This might be made better by thinking of comments in the context of token lists (i.e. the comment originally occurred after this opening parens token so let's put it back in right after the opening parens token), but I think there will always be cases where the comments will be in the "incorrect" location. For example, for the following code, given the following comment locations: function foo({ a = /* comment */ true }) {}
function foo({ /* comment */ a = true }) {}
function foo(/* comment */ { a = true }) {}
function foo({ a = true /* comment */ }) {}
function foo({ a = true } /* comment */) {} Where should the comments go in this transformed output for each respective comment location? function foo(_ref) {
var _ref$a = _ref.a,
a = _ref$a === undefined ? true : _ref$a;
} When the output is so different from the input, it's really hard to know, and I'm not sure it's worth solving this problem right now. Would you mind explaining the situations where the comments in the transpiled output are necessary? Would love to understand why you think this should be prioritized. |
Thanks for your answer. We develops UI Framework for APS Standard. It based on Dojo Toolkit and use DojoDoc for auto-generate documentation. DojoDoc have syntax: _attrToDom: function(/String/ attr, /String/ value){ or _attrToDom: function(/String/ /== attr ==/){. We transforms ES modules with ES2015+ features to AMD modules and build it with help Dojo Builder. Dojo Builder try generate documentation and crashes on moved comments. |
As #5503 demonstrates leading comments are messed up. I made PR #5504 to fix this issue, I haven't tested it on your example but I'm confident it would fix it. But keep in mind that comment output remains strange. To see what I mean you can check tests associated to that PR. It will help you to understand where comments will be when a node is removed using |
I have a similar issue with comments. My important comments are just headers. This means that I want my comments to stay on top of the code, and I want them to preserve the format. In my case, funny things happens depending if the original code includes For example, this code: /*\
title: $:/plugins/danielo515/tiddlypouch/config/single-db-config
type: application/javascript
module-type: library
@preserve
\*/
function dbConfig(name, remote) {
if ( typeof name === 'object' ){
remote = name.remote;
name = name.name;
}
this.name = name;
} Produces an unexpected output that breaks my headers: 'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
/*\
title: $:/plugins/danielo515/tiddlypouch/config/single-db-config
type: application/javascript
module-type: library
@preserve
\*/
function dbConfig(name, remote) {
if ((typeof name === 'undefined' ? 'undefined' : _typeof(name)) === 'object') {
remote = name.remote;
name = name.name;
}
this.name = name;
} BUT if I manually add input: /*\
title: $:/plugins/danielo515/tiddlypouch/config/single-db-config
type: application/javascript
module-type: library
@preserve
\*/
'use strict'
function dbConfig(name, remote) {
if ( typeof name === 'object' ){
remote = name.remote;
name = name.name;
}
this.name = name;
} Output: /*\
title: $:/plugins/danielo515/tiddlypouch/config/single-db-config
type: application/javascript
module-type: library
@preserve
\*/
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
function dbConfig(name, remote) {
if ((typeof name === 'undefined' ? 'undefined' : _typeof(name)) === 'object') {
remote = name.remote;
name = name.name;
}
this.name = name;
} Not sure if this is a feature or an unexpected behavior, but at least it allows me to save my comments, so please do not remove it. It will be also useful to document this. Regards |
wort mention that if you place your /*\
title: $:/plugins/danielo515/tiddlypouch/config/single-db-config
type: application/javascript
module-type: library
@preserve
\*/
(function(){
'use strict'
function dbConfig(name, remote) {
if ( typeof name === 'object' ){
remote = name.remote;
name = name.name;
}
this.name = name;
}})() Will produce the previous unexpected output |
@danielo515 The case you're referring to could just as easily go the other way. If I had
then
would be the opposite of what you'd expect because the docs are meant to be attached to the function, not the top of the file. I don't see a way for us the tell the difference between these two cases. |
@loganfsmyth as you can see in my previous answer I found the way to workaround my problem. I was just specifying in which cases you get an output other than the desired. I'm fine about putting the Regards |
Maybe |
|
The issue is that there is no "top" to keep things attached to. Comments just get attached to the thing they are nearest to. It's not a great system for keeping comment locations, but handling comments with anything more than a best-effort attempt is not something Babel has ever tried to do. It would likely require that we use a full concrete syntax tree instead of a general abstract syntax tree, and it would make transforms likely much more complex. To me it sounds like the best solution would be to update the documentation generator to run on the original source files. |
That makes sense in the case your objective is generating documentation. But some projects require comments and we just want to use the latest javascript with them. Regards |
What kind of examples do you have in mind? |
I'll give you my example, though I'm sure I can find a workaround. It feels most correct to me to keep my license header in every source file. Since these headers have I understand there is no logical top of the file to attach a comment to, but there is an implicit top that exists by inserting a comment before the first expression, I would think. It certainly seems like I could write a plugin that strips |
@loganfsmyth that's incredibly nice of you. Since you know this stuff in and out I guess that might not have been huge effort for you, but I appreciate it deeply. Thanks for being amazing. |
No problem! I was gonna post it in a comment and then decided I might as well just publish it :D |
Hello @loganfsmyth , tyddlywiki is an example. It uses magic headers on the JS files to determine the type of module, its name etc. What I'm doing is including the |
@danielo515 That is likely something that module can do. It is configurable so you should be able to just set |
Babel very strange move comments: example, DojoDoc comments in function and info about copyright.
Input Code
Babel Configuration (.babelrc, package.json, cli command)
es2015 preset
Expected Behavior
Current Behavior
Your Environment
You can reproduce on your sandbox ("Try it out"): http://babeljs.io/repl/
The text was updated successfully, but these errors were encountered: