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 decorator auto accessors syntax #13919
Support decorator auto accessors syntax #13919
Conversation
So, should we insert Prettier pr-13919 --parser typescript
--no-semi Input: class Foo {
accessor;
foo = 1
} Output: class Foo {
accessor
foo = 1
} Second Output: class Foo {
accessor foo = 1
} |
prettier/src/language-js/print/statement.js Line 180 in 655a161
|
Co-authored-by: fisker Cheung <lionkay@gmail.com>
Co-authored-by: fisker Cheung <lionkay@gmail.com>
e094ef3
to
c641b96
Compare
rebased |
(name === "static" || | ||
name === "get" || | ||
name === "set" || | ||
name === "accessor") && |
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.
Please add a TODO comment about microsoft/TypeScript#51707
type === "ClassAccessorProperty"; | ||
type === "ClassAccessorProperty" || | ||
type === "AccessorProperty" || | ||
type === "TSAbstractAccessorProperty"; |
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.
Can you investigate why TSAbstractPropertyDefinition
not listed here, or why TSAbstractAccessorProperty
should be added here?
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.
My guess is it's missed.
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.
If I add "TSAbstractPropertyDefinition"
to this list, the test does not fail. So it is maybe missed.
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.
What a TSAbstractPropertyDefinition
look like
class A {
abstract get;
foo() {}
}
?
We can test it.
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.
Yes, missed playground, playground 2
src/language-js/print/statement.js
Outdated
type === "ClassAccessorProperty"; | ||
type === "ClassAccessorProperty" || | ||
type === "AccessorProperty" || | ||
type === "TSAbstractAccessorProperty"; | ||
/** | ||
* @returns {boolean} | ||
*/ | ||
function shouldPrintSemicolonAfterClassProperty(node, nextNode) { | ||
const name = node.key && node.key.name; |
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.
Since we are here. Let's remove this chaining, I don't think the key
can be nullish here.
const name = node.key && node.key.name; | |
const { name } = node.key; |
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.
Good work
if ( | ||
(name === "static" || | ||
name === "get" || | ||
name === "set" || | ||
// TODO: Remove this https://github.com/microsoft/TypeScript/issues/51707 is fixed | ||
name === "accessor") && | ||
!node.value && | ||
!node.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.
printClassProperty
also prints ?
or !
here
prettier/src/language-js/print/class.js
Line 229 in aa34209
printOptionalToken(path), |
We should also test private fields, since we use .key.name
directly without check type.
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 are not checking computed
either...
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [prettier](https://prettier.io) ([source](https://github.com/prettier/prettier)) | devDependencies | patch | [`2.8.0` -> `2.8.1`](https://renovatebot.com/diffs/npm/prettier/2.8.0/2.8.1) | --- ### Release Notes <details> <summary>prettier/prettier</summary> ### [`v2.8.1`](https://github.com/prettier/prettier/blob/HEAD/CHANGELOG.md#​281) [Compare Source](prettier/prettier@2.8.0...2.8.1) [diff](prettier/prettier@2.8.0...2.8.1) ##### Fix SCSS map in arguments ([#​9184](prettier/prettier#9184) by [@​agamkrbit](https://github.com/agamkrbit)) <!-- prettier-ignore --> ```scss // Input $display-breakpoints: map-deep-merge( ( "print-only": "only print", "screen-only": "only screen", "xs-only": "only screen and (max-width: #{map-get($grid-breakpoints, "sm")-1})", ), $display-breakpoints ); // Prettier 2.8.0 $display-breakpoints: map-deep-merge( ( "print-only": "only print", "screen-only": "only screen", "xs-only": "only screen and (max-width: #{map-get($grid-breakpoints, " sm ")-1})", ), $display-breakpoints ); // Prettier 2.8.1 $display-breakpoints: map-deep-merge( ( "print-only": "only print", "screen-only": "only screen", "xs-only": "only screen and (max-width: #{map-get($grid-breakpoints, "sm")-1})", ), $display-breakpoints ); ``` ##### Support auto accessors syntax ([#​13919](prettier/prettier#13919) by [@​sosukesuzuki](https://github.com/sosukesuzuki)) Support for [Auto Accessors Syntax](https://devblogs.microsoft.com/typescript/announcing-typescript-4-9/#auto-accessors-in-classes) landed in TypeScript 4.9. (Doesn't work well with `babel-ts` parser) <!-- prettier-ignore --> ```tsx class Foo { accessor foo: number = 3; } ``` </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC41MS4wIiwidXBkYXRlZEluVmVyIjoiMzQuNzAuNCJ9--> Co-authored-by: cabr2-bot <cabr2.help@gmail.com> Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1671 Reviewed-by: Epsilon_02 <epsilon_02@noreply.codeberg.org> Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org> Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
* Install new ts-estree * Print auto accessor properties * Support edge cases * Misc tests * Add tests for auto accessor with type annotations * Add changelog * Update changelog_unreleased/typescript/13919.md Co-authored-by: fisker Cheung <lionkay@gmail.com> * Update changelog_unreleased/typescript/13919.md Co-authored-by: fisker Cheung <lionkay@gmail.com> * Print semicolon for accessor and add tests * Add TODO comment * Add to properties list * Address review Co-authored-by: fisker Cheung <lionkay@gmail.com>
Description
Partial of #13516
Note 1:
babel-ts
does not support auto accessors with type annotations yet (babel/babel#15209), so tests for it is inmisc/typescript-only
.Note 2:
Edit: reported to babel babel/babel#15238
babel-ts
parsesto the AST like
but
typescript
parses to the AST likeso, we have placed separate tests for it in
format/misc/typescript-only
andformat/misc/typescript-babel-only
, respectively.Checklist
docs/
directory).changelog_unreleased/*/XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨