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

Support TS4.9 satisfies operator #13764

Merged
merged 23 commits into from
Nov 1, 2022
Merged

Conversation

sosukesuzuki
Copy link
Member

@sosukesuzuki sosukesuzuki commented Oct 30, 2022

Description

babel-ts parser only

Partial of #13516
Ref #13750

Checklist

  • I’ve added tests to confirm my change works.
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@sosukesuzuki sosukesuzuki marked this pull request as ready for review October 30, 2022 03:10
@fisker

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sosukesuzuki sosukesuzuki marked this pull request as draft October 30, 2022 05:36
Comment on lines 234 to 240
case "TSAsExpression":
// example: foo as unknown as Bar
return node.type !== "TSAsExpression";

case "TSSatisfiesExpression":
// example: foo satisfies unknown satisfies Bar
return node.type !== "TSSatisfiesExpression";
Copy link
Member

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;

@@ -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")];
Copy link
Member

@fisker fisker Oct 30, 2022

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 241 to 244
return (
node.type !== "TSAsExpression" &&
node.type !== "TSSatisfiesExpression"
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (
node.type !== "TSAsExpression" &&
node.type !== "TSSatisfiesExpression"
);
return !isTSTypeExpression(node);

Comment on lines 278 to 283
(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")
Copy link
Member

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.

Copy link
Member

@fisker fisker left a 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.

@sosukesuzuki sosukesuzuki marked this pull request as ready for review October 30, 2022 15:26
@fisker
Copy link
Member

fisker commented Oct 30, 2022

FYI: @thorn0 may disagree to use isTSTypeExpression babel/babel#14211 (comment)

@kachkaev
Copy link
Member

kachkaev commented Oct 31, 2022

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.

typescript@4.9.3 is scheduled for November 15 (microsoft/TypeScript#50457).

@fisker
Copy link
Member

fisker commented Oct 31, 2022

It should not be easy, let's merge to next first and backport to main later.

@fisker fisker mentioned this pull request Oct 31, 2022
3 tasks
@sosukesuzuki sosukesuzuki merged commit 931661a into prettier:next Nov 1, 2022
@sosukesuzuki sosukesuzuki deleted the satisfies branch November 1, 2022 10:09
@sosukesuzuki sosukesuzuki mentioned this pull request Nov 1, 2022
2 tasks
@karlhorky
Copy link
Contributor

This is great, thanks @sosukesuzuki 🙌 Will this be released in prettier@2.8.0?

@sosukesuzuki
Copy link
Member Author

Maybe yes.

@sosukesuzuki sosukesuzuki mentioned this pull request Nov 4, 2022
11 tasks
@kachkaev kachkaev added this to the 2.8 milestone Nov 16, 2022
@hiyangguo
Copy link

This is great, thanks @sosukesuzuki 🙌 Will this be released in prettier@2.8.0?

When will it be released?

@kachkaev
Copy link
Member

kachkaev commented Nov 22, 2022

@hiyangguo please follow #13792 and #13813 for progress.

medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Jan 4, 2024
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Feb 6, 2024
* 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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants