Put decorators on the same line (#459)

Mobx is the only popular JavaScript library that I know about which uses decorators. They put things on the same line so we should follow their conventions.

The logic implemented here is the following: if there is one decorator, it's on the same line. If there is more than one, they are each on their own line.

Fixes #325
master
Christopher Chedeau 2017-01-25 13:30:09 -08:00 committed by James Long
parent 1334dbc781
commit 888c7a52dd
6 changed files with 94 additions and 1 deletions

View File

@ -51,9 +51,12 @@ function genericPrint(path, options, printPath) {
// responsible for printing node.decorators.
!util.getParentExportDeclaration(path)
) {
const separator = node.decorators.length === 1 &&
node.decorators[0].expression.type === "Identifier"
? " " : hardline;
path.each(
function(decoratorPath) {
parts.push(printPath(decoratorPath), line);
parts.push(printPath(decoratorPath), separator);
},
"decorators"
);

View File

@ -0,0 +1,64 @@
exports[`test mobx.js 1`] = `
"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;
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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;
}
}
"
`;
exports[`test multiple.js 1`] = `
"const dog = {
@readonly
@nonenumerable
@doubledValue
legs: 4
};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const dog = {
@readonly
@nonenumerable
@doubledValue
legs: 4
};
"
`;
exports[`test redux.js 1`] = `
"@connect(mapStateToProps, mapDispatchToProps)
export class MyApp extends React.Component {}
@connect(state => ({ todos: state.todos }))
export class Home extends React.Component {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@connect(mapStateToProps, mapDispatchToProps)
export class MyApp extends React.Component {}
@connect(state => ({ todos: state.todos }))
export class Home extends React.Component {}
"
`;

View File

@ -0,0 +1 @@
run_spec(__dirname, {parser: 'babylon'});

14
tests/decorators/mobx.js Normal file
View File

@ -0,0 +1,14 @@
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;
}
}

View File

@ -0,0 +1,6 @@
const dog = {
@readonly
@nonenumerable
@doubledValue
legs: 4
};

View File

@ -0,0 +1,5 @@
@connect(mapStateToProps, mapDispatchToProps)
export class MyApp extends React.Component {}
@connect(state => ({ todos: state.todos }))
export class Home extends React.Component {}