Skip to content

Commit

Permalink
fix(javascript): remove that special case for class
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Nov 9, 2018
1 parent 6c0d6d6 commit 32a981c
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 38 deletions.
18 changes: 0 additions & 18 deletions docs/rationale.md
Expand Up @@ -99,24 +99,6 @@ class HeroButtonComponent {
}
```

There's one exception: classes. We don't think it ever makes sense to inline the decorators for them, so they are always moved to their own line.

<!-- prettier-ignore -->
```js
// Before running Prettier:
@observer class OrderLine {
@observable price: number = 0;
}
```

```js
// After running Prettier:
@observer
class OrderLine {
@observable price: number = 0;
}
```

Note: Prettier 1.14.x and older tried to automatically move your decorators, so if you've run an older Prettier version on your code you might need to manually join up some decorators here and there to avoid inconsistencies:

```js
Expand Down
11 changes: 4 additions & 7 deletions src/language-js/printer-estree.js
Expand Up @@ -117,12 +117,9 @@ function genericPrint(path, options, printPath, args) {
options.locStart(node.decorators[0])
)
) {
const shouldBreak =
node.type === "ClassExpression" ||
node.type === "ClassDeclaration" ||
hasNewlineBetweenOrAfterDecorators(node, options);

const separator = shouldBreak ? hardline : line;
const separator = hasNewlineBetweenOrAfterDecorators(node, options)
? hardline
: line;

path.each(decoratorPath => {
let decorator = decoratorPath.getValue();
Expand All @@ -136,7 +133,7 @@ function genericPrint(path, options, printPath, args) {
}, "decorators");

if (parentExportDecl) {
decorators.unshift(hardline);
decorators.unshift(softline);
}
} else if (
isExportDeclaration(node) &&
Expand Down
3 changes: 1 addition & 2 deletions tests/decorators/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -17,8 +17,7 @@ const foo =
//
};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@deco
class Foo {}
@deco class Foo {}
@deco
export class Bar {}
Expand Down
12 changes: 3 additions & 9 deletions tests/decorators_export/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -14,13 +14,9 @@ class Hello {}
export default @decorator class {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export
@decorator
class Foo {}
export @decorator class Foo {}
export
@decorator
class Bar {}
export @decorator class Bar {}
export
@decorator
Expand All @@ -36,9 +32,7 @@ export
@decorator
class Hello {}
export default
@decorator
class {}
export default @decorator class {}
`;

Expand Down
3 changes: 1 addition & 2 deletions tests/typescript_decorators/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -233,8 +233,7 @@ class Class3 {
) {}
}
@decorated
class Foo {}
@decorated class Foo {}
class Bar {
@decorated method() {}
Expand Down

0 comments on commit 32a981c

Please sign in to comment.