@@ -25,7 +25,7 @@ module.exports = {
2525 fixable : 'code' ,
2626 docs : {
2727 category : 'Stylistic Issues' ,
28- description : 'require react dependency arrays to be sorted ' ,
28+ description : 'Sort React dependency arrays' ,
2929 recommended : false ,
3030 url : 'https://github.com/stevensacks/eslint-plugin-sort-react-dependency-arrays'
3131 } ,
@@ -38,16 +38,22 @@ module.exports = {
3838 const dependencies = node . arguments [ 1 ] ;
3939
4040 if ( dependencies && dependencies . type === 'ArrayExpression' && dependencies . elements . length > 1 ) {
41- const currentNames = dependencies . elements . map ( item => item . name ) ;
42- const sorted = [ ...dependencies . elements ] . sort ( ( a , b ) => ( a . name < b . name ? - 1 : 1 ) ) ;
43- const sortedNames = sorted . map ( ( dep ) => dep . name ) ;
41+ const currentDependencies = [ ...dependencies . elements ] ;
42+ const sortedDependencies = [ ...dependencies . elements ] . sort ( ( a , b ) => ( a . name < b . name ? - 1 : 1 ) ) ;
43+ const currentNames = currentDependencies . map ( item => item . name ) ;
44+ const sortedNames = sortedDependencies . map ( item => item . name ) ;
4445
4546 if ( String ( currentNames ) !== String ( sortedNames ) ) {
4647 context . report ( {
4748 node,
48- message : 'Sorting dependencies' ,
49+ message : 'Sort dependencies' ,
4950 fix : function ( fixer ) {
50- return fixer . replaceText ( dependencies , `[${ sortedNames } ]` ) ;
51+ const fixes = [ ] ;
52+ currentDependencies . forEach (
53+ ( dep , index ) =>
54+ fixes . push ( fixer . replaceText ( dep , sortedNames [ index ] ) )
55+ ) ;
56+ return fixes ;
5157 }
5258 } ) ;
5359 }
0 commit comments