11const {
22 doc : {
3- builders : { group, ifBreak , indent, label, softline }
3+ builders : { group, indent, label, softline }
44 }
55} = require ( 'prettier' ) ;
66
@@ -55,15 +55,22 @@ const isEndOfChain = (node, path) => {
5555 * first separator.
5656 * The second array contains the rest of the chain.
5757 *
58- * The indentation of the whole chain depends on the result of the first
59- * element .
58+ * The second array is grouped and indented, while the first element's
59+ * formatting logic remains separated .
6060 *
61- * If the first element breaks into multiple lines, we won't indent the rest of
62- * the chain as the last line (most likely a closing parentheses) won't be
63- * indented.
61+ * That way the first element can safely split into multiple lines and the rest
62+ * of the chain will continue its formatting rules as normal.
6463 *
6564 * i.e.
6665 * ```
66+ * functionCall(arg1, arg2).rest.of.chain
67+ *
68+ * functionCall(arg1, arg2)
69+ * .long
70+ * .rest
71+ * .of
72+ * .chain
73+ *
6774 * functionCall(
6875 * arg1,
6976 * arg2
@@ -73,21 +80,6 @@ const isEndOfChain = (node, path) => {
7380 * arg1,
7481 * arg2
7582 * )
76- * .long
77- * .rest
78- * .of
79- * .chain
80- * ```
81- *
82- * If the first element doesn't break into multiple lines we treat the rest of
83- * the chain as a normal chain and proceed to indent it.
84- *
85- *
86- * i.e.
87- * ```
88- * a = functionCall(arg1, arg2).rest.of.chain
89- *
90- * b = functionCall(arg1, arg2)
9183 * .long
9284 * .rest
9385 * .of
@@ -102,51 +94,31 @@ const isEndOfChain = (node, path) => {
10294 * be printed.
10395 */
10496const processChain = ( chain ) => {
105- const firstSeparatorIndex = chain . findIndex ( ( element ) => {
106- if ( element . label ) {
107- return JSON . parse ( element . label ) . type === 'separator' ;
108- }
109- return false ;
110- } ) ;
111- // We fetch the groupId from the firstSeparator
112- const { groupId } = JSON . parse ( chain [ firstSeparatorIndex ] . label ) ;
97+ const firstSeparatorIndex = chain . findIndex (
98+ ( element ) => element . label === 'separator'
99+ ) ;
113100 // The doc[] before the first separator
114101 const firstExpression = chain . slice ( 0 , firstSeparatorIndex ) ;
115102 // The doc[] containing the rest of the chain
116- const restOfChain = group ( chain . slice ( firstSeparatorIndex ) ) ;
103+ const restOfChain = group ( indent ( chain . slice ( firstSeparatorIndex ) ) ) ;
117104
118- return groupId
119- ? [
120- ...firstExpression ,
121- ifBreak ( restOfChain , indent ( restOfChain ) , { groupId } )
122- ]
123- : [ ...firstExpression , indent ( restOfChain ) ] ;
105+ return group ( [ firstExpression , restOfChain ] ) ;
124106} ;
125107
126108const MemberAccess = {
127109 print : ( { node, path, print } ) => {
128110 let expressionDoc = path . call ( print , 'expression' ) ;
129- const separatorLabel = {
130- type : 'separator'
131- } ;
132-
133- if ( expressionDoc . label ) {
134- const labelData = JSON . parse ( expressionDoc . label ) ;
135- if ( labelData && labelData . groupId ) {
136- // if there's a groupId in the data, we pass it to the separator as
137- // this doc[] is going to be stripped of it's metadata
138- separatorLabel . groupId = labelData . groupId ;
139- }
140- expressionDoc = expressionDoc . contents . flat ( ) ;
111+ if ( Array . isArray ( expressionDoc ) ) {
112+ expressionDoc = expressionDoc . flat ( ) ;
141113 }
142114
143115 const doc = [
144116 expressionDoc ,
145- label ( JSON . stringify ( separatorLabel ) , [ softline , '.' ] ) ,
117+ label ( 'separator' , [ softline , '.' ] ) ,
146118 node . memberName
147119 ] . flat ( ) ;
148120
149- return isEndOfChain ( node , path ) ? group ( processChain ( doc ) ) : doc ;
121+ return isEndOfChain ( node , path ) ? processChain ( doc ) : doc ;
150122 }
151123} ;
152124
0 commit comments