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
feat(typescript-estree): add ParenthesizedExpression when surrounded by JSDoc type comment #3135
Conversation
Thanks for the PR, @armano2! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day. |
do we want to generate them in when comment is outside of node? const x = /* test */ (2);
const x = /* test */(2);
const x = (2) /* test */;
const x = (2)/* test */; |
For the specific usecase of a JSDoc type assertion - I think we should specifically handle that case with an AST node as part of the normal parse cycle (with no option required). For other cases - I thought the intention was to indicate all parentheses via the AST? |
this is breaking change, and will require major release
if that's a case we can just remove condition hasComments(this.ast, node) |
Codecov Report
@@ Coverage Diff @@
## master #3135 +/- ##
=======================================
Coverage 92.88% 92.88%
=======================================
Files 315 315
Lines 10706 10708 +2
Branches 3025 3026 +1
=======================================
+ Hits 9944 9946 +2
Misses 342 342
Partials 420 420
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Yes, a little. When I was talking about nested parentheses, first and foremost I had nested JSDoc type assertions in mind. Prettier doesn't care about other parenthesized expressions. Just like in TypeScript, you can sometimes see things like |
That said, as mentioned in estree/estree#194, seemingly redundant parentheses sometimes can have runtime consequences in JS, so probably it's another use case where it makes sense to represent them in the AST. Nestedness doesn't matter in that case though. |
this option name seem better than mine we can go both ways, i can generate them for all or when comments are present, i have no opinion on this. |
That's the only case when I personally would like to have them generated. Besides, the comment should be leading and contain The syntax of JSDoc type assertions is simple: |
In that case I'd suggest that we just handle the specific case of a JSDoc type assertion and ignore all others as I mentioned here: #1682 (comment) I don't think this is a breaking change - we don't usually treat adding a new AST node as a breaking change. In the absolute strictest sense: yes, a new node is a breaking change, and a new property value is too (eg nullish coalescing added That is a bad line to follow and was what lead the old project to release 18 major versions in 4 months. |
ok updated base on feedback, and i changed tests a little for this |
TypeScript expects the type to be enclosed in curly brackets, however Closure Compiler accepts types in parens and even without any delimiters at all. That's why Prettier searches comments only for starting with upd: Looks like this has changed. TS is fine with types without delimiters, but still doesn't accept types in parens ( |
requested changes has been applied |
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.
all the code and tests LGTM.
One comment.
export interface ParenthesizedExpression extends BaseNode { | ||
type: AST_NODE_TYPES.ParenthesizedExpression; |
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.
Because this case is specifically about JSDoc type assertions, and we have no immediate or future plans to support more parenthesized expressions - I think we should call this JSDocTypeAssertion
.
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.
ParenthesizedExpression
is an established thing in the ecosystem. Could we keep this type
and add a boolean property jsDocTypeAnnotation: true
instead? (The typeAnnotation
property containing a type will do too, but it's out of scope for now I guess.)
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.
Within ESTree its not an established practice. Most JS parsers don't emit it either. Babel does emit it all-or-nothing via a flag.
Adding a new node type is a much better API than using something generic because it's a special type of expression which has special meaning AND syntax to typescript and we should treat it akin to an as
or angle bracket assertion. This isn't just a regular parenthesised expression where the parentheses control order of operations.
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.
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.
Babel does emit it all-or-nothing via a flag.
I'd say that's what most JS parsers do. I was a bit worried about compatibility issues for various tools like recast. There is no evidence people use it with this parser though. If you think it's not a big deal, so be it.
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.
@armano2 Note that that PR doesn't change whether or not we create ParentesizedExpression
nodes, it only ensures that we don't throw when we do emit them.
I forgot to mention that, just like usual TS type assertions, this syntax can be the RHS of an assignment (see TS playground). This might be something that should be taken into account in this PR. |
This PR adds option to generate
ParenthesizedExpression
if they are surrounded by jsDoc comments#1682
cc. @thorn0