-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Beautify mode: comments are lost in the end of function body #88
Comments
There are other cases too. Generally, comments are lost if there is no AST node following them on the current nesting level. More examples: if (foo /* lost comment */ && bar /* lost comment */) {
// this one is kept
bar();
/* lost comment */
}
// comments right before EOF are lost as well It's a known problem and I kept thinking of it but there's no easy fix. |
Hello, It seems comments are also lost when code follows as opposed to closing curly brace in your example: /*! a */
var someVar=1;
/*! someArray */
var someArray=[];
/*! someObject */
var someObject={};
var blabla; |
Another troublesome example of this problem is with dropped function names: (function/*!foo*/a(){ console.log(a); })(); => !function/*!foo*/o(){console.log(o)}(); But: (function/*!foo*/a(){ console.log("uh oh"); })(); => !function(){console.log("uh oh"))}(); Note: I think my particular example of this comment-dropping is related to #547 in that I think going forward we're going to need an option for not dropping function expression names (even if they're not lexically referenced) -- just filed #552 for such. BUT, it would certainly be nice if (function/*!foo*/(){ console.log(a); })(); => !function(){console.log(a)}(); |
Any solution for this? :) |
While I'm not certain that the causes are related, I've found another case where comments will not be preserved. Apologies if there is a duplicate issue: closest potential I found was #478. With // example one
if ( true ) { console.log( true ); }
// @foo bar
if ( true ) { console.log( true ); }
// example two
if ( true ) { console.log( true ); }
// @foo bar
if ( true ) { console.log( true ); }
var x;
// example three
if ( true ) { console.log( true ); }
// @foo bar
if ( true ) { console.log( true ); }
anything(); However, this configuration will produce a comment: if ( true ) { console.log( true ); }
// @foo bar
var x; Someone should be able to reproduce with version require( 'uglify-js' ).minify( 'foo.js', {
output: {
comments: /@foo/,
},
}); Important note: I can get this behavior with a regular expression and the 'license' comments option. @avdg may have alluded to why that might be, but even passing 'all' or |
Certain AST transformations will cause comments to be lost. If you selectively disable some compress options your example will work.
but comments in the following code will not be preserved with these options:
===>
Some optimizations are destructive and leave no place for comments. I suppose they could be collected and put at the end of the transformed expression but it would look odd. |
I wonder if AST_Scope can contain the comments at the end of a block. This will need to be investigated technically to see if this can be implemented... |
(please note that this doesn't mean that I will work on it. I'll be probably too busy to implement this for now) |
Just to confirm that the issue is still there in version 2.6.4. |
👍 |
For example,
becomes
The text was updated successfully, but these errors were encountered: