prettier/prettier-eslint

BUG: prettier-eslint introduce bugs in code by removing necessary parenthesis

Bow2Jesus opened this issue · 3 comments

Versions:

  • prettier-eslint version: 7.9.0
  • node version: 13.14.0
  • npm (or yarn) version: 6.14.8

Have you followed the debugging tips?

Yes || No

No. This is a bug in the remove-unnecessary-parens rule itself. It is removing necessary parens.

Relevant code or config


data = (data === '' || data === 'false') ? 'false' : 'true';

**What I did:**

Ran "eslint --ext ts --fix" on the command line in the root of the project.

**What happened:**

data = (data === '' || data === 'false') ? 'false' : 'true';
was changed to:
data = data === '' || data === 'false' ? 'false' : 'true';
by eslint-prettier.

This is NOT logically the same.  The parens are necessary for the statement to evaluate correctly.

<!-- Please provide the full error message/screenshots/anything -->

**Reproduction repository:**

<!--
If possible, please create a repository that reproduces the issue with the
minimal amount of code possible.
-->

Create a repository that has a single object that has this as the only method:

  isTrueorFalse(data) {
    data = (data === '' || data === 'false') ? 'false' : 'true';
  }

**Problem description:**

data = (data === '' || data === 'false') ? 'false' : 'true';
was changed to:
data = data === '' || data === 'false' ? 'false' : 'true';
by eslint-prettier.

This is NOT logically the same.  The parens are necessary.

**Suggested solution:**

**Prefered solution:**

Fix remove_unnecessary_parens to not remove necessary parens. 

**Alternative solution:**

Ability to turn off remove_unnecessary_parens so the eslint-prettier does no harm to logic when formatting the code.



My co-workers thought that the example above changed the output of the code, but upon further review, I do not think that it does. However, it is a lot harder to understand without the parens, so they misinterpreted the outcome of the code after the parens were removed.. It is also a lot uglier without the parens.

So, I think that simply giving us the option to turn off the remove_unnecessary_parens rule will make our code a lot prettier and easier to understand without having to mark each and every line with parenthesis with a prettier-ignore comment.

So, I think that simply giving us the option to turn off the remove_unnecessary_parens rule will make our code a lot prettier and easier to understand without having to mark each and every line with parenthesis with a prettier-ignore comment.

It sounds like you might want to use an ESLint configuration file: https://eslint.org/docs/user-guide/configuring#using-configuration-files. You can tell the prettier-eslint command (if using prettier-eslint-cli) where to find that file with the --eslint-config-path flag (or you can browse more flags with prettier-eslint --help).

https://antfu.me/posts/why-reproductions-are-required

Feel free to comment to provide a minimal but runnable reproduction.