prettier / prettier/prettier

issue with a nested unary (not) condition

Open
#8,879 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

lang:javascript
Dominant language
JavaScript
Stars
52.3k
Forks
5k
Avg merge
19h 2m
Merged PRs (30d)
117

Description

Prettier 2.0.5
Playground link

--parser babel

Input:

if (veryFirstCondition ||
   (guardCondition1 &&
   guardCondition2 &&
   guardCondition3 &&
   !(negativeTestCondition1 &&
    looooooooooongNegativeTestCondition &&
    negativeCondition3)) // <===
) {
  doSOmething()
}

Output:

if (
  veryFirstCondition ||
  (guardCondition1 &&
    guardCondition2 &&
    guardCondition3 &&
    !(
      negativeTestCondition1 &&
      looooooooooongNegativeTestCondition &&
      negativeCondition3
    )) // <===
) {
  doSOmething();
}

Expected behavior:

if (
  veryFirstCondition ||
  (guardCondition1 &&
    guardCondition2 &&
    guardCondition3 &&
    !(
      negativeTestCondition1 &&
      looooooooooongNegativeTestCondition &&
      negativeCondition3
    )
  ) // <===
) {
  doSOmething();
}

Something similar but without the veryFirstCondition part seems to be formatted correctly:

Prettier 2.0.5
Playground link

--parser babel

Input:

if (
   guardCondition1 &&
   guardCondition2 &&
   guardCondition3 &&
   !(negativeTestCondition1 &&
    looooooooooongNegativeTestCondition &&
    negativeCondition3)) {
  doSOmething()
}

Output:

if (
  guardCondition1 &&
  guardCondition2 &&
  guardCondition3 &&
  !(
    negativeTestCondition1 &&
    looooooooooongNegativeTestCondition &&
    negativeCondition3
  )
) {
  doSOmething();
}


I did try the same thing with the recent updates from the master branch, with the same result.

I think this is not a super major issue but it would be nice to see it fixed someday.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the formatting difference from the linked Playground with the babel parser and the nested unary-condition input. Compare the actual output with the expected closing-parenthesis and comment placement, then locate the formatter tests covering similar conditional expressions and add a regression case whose output matches the expected example.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.