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

Recast.print removes trailing new line #199

Closed
AugustinLF opened this issue Jul 14, 2015 · 2 comments
Closed

Recast.print removes trailing new line #199

AugustinLF opened this issue Jul 14, 2015 · 2 comments

Comments

@AugustinLF
Copy link
Contributor

I added a line in the middle of a file, and reprinted it, the trailing new line I had disappear. I didn't change any options, but isn't the reuseWhitespace suppose to be sufficient? Is there anything I misunderstood?

Here is the flow:

function File(filestring) {
  this.ast = recast.parse(filestring, {
    esprima: require('babel-core')
  });
}

File.prototype.print = function() {
  return recast.print(this.ast).code;
};

// Some modifications on the tree

fs.writeFile(filePath, file.print(), callback);

Nothing fancy, the print works perfectly, I got the line I wanted, but the trailing new line of my file disappeared. Any clues?

@benjamn
Copy link
Owner

benjamn commented Jul 14, 2015

Ah, options.reuseWhitespace is just for indentation whitespace (spaces and possibly tabs), though its meaning could possibly be extended its meaning to include lines as well.

In this case, I think the culprit is printStatementSequence, which tries to reuse the whitespace in between statements in a list of reprinted statements, but generally trims away any leading/trailing space before/after the first/last statements.

I think I can fix that pretty quickly (today).

Thanks for the report!

@AugustinLF
Copy link
Contributor Author

That would be awesome, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants