diff --git a/src/printer.js b/src/printer.js index 63ef2b007..e0a84f9d2 100644 --- a/src/printer.js +++ b/src/printer.js @@ -92,10 +92,18 @@ function genericPrint(path, options, printPath, args) { prefix = ""; } + // #1817 if ( node.decorators.length === 1 && + node.type !== "ClassDeclaration" && + node.type !== "MethodDefinition" && (decorator.type === "Identifier" || - decorator.type === "MemberExpression") + decorator.type === "MemberExpression" || + (decorator.type === "CallExpression" && + decorator.arguments.length === 1 && + (isStringLiteral(decorator.arguments[0]) || + decorator.arguments[0].type === "Identifier" || + decorator.arguments[0].type === "MemberExpression"))) ) { separator = " "; } diff --git a/tests/decorators/__snapshots__/jsfmt.spec.js.snap b/tests/decorators/__snapshots__/jsfmt.spec.js.snap index f08c5a116..3d7f419fe 100644 --- a/tests/decorators/__snapshots__/jsfmt.spec.js.snap +++ b/tests/decorators/__snapshots__/jsfmt.spec.js.snap @@ -45,7 +45,8 @@ import {observable} from "mobx"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ import { observable } from "mobx"; -@observer class OrderLine { +@observer +class OrderLine { @observable price: number = 0; @observable amount: number = 1; diff --git a/tests/typescript_decorators/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_decorators/__snapshots__/jsfmt.spec.js.snap index f039eca5a..3d7fd3ef3 100644 --- a/tests/typescript_decorators/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_decorators/__snapshots__/jsfmt.spec.js.snap @@ -20,3 +20,101 @@ export class TestTextFileService { export class TabCompletionController {} `; + +exports[`inline-decorators.ts 1`] = ` + +@d1 +@d2(foo) +@d3.bar +@d4.baz() +class Class1 {} + +class Class2 { + @d1 + @d2(foo) + @d3.bar + @d4.baz() + method1() {} + + @d1 + method2() {} + + @d2(foo) + method3() {} + + @d3.bar + method4() {} +} + +class Class3 { + @d1 fieldA; + @d2(foo) fieldB; + @d3.bar fieldC; + @d4.baz() fieldD; + + constructor ( + @d1 private x: number, + @d2(foo) private y: number, + @d3('foo') private z: number, + @d4({ + x: string + }) private a: string, + ) {} +} + +@decorated class Foo {} + +class Bar { + @decorated method() {} +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +@d1 +@d2(foo) +@d3.bar +@d4.baz() +class Class1 {} + +class Class2 { + @d1 + @d2(foo) + @d3.bar + @d4.baz() + method1() {} + + @d1 + method2() {} + + @d2(foo) + method3() {} + + @d3.bar + method4() {} +} + +class Class3 { + @d1 fieldA; + @d2(foo) fieldB; + @d3.bar fieldC; + @d4.baz() + fieldD; + + constructor( + @d1 private x: number, + @d2(foo) private y: number, + @d3("foo") private z: number, + @d4({ + x: string + }) + private a: string + ) {} +} + +@decorated +class Foo {} + +class Bar { + @decorated + method() {} +} + +`; diff --git a/tests/typescript_decorators/inline-decorators.ts b/tests/typescript_decorators/inline-decorators.ts new file mode 100644 index 000000000..ab3cf408a --- /dev/null +++ b/tests/typescript_decorators/inline-decorators.ts @@ -0,0 +1,45 @@ + +@d1 +@d2(foo) +@d3.bar +@d4.baz() +class Class1 {} + +class Class2 { + @d1 + @d2(foo) + @d3.bar + @d4.baz() + method1() {} + + @d1 + method2() {} + + @d2(foo) + method3() {} + + @d3.bar + method4() {} +} + +class Class3 { + @d1 fieldA; + @d2(foo) fieldB; + @d3.bar fieldC; + @d4.baz() fieldD; + + constructor ( + @d1 private x: number, + @d2(foo) private y: number, + @d3('foo') private z: number, + @d4({ + x: string + }) private a: string, + ) {} +} + +@decorated class Foo {} + +class Bar { + @decorated method() {} +}