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
Flow Typecast Comments #719
Comments
Thanks for the report, we need to fix it! |
The implementation is not super elegant but it seems to work. Fixes prettier#719
Do you mind running this pull request and try to find crazy annotations that wouldn't work with it? Thanks! |
The implementation is not super elegant but it seems to work. Fixes prettier#719
Seemed to work! |
Another idea: use the flow parser to parse comments and have a way to output flow types as comments instead of the ast version. |
Related to #204 ? |
I've just encountered the Flow comment syntax issues with functions Here are a few more test cases, I hope these are helpful https://prettier.github.io v0.22.0 // input
function fnNoArgs () /* : Promise<void> */ {
return Promise.resolve()
}
// output
function fnNoArgs(/* : Promise<void> */) {
return Promise.resolve();
} // input
function fnOneArg (a /* : string */) /* : Promise<string> */ {
return Promise.resolve(a)
}
// output
function fnOneArg(a /* : string */) /* : Promise<string> */ {
return Promise.resolve(a);
} // input
const arrowNoArgs = () /* : Promise<void> */ => {
return Promise.resolve()
}
// output
const arrowNoArgs = (/* : Promise<void> */) => {
return Promise.resolve();
}; // input
const arrowOneArge = (a /* : string */) /* : Promise<string> */ => {
return Promise.resolve(a)
}
// output
const arrowOneArge = (a /* : string */ /* : Promise<string> */) => {
return Promise.resolve(a);
}; |
Awesome work! It looks like most of the above has been resolved. As of 1.2.2, the first two examples above are now working perfectly. That is, these are the cases with traditional Arrow functions ( // input
const arrowNoArgs = () /* : Promise<void> */ => {
return Promise.resolve()
}
// output
const arrowNoArgs = () => /* : Promise<void> */ {
return Promise.resolve();
}; // input
const arrowOneArge = (a /* : string */) /* : Promise<string> */ => {
return Promise.resolve(a)
}
// output
const arrowOneArge = (a /* : string */ /* : Promise<string> */) => {
return Promise.resolve(a);
}; |
We now have a much better infrastructure for dealing with comments so we should be able to fix all the issues reported here :) |
Any update on this one? Flow comment-style annotations are still being moved from outside to inside the parens on arrow functions for me. I'm using Prettier 1.7.0. |
All the examples off arrow and regular functions are working correctly in master now. You can check out this preview (I copied all the samples in this issue): link I'll work on the OP example now |
Trying 1.10, this fixes a lot of cases, thanks! One remaining case I ran into is: this.state = ({ a, b } /* : AddDemoState */); |
As a workaround for that particular case, I think you could do: this.state /* : AddDemoState */ = { a, b }; |
Ah, just tried that. flow doesn't seem to like that, I'm guessing because |
I guess you could do this as a workaround then: const addDemoState /* : AddDemoState */ = { a, b };
this.state = addDemoState; |
(We still want to fix this long-term though) |
* install and run prettier * ignore style errors from eslint, rely on prettier for that, fixes #68 * fix commitlint task on linux * add precommit hook * add .prettierignore file * add prettier task to package.json * run prettier task on project * fix issues related to prettier/prettier#719
I'm not sure I'm understanding this correctly but I'm having problems with flow types using the comment syntax. This:
is becoming this:
which is valid flow syntax of course. However, I was expecting the comment syntax to be preserved. Am I missing something? |
@k7n4n5t3w4rt Can you reproduce that bug on the playground (prettier.io)? I ask because I've never personally experienced Prettier changing my type comments to code. |
@hawkrives It seems to be doing the same thing in the playground when the parser option is set to "flow": |
@k7n4n5t3w4rt It's a known bug, you can follow it on #3493 |
@k7n4n5t3w4rt Ah, you're using the Flow parser. If you don't need to use it, you can use the (default) Babylon parser, which also supports Flow types, and doesn't have this bug: playground link |
@hawkrives Aha! I didn't realise I could use the babylon parser, thanks. @duailibe Thanks for that, I'll keep an eye on it. |
can prettier be used to convert flow annotations into flow comments?
|
@pannous I don't think so, it's out of Prettier's scope, but it's really interesting and someone should totally build a tool that does that. |
@pannous you can use https://www.npmjs.com/package/@babel/plugin-transform-flow-comments to do that, though the formatting isn't great: const { code } = require("@babel/core").transform("var test:int=8", {
plugins: [require("@babel/plugin-transform-flow-comments")]
});
console.log(code); // "var test\n/*:int*/\n= 8;" |
What's the overall status of this? It seems there are two options, but neither is actually compatible with using Flow's comment syntax. The first option is to specify The second option is to stick with the default parser, which retains Flow's comments, but removes the necessary parentheses to satisfy Flow. So something like this:
This is no longer valid Flow syntax, so Flow throws an error. Am I correct that because of these two issues together, there's effectively no way to use Flow's comment syntax with Prettier (barring heavy use of
this slightly defeats the purpose of Prettier, since it no longer serves as an autoformatter that you can just forget about. |
@swac PR welcome |
@evilebottnawi Apologies if my comment sounded overly critical. I was just trying to sum up the current state of affairs, since it took me a while to piece together everything from this comment thread 😅 |
@swac don't worry, we have limited time and contributors to fix all problems, do it yourself it's true the quickest solution 😄 |
@evilebottnawi I took a stab at fixing this in #5280 😄 |
Fixed by #5280. |
In flow, you can typecast with the following,
You can also do this with comments,
However, this gets transformed into,
Both are syntactically valid, but only one is recognised by flow. Maybe we'd just want to check comments that start with
:
?Happy to help if you decide what direction we should go!
Edit
Also,
The text was updated successfully, but these errors were encountered: