-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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: removed support for Flow comments breaks codebases with mixed styles #15024
Comments
@mrtnzlml Can't you detect by file and directory name? prettier ./flow-types --parser=flow && prettier ./flow-comments --parser=babel works fine for you? |
Hi @sosukesuzuki, this is unfortunately not possible in my case. It's a large monorepo, and there really is a mix of styles in |
@mrtnzlml Honestly, we didn't know of such a use case. And I don't think it is a commonly used way either. So we may never support it. However, I understand that issue. Although it is an ad hoc approach, how about writing a script like the following? import fs from "node:fs/promises";
import * as prettier from "prettier";
import glob from "fast-glob";
const files = glob.sync("src/**/*.js", { absolute: true });
async function* format() {
for (const file of files) {
const data = await fs.readFile(file, "utf-8");
let result;
try {
result = await prettier.format(data, { parser: "babel" });
} catch {
try {
result = await prettier.format(data, { parser: "babel-flow" });
} catch {
yield { file, result: null };
}
}
if (data === result) {
yield { file, result: null };
} else {
yield { file, result };
}
}
}
for await (const { file, result } of format()) {
if (result === null) {
continue;
}
fs.writeFile(file, result);
} The script first formats using the babel parser, and if that fails, formats using the babel-flow parser. I know the execution time will be longer, but does this solve your problem? |
Maybe |
I do have a similar issue and it's not clear to me how to fix it.
By chance, is there a way to tell prettier to not rewrite the comments even in such files? update : we were using the parser |
We were also using some special comment to support iterators properly with flow: /*::
@@iterator(): * {
// $FlowFixMe ignore Flow error about Symbol support
return this[Symbol.iterator]()
}
*/ This tells flow about the existence of an iterator. But this isn't proper javascript code, that's why it's inside a flow comment. But now prettier tries to parse it and fails. I tried all parsers (babel, babel-flow and flow). It would be good to ignore the comments as before, if that's possible. |
Use config overrides to specify which files should be processed with which parser. @julienw Use any non-Flow parser for your files. E.g. |
This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further. |
The file that uses |
I managed to make it work finally by switching to the For my other files with only comments, I used |
I forgot why we don't just treat flow comments as normal comments. It doesn't make sense to me now. |
Found #13687 (comment) |
There's also some context in the blog post: https://prettier.io/blog/2023/07/05/3.0.0.html#flow |
Prettier 3.0.0
Playground link
I have a codebase that mixes Flow comments with Flow types (some files can have Flow types, but some files are not transpiled and therefore are using Flow comments). The latest change introduces a breaking change without a clear migration strategy:
Input:
This is just to demonstrate the issue; please read "Expected behavior" point 2 for the main issue. Point 1 is valid as well though.
Output with
parser:flow
:This is incorrect because I wanted to preserve the Flow comments (since I wrote them there for some reason). The same behavior is for
parser:babel-flow
. Usingparser:babel
is also not a valid option because it doesn't understand Flow (this is expected):Expected behavior:
There are basically 2 problems:
typescript
parser does it). I am OK not to mix the styles in one file, though. More importantly though:parser:babel
, but that would break on other JS files that contain Flow comments. Please note that both files have extension*.js
, so there is no easy way to distinguish these two cases.parser:babel-flow
orparser:flow
, but that would nuke the Flow comments in the other files where it's not desirable to remove them.How to deal with files containing multiple Flow styles after this change? 🤔
I am also trying to use
prettier-plugin-hermes-parser
which seem to work well even though it has its own issues: facebook/hermes#1047The text was updated successfully, but these errors were encountered: