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
Fix closure compiler type casts #5947
Conversation
Babel does this: |
Yup, but prettier seems to use allow using several different AST parsers. |
It does, but is this change relevant for TypeScript and Flow? If not, only Babel is left. |
Are the tests run with babel parser? I tried to do this directly with |
JS tests are usually run with the 3 parsers, e.g. https://github.com/prettier/prettier/blob/master/tests/multiparser_js_html/jsfmt.spec.js |
I'm not sure how to get the tests to pass when running the flow or typescript parsers, then. |
You can move your test case to a separate directory and make it babel-only by specifying only E.g. you changed run_spec(__dirname, ["flow", "babel"]); |
ba209c3
to
b8261d5
Compare
Awesome, thanks for the help! I've updated to take advantage of Babel's |
@vjeux @j-f1 Is this PR good to merge? And if so, any chance it can be pushed as a new release? For context, this is the last issue that's preventing the AMP Project from adopting Prettier. See ampproject/amphtml#21212 (comment) for more info. |
This fixes casts when they are followed by a closing parenthesis, eg: ```js foo( /** @type {!Array} */(arrOrString).length ); ``` The old code would see the `CallExpresion`'s closing `)` and assume the typecast belonged to the `MemberExpression`, not the `arrOrString` `Identifier`. This would be easier to accomplish if every AST would tell us if the expression were parenthesized. If they did, we could check that the node were parenthesized and either it or an ancestor has a typecast, stopping when we find an ancestor is itself parenthesized.
b8261d5
to
b337a54
Compare
Thanks for contributing! |
@duailibe Any chance this fix can be pushed as a new release? |
For now you can install |
I'll cut a release today or tomorrow! I'm sorry for the delay |
1.17.1 is out |
Thanks! |
This fixes casts when they are followed by a closing parenthesis, eg:
The old code would see the
CallExpresion
's closing)
and assume everything belonged to the typecast up till that closing parenthesis.This would be easier to accomplish if every AST would tell us if the expression were parenthesized. If they did, we could check that the node were parenthesized and either it or an ancestor has a typecast, stopping when we find an ancestor that is itself parenthesized.
Fixes #4799.
CHANGELOG.unreleased.md
file following the template.✨Try the playground for this PR✨