Skip to content
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

Allow keeping parenthesis for closure compiler type casting #1445

Closed
stringham opened this issue Apr 27, 2017 · 6 comments
Closed

Allow keeping parenthesis for closure compiler type casting #1445

stringham opened this issue Apr 27, 2017 · 6 comments
Labels
area:comments Issues with how Prettier prints comments locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program

Comments

@stringham
Copy link

When writing javascript for use with the Closure compiler, parenthesis directly following a type annotation comment have meaning.

Type cast precede a parenthesized expression.

var x = /** @type {string} */ (fruit);

prettier outputs:

var x /** @type {string} */ = fruit;
@vjeux vjeux added type:bug Issues identifying ugly output, or a defect in the program area:comments Issues with how Prettier prints comments labels Apr 27, 2017
@vjeux
Copy link
Contributor

vjeux commented Apr 27, 2017

We're not doing a good job at respecting all those comments. We also have a bunch of issues with flow comments.

@bakkot
Copy link
Collaborator

bakkot commented Apr 27, 2017

@vjeux It's not just that the comment is moved; Closure is treating the parentheses as semantic, and will give a warning if they're dropped. (Ugh.)

yangsu pushed a commit to quip/prettier that referenced this issue Jul 15, 2017
yangsu pushed a commit to quip/prettier that referenced this issue Jul 15, 2017
yangsu pushed a commit to quip/prettier that referenced this issue Jul 17, 2017
yangsu pushed a commit to quip/prettier that referenced this issue Jul 21, 2017
@azz azz closed this as completed in #2484 Jul 25, 2017
azz pushed a commit that referenced this issue Jul 25, 2017
* Handle closure compiler type cast syntax correctly

Fixes #1445

* Move closure type cast detection to needParens in fast-path.js

* every => some and added additional check for leading comment
@DrRataplan
Copy link

Thanks a bunch for addressing this, I found one oversight though.
When using an inline type cast, like this:

const charCode = /** @type {string} */ (fruit).charCodeAt(0);

prettier outputs

const charCode = /** @type {string} */ (fruit.charCodeAt(0));

The workaround is easy though: just move the cast to a different statement, like this:

const strFruit = /** @type {string} */ (fruit);
const charCode = strFruit.charCodeAt(0);

or add additional parentheses:

const charCode = (/** @type {string} */ (fruit)).charCodeAt(0);

@roelkok
Copy link

roelkok commented Mar 9, 2018

This doesn't seem to work for me. It should work without setting a specific option, right?

a.) /** @type {MeshBasicMaterial} */(this.plane.material).map.needsUpdate = true;

Gets rewritten to

b.) /** @type {MeshBasicMaterial} */ this.plane.material.map.needsUpdate = true;

And if I do this

c.) (/** @type {MeshBasicMaterial} */ (this.plane.material)).map.needsUpdate = true;

It gets rewritten to a. on the first save and eventually to b. on the second save.

I'm using the 'Prettier - Code formatter' extension for VS Code.

@j-f1
Copy link
Member

j-f1 commented Mar 9, 2018

@roelkok That sounds like a bug. Can you open a new issue and fill out the template please?

@roelkok
Copy link

roelkok commented Mar 9, 2018

Opened an new issue here #4124

@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jul 5, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area:comments Issues with how Prettier prints comments locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

No branches or pull requests

6 participants