-
Notifications
You must be signed in to change notification settings - Fork 342
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
Control of empty lines #147
Comments
Hmm, I wonder if we could support attaching a custom property like Note that, currently, the space preservation logic only works for lists of statements: https://github.com/benjamn/recast/blob/master/lib/printer.js#L1078-L1092 It might be worth supporting space preservation and |
This might be similar to my use case. I'm trying to concatenate files while preserving comments. The problem is as I do something like: var body1 = ast1.program.body;
var body2 = ast2.program.body;
body1.push.apply(body1, body2); They get squished together. I'd like to add a bit of whitespace to them and pretty printing does that, but it also removes the comments which are at the top of each concatenated file. Is there a better way to do this? I've combed the documentation pretty good but I could have missed something. |
@treshugart Ah, that's an interesting use case! I suspect the comments you're talking about are attached as Maybe try something like this? var body1 = ast1.program.body;
var body2 = ast2.program.body;
var firstStmt = body2[0];
var prog2Comments = ast2.program.comments;
if (prog2Comments && firstStmt) {
firstStmt.comments = prog2Comments.concat(firstStmt.comments || []);
}
body1.push.apply(body1, body2); |
Hmm, the comments are preserved if I don't pretty print, but what I really want to do is put some whitespace between the two. Taking my earlier example: var body1 = ast1.program.body;
var body2 = ast2.program.body;
// I know whitespace isn't really a valid token in ESTree, but Recast maintains
// the original formatting, so I was hoping something like this could be achieved.
body1.push('\n', '\n');
body1.push.apply(body1, body2); |
Another option is just to |
My main concern is keeping the original references to AST nodes so that I can generate source maps from the concatenated source from |
Anyways, I don't want to detract from the main issue too much. Thanks again for your help. |
This has been a blocker for us trying to create an auto formatter for |
This still seems worthwhile (cf. https://twitter.com/phuunet/status/661246993822580736), and I hope to find some time to work on it soon. If we support both Another question: is there every any reason to add whitespace other than newlines? If not, then we could go with numeric properties like |
Unfortunately, these properties looks like a hack for the concrete cases. Are you really sure you want to keep trying it with just AST? Since there are already some implementations of CST: |
This issue predates both those CST implementations. A good CST implementation is probably the optimal way forward, but they are a little immature to introduce into a It would be really interesting to modify |
Idk if you care about this, but I found a place where recast is pointlessly removing a newline. If you have two type declarations separated by a newline and you run it through the I explored that here: zxbodya/flowts#12 |
Is there any plans to fix this? I'm having issues with recast inserting unnecessary line breaks and empty lines in object declarations I'm generating. Which Prettier of course won't strip out since recast inserts them the way a user would intentionally add them. I don't know of any alternative to recast that can be used instead. |
Hi,
I have just made a recast script and it is working well but I have a final request that I did not manage to solve. I wonder if it possible.
My script reorders
requirejs
define's calls according to a giving priority.with priority
[ 'views', 'models' ]
is transformed intoThe only problem here is that recast is smart enough to reuse the whitespace so it puts an empty line between
views/1
andviews/3
but I want to put this empty line betweenviews/3
andmodels/2
. I was thinking of trickingrecast
by modifying thenode.original
object but skimming through the source code oflines.js
, I saw that lines are supposed to be immutable.Is there a proper way to do that ? I have tried with
reuseWhitespace
set tofalse
but to no avail..Thanks for recast, it is awesome 👍
My complete script is here : https://gist.github.com/ptbrowne/2b5e9682698cdd5316f1
The text was updated successfully, but these errors were encountered: