Fix line break in test declarations with a single argument function declaration (#2877)

This commit is contained in:
Lucas Duailibe authored and Simon Lydell committed 2017-09-26 19:24:25 +02:00
1 parent 88049a3d6b
commit 3703cb5b07
3 files changed
+56 -17

No files matched your search

+23 -17
View File
@@ -845,6 +845,23 @@ function genericPrintNoParens(path, options, print, args) {
case "CallExpression": {
const isNew = n.type === "NewExpression";
const unitTestRe = /^(f|x)?(it|describe|test)$/;
const isTestDeclaration =
!isNew &&
((n.callee.type === "Identifier" && unitTestRe.test(n.callee.name)) ||
(n.callee.type === "MemberExpression" &&
n.callee.object.type === "Identifier" &&
n.callee.property.type === "Identifier" &&
unitTestRe.test(n.callee.object.name) &&
(n.callee.property.name === "only" ||
n.callee.property.name === "skip"))) &&
n.arguments.length === 2 &&
(n.arguments[0].type === "StringLiteral" ||
n.arguments[0].type === "TemplateLiteral" ||
(n.arguments[0].type === "Literal" &&
typeof n.arguments[0].value === "string")) &&
(n.arguments[1].type === "FunctionExpression" ||
n.arguments[1].type === "ArrowFunctionExpression") &&
n.arguments[1].params.length <= 1;
const optional = printOptionalToken(path);
if (
@@ -858,29 +875,18 @@ function genericPrintNoParens(path, options, print, args) {
isTemplateOnItsOwnLine(n.arguments[0], options.originalText)) ||
// Keep test declarations on a single line
// e.g. `it('long name', () => {`
(!isNew &&
((n.callee.type === "Identifier" && unitTestRe.test(n.callee.name)) ||
(n.callee.type === "MemberExpression" &&
n.callee.object.type === "Identifier" &&
n.callee.property.type === "Identifier" &&
unitTestRe.test(n.callee.object.name) &&
(n.callee.property.name === "only" ||
n.callee.property.name === "skip"))) &&
n.arguments.length === 2 &&
(n.arguments[0].type === "StringLiteral" ||
n.arguments[0].type === "TemplateLiteral" ||
(n.arguments[0].type === "Literal" &&
typeof n.arguments[0].value === "string")) &&
(n.arguments[1].type === "FunctionExpression" ||
n.arguments[1].type === "ArrowFunctionExpression") &&
n.arguments[1].params.length <= 1)
isTestDeclaration
) {
let printedArgs = path.map(print, "arguments");
if (isTestDeclaration) {
printedArgs = printedArgs.map(docUtils.removeLines);
}
return concat([
isNew ? "new " : "",
path.call(print, "callee"),
optional,
path.call(print, "typeParameters"),
concat(["(", join(", ", path.map(print, "arguments")), ")"])
concat(["(", join(", ", printedArgs), ")"])
]);
}
@@ -11,6 +11,17 @@ it("does something really long and complicated so I have to write a very long na
console.log("hello!");
});
it("does something really long and complicated so I have to write a very long name for the test", function(done) {
console.log("hello!");
});
it(\`handles
some
newlines
does something really long and complicated so I have to write a very long name for the test\`, function(done) {
console.log("hello!");
});
it(\`does something really long and complicated so I have to write a very long name for the test\`, function() {
console.log("hello!");
});
@@ -100,6 +111,17 @@ it("does something really long and complicated so I have to write a very long na
console.log("hello!");
});
it("does something really long and complicated so I have to write a very long name for the test", function(done) {
console.log("hello!");
});
it(\`handles
some
newlines
does something really long and complicated so I have to write a very long name for the test\`, function(done) {
console.log("hello!");
});
it(\`does something really long and complicated so I have to write a very long name for the test\`, function() {
console.log("hello!");
});
@@ -8,6 +8,17 @@ it("does something really long and complicated so I have to write a very long na
console.log("hello!");
});
it("does something really long and complicated so I have to write a very long name for the test", function(done) {
console.log("hello!");
});
it(`handles
some
newlines
does something really long and complicated so I have to write a very long name for the test`, function(done) {
console.log("hello!");
});
it(`does something really long and complicated so I have to write a very long name for the test`, function() {
console.log("hello!");
});