Skip to content

Commit 1cf760a

Browse files
Sosuke Suzukifisker
andauthored
Support decorator auto accessors syntax (#13919)
* 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>
1 parent aa34209 commit 1cf760a

25 files changed

Lines changed: 744 additions & 177 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#### Support auto accessors syntax (#13919 by @sosukesuzuki)
2+
3+
Support for [Auto Accessors Syntax](https://devblogs.microsoft.com/typescript/announcing-typescript-4-9/#auto-accessors-in-classes) landed in TypeScript 4.9.
4+
5+
(Doesn't work well with `babel-ts` parser)
6+
7+
<!-- prettier-ignore -->
8+
```tsx
9+
class Foo {
10+
accessor foo: number = 3;
11+
}
12+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@babel/parser": "7.20.1",
2828
"@glimmer/syntax": "0.84.2",
2929
"@iarna/toml": "2.2.5",
30-
"@typescript-eslint/typescript-estree": "5.44.0",
30+
"@typescript-eslint/typescript-estree": "5.45.0",
3131
"acorn": "8.8.0",
3232
"acorn-jsx": "5.3.2",
3333
"angular-estree-parser": "2.5.1",

src/language-js/print/class.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ function printClassProperty(path, options, print) {
209209
if (node.static) {
210210
parts.push("static ");
211211
}
212-
if (node.type === "TSAbstractPropertyDefinition" || node.abstract) {
212+
if (
213+
node.type === "TSAbstractPropertyDefinition" ||
214+
node.type === "TSAbstractAccessorProperty" ||
215+
node.abstract
216+
) {
213217
parts.push("abstract ");
214218
}
215219
if (node.override) {
@@ -221,7 +225,11 @@ function printClassProperty(path, options, print) {
221225
if (node.variance) {
222226
parts.push(print("variance"));
223227
}
224-
if (node.type === "ClassAccessorProperty") {
228+
if (
229+
node.type === "ClassAccessorProperty" ||
230+
node.type === "AccessorProperty" ||
231+
node.type === "TSAbstractAccessorProperty"
232+
) {
225233
parts.push("accessor ");
226234
}
227235
parts.push(

src/language-js/print/statement.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,21 @@ const isClassProperty = ({ type }) =>
168168
type === "ClassProperty" ||
169169
type === "PropertyDefinition" ||
170170
type === "ClassPrivateProperty" ||
171-
type === "ClassAccessorProperty";
171+
type === "ClassAccessorProperty" ||
172+
type === "AccessorProperty" ||
173+
type === "TSAbstractPropertyDefinition" ||
174+
type === "TSAbstractAccessorProperty";
172175
/**
173176
* @returns {boolean}
174177
*/
175178
function shouldPrintSemicolonAfterClassProperty(node, nextNode) {
176-
const name = node.key && node.key.name;
179+
const { name } = node.key;
177180
if (
178-
(name === "static" || name === "get" || name === "set") &&
181+
(name === "static" ||
182+
name === "get" ||
183+
name === "set" ||
184+
// TODO: Remove this https://github.com/microsoft/TypeScript/issues/51707 is fixed
185+
name === "accessor") &&
179186
!node.value &&
180187
!node.typeAnnotation
181188
) {

src/language-js/print/typescript.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ function printTypescript(path, options, print) {
115115
case "TSAbstractMethodDefinition":
116116
case "TSDeclareMethod":
117117
return printClassMethod(path, options, print);
118+
case "TSAbstractAccessorProperty":
118119
case "TSAbstractPropertyDefinition":
119120
return printClassProperty(path, options, print);
120121
case "TSInterfaceHeritage":

src/language-js/printer-estree.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ function genericPrint(path, options, print, args) {
9898
type === "ClassPrivateMethod" ||
9999
type === "ClassProperty" ||
100100
type === "ClassAccessorProperty" ||
101+
type === "AccessorProperty" ||
102+
type === "TSAbstractAccessorProperty" ||
101103
type === "PropertyDefinition" ||
102104
type === "TSAbstractPropertyDefinition" ||
103105
type === "ClassPrivateProperty" ||
@@ -766,6 +768,7 @@ function printPathNoParens(path, options, print, args) {
766768
case "PropertyDefinition":
767769
case "ClassPrivateProperty":
768770
case "ClassAccessorProperty":
771+
case "AccessorProperty":
769772
return printClassProperty(path, options, print);
770773
case "TemplateElement":
771774
return replaceTextEndOfLine(node.value.raw);

0 commit comments

Comments
 (0)