@@ -150,15 +150,19 @@ function fixPropTypesSort(
150150 function sortInSource ( allNodes , source ) {
151151 const originalSource = source ;
152152 const sourceCode = getSourceCode ( context ) ;
153+ const trailingCommentRanges = new Set ( ) ;
154+
153155 for ( let i = 0 ; i < allNodes . length ; i ++ ) {
154156 const node = allNodes [ i ] ;
155157 let commentAfter = [ ] ;
156158 let commentBefore = [ ] ;
157159 let newStart = 0 ;
158160 let newEnd = 0 ;
159161 try {
160- commentBefore = sourceCode . getCommentsBefore ( node ) ;
162+ commentBefore = sourceCode . getCommentsBefore ( node )
163+ . filter ( ( comment ) => ! trailingCommentRanges . has ( comment . range . join ( ':' ) ) ) ;
161164 commentAfter = sourceCode . getCommentsAfter ( node ) ;
165+ commentAfter . forEach ( ( comment ) => trailingCommentRanges . add ( comment . range . join ( ':' ) ) ) ;
162166 } catch ( e ) { /**/ }
163167
164168 if ( commentAfter . length === 0 || commentBefore . length === 0 ) {
@@ -197,10 +201,7 @@ function fixPropTypesSort(
197201 const sortedAttr = sortedAttributes [ index ] ;
198202 const commentNode = commentnodeMap . get ( sortedAttr ) ;
199203 let sortedAttrText = sourceCodeText . slice ( commentNode . start , commentNode . end ) ;
200- const sortedAttrTextLastChar = sortedAttrText [ sortedAttrText . length - 1 ] ;
201- if ( ! separator && [ ';' , ',' ] . some ( ( allowedSep ) => sortedAttrTextLastChar === allowedSep ) ) {
202- separator = sortedAttrTextLastChar ;
203- }
204+ let sortedAttrTextStart = commentNode . start ;
204205 if ( sortShapeProp && isShapeProp ( sortedAttr . value ) ) {
205206 const shape = getShapeProperties ( sortedAttr . value ) ;
206207 if ( shape ) {
@@ -209,9 +210,20 @@ function fixPropTypesSort(
209210 originalSource
210211 ) ;
211212 sortedAttrText = attrSource . slice ( sortedAttr . range [ 0 ] , sortedAttr . range [ 1 ] ) ;
213+ sortedAttrTextStart = sortedAttr . range [ 0 ] ;
212214 }
213215 }
214- const sortedAttrTextVal = checkTypes && ! sortedAttrText . endsWith ( separator ) ? `${ sortedAttrText } ${ separator } ` : sortedAttrText ;
216+ const trailingComment = sourceCode . getCommentsAfter ( sortedAttr ) [ 0 ] ;
217+ const separatorIndex = trailingComment ? trailingComment . range [ 0 ] - sortedAttrTextStart : sortedAttrText . length ;
218+ const sortedAttrTextLastChar = sortedAttrText . slice ( 0 , separatorIndex ) . trim ( ) . slice ( - 1 ) ;
219+ if ( ! separator && [ ';' , ',' ] . some ( ( allowedSep ) => sortedAttrTextLastChar === allowedSep ) ) {
220+ separator = sortedAttrTextLastChar ;
221+ }
222+ const hasSeparator = sortedAttrText . slice ( 0 , separatorIndex ) . trim ( ) . endsWith ( separator ) ;
223+ const needsSeparator = checkTypes && separator && ! hasSeparator ;
224+ const sortedAttrTextVal = needsSeparator
225+ ? `${ sortedAttrText . slice ( 0 , separatorIndex ) } ${ separator } ${ sortedAttrText . slice ( separatorIndex ) } `
226+ : sortedAttrText ;
215227 return `${ acc . slice ( 0 , commentnodeMap . get ( attr ) . start ) } ${ sortedAttrTextVal } ${ acc . slice ( commentnodeMap . get ( attr ) . end ) } ` ;
216228 } , source ) ;
217229 } ) ;
0 commit comments