Files
test-repo/changelog_unreleased/javascript/pr-7791.md
T
Georgii DolzhykovandJed Fox 8334610aa2 Blog post for 2.0 (#7417)
* add a script that concatenates changelog_unreleased into a blog post draft

* syntax highlighting issues

* generate post draft

* Copy edit, remove error stacks

* Update 2020-00-00-2.0.0.md

* Update changelog source files

* edit entries, regenerate the post

* add lost file in versioned_docs

Co-authored-by: Jed Fox <git@twopointzero.us>
2020-03-21 22:24:23 +02:00

1.8 KiB

[HIGHLIGHT] Ultimate fix for Closure-style type casts (#7791 by @thorn0, #7011 by @evilebottnawi)

Prettier has been trying not to corrupt these JSDoc type assertions since v1.6.0, with mixed results. As type checking based on JSDoc becomes increasingly common, we've been getting new bug reports about this syntax. The bugs were tricky because the required parentheses around the expression weren't part of the AST, so Prettier didn't have a good way to detect their presence.

Finally, we used the createParenthesizedExpressions option of the Babel parser to represent parentheses in the AST using non-standard nodes. This helped fix all the reported bugs.

Consequently, Prettier won't recognize JSDoc type casts if the flow or typescript parser is used, but this is reasonable as this syntax makes little sense in Flow and TypeScript files.

// Input
const nestedAssertions = /** @type {MyType} */
  (/** @type {unknown} */
      (x));

// Prettier stable
const nestedAssertions /** @type {MyType} */ /** @type {unknown} */ = x;

// Prettier master
const nestedAssertions = /** @type {MyType} */ (/** @type {unknown} */ (x));

Reference documentation for this syntax: Closure Compiler, TypeScript (with --checkJs).