@@ -57,32 +57,18 @@ module.exports = {
57
57
message,
58
58
data : { order : orderMsg . join ( ', ' ) } ,
59
59
fix ( fixer ) {
60
- const source = sourceCode . getText ( ) ;
61
- const last = properties [ properties . length - 1 ] ;
62
- // end loc to replace(including whitespaces).
63
- const finalEnd = sourceCode . getTokenAfter ( last , token => token . value === '}' && token . type === 'Punctuator' ) . range [ 0 ] ;
64
60
// reorder the properties and put in result array.
65
61
const result = [ ] ;
66
62
for ( let j = 0 ; j < keyNames . length ; j ++ ) {
67
63
const insertedIndex = orderMsg . indexOf ( keyNames [ j ] ) ;
68
64
const propertyCode = sourceCode . getText ( properties [ j ] ) ;
69
- const propertyStart = properties [ j ] . range [ 1 ] ;
70
- const propertyEnd = j < properties . length - 1 ? properties [ j + 1 ] . range [ 0 ] : finalEnd ;
71
- let trailing = source . slice ( propertyStart , propertyEnd ) ;
72
-
73
- // for last property, should check & add trailling commas.
74
- if ( j === properties . length - 1 && sourceCode . getTokenAfter ( last ) . value !== ',' ) {
75
- trailing = ', ' + trailing ;
76
- }
77
- result [ insertedIndex ] = propertyCode + trailing ;
65
+ result [ insertedIndex ] = propertyCode ;
78
66
}
79
67
80
- const start = properties [ 0 ] . range [ 0 ] ;
81
-
82
- return fixer . replaceTextRange (
83
- [ start , finalEnd ] ,
84
- result . join ( '' )
85
- ) ;
68
+ return orderMsg . map ( ( key , index ) => {
69
+ const propertyToInsert = test . properties [ keyNames . indexOf ( key ) ] ;
70
+ return fixer . replaceText ( test . properties [ index ] , sourceCode . getText ( propertyToInsert ) ) ;
71
+ } ) ;
86
72
} ,
87
73
} ) ;
88
74
}
0 commit comments