From eba42abb0debfcccfbb3b7eba94b8cb69085159f Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 7 Nov 2018 16:30:33 +0800 Subject: [PATCH 1/2] test: add tests --- .../__snapshots__/jsfmt.spec.js.snap | 35 +++++++++++++ tests/decorators/mobx.js | 50 ++++++++++++------- 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/tests/decorators/__snapshots__/jsfmt.spec.js.snap b/tests/decorators/__snapshots__/jsfmt.spec.js.snap index c4cf1fbc861d..ca18b4563d15 100644 --- a/tests/decorators/__snapshots__/jsfmt.spec.js.snap +++ b/tests/decorators/__snapshots__/jsfmt.spec.js.snap @@ -147,6 +147,20 @@ import {observable} from "mobx"; @action.bound setPrice(price) { this.price = price; } + + @computed + get total() { + return this.price * this.amount; + } + + @action.bound + setPrice(price) { + this.price = price; + } + + @computed @computed @computed @computed @computed @computed @computed get total() { + return this.price * this.amount; + } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ import { observable } from "mobx"; @@ -169,6 +183,27 @@ class OrderLine { setPrice(price) { this.price = price; } + + @computed + get total() { + return this.price * this.amount; + } + + @action.bound + setPrice(price) { + this.price = price; + } + + @computed + @computed + @computed + @computed + @computed + @computed + @computed + get total() { + return this.price * this.amount; + } } `; diff --git a/tests/decorators/mobx.js b/tests/decorators/mobx.js index de1357a5904c..2ab8ebd45458 100644 --- a/tests/decorators/mobx.js +++ b/tests/decorators/mobx.js @@ -1,18 +1,32 @@ -import {observable} from "mobx"; - -@observer class OrderLine { - @observable price:number = 0; - @observable amount:number = 1; - - constructor(price) { - this.price = price; - } - - @computed get total() { - return this.price * this.amount; - } - - @action.bound setPrice(price) { - this.price = price; - } -} +import {observable} from "mobx"; + +@observer class OrderLine { + @observable price:number = 0; + @observable amount:number = 1; + + constructor(price) { + this.price = price; + } + + @computed get total() { + return this.price * this.amount; + } + + @action.bound setPrice(price) { + this.price = price; + } + + @computed + get total() { + return this.price * this.amount; + } + + @action.bound + setPrice(price) { + this.price = price; + } + + @computed @computed @computed @computed @computed @computed @computed get total() { + return this.price * this.amount; + } +} From fd00420df84bc3faf96381f9cb57239e4a076538 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 7 Nov 2018 16:33:58 +0800 Subject: [PATCH 2/2] fix(javascript): inline property decorator should stay inline --- src/language-js/printer-estree.js | 18 +++++++++++------- .../__snapshots__/jsfmt.spec.js.snap | 6 ++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index d2836a670436..c9989d1c557d 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -98,6 +98,7 @@ function genericPrint(path, options, printPath, args) { const parentExportDecl = getParentExportDeclaration(path); const decorators = []; + let shouldBreakDecorators = false; if ( node.decorators && node.decorators.length > 0 && @@ -110,19 +111,20 @@ function genericPrint(path, options, printPath, args) { options.locStart(node.decorators[0]) ) ) { - const shouldBreak = + if ( + node.type === "ClassExpression" || node.type === "ClassDeclaration" || hasNewlineInRange( options.originalText, options.locStart(node.decorators[0]), options.locEnd(getLast(node.decorators)) ) || - hasNewline( - options.originalText, - options.locEnd(getLast(node.decorators)) - ); + hasNewline(options.originalText, options.locEnd(getLast(node.decorators))) + ) { + shouldBreakDecorators = true; + } - const separator = shouldBreak ? hardline : line; + const separator = shouldBreakDecorators ? hardline : line; path.each(decoratorPath => { let decorator = decoratorPath.getValue(); @@ -185,7 +187,9 @@ function genericPrint(path, options, printPath, args) { } if (decorators.length > 0) { - return group(concat(decorators.concat(parts))); + return !shouldBreakDecorators && willBreak(concat(parts)) + ? group(concat([group(concat(decorators)), concat(parts)])) + : group(concat(decorators.concat(parts))); } return concat(parts); } diff --git a/tests/decorators/__snapshots__/jsfmt.spec.js.snap b/tests/decorators/__snapshots__/jsfmt.spec.js.snap index ca18b4563d15..aebdd51b21d8 100644 --- a/tests/decorators/__snapshots__/jsfmt.spec.js.snap +++ b/tests/decorators/__snapshots__/jsfmt.spec.js.snap @@ -174,13 +174,11 @@ class OrderLine { this.price = price; } - @computed - get total() { + @computed get total() { return this.price * this.amount; } - @action.bound - setPrice(price) { + @action.bound setPrice(price) { this.price = price; }