Skip to content

Recast.print removes trailing new line #199

Closed

Description

@AugustinLF

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?

Activity

  1. benjamn commented on Jul 14, 2015

    @benjamn
    Owner

    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!

  2. AugustinLF commented on Jul 14, 2015

    @AugustinLF
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions