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 (part 2) #5423

Merged
merged 6 commits into from Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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: 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.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


<!-- 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
58 changes: 39 additions & 19 deletions src/language-js/printer-estree.js
Expand Up @@ -98,8 +98,14 @@ function genericPrint(path, options, printPath, args) {

const parentExportDecl = getParentExportDeclaration(path);
const decorators = [];
let shouldBreakDecorators = false;
if (
node.type === "ClassMethod" ||
node.type === "ClassProperty" ||
node.type === "TSAbstractClassProperty" ||
node.type === "ClassPrivateProperty"
) {
// their decorators are handled themselves
} else if (
node.decorators &&
node.decorators.length > 0 &&
// If the parent node is an export declaration and the decorator
Expand All @@ -111,20 +117,9 @@ function genericPrint(path, options, printPath, args) {
options.locStart(node.decorators[0])
)
) {
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)))
) {
shouldBreakDecorators = true;
}

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

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

if (parentExportDecl) {
decorators.unshift(hardline);
decorators.unshift(softline);
}
} else if (
isExportDeclaration(node) &&
Expand Down Expand Up @@ -187,13 +182,32 @@ function genericPrint(path, options, printPath, args) {
}

if (decorators.length > 0) {
return !shouldBreakDecorators && willBreak(concat(parts))
? group(concat([group(concat(decorators)), concat(parts)]))
: group(concat(decorators.concat(parts)));
return group(concat(decorators.concat(parts)));
}
return concat(parts);
}

function hasNewlineBetweenOrAfterDecorators(node, options) {
return (
hasNewlineInRange(
options.originalText,
options.locStart(node.decorators[0]),
options.locEnd(getLast(node.decorators))
) ||
hasNewline(options.originalText, options.locEnd(getLast(node.decorators)))
);
}

function printDecorators(path, options, print) {
const node = path.getValue();
return group(
concat([
join(line, path.map(print, "decorators")),
hasNewlineBetweenOrAfterDecorators(node, options) ? hardline : line
])
);
}

function hasPrettierIgnore(path) {
return hasIgnoreComment(path) || hasJsxIgnoreComment(path);
}
Expand Down Expand Up @@ -1421,6 +1435,9 @@ function printPathNoParens(path, options, print, args) {

return concat(parts); // Babel 6
case "ClassMethod":
if (n.decorators && n.decorators.length !== 0) {
parts.push(printDecorators(path, options, print));
}
if (n.static) {
parts.push("static ");
}
Expand Down Expand Up @@ -2236,6 +2253,9 @@ function printPathNoParens(path, options, print, args) {
case "ClassProperty":
case "TSAbstractClassProperty":
case "ClassPrivateProperty": {
if (n.decorators && n.decorators.length !== 0) {
parts.push(printDecorators(path, options, print));
}
if (n.accessibility) {
parts.push(n.accessibility + " ");
}
Expand Down
13 changes: 11 additions & 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 Expand Up @@ -161,6 +160,10 @@ import {observable} from "mobx";
@computed @computed @computed @computed @computed @computed @computed get total() {
return this.price * this.amount;
}

@action handleDecrease = (event: React.ChangeEvent<HTMLInputElement>) => this.count--;

@action handleSomething = (event: React.ChangeEvent<HTMLInputElement>) => doSomething();
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import { observable } from "mobx";
Expand Down Expand Up @@ -202,6 +205,12 @@ class OrderLine {
get total() {
return this.price * this.amount;
}

@action handleDecrease = (event: React.ChangeEvent<HTMLInputElement>) =>
this.count--;

@action handleSomething = (event: React.ChangeEvent<HTMLInputElement>) =>
doSomething();
}

`;
Expand Down
4 changes: 4 additions & 0 deletions tests/decorators/mobx.js
Expand Up @@ -29,4 +29,8 @@ import {observable} from "mobx";
@computed @computed @computed @computed @computed @computed @computed get total() {
return this.price * this.amount;
}

@action handleDecrease = (event: React.ChangeEvent<HTMLInputElement>) => this.count--;

@action handleSomething = (event: React.ChangeEvent<HTMLInputElement>) => doSomething();
}
42 changes: 39 additions & 3 deletions tests/decorators_export/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -3,28 +3,64 @@
exports[`after_export.js - babylon-verify 1`] = `
export @decorator class Foo {}

export
@decorator class Bar {}

export @decorator
class Baz {}

export @decorator @decorator @decorator @decorator @decorator @decorator @decorator
class Hello {}

export default @decorator class {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export @decorator class Foo {}

export @decorator class Bar {}

export
@decorator
class Foo {}
class Baz {}

export default
export
@decorator
@decorator
@decorator
@decorator
@decorator
@decorator
@decorator
class {}
class Hello {}

export default @decorator class {}

`;

exports[`before_export.js - babylon-verify 1`] = `
@decorator
export class Foo {}

@decorator export class Bar {}

@decorator @decorator @decorator @decorator @decorator @decorator export class Baz {}

@decorator
export default class {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@decorator
export class Foo {}

@decorator
export class Bar {}

@decorator
@decorator
@decorator
@decorator
@decorator
@decorator
export class Baz {}

@decorator
export default class {}

Expand Down
15 changes: 12 additions & 3 deletions tests/decorators_export/after_export.js
@@ -1,3 +1,12 @@
export @decorator class Foo {}

export default @decorator class {}
export @decorator class Foo {}

export
@decorator class Bar {}

export @decorator
class Baz {}

export @decorator @decorator @decorator @decorator @decorator @decorator @decorator
class Hello {}

export default @decorator class {}
4 changes: 4 additions & 0 deletions tests/decorators_export/before_export.js
@@ -1,5 +1,9 @@
@decorator
export class Foo {}

@decorator export class Bar {}

@decorator @decorator @decorator @decorator @decorator @decorator export class Baz {}

@decorator
export default class {}
8 changes: 4 additions & 4 deletions tests/typescript_decorators/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -233,16 +233,16 @@ class Class3 {
) {}
}

@decorated
class Foo {}
@decorated class Foo {}

class Bar {
@decorated method() {}
}

class MyContainerComponent {
@ContentChildren(MyComponent)
components: QueryListSomeBigName<MyComponentThat>;
@ContentChildren(MyComponent) components: QueryListSomeBigName<
MyComponentThat
>;
}

`;
Expand Down