diff --git a/changelog_unreleased/typescript/13919.md b/changelog_unreleased/typescript/13919.md new file mode 100644 index 000000000000..1b93ba0fd28a --- /dev/null +++ b/changelog_unreleased/typescript/13919.md @@ -0,0 +1,12 @@ +#### Support auto accessors syntax (#13919 by @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) + + +```tsx +class Foo { + accessor foo: number = 3; +} +``` diff --git a/package.json b/package.json index 31597d358616..8b48eac35bf5 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "@babel/parser": "7.20.1", "@glimmer/syntax": "0.84.2", "@iarna/toml": "2.2.5", - "@typescript-eslint/typescript-estree": "5.44.0", + "@typescript-eslint/typescript-estree": "5.45.0", "acorn": "8.8.0", "acorn-jsx": "5.3.2", "angular-estree-parser": "2.5.1", diff --git a/src/language-js/print/class.js b/src/language-js/print/class.js index d8675c7d9b5d..93bc01423944 100644 --- a/src/language-js/print/class.js +++ b/src/language-js/print/class.js @@ -209,7 +209,11 @@ function printClassProperty(path, options, print) { if (node.static) { parts.push("static "); } - if (node.type === "TSAbstractPropertyDefinition" || node.abstract) { + if ( + node.type === "TSAbstractPropertyDefinition" || + node.type === "TSAbstractAccessorProperty" || + node.abstract + ) { parts.push("abstract "); } if (node.override) { @@ -221,7 +225,11 @@ function printClassProperty(path, options, print) { if (node.variance) { parts.push(print("variance")); } - if (node.type === "ClassAccessorProperty") { + if ( + node.type === "ClassAccessorProperty" || + node.type === "AccessorProperty" || + node.type === "TSAbstractAccessorProperty" + ) { parts.push("accessor "); } parts.push( diff --git a/src/language-js/print/statement.js b/src/language-js/print/statement.js index 69675d3c219e..08165848d8d0 100644 --- a/src/language-js/print/statement.js +++ b/src/language-js/print/statement.js @@ -168,14 +168,21 @@ const isClassProperty = ({ type }) => type === "ClassProperty" || type === "PropertyDefinition" || type === "ClassPrivateProperty" || - type === "ClassAccessorProperty"; + type === "ClassAccessorProperty" || + type === "AccessorProperty" || + type === "TSAbstractPropertyDefinition" || + type === "TSAbstractAccessorProperty"; /** * @returns {boolean} */ function shouldPrintSemicolonAfterClassProperty(node, nextNode) { - const name = node.key && node.key.name; + const { name } = node.key; if ( - (name === "static" || name === "get" || name === "set") && + (name === "static" || + name === "get" || + name === "set" || + // TODO: Remove this https://github.com/microsoft/TypeScript/issues/51707 is fixed + name === "accessor") && !node.value && !node.typeAnnotation ) { diff --git a/src/language-js/print/typescript.js b/src/language-js/print/typescript.js index a67193627da1..63e3e109b8bc 100644 --- a/src/language-js/print/typescript.js +++ b/src/language-js/print/typescript.js @@ -115,6 +115,7 @@ function printTypescript(path, options, print) { case "TSAbstractMethodDefinition": case "TSDeclareMethod": return printClassMethod(path, options, print); + case "TSAbstractAccessorProperty": case "TSAbstractPropertyDefinition": return printClassProperty(path, options, print); case "TSInterfaceHeritage": diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index c01338f8b1c7..75e8a71f89c3 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -98,6 +98,8 @@ function genericPrint(path, options, print, args) { type === "ClassPrivateMethod" || type === "ClassProperty" || type === "ClassAccessorProperty" || + type === "AccessorProperty" || + type === "TSAbstractAccessorProperty" || type === "PropertyDefinition" || type === "TSAbstractPropertyDefinition" || type === "ClassPrivateProperty" || @@ -766,6 +768,7 @@ function printPathNoParens(path, options, print, args) { case "PropertyDefinition": case "ClassPrivateProperty": case "ClassAccessorProperty": + case "AccessorProperty": return printClassProperty(path, options, print); case "TemplateElement": return replaceTextEndOfLine(node.value.raw); diff --git a/tests/format/js/decorator-auto-accessors/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/decorator-auto-accessors/__snapshots__/jsfmt.spec.js.snap index c8cbc76bb8be..a6850f56f69b 100644 --- a/tests/format/js/decorator-auto-accessors/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/decorator-auto-accessors/__snapshots__/jsfmt.spec.js.snap @@ -38,7 +38,7 @@ exports[`basic.js - {"semi":false} [espree] format 1`] = ` exports[`basic.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -57,7 +57,7 @@ class Foo { exports[`basic.js format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -111,7 +111,7 @@ exports[`computed.js - {"semi":false} [espree] format 1`] = ` exports[`computed.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -130,7 +130,7 @@ class Foo { exports[`computed.js format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -146,50 +146,9 @@ class Foo { ================================================================================ `; -exports[`computed-with-newline.js - {"semi":false} format 1`] = ` -====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] -printWidth: 80 -semi: false - | printWidth -=====================================input====================================== -class Foo { - accessor - ["bar"]; -} - -=====================================output===================================== -class Foo { - accessor; - ["bar"] -} - -================================================================================ -`; - -exports[`computed-with-newline.js format 1`] = ` -====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] -printWidth: 80 - | printWidth -=====================================input====================================== -class Foo { - accessor - ["bar"]; -} - -=====================================output===================================== -class Foo { - accessor; - ["bar"]; -} - -================================================================================ -`; - exports[`not-accessor-method.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -208,7 +167,7 @@ class Foo { exports[`not-accessor-method.js format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -226,7 +185,7 @@ class Foo { exports[`not-accessor-property.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -245,7 +204,7 @@ class Foo { exports[`not-accessor-property.js format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -299,7 +258,7 @@ exports[`private.js - {"semi":false} [espree] format 1`] = ` exports[`private.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -318,7 +277,7 @@ class Foo { exports[`private.js format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -372,7 +331,7 @@ exports[`static.js - {"semi":false} [espree] format 1`] = ` exports[`static.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -391,7 +350,7 @@ class Foo { exports[`static.js format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -445,7 +404,7 @@ exports[`static-computed.js - {"semi":false} [espree] format 1`] = ` exports[`static-computed.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -464,7 +423,7 @@ class Foo { exports[`static-computed.js format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -518,7 +477,7 @@ exports[`static-private.js - {"semi":false} [espree] format 1`] = ` exports[`static-private.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -537,7 +496,7 @@ class Foo { exports[`static-private.js format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -553,88 +512,6 @@ class Foo { ================================================================================ `; -exports[`static-with-newline.js - {"semi":false} format 1`] = ` -====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] -printWidth: 80 -semi: false - | printWidth -=====================================input====================================== -class Foo { - static accessor - bar; -} - -=====================================output===================================== -class Foo { - static accessor - bar -} - -================================================================================ -`; - -exports[`static-with-newline.js format 1`] = ` -====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] -printWidth: 80 - | printWidth -=====================================input====================================== -class Foo { - static accessor - bar; -} - -=====================================output===================================== -class Foo { - static accessor; - bar; -} - -================================================================================ -`; - -exports[`with-newline.js - {"semi":false} format 1`] = ` -====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] -printWidth: 80 -semi: false - | printWidth -=====================================input====================================== -class Foo { - accessor - bar; -} - -=====================================output===================================== -class Foo { - accessor - bar -} - -================================================================================ -`; - -exports[`with-newline.js format 1`] = ` -====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] -printWidth: 80 - | printWidth -=====================================input====================================== -class Foo { - accessor - bar; -} - -=====================================output===================================== -class Foo { - accessor; - bar; -} - -================================================================================ -`; - exports[`with-semicolon-1.js [acorn] format 1`] = ` "Unexpected token (2:12) 1 | class C { @@ -677,7 +554,7 @@ exports[`with-semicolon-1.js - {"semi":false} [espree] format 1`] = ` exports[`with-semicolon-1.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -698,7 +575,7 @@ class C { exports[`with-semicolon-1.js format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -758,7 +635,7 @@ exports[`with-semicolon-2.js - {"semi":false} [espree] format 1`] = ` exports[`with-semicolon-2.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -779,7 +656,7 @@ class C { exports[`with-semicolon-2.js format 1`] = ` ====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] +parsers: ["babel", "typescript", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/format/js/decorator-auto-accessors/computed-with-newline.js b/tests/format/js/decorator-auto-accessors/computed-with-newline.js deleted file mode 100644 index a067c2ad5c2d..000000000000 --- a/tests/format/js/decorator-auto-accessors/computed-with-newline.js +++ /dev/null @@ -1,4 +0,0 @@ -class Foo { - accessor - ["bar"]; -} diff --git a/tests/format/js/decorator-auto-accessors/jsfmt.spec.js b/tests/format/js/decorator-auto-accessors/jsfmt.spec.js index 818b9639f0bb..85acc64686de 100644 --- a/tests/format/js/decorator-auto-accessors/jsfmt.spec.js +++ b/tests/format/js/decorator-auto-accessors/jsfmt.spec.js @@ -1,4 +1,4 @@ -const parsers = ["babel", "babel-ts", "babel-flow"]; +const parsers = ["babel", "typescript", "babel-flow"]; const errors = { espree: [ "basic.js", diff --git a/tests/format/js/decorator-auto-accessors/static-with-newline.js b/tests/format/js/decorator-auto-accessors/static-with-newline.js deleted file mode 100644 index e297f3f665b3..000000000000 --- a/tests/format/js/decorator-auto-accessors/static-with-newline.js +++ /dev/null @@ -1,4 +0,0 @@ -class Foo { - static accessor - bar; -} diff --git a/tests/format/js/decorator-auto-accessors/with-newline.js b/tests/format/js/decorator-auto-accessors/with-newline.js deleted file mode 100644 index 30bf86a73fd0..000000000000 --- a/tests/format/js/decorator-auto-accessors/with-newline.js +++ /dev/null @@ -1,4 +0,0 @@ -class Foo { - accessor - bar; -} diff --git a/tests/format/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap b/tests/format/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap index 9a2dd84d4b00..67aac31adb32 100644 --- a/tests/format/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap @@ -20,6 +20,46 @@ class C { ================================================================================ `; +exports[`decorator-auto-accessors-new-line.ts format 1`] = ` +====================================options===================================== +parsers: ["babel-ts"] +printWidth: 80 + | printWidth +=====================================input====================================== +class Foo { + accessor + ["bar"]; +} + +class Foo { + static accessor + bar; +} + +class Foo { + accessor + bar; +} + +=====================================output===================================== +class Foo { + accessor; + ["bar"]; +} + +class Foo { + static accessor; + bar; +} + +class Foo { + accessor; + bar; +} + +================================================================================ +`; + exports[`invalid-modifiers.ts format 1`] = ` ====================================options===================================== parsers: ["babel-ts"] diff --git a/tests/format/misc/typescript-babel-only/decorator-auto-accessors-new-line.ts b/tests/format/misc/typescript-babel-only/decorator-auto-accessors-new-line.ts new file mode 100644 index 000000000000..084a69a60234 --- /dev/null +++ b/tests/format/misc/typescript-babel-only/decorator-auto-accessors-new-line.ts @@ -0,0 +1,14 @@ +class Foo { + accessor + ["bar"]; +} + +class Foo { + static accessor + bar; +} + +class Foo { + accessor + bar; +} diff --git a/tests/format/misc/typescript-only/__snapshots__/jsfmt.spec.js.snap b/tests/format/misc/typescript-only/__snapshots__/jsfmt.spec.js.snap index 17d4458d1963..eb9cd97c61bf 100644 --- a/tests/format/misc/typescript-only/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/misc/typescript-only/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,57 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`await-with-parens.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +function foo(promise) { await (promise); } + +function a() { + return await (1) +} + +() => { await (x) }; + +function foo() { + await + (foo); +} + +export class C { + p = await (0); +} + +await (0); + +=====================================output===================================== +function foo(promise) { + await(promise) +} + +function a() { + return await(1) +} + +;() => { + await(x) +} + +function foo() { + await(foo) +} + +export class C { + p = await(0) +} + +await 0 + +================================================================================ +`; + exports[`await-with-parens.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -51,6 +103,409 @@ await 0; ================================================================================ `; +exports[`decorator-auto-accessor-like-property-name.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class C { + accessor; + bar; +} + +=====================================output===================================== +class C { + accessor; + bar +} + +================================================================================ +`; + +exports[`decorator-auto-accessor-like-property-name.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class C { + accessor; + bar; +} + +=====================================output===================================== +class C { + accessor; + bar; +} + +================================================================================ +`; + +exports[`decorator-auto-accessors-abstract-class.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +abstract class Foo { + declare accessor prop7: number; + private accessor #p: any; + + accessor a!; + abstract accessor #s; + accessor #d?; + abstract accessor f; + readonly accessor g; +} + +=====================================output===================================== +abstract class Foo { + declare accessor prop7: number + private accessor #p: any + + accessor a! + abstract accessor #s + accessor #d? + abstract accessor f + readonly accessor g +} + +================================================================================ +`; + +exports[`decorator-auto-accessors-abstract-class.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +abstract class Foo { + declare accessor prop7: number; + private accessor #p: any; + + accessor a!; + abstract accessor #s; + accessor #d?; + abstract accessor f; + readonly accessor g; +} + +=====================================output===================================== +abstract class Foo { + declare accessor prop7: number; + private accessor #p: any; + + accessor a!; + abstract accessor #s; + accessor #d?; + abstract accessor f; + readonly accessor g; +} + +================================================================================ +`; + +exports[`decorator-auto-accessors-declara-class.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +declare class C { + accessor x = 1; + #y = 1; +} + +=====================================output===================================== +declare class C { + accessor x = 1 + #y = 1 +} + +================================================================================ +`; + +exports[`decorator-auto-accessors-declara-class.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +declare class C { + accessor x = 1; + #y = 1; +} + +=====================================output===================================== +declare class C { + accessor x = 1; + #y = 1; +} + +================================================================================ +`; + +exports[`decorator-auto-accessors-mixed-modifiers.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +abstract class Foo { + accessor prop: number = 1; + static accessor prop2: number = 1; + accessor #prop3: number = 1; + accessor [prop4]: number = 1; + private accessor prop5: number = 1; + abstract accessor prop6: number; + private accessor #p: any; + + accessor a!: any; + accessor aa!: any; + abstract accessor #s; + readonly accessor g; +} + +=====================================output===================================== +abstract class Foo { + accessor prop: number = 1 + static accessor prop2: number = 1 + accessor #prop3: number = 1 + accessor [prop4]: number = 1 + private accessor prop5: number = 1 + abstract accessor prop6: number + private accessor #p: any + + accessor a!: any + accessor aa!: any + abstract accessor #s + readonly accessor g +} + +================================================================================ +`; + +exports[`decorator-auto-accessors-mixed-modifiers.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +abstract class Foo { + accessor prop: number = 1; + static accessor prop2: number = 1; + accessor #prop3: number = 1; + accessor [prop4]: number = 1; + private accessor prop5: number = 1; + abstract accessor prop6: number; + private accessor #p: any; + + accessor a!: any; + accessor aa!: any; + abstract accessor #s; + readonly accessor g; +} + +=====================================output===================================== +abstract class Foo { + accessor prop: number = 1; + static accessor prop2: number = 1; + accessor #prop3: number = 1; + accessor [prop4]: number = 1; + private accessor prop5: number = 1; + abstract accessor prop6: number; + private accessor #p: any; + + accessor a!: any; + accessor aa!: any; + abstract accessor #s; + readonly accessor g; +} + +================================================================================ +`; + +exports[`decorator-auto-accessors-new-line.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class Foo { + accessor + ["bar"]; +} + +class Foo { + static accessor + bar; +} + +class Foo { + accessor + bar; +} + +=====================================output===================================== +class Foo { + accessor ["bar"] +} + +class Foo { + static accessor bar +} + +class Foo { + accessor bar +} + +================================================================================ +`; + +exports[`decorator-auto-accessors-new-line.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class Foo { + accessor + ["bar"]; +} + +class Foo { + static accessor + bar; +} + +class Foo { + accessor + bar; +} + +=====================================output===================================== +class Foo { + accessor ["bar"]; +} + +class Foo { + static accessor bar; +} + +class Foo { + accessor bar; +} + +================================================================================ +`; + +exports[`decorator-auto-accessors-type-annotations.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +abstract class Foo { + accessor prop: number = 1; + static accessor prop2: number = 1; + accessor #prop3: number = 1; + accessor [prop4]: number = 1; + private accessor prop5: number = 1; + abstract accessor prop6: number; +} + +=====================================output===================================== +abstract class Foo { + accessor prop: number = 1 + static accessor prop2: number = 1 + accessor #prop3: number = 1 + accessor [prop4]: number = 1 + private accessor prop5: number = 1 + abstract accessor prop6: number +} + +================================================================================ +`; + +exports[`decorator-auto-accessors-type-annotations.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +abstract class Foo { + accessor prop: number = 1; + static accessor prop2: number = 1; + accessor #prop3: number = 1; + accessor [prop4]: number = 1; + private accessor prop5: number = 1; + abstract accessor prop6: number; +} + +=====================================output===================================== +abstract class Foo { + accessor prop: number = 1; + static accessor prop2: number = 1; + accessor #prop3: number = 1; + accessor [prop4]: number = 1; + private accessor prop5: number = 1; + abstract accessor prop6: number; +} + +================================================================================ +`; + +exports[`invalid-modifiers.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +interface Foo { + private a(); + public b(); + protected c(); + static d(); + declare e(); + abstract f(); + readonly g(); +} + +class Bar { + declare e() {}; + abstract f() {}; + readonly g() {}; +} + +=====================================output===================================== +interface Foo { + private a() + public b() + protected c() + static d() + e() + f() + readonly g() +} + +class Bar { + e() {} + abstract f() {} + g() {} +} + +================================================================================ +`; + exports[`invalid-modifiers.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -93,6 +548,23 @@ class Bar { ================================================================================ `; +exports[`parenthesized-decorators-call-expression.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +@(test().x("global").y()) +class X {} + +=====================================output===================================== +@test().x("global").y() +class X {} + +================================================================================ +`; + exports[`parenthesized-decorators-call-expression.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -109,6 +581,27 @@ class X {} ================================================================================ `; +exports[`parenthesized-decorators-tagged-template.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class Test { + @foo\`bar\` + text: string = "text" +} + +=====================================output===================================== +class Test { + @foo\`bar\` + text: string = "text" +} + +================================================================================ +`; + exports[`parenthesized-decorators-tagged-template.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -129,6 +622,27 @@ class Test { ================================================================================ `; +exports[`prettier-ignore-parenthesized-type.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +type Foo = + // prettier-ignore + ( + aa + ); + +=====================================output===================================== +type Foo = + // prettier-ignore + aa + +================================================================================ +`; + exports[`prettier-ignore-parenthesized-type.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/misc/typescript-only/decorator-auto-accessor-like-property-name.ts b/tests/format/misc/typescript-only/decorator-auto-accessor-like-property-name.ts new file mode 100644 index 000000000000..e69e9775af7f --- /dev/null +++ b/tests/format/misc/typescript-only/decorator-auto-accessor-like-property-name.ts @@ -0,0 +1,4 @@ +class C { + accessor; + bar; +} diff --git a/tests/format/misc/typescript-only/decorator-auto-accessors-abstract-class.ts b/tests/format/misc/typescript-only/decorator-auto-accessors-abstract-class.ts new file mode 100644 index 000000000000..7009742dec9a --- /dev/null +++ b/tests/format/misc/typescript-only/decorator-auto-accessors-abstract-class.ts @@ -0,0 +1,10 @@ +abstract class Foo { + declare accessor prop7: number; + private accessor #p: any; + + accessor a!; + abstract accessor #s; + accessor #d?; + abstract accessor f; + readonly accessor g; +} diff --git a/tests/format/misc/typescript-only/decorator-auto-accessors-declara-class.ts b/tests/format/misc/typescript-only/decorator-auto-accessors-declara-class.ts new file mode 100644 index 000000000000..dfd1036fd311 --- /dev/null +++ b/tests/format/misc/typescript-only/decorator-auto-accessors-declara-class.ts @@ -0,0 +1,4 @@ +declare class C { + accessor x = 1; + #y = 1; +} diff --git a/tests/format/misc/typescript-only/decorator-auto-accessors-mixed-modifiers.ts b/tests/format/misc/typescript-only/decorator-auto-accessors-mixed-modifiers.ts new file mode 100644 index 000000000000..d10849c55031 --- /dev/null +++ b/tests/format/misc/typescript-only/decorator-auto-accessors-mixed-modifiers.ts @@ -0,0 +1,14 @@ +abstract class Foo { + accessor prop: number = 1; + static accessor prop2: number = 1; + accessor #prop3: number = 1; + accessor [prop4]: number = 1; + private accessor prop5: number = 1; + abstract accessor prop6: number; + private accessor #p: any; + + accessor a!: any; + accessor aa!: any; + abstract accessor #s; + readonly accessor g; +} diff --git a/tests/format/misc/typescript-only/decorator-auto-accessors-new-line.ts b/tests/format/misc/typescript-only/decorator-auto-accessors-new-line.ts new file mode 100644 index 000000000000..084a69a60234 --- /dev/null +++ b/tests/format/misc/typescript-only/decorator-auto-accessors-new-line.ts @@ -0,0 +1,14 @@ +class Foo { + accessor + ["bar"]; +} + +class Foo { + static accessor + bar; +} + +class Foo { + accessor + bar; +} diff --git a/tests/format/misc/typescript-only/decorator-auto-accessors-type-annotations.ts b/tests/format/misc/typescript-only/decorator-auto-accessors-type-annotations.ts new file mode 100644 index 000000000000..2099b517a0f2 --- /dev/null +++ b/tests/format/misc/typescript-only/decorator-auto-accessors-type-annotations.ts @@ -0,0 +1,8 @@ +abstract class Foo { + accessor prop: number = 1; + static accessor prop2: number = 1; + accessor #prop3: number = 1; + accessor [prop4]: number = 1; + private accessor prop5: number = 1; + abstract accessor prop6: number; +} diff --git a/tests/format/misc/typescript-only/jsfmt.spec.js b/tests/format/misc/typescript-only/jsfmt.spec.js index 2ea3bb6eb2e4..ba52aeb62efa 100644 --- a/tests/format/misc/typescript-only/jsfmt.spec.js +++ b/tests/format/misc/typescript-only/jsfmt.spec.js @@ -1 +1,2 @@ run_spec(__dirname, ["typescript"]); +run_spec(__dirname, ["typescript"], { semi: false }); diff --git a/tests/format/typescript/abstract-property/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/abstract-property/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..57b9dc9fbe80 --- /dev/null +++ b/tests/format/typescript/abstract-property/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,42 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`semicolon.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +abstract class C { + abstract get; + x() {} +} + +=====================================output===================================== +abstract class C { + abstract get; + x() {} +} + +================================================================================ +`; + +exports[`semicolon.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +abstract class C { + abstract get; + x() {} +} + +=====================================output===================================== +abstract class C { + abstract get; + x() {} +} + +================================================================================ +`; diff --git a/tests/format/typescript/abstract-property/jsfmt.spec.js b/tests/format/typescript/abstract-property/jsfmt.spec.js new file mode 100644 index 000000000000..ba52aeb62efa --- /dev/null +++ b/tests/format/typescript/abstract-property/jsfmt.spec.js @@ -0,0 +1,2 @@ +run_spec(__dirname, ["typescript"]); +run_spec(__dirname, ["typescript"], { semi: false }); diff --git a/tests/format/typescript/abstract-property/semicolon.ts b/tests/format/typescript/abstract-property/semicolon.ts new file mode 100644 index 000000000000..e53e3306b28b --- /dev/null +++ b/tests/format/typescript/abstract-property/semicolon.ts @@ -0,0 +1,4 @@ +abstract class C { + abstract get; + x() {} +} diff --git a/yarn.lock b/yarn.lock index 743ece0c6532..e5685b8683d8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1729,10 +1729,10 @@ resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.36.2.tgz#a5066e500ebcfcee36694186ccc57b955c05faf9" integrity sha512-9OJSvvwuF1L5eS2EQgFUbECb99F0mwq501w0H0EkYULkhFa19Qq7WFbycdw1PexAc929asupbZcgjVIe6OK/XQ== -"@typescript-eslint/types@5.44.0": - version "5.44.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.44.0.tgz#f3f0b89aaff78f097a2927fe5688c07e786a0241" - integrity sha512-Tp+zDnHmGk4qKR1l+Y1rBvpjpm5tGXX339eAlRBDg+kgZkz9Bw+pqi4dyseOZMsGuSH69fYfPJCBKBrbPCxYFQ== +"@typescript-eslint/types@5.45.0": + version "5.45.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.45.0.tgz#794760b9037ee4154c09549ef5a96599621109c5" + integrity sha512-QQij+u/vgskA66azc9dCmx+rev79PzX8uDHpsqSjEFtfF2gBUTRCpvYMh2gw2ghkJabNkPlSUCimsyBEQZd1DA== "@typescript-eslint/typescript-estree@5.36.2": version "5.36.2" @@ -1747,13 +1747,13 @@ semver "^7.3.7" tsutils "^3.21.0" -"@typescript-eslint/typescript-estree@5.44.0": - version "5.44.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.44.0.tgz#0461b386203e8d383bb1268b1ed1da9bc905b045" - integrity sha512-M6Jr+RM7M5zeRj2maSfsZK2660HKAJawv4Ud0xT+yauyvgrsHu276VtXlKDFnEmhG+nVEd0fYZNXGoAgxwDWJw== +"@typescript-eslint/typescript-estree@5.45.0": + version "5.45.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.45.0.tgz#f70a0d646d7f38c0dfd6936a5e171a77f1e5291d" + integrity sha512-maRhLGSzqUpFcZgXxg1qc/+H0bT36lHK4APhp0AEUVrpSwXiRAomm/JGjSG+kNUio5kAa3uekCYu/47cnGn5EQ== dependencies: - "@typescript-eslint/types" "5.44.0" - "@typescript-eslint/visitor-keys" "5.44.0" + "@typescript-eslint/types" "5.45.0" + "@typescript-eslint/visitor-keys" "5.45.0" debug "^4.3.4" globby "^11.1.0" is-glob "^4.0.3" @@ -1780,12 +1780,12 @@ "@typescript-eslint/types" "5.36.2" eslint-visitor-keys "^3.3.0" -"@typescript-eslint/visitor-keys@5.44.0": - version "5.44.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.44.0.tgz#10740dc28902bb903d12ee3a005cc3a70207d433" - integrity sha512-a48tLG8/4m62gPFbJ27FxwCOqPKxsb8KC3HkmYoq2As/4YyjQl1jDbRr1s63+g4FS/iIehjmN3L5UjmKva1HzQ== +"@typescript-eslint/visitor-keys@5.45.0": + version "5.45.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.45.0.tgz#e0d160e9e7fdb7f8da697a5b78e7a14a22a70528" + integrity sha512-jc6Eccbn2RtQPr1s7th6jJWQHBHI6GBVQkCHoJFQ5UreaKm59Vxw+ynQUPPY2u2Amquc+7tmEoC2G52ApsGNNg== dependencies: - "@typescript-eslint/types" "5.44.0" + "@typescript-eslint/types" "5.45.0" eslint-visitor-keys "^3.3.0" abab@^2.0.3, abab@^2.0.5: