diff --git a/src/language-js/print/member-chain.js b/src/language-js/print/member-chain.js index fa2cd2535..787223c75 100644 --- a/src/language-js/print/member-chain.js +++ b/src/language-js/print/member-chain.js @@ -361,7 +361,7 @@ function printMemberChain(path, options, print) { const expanded = concat([ printGroup(groups[0]), - shouldMerge ? printGroup(groups[1]) : "", + shouldMerge ? concat(groups.slice(1, 2).map(printGroup)) : "", shouldHaveEmptyLineBeforeIndent ? hardline : "", printIndentedGroup(groups.slice(shouldMerge ? 2 : 1)), ]); @@ -370,13 +370,6 @@ function printMemberChain(path, options, print) { .map(({ node }) => node) .filter(isCallOrOptionalCallExpression); - function callHasComplexArguments(expr, index) { - return ( - (index !== 0 && expr.arguments.length > 2) || - !expr.arguments.every((arg) => isSimpleCallArgument(arg, 0)) - ); - } - function lastGroupWillBreakAndOtherCallsHaveFunctionArguments() { const lastGroupNode = getLast(getLast(groups)).node; const lastGroupDoc = getLast(printedGroups); @@ -391,16 +384,16 @@ function printMemberChain(path, options, print) { // We don't want to print in one line if at least one of these conditions occurs: // * the chain has comments, - // * the head of the chain is a constructor call, // * the chain is an expression statement and all the arguments are literal-like ("fluent configuration" pattern), // * the chain is longer than 2 calls and has non-trivial arguments or more than 2 arguments in any call but the first one, // * any group but the last one has a hard line, // * the last call's arguments have a hard line and other calls have non-trivial arguments. if ( hasComment || - printedNodes[0].node.type === "NewExpression" || (callExpressions.length > 2 && - callExpressions.some(callHasComplexArguments)) || + callExpressions.some( + (expr) => !expr.arguments.every((arg) => isSimpleCallArgument(arg, 0)) + )) || printedGroups.slice(0, -1).some(willBreak) || lastGroupWillBreakAndOtherCallsHaveFunctionArguments() ) { diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 88910624b..7ce121e44 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -203,28 +203,6 @@ function isLiteral(node) { ); } -/** - * @param {any} node - * @returns {boolean} - */ -function isLiteralLikeValue(node) { - return ( - isLiteral(node) || - (node.type === "Identifier" && /^[A-Z_]+$/.test(node.name)) || - (node.type === "ArrayExpression" && - node.elements.every( - (element) => element !== null && isLiteralLikeValue(element) - )) || - (node.type === "ObjectExpression" && - node.properties.every( - (property) => - !property.computed && - property.value && - isLiteralLikeValue(property.value) - )) - ); -} - /** * @param {Node} node * @returns {boolean} @@ -1415,7 +1393,6 @@ module.exports = { isJSXWhitespaceExpression, isLastStatement, isLiteral, - isLiteralLikeValue, isLongCurriedCallExpression, isSimpleCallArgument, isMeaningfulJSXText, diff --git a/tests/js/arrays/__snapshots__/jsfmt.spec.js.snap b/tests/js/arrays/__snapshots__/jsfmt.spec.js.snap index 56e0f51f6..9c807266f 100644 --- a/tests/js/arrays/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/arrays/__snapshots__/jsfmt.spec.js.snap @@ -30,10 +30,7 @@ new Test() .test(); =====================================output===================================== -new Test() - .test() - .test([, 0]) - .test(); +new Test().test().test([, 0]).test(); ================================================================================ `; diff --git a/tests/js/method-chain/__snapshots__/jsfmt.spec.js.snap b/tests/js/method-chain/__snapshots__/jsfmt.spec.js.snap index c43f77bda..a684d3882 100644 --- a/tests/js/method-chain/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/method-chain/__snapshots__/jsfmt.spec.js.snap @@ -545,9 +545,7 @@ cy.get(".ready") =====================================output===================================== cy.get("option:first").should("be.selected").and("have.value", "Metallica"); -cy.get(".ready") - .should("have.text", "FOO") - .should("have.css", "color", "#aaa"); +cy.get(".ready").should("have.text", "FOO").should("have.css", "color", "#aaa"); ================================================================================ `; @@ -779,9 +777,7 @@ fetched .then((response) => response.json()) .then((json) => processThings(json.data.things)); -let column = new Column(null, conn) - .table(data.table) - .json(data.column); +let column = new Column(null, conn).table(data.table).json(data.column); ================================================================================ `; diff --git a/tests/js/method-chain/print-width-120/__snapshots__/jsfmt.spec.js.snap b/tests/js/method-chain/print-width-120/__snapshots__/jsfmt.spec.js.snap index fe5f00a83..df75f1f81 100644 --- a/tests/js/method-chain/print-width-120/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/method-chain/print-width-120/__snapshots__/jsfmt.spec.js.snap @@ -22,17 +22,11 @@ const writer2 = new BufferStackItem( =====================================output===================================== const writer = new BufferStackItem( - new BinaryWriter() - .writeUInt8(StackItemType.ByteArray) - .writeVarBytesLE(Buffer.alloc(10, 1)) - .toBuffer() + new BinaryWriter().writeUInt8(StackItemType.ByteArray).writeVarBytesLE(Buffer.alloc(10, 1)).toBuffer() ); const writer2 = new BufferStackItem( - new Extra.BinaryWriter() - .writeUInt8(StackItemType.ByteArray) - .writeVarBytesLE(Buffer.alloc(10, 1)) - .toBuffer() + new Extra.BinaryWriter().writeUInt8(StackItemType.ByteArray).writeVarBytesLE(Buffer.alloc(10, 1)).toBuffer() ); ================================================================================ @@ -61,21 +55,11 @@ printWidth: 120 .should("have.css", "background-color", "rgb(222, 222, 222)"); =====================================output===================================== -cy.get(".ready") - .should("have.text", "READY") - .should("have.css", "background-color", "rgb(136, 228, 229)"); -cy.get(".pending") - .should("have.text", "PENDING") - .should("have.css", "background-color", "rgb(253, 212, 90)"); -cy.get(".live") - .should("have.text", "LIVE") - .should("have.css", "background-color", "rgb(175, 221, 255)"); -cy.get(".draft") - .should("have.text", "DRAFT") - .should("have.css", "background-color", "rgb(255, 181, 181)"); -cy.get(".scheduled") - .should("have.text", "SCHEDULED") - .should("have.css", "background-color", "rgb(222, 222, 222)"); +cy.get(".ready").should("have.text", "READY").should("have.css", "background-color", "rgb(136, 228, 229)"); +cy.get(".pending").should("have.text", "PENDING").should("have.css", "background-color", "rgb(253, 212, 90)"); +cy.get(".live").should("have.text", "LIVE").should("have.css", "background-color", "rgb(175, 221, 255)"); +cy.get(".draft").should("have.text", "DRAFT").should("have.css", "background-color", "rgb(255, 181, 181)"); +cy.get(".scheduled").should("have.text", "SCHEDULED").should("have.css", "background-color", "rgb(222, 222, 222)"); ================================================================================ `;