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] Parsing error when calling generic functions with type arguments when flow pragma is not first comment #9240
Comments
Hey @FermiDirak! We really appreciate you taking the time to report an issue. The collaborators If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack |
@FermiDirak Thank you very much, you saved me at least a few hours as without seeing this issue I would never guess that the problem is because of a position of the |
I'm experiencing the same issue with flow enabled by default on all files:
Adding |
Babel doesn't read your |
@nicolo-ribaudo yes, but afaik It comes with |
That preset taks an |
Ref: babel/website#1973 |
Thanks, @nicolo-ribaudo, it works 👍 In my case it was
|
Looks like this is probably because of
flowPragma to null for the first comment it sees, meaning that it will never try to process later comments.
That line should just be removed since the logic in babel/packages/babel-parser/src/plugins/flow.js Line 1601 in 165ef29
parseStatement call is actually a good place for it, or at least we want to make sure that code like
also doesn't count (I assume), but that comment isn't after any statement has finished. |
Just a quick note that this affects prettier as well, because prettier uses babel to parse JS files by default. Setting prettier's parser to |
after some investigation on the flow's source code, i noticed that the logic babel used to determine whether a file is deemed a "flow" file is different than flow's implementation Basically, in flow, it
in babel-parser flow-plugin, we look for
one edge case for this logic would be a<x>(y);
// @flow
a<x>(y); anything before the first flow comment would be parsed as non-flow js, and anything after it would be treated as flow, eg: first statement treated as |
I would like to contribute to fix this behavior, by adding a similar I've started with some test cases and boilerplates, #9885, but I am not sure on how to lookahead and parse the first |
Fixed by #9891 |
Bug Report
Current Behavior
When the @flow pragma is not specified as the first comment of a JS or JSX file, babel parser will not be able to parse any code that passes type arguments.
Input Code
Expected behavior/code
Flow should be able to parse functions that are called with type arguments so long as the @flow pragma exists before any code.
Babel Configuration (.babelrc)
Environment
loader
]Possible Solution
It appears this line ignores the flow pragma if it isn't found in the first comment of the source file.
babel/packages/babel-parser/src/plugins/flow.js
Line 77 in c586d4e
This could be changed a-la Flow-Strip-Types, which first checks all comments for a flow pragma before it begins transforming. (https://github.com/babel/babel/blob/c586d4e8cac816941bf2ba559472fd9cc3150921/packages/babel-plugin-transform-flow-strip-types/src/index.js). Perhaps a solution like this could be used here as well.
The text was updated successfully, but these errors were encountered: