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
TypeScript: format TSAsExpression with same logic as BinaryExpression #6450
Conversation
5ef0df8
to
b6c8ee8
Compare
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.
Need better check, regexp is bad in this case
3086b23
to
0d27d3f
Compare
After thinking about this some more, I took a completely different approach. It feels like we should treat |
0d27d3f
to
74573bf
Compare
fbcd486
to
8e752db
Compare
8e752db
to
1e8b0b5
Compare
I'm having trouble recreating these test failures locally. Any idea what might be going on? I'm running them on Node v10.16.3 in macOS v10.14.6. |
case "VariableDeclaration": { | ||
const lastDeclaration = getLast(node.declarations); | ||
if (lastDeclaration && lastDeclaration.init) { | ||
overrideLocEnd(node, lastDeclaration); | ||
} | ||
break; | ||
} | ||
case "TSAsExpression": { | ||
convertTSAsExpression(node); |
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.
Feels pretty hacky to me, but I'm not sure how to do it better. There's quite a bit of code for printing binary expressions. On the other hand – do as
expressions really need to go through all of the binary expression stuff, like the JSX related parts? Maybe there’s a way to extract or copy the parts needed for as
?
@vjeux You have had useful insight before on PRs with hacky code – what does your experience tell you here (gut feeling)?
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.
Maybe there’s a way to extract or copy the parts needed for
as
?
My thought here was that keeping the two in sync would be a bigger maintenance burden, but if that's the preferred way of doing things, I can definitely look into it!
I'm also coming from working on babel-eslint, where the parser has to do a lot of massaging of the AST to get it to work with ESLint. 😅
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.
Does it modify the passed AST itself or a copy of it? What if someone else wanted to use that AST after Prettier? Or is such a scenario already unsupported anyway?
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.
do as expressions really need to go through all of the binary expression stuff, like the JSX related parts
Why not? as
can be used anywhere, just like +
or say &&
,
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.
Happy to work on this again once the team decides what the preferred approach is!
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.
Another case of aligning the formatting of as
with how other binary expressions are formatted: #6471
Do you run the tests with |
@thorn0 Ah, thanks! That's what I was missing! |
Friendly ping for the Prettier team - any insight on how we'd like to proceed here? |
@kaicataldo need fix CI problems |
I don't represent the Prettier team, but I expect them to support the following points:
|
I'm revisiting this tonight, and it looks like our options are either to:
The challenge of the first strategy is that we have to convert the The challenge of the latter is that we'll have a fork in that logic and will have to make future changes in both code paths. This feels more feasible, but still doesn't feel great to me. Thoughts? |
Might it make sense to replace all occurrences of |
@kaicataldo I think we should start with option 2 and then work from that to minimize the problems with forking later |
On the playground for this PR, I see that Prettier pr-6450 --parser typescript Input: const iter1 = createIterator(this.controller, child, this.tag as SyncFunctionComponent);
const iter2 = createIterator(self.controller, child, self.tag as SyncFunctionComponent);
const iter1 = createIterator(this.controller, child, this.tag ** SyncFunctionComponent);
const iter2 = createIterator(self.controller, child, self.tag ** SyncFunctionComponent); Output: const iter1 = createIterator(this.controller, child, this.tag as
SyncFunctionComponent);
const iter2 = createIterator(self.controller, child, self.tag as
SyncFunctionComponent);
const iter1 = createIterator(
this.controller,
child,
this.tag ** SyncFunctionComponent
);
const iter2 = createIterator(
self.controller,
child,
self.tag ** SyncFunctionComponent
); |
Sorry, haven’t been able to get back to this. Will close this for now so that someone else can work on it. |
fixes #6443.
I took a stab at working on wrapping
TSAsExpression
nodes!In taking a look at this, it looks like the behavior of moving the initial declaration value up to the same line as the=
operator happens with other declarations and I think the change might have to happen in that logic (here's an example using the Prettier playground).This PR instead focuses on wrapping theTSAsExpression
node correctly (ensuring that the line break occurs after theas
operator).This now takes a different approach and converts the
TSAsExpression
node to match theBinaryExpression
node interface so we can use the same formatting logic for both.I'm pretty new to the Prettier codebase, and any and all guidance is very welcome!
docs/
directory)CHANGELOG.unreleased.md
file following the template.✨Try the playground for this PR✨