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