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

Fix closure compiler type casts #5947

Merged
merged 7 commits into from Apr 26, 2019
Merged

Conversation

jridgewell
Copy link
Contributor

@jridgewell jridgewell commented Mar 7, 2019

This fixes casts when they are followed by a closing parenthesis, eg:

foo(/** @type {number} */(num) + 1);
foo(/** @type {!Array} */(arrOrString).length);

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.

  • I’ve added tests to confirm my change works.
  • (If the change is user-facing) I’ve added my changes to the CHANGELOG.unreleased.md file following the template.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@thorn0
Copy link
Member

thorn0 commented Mar 26, 2019

This would be easier to accomplish if every AST would tell us if the expression were parenthesized.

Babel does this: node.extra.parenthesized.

@jridgewell
Copy link
Contributor Author

jridgewell commented Mar 26, 2019

Babel does this: node.extra.parenthesized.

Yup, but prettier seems to use allow using several different AST parsers.

@thorn0
Copy link
Member

thorn0 commented Mar 26, 2019

Yup, 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.

@jridgewell
Copy link
Contributor Author

Are the tests run with babel parser? I tried to do this directly with node.extra.parenthesized originally, but the tests always failed.

@thorn0
Copy link
Member

thorn0 commented Mar 26, 2019

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

@jridgewell
Copy link
Contributor Author

I'm not sure how to get the tests to pass when running the flow or typescript parsers, then.

@thorn0
Copy link
Member

thorn0 commented Mar 26, 2019

You can move your test case to a separate directory and make it babel-only by specifying only babel in jsfmt.spec.js.

E.g. you changed tests/comments/closure-compiler-type-cast.js. This test isn't run against TypeScript. See tests/comments/jsfmt.spec.js:

run_spec(__dirname, ["flow", "babel"]);

@jridgewell
Copy link
Contributor Author

Awesome, thanks for the help! I've updated to take advantage of Babel's node.extra.parenthesized, and it's much nicer.

@rsimha
Copy link

rsimha commented Apr 26, 2019

@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.

jridgewell and others added 7 commits April 25, 2019 22:19
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.
@vjeux
Copy link
Contributor

vjeux commented Apr 26, 2019

@j-f1 could you merge it assuming it's in a good shape?
@duailibe any way you could release 1.17.1 with this fix?

Thanks!

@j-f1 j-f1 merged commit c085aeb into prettier:master Apr 26, 2019
@j-f1
Copy link
Member

j-f1 commented Apr 26, 2019

Thanks for contributing!

@jridgewell jridgewell deleted the closure-type-casts branch April 26, 2019 17:12
@rsimha
Copy link

rsimha commented Apr 30, 2019

@duailibe Any chance this fix can be pushed as a new release?

@rsimha
Copy link

rsimha commented May 8, 2019

@vjeux @duailibe Bumping this thread to see if this fix can be released anytime soon.

@j-f1
Copy link
Member

j-f1 commented May 9, 2019

For now you can install prettier/prettier to get the latest version from GitHub.

@duailibe
Copy link
Member

duailibe commented May 9, 2019

@jridgewell @rsimha

I'll cut a release today or tomorrow! I'm sorry for the delay

@duailibe
Copy link
Member

1.17.1 is out

@jridgewell
Copy link
Contributor Author

Thanks!

@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Aug 11, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Aug 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JSDoc type cast parentheses is removed
7 participants