11'use strict' ;
22
33const { types} = require ( '@putout/babel' ) ;
4-
54const { wrongShorthand} = require ( './wrong-shortand' ) ;
65
76const {
@@ -15,13 +14,20 @@ const {moreThenMaxPropertiesInOneLine} = require('./more-then-max-properties-in-
1514const { maybeTypeAnnotation} = require ( '../../maybe/maybe-type-annotation' ) ;
1615const { moreThenMaxPropertiesLengthInOneLine} = require ( './more-then-max-properties-length-in-one-line' ) ;
1716const { printKey} = require ( '../object-expression/print-key' ) ;
18- const isInsideFn = ( path ) => path . parentPath . isFunction ( ) ;
17+
18+ const isInsideFn = ( path ) => {
19+ if ( isFunction ( path . parentPath ) )
20+ return true ;
21+
22+ return isFunction ( path . parentPath . parentPath ) ;
23+ } ;
1924
2025const {
2126 isIdentifier,
2227 isObjectPattern,
2328 isAssignmentPattern,
2429 isVariableDeclarator,
30+ isFunction,
2531} = types ;
2632
2733function isIndent ( path ) {
@@ -67,8 +73,8 @@ module.exports.ObjectPattern = {
6773 } ) ;
6874
6975 const hasObject = n && hasObjectPattern ( properties ) ;
70-
7176 const notInsideFn = ! isInsideFn ( path ) ;
77+
7278 maybe . print . newline ( is && notInsideFn ) ;
7379
7480 for ( const [ i , property ] of properties . entries ( ) ) {
@@ -81,6 +87,9 @@ module.exports.ObjectPattern = {
8187 continue ;
8288 }
8389
90+ const prev = property . getPrevSibling ( ) ;
91+ const prevAssign = i && isAssignmentPattern ( prev . node . value ) ;
92+
8493 const valuePath = property . get ( 'value' ) ;
8594 const keyPath = property . get ( 'key' ) ;
8695 const isAssign = valuePath . isAssignmentPattern ( ) ;
@@ -92,7 +101,7 @@ module.exports.ObjectPattern = {
92101 valuePath,
93102 } ) ;
94103
95- maybe . indent ( is && notInsideFn ) ;
104+ maybe . indent ( ( prevAssign || is ) && notInsideFn ) ;
96105 maybe . print . breakline ( couple ) ;
97106
98107 printKey ( property , printer ) ;
@@ -108,7 +117,7 @@ module.exports.ObjectPattern = {
108117 maybe . print . newline ( couple ) ;
109118 }
110119
111- if ( is || hasObject ) {
120+ if ( is || hasObject || prevAssign && notInsideFn ) {
112121 print ( ',' ) ;
113122 print . newline ( ) ;
114123
@@ -209,3 +218,4 @@ function isFunctionParam({parentPath}) {
209218
210219 return parentPath . parentPath . isFunction ( ) ;
211220}
221+
0 commit comments