siefkenj/prettier-plugin-pegjs

Newlines before end of block?

Closed this issue · 4 comments

nvie commented

Hey, just found this plugin, and it looks exciting! 💯

I tried it out, but I consistently get the following behavior, where an empty line will get inserted right before the end of a JS block.

Example output I'm getting:

start = StatementList

StatementList
  = first:Statement SEMICOLON* rest:StatementList {
      return [first, ...rest];

    }
  / only:Statement SEMICOLON? {
      return [only];

    }

Notice those empty lines?

I tried various settings, but wasn't able to get it to format this anyway else. When copy/pasting this example into the playground, it does not behave this way and works as expected! 🤯

I'm using prettier@2.3.0 and prettier-plugin-pegjs@0.3.2.

My .prettierrc:

{
  "semi": true,
  "printWidth": 120,
  "tabWidth": 2,
  "singleQuote": true,
  "trailingComma": "all"
}

Any ideas what this could be?

I'm glad you enjoy the plugin!

I suspect this is related to prettier 2.3. I haven't tested prettier 2.3 yet, and they changed they changed the internal representation of data from 2.2 to 2.3.

I suspect a change is needed here:

const formatted = utils.stripTrailingHardline(

Hopefully, I can get to this soon, but if you're up to it, a PR would be welcome :-)

nvie commented

Ah yes, can confirm this is an issue with 2.3.x only. Unfortunately I am not familiar with the structure of prettier's internal "Doc" tree format and its intricacies. I did noticed the textToDoc() function in Prettier now takes a { stripTrailingHardline: true } 5th argument, which seems to be new since 2.3? Perhaps this means that if you somehow pass it this option, you don't need to manually call utils.stripTrailingHardline() any longer?

I tried changing this, but there is a lot of textToDoc wrapping going on, and I can't seem to find the right place to populate this argument. None of my attempts changed anything. Could also very well be that I'm on the completely wrong track here. I guess the only way to actually make this fix is if you understand the Doc tree and its rules.

Hopefully this helps you as a pointer?

I just published 0.4.0, which loads prettier 2.3 as a dependency. I wasn't able to reproduce your issue...(the only thing I changed was the Prettier dependency.) I also upgraded the playground to 0.4.0 and prettier 2.3.0, and it seems to format correctly.

Please let me know if the new version magically fixes your issue.

nvie commented

Amazing! This magically worked — thanks so much for this great plugin! 💯