Prettier: Make empty catch block with comment remain on one line

Created on 3 Feb 2017  路  3Comments  路  Source: prettier/prettier

I have some code like this

    try {
      body = JSON.parse(body);
    } catch (e) {} // eslint-disable-line no-empty

With prettyier v0.15.0, it becomes

    try {
      body = JSON.parse(body);
    } catch (e) {
    } // eslint-disable-line no-empty

And then eslint fails it because my no-empty comment is now on the wrong line.

As a fallback, I can change my code to:

    try {
      body = JSON.parse(body);
      // eslint-disable-next-line no-empty
    } catch (e) {
    }

Which then passes both prettier and eslint.

However, I'm still filing a bug because I think my original version looks better that either of the ones that prettier likes - I prefer the look of the empty block when it's only a single line.

I guess you can close this if you disagree...

locked-due-to-inactivity needs discussion

Most helpful comment

There are a combinatorial explosion of different ways to mix syntax. We want to keep it as consistent as possible, so your solution looks good to me. There are many places where the eslint-disable-line is going to be problematic because this printer does not work like that. We are going to break things across multiple lines when we need to.

My suggestion is to turn off rules that you find yourself disabling. I don't think they are worth it.

All 3 comments

Actually, I found a better solution:

    try {
      body = JSON.parse(body);
    } catch (e) {
      // if it fails, just return the body as a string
    }

Putting a comment inside the block makes both eslint and prettier happy without looking silly. I still think prettier should have handled the original version differently, but moving the comment to inside the block might be a valid option if it insists on spreading the block out over multiple lines.

There are a combinatorial explosion of different ways to mix syntax. We want to keep it as consistent as possible, so your solution looks good to me. There are many places where the eslint-disable-line is going to be problematic because this printer does not work like that. We are going to break things across multiple lines when we need to.

My suggestion is to turn off rules that you find yourself disabling. I don't think they are worth it.

Prettier now leaves the input as is

    try {
      body = JSON.parse(body);
    } catch (e) {} // eslint-disable-line no-empty
Was this page helpful?
0 / 5 - 0 ratings