-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle exception in needs parens with optional chaining #16500
Handle exception in needs parens with optional chaining #16500
Conversation
Prettier pr-16500 --parser babel Input: (getConsoleLog?.())`` Output: getConsoleLog?.()``; Second Output: SyntaxError: Tagged Template Literals are not allowed in optionalChain. (1:1)
> 1 | getConsoleLog?.()``;
| ^
2 | |
In addition to the case you mentioned, i considered a few more exceptional cases. |
Co-authored-by: fisker Cheung <[email protected]>
Moved to |
There are two other cases need to fix. (a?.b)!``;
(a?.b!)``; Can you try to fix them? |
Okay. I will try this. |
@fisker |
tests/format/js/tagged-template-expression-ts/non-null-optional-chaning.ts
Outdated
Show resolved
Hide resolved
tests/format/typescript/tagged-template-expression/non-null-optional-chaning.ts
Outdated
Show resolved
Hide resolved
Looks good, I'll take another look later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a changelog entry https://github.com/prettier/prettier/blob/main/CONTRIBUTING.md#pull-requests
I added changelog. But what should I do about duplication error in lint? Should I combine js and ts changelog? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job, thanks.
Looks like I found 4 cases that failed
|
But should not related to tagged template literals. Prettier pr-16500 --parser babel-ts Input: (a?.b!)() Output: a?.b!(); This is wrong, it changed the call to optional chainning. But we format (a?.b!).c correctly, so we missed some checks for babel parser, can you try to fix? (Can be separate PR, since it's different case) |
Maybe it's here prettier/src/language-js/needs-parens.js Lines 1196 to 1198 in 11c3243
|
Also, can you move these tests to |
Separating PR may reduce the scope of work and make it easier to manage, but this actually seems to be handling with the exception of chain expression not only related to tagged template literals. I'll fix it soon.
I'll move test. and we can also move new expression also into chain expression in this pr? |
I modified existing implementation it to cover this. |
2a29e60
to
e004478
Compare
Let's not make this huge. We don't know what we will face. I'm going to merge this first. |
Would like to continue work on this? In next PR, we duplicate tests in this PR. And change For - ... ``;
new ...; For - ... ``;
... . foo;
foo [...]; For - ... ``;
... (foo);
foo(...); |
Thank you for organizing. I'll research this and submit PR soon. |
After we add/fix all tests. We need rewrite the existing logic, currently it's a mess. The logic should be:
This can be done in the followup PR. |
Description
This PR is just an exception handling that determines that parentheses are necessary when there is tagged template expression and optional chaining.
Fix: #16455
Checklist
docs/
directory).changelog_unreleased/*/XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨