diff --git a/src/language-js/print/statement.js b/src/language-js/print/statement.js index b76c212e6e75..3629c14d0d36 100644 --- a/src/language-js/print/statement.js +++ b/src/language-js/print/statement.js @@ -177,7 +177,10 @@ const isClassProperty = ({ type }) => function shouldPrintSemicolonAfterClassProperty(node, nextNode) { const name = node.key && node.key.name; if ( - (name === "static" || name === "get" || name === "set") && + (name === "static" || + name === "get" || + name === "set" || + name === "accessor") && !node.value && !node.typeAnnotation ) { 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 92a13606470c..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,80 @@ 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"] @@ -83,6 +209,27 @@ abstract class Foo { ================================================================================ `; +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"] @@ -103,6 +250,47 @@ declare class C { ================================================================================ `; +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"] @@ -143,6 +331,44 @@ abstract class Foo { ================================================================================ `; +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"] @@ -180,6 +406,35 @@ class Foo { ================================================================================ `; +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"] @@ -208,6 +463,49 @@ abstract class Foo { ================================================================================ `; +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"] @@ -250,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"] @@ -266,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"] @@ -286,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/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 });