Prettier adds redundant parentheses if there is a comment at the start, disrupting comment placement
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 52.3k
- Forks
- 5k
- Avg merge
- 19h 2m
- Merged PRs (30d)
- 117
Description
Prettier 3.6.2
Playground link
--parser typescript
Input:
// no extra () inserted
function testA(): boolean {
return !(
test1 ||
// comment in middle
test2 ||
// etc ...
test3
);
}
// extra () inserted
function testB(): boolean {
return !(
// comment at start
test1 ||
// etc ...
test2 ||
test3
);
}
Output:
// no extra () inserted
function testA(): boolean {
return !(
test1 ||
// comment in middle
test2 ||
// etc ...
test3
);
}
// extra () inserted
function testB(): boolean {
return !(
// comment at start
(
test1 ||
// etc ...
test2 ||
test3
)
);
}
Expected output:
// "input === output" in both cases and not just the first
Why?
The added parentheses are redundant and prevent the comment from being attached to the line it's supposed to be attached to.
Even letting the redundant parens be added and trying to add a new comment results in the comment being shoved outside the inner parens:
// input
function testB(): boolean {
return !(
// comment a
(
// comment b
test1 ||
// etc ...
test2 ||
test3
)
);
}
// output
function testB(): boolean {
return !(
// comment a
// comment b
(
test1 ||
// etc ...
test2 ||
test3
)
);
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Playground example using the TypeScript parser and compare the testA and testB outputs. Investigate the formatting behavior for a leading comment inside the negated expression; done means redundant parentheses are not added and comment placement remains correct in both examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100