-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Support TS4.9 satisfies
operator
#13764
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
(node.type === "TSTypeAssertion" || | ||
node.type === "TSAsExpression" || | ||
// babel-parser cannot parse `satisfies` operator in left side of assignment | ||
// https://github.com/babel/babel/issues/15095 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/language-js/needs-parens.js
Outdated
case "TSAsExpression": | ||
// example: foo as unknown as Bar | ||
return node.type !== "TSAsExpression"; | ||
|
||
case "TSSatisfiesExpression": | ||
// example: foo satisfies unknown satisfies Bar | ||
return node.type !== "TSSatisfiesExpression"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe mixed should not add parens either
Prettier pr-13764
Playground link
--parser babel-ts
Input:
foo satisfies unknown satisfies Bar
foo as unknown as Bar
foo satisfies unknown as Bar
foo as unknown satisfies Bar
Output:
foo satisfies unknown satisfies Bar;
foo as unknown as Bar;
(foo satisfies unknown) as Bar;
(foo as unknown) satisfies Bar;
src/language-js/print/typescript.js
Outdated
@@ -515,6 +515,8 @@ function printTypescript(path, options, print) { | |||
return printJSDocType(path, print, /* token */ "!"); | |||
case "TSInstantiationExpression": | |||
return [print("expression"), print("typeParameters")]; | |||
case "TSSatisfiesExpression": | |||
return [print("expression"), " satisfies ", print("typeAnnotation")]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merge this into TSAsExpression
too? There is an extra check there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/language-js/needs-parens.js
Outdated
return ( | ||
node.type !== "TSAsExpression" && | ||
node.type !== "TSSatisfiesExpression" | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return ( | |
node.type !== "TSAsExpression" && | |
node.type !== "TSSatisfiesExpression" | |
); | |
return !isTSTypeExpression(node); |
src/language-js/needs-parens.js
Outdated
(node.type === "TSTypeAssertion" || | ||
node.type === "TSAsExpression" || | ||
// babel-parser cannot parse `satisfies` operator in left side of assignment | ||
// https://github.com/babel/babel/issues/15095 | ||
// TODO: Add tests after the bug is fixed | ||
node.type === "TSSatisfiesExpression") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can reuse isTSTypeExpression
here too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very good, thanks for the hard work, we can either wait for babel or update later.
FYI: @thorn0 may disagree to use |
Would it be possible to release this in 2.x? Upgrading to Prettier 3.0 may be more involved, so another 2.x release will allow folks to support TS 4.9 much faster.
|
It should not be easy, let's merge to |
This is great, thanks @sosukesuzuki 🙌 Will this be released in |
Maybe yes. |
When will it be released? |
@hiyangguo please follow #13792 and #13813 for progress. |
* Print `satisfies` operators * Add tests * Add changelog * Add `hug-args` tests * Add more tests for TSAsExpression * Add tests for argument expansion * Add tests for template literal * Add tests for ternary * Add tests for export default satisfies * Add tests for no lookahead tokens * Add more tests for needs parens 1 * Add more tests for needs-parens 2 * Add more tests for needs parens 3 * Add tests for needs parens 4 * Add comment * Add more tests for needs parens 5 * Add more tests for needs parens 6 * Fix test cases * Fix snapshots * Fix unstable tests * No parens for mixed * Merge printing logic * More reuse `isTSTypeExpression`
Description
babel-ts
parser onlyPartial of #13516
Ref #13750
Checklist
changelog_unreleased/*/XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨