Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(javascript): inline property decorator should stay inline #5364

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/language-js/printer-estree.js
Expand Up @@ -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 &&
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
33 changes: 33 additions & 0 deletions tests/decorators/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -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";
Expand All @@ -160,6 +174,14 @@ class OrderLine {
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;
Expand All @@ -169,6 +191,17 @@ class OrderLine {
setPrice(price) {
this.price = price;
}

@computed
@computed
@computed
@computed
@computed
@computed
@computed
get total() {
return this.price * this.amount;
}
}

`;
Expand Down
50 changes: 32 additions & 18 deletions 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;
}
}