diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index b373a295b..c2feffcb2 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -1039,15 +1039,26 @@ function printPathNoParens(path, options, print, args) { case "TSInterfaceBody": case "TSTypeLiteral": { const isTypeAnnotation = n.type === "ObjectTypeAnnotation"; + const parent = path.getParentNode(0); const shouldBreak = n.type === "TSInterfaceBody" || + (n.type === "ObjectPattern" && + parent.type !== "FunctionDeclaration" && + parent.type !== "FunctionExpression" && + parent.type !== "ArrowFunctionExpression" && + parent.type !== "AssignmentPattern" && + n.properties.some( + property => + property.value && + (property.value.type === "ObjectPattern" || + property.value.type === "ArrayPattern") + )) || (n.type !== "ObjectPattern" && privateUtil.hasNewlineInRange( options.originalText, options.locStart(n), options.locEnd(n) )); - const parent = path.getParentNode(0); const isFlowInterfaceLikeBody = isTypeAnnotation && parent && diff --git a/tests/destructuring/__snapshots__/jsfmt.spec.js.snap b/tests/destructuring/__snapshots__/jsfmt.spec.js.snap index 6af373164..0c932673a 100644 --- a/tests/destructuring/__snapshots__/jsfmt.spec.js.snap +++ b/tests/destructuring/__snapshots__/jsfmt.spec.js.snap @@ -4,9 +4,56 @@ exports[`destructuring.js 1`] = ` const [one, two = null, three = null] = arr; a = ([s=1,]) => 1 const { children, ...props } = this.props + +const { user: { firstName, lastName } } = this.props; + +const { + name: { first, last }, + organisation: { address: { street: orgStreetAddress, postcode: orgPostcode } } +} = user; + +function f({ data: { name } }) {} + +const UserComponent = function({ + name: { first, last }, + organisation: { address: { street: orgStreetAddress, postcode: orgPostcode } }, +}) { + return +}; + +const { a, b, c, d: { e } } = someObject; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ const [one, two = null, three = null] = arr; a = ([s = 1]) => 1; const { children, ...props } = this.props; +const { + user: { firstName, lastName } +} = this.props; + +const { + name: { first, last }, + organisation: { + address: { street: orgStreetAddress, postcode: orgPostcode } + } +} = user; + +function f({ data: { name } }) {} + +const UserComponent = function({ + name: { first, last }, + organisation: { + address: { street: orgStreetAddress, postcode: orgPostcode } + } +}) { + return; +}; + +const { + a, + b, + c, + d: { e } +} = someObject; + `; diff --git a/tests/destructuring/destructuring.js b/tests/destructuring/destructuring.js index c26994b16..6b774e3db 100644 --- a/tests/destructuring/destructuring.js +++ b/tests/destructuring/destructuring.js @@ -1,3 +1,21 @@ const [one, two = null, three = null] = arr; a = ([s=1,]) => 1 const { children, ...props } = this.props + +const { user: { firstName, lastName } } = this.props; + +const { + name: { first, last }, + organisation: { address: { street: orgStreetAddress, postcode: orgPostcode } } +} = user; + +function f({ data: { name } }) {} + +const UserComponent = function({ + name: { first, last }, + organisation: { address: { street: orgStreetAddress, postcode: orgPostcode } }, +}) { + return +}; + +const { a, b, c, d: { e } } = someObject;