@@ -126,6 +126,7 @@ const commentnodeMap = new WeakMap(); // all nodes reference WeakMap for start a
126126 * @param {Boolean= } callbacksLast whether or not to sort callbacks after everything else.
127127 * @param {Boolean= } noSortAlphabetically whether or not to disable alphabetical sorting of the elements.
128128 * @param {Boolean= } sortShapeProp whether or not to sort propTypes defined in PropTypes.shape.
129+ * @param {Boolean= } checkTypes whether or not sorting of prop type definitions are checked.
129130 * @returns {Object|*|{range, text} } the sort order of the two elements.
130131 */
131132function fixPropTypesSort (
@@ -136,7 +137,8 @@ function fixPropTypesSort(
136137 requiredFirst ,
137138 callbacksLast ,
138139 noSortAlphabetically ,
139- sortShapeProp
140+ sortShapeProp ,
141+ checkTypes
140142) {
141143 function sortInSource ( allNodes , source ) {
142144 const originalSource = source ;
@@ -183,10 +185,15 @@ function fixPropTypesSort(
183185 ) ;
184186
185187 const sourceCodeText = getText ( context ) ;
188+ let separator = '' ;
186189 source = nodes . reduceRight ( ( acc , attr , index ) => {
187190 const sortedAttr = sortedAttributes [ index ] ;
188191 const commentNode = commentnodeMap . get ( sortedAttr ) ;
189192 let sortedAttrText = sourceCodeText . slice ( commentNode . start , commentNode . end ) ;
193+ const sortedAttrTextLastChar = sortedAttrText [ sortedAttrText . length - 1 ] ;
194+ if ( ! separator && [ ';' , ',' ] . some ( ( allowedSep ) => sortedAttrTextLastChar === allowedSep ) ) {
195+ separator = sortedAttrTextLastChar ;
196+ }
190197 if ( sortShapeProp && isShapeProp ( sortedAttr . value ) ) {
191198 const shape = getShapeProperties ( sortedAttr . value ) ;
192199 if ( shape ) {
@@ -197,7 +204,8 @@ function fixPropTypesSort(
197204 sortedAttrText = attrSource . slice ( sortedAttr . range [ 0 ] , sortedAttr . range [ 1 ] ) ;
198205 }
199206 }
200- return `${ acc . slice ( 0 , commentnodeMap . get ( attr ) . start ) } ${ sortedAttrText } ${ acc . slice ( commentnodeMap . get ( attr ) . end ) } ` ;
207+ const sortedAttrTextVal = checkTypes && ! sortedAttrText . endsWith ( separator ) ? `${ sortedAttrText } ${ separator } ` : sortedAttrText ;
208+ return `${ acc . slice ( 0 , commentnodeMap . get ( attr ) . start ) } ${ sortedAttrTextVal } ${ acc . slice ( commentnodeMap . get ( attr ) . end ) } ` ;
201209 } , source ) ;
202210 } ) ;
203211 return source ;
0 commit comments