Skip to content

Commit

Permalink
Always use hardline for decorators, unless they're parameter decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
suchipi committed Jul 20, 2018
1 parent bf473af commit c458d34
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 38 deletions.
24 changes: 23 additions & 1 deletion src/common/util.js
Expand Up @@ -786,6 +786,27 @@ function addTrailingComment(node, comment) {
addCommentHelper(node, comment);
}

function isWithinParentArrayProperty(path, propertyName) {
const node = path.getValue();
const parent = path.getParentNode();

if (parent == null) {
return false;
}

if (
!(
parent[propertyName] &&
Object.prototype.toString.call(parent[propertyName]) === "[object Array]"
)
) {
return false;
}

const key = path.getName();
return parent[propertyName][key] === node;
}

module.exports = {
punctuationRegex,
punctuationCharRange,
Expand Down Expand Up @@ -823,5 +844,6 @@ module.exports = {
matchAncestorTypes,
addLeadingComment,
addDanglingComment,
addTrailingComment
addTrailingComment,
isWithinParentArrayProperty
};
31 changes: 8 additions & 23 deletions src/language-js/printer-estree.js
Expand Up @@ -20,7 +20,8 @@ const {
getPenultimate,
startsWithNoLookaheadToken,
getIndentSize,
matchAncestorTypes
matchAncestorTypes,
isWithinParentArrayProperty
} = require("../common/util");
const {
isNextLineEmpty,
Expand Down Expand Up @@ -94,7 +95,12 @@ function genericPrint(path, options, printPath, args) {
// responsible for printing node.decorators.
!getParentExportDeclaration(path)
) {
let separator = hardline;
const separator =
node.decorators.length === 1 &&
isWithinParentArrayProperty(path, "params")
? line
: hardline;

path.each(decoratorPath => {
let decorator = decoratorPath.getValue();
if (decorator.expression) {
Expand All @@ -103,27 +109,6 @@ function genericPrint(path, options, printPath, args) {
decorator = decorator.callee;
}

if (
node.decorators.length === 1 &&
node.type !== "ClassDeclaration" &&
node.type !== "MethodDefinition" &&
node.type !== "ClassMethod" &&
(decorator.type === "Identifier" ||
decorator.type === "MemberExpression" ||
decorator.type === "OptionalMemberExpression" ||
((decorator.type === "CallExpression" ||
decorator.type === "OptionalCallExpression") &&
(decorator.arguments.length === 0 ||
(decorator.arguments.length === 1 &&
(isStringLiteral(decorator.arguments[0]) ||
decorator.arguments[0].type === "Identifier" ||
decorator.arguments[0].type === "MemberExpression" ||
decorator.arguments[0].type ===
"OptionalMemberExpression")))))
) {
separator = line;
}

decorators.push(printPath(decoratorPath), separator);
}, "decorators");
} else if (
Expand Down
18 changes: 12 additions & 6 deletions tests/decorators-ts/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -161,7 +161,8 @@ class Greeter {
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Greeter {
@format("Hello, %s") greeting: string;
@format("Hello, %s")
greeting: string;
constructor(message: string) {
this.greeting = message;
Expand Down Expand Up @@ -200,15 +201,20 @@ export class Board {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Entity()
export class Board {
@PrimaryGeneratedColumn() id: number;
@PrimaryGeneratedColumn()
id: number;
@Column() slug: string;
@Column()
slug: string;
@Column() name: string;
@Column()
name: string;
@Column() theme: string;
@Column()
theme: string;
@Column() description: string;
@Column()
description: string;
@OneToMany(type => Topic, topic => topic.board)
topics: Topic[];
Expand Down
6 changes: 4 additions & 2 deletions tests/decorators/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -108,8 +108,10 @@ import { observable } from "mobx";
@observer
class OrderLine {
@observable price: number = 0;
@observable amount: number = 1;
@observable
price: number = 0;
@observable
amount: number = 1;
constructor(price) {
this.price = price;
Expand Down
Expand Up @@ -8,6 +8,7 @@ enum E {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
declare function dec<T>(target: T): T;
@dec enum E {}
@dec
enum E {}
`;
15 changes: 10 additions & 5 deletions tests/typescript_decorators/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -49,7 +49,8 @@ export class TabCompletionController {}
selector: "angular-component"
})
class AngularComponent {
@Input() myInput: string;
@Input()
myInput: string;
}
`;
Expand Down Expand Up @@ -217,10 +218,14 @@ class Class2 {
}
class Class3 {
@d1 fieldA;
@d2(foo) fieldB;
@d3.bar fieldC;
@d4.baz() fieldD;
@d1
fieldA;
@d2(foo)
fieldB;
@d3.bar
fieldC;
@d4.baz()
fieldD;
constructor(
@d1 private x: number,
Expand Down

0 comments on commit c458d34

Please sign in to comment.