@@ -81,19 +81,88 @@ function main( context ) {
8181 break ;
8282 }
8383
84+ /**
85+ * Sorts the variable declarations by name length.
86+ *
87+ * @private
88+ * @param {Object } a - input object
89+ * @param {Object } b - comparison object
90+ * @returns {number } number indicating sort order
91+ */
92+ function sortVars ( a , b ) {
93+ if ( fun ( a . name . length , b . name . length ) ) {
94+ return 1 ;
95+ }
96+ return - 1 ;
97+ }
98+
8499 /**
85100 * Reports the error message.
86101 *
87102 * @private
88103 * @param {string } msg - error message
89- * @param {Object } loc - lines of code (object with `start` and `end` properties)
104+ * @param {ASTNode } node - node to fix
90105 */
91- function report ( msg , loc ) {
106+ function report ( msg , node ) {
92107 context . report ( {
93108 'node' : null ,
94109 'message' : msg ,
95- 'loc' : loc
110+ 'loc' : node . loc ,
111+ 'fix' : fix
96112 } ) ;
113+
114+ /**
115+ * Fixes the lint error by reordering the variable declarations inside of the function.
116+ *
117+ * @private
118+ * @param {Function } fixer - ESLint fixer
119+ * @returns {(Object|null) } fix or null
120+ */
121+ function fix ( fixer ) {
122+ var replacingText ;
123+ var declarations ;
124+ var startRange ;
125+ var endRange ;
126+ var source ;
127+ var elem ;
128+ var body ;
129+ var i ;
130+ var j ;
131+
132+ declarations = [ ] ;
133+ replacingText = '' ;
134+ body = node . body . body ;
135+ source = context . getSourceCode ( ) ;
136+
137+ for ( i = 0 ; i < body . length ; i ++ ) {
138+ elem = body [ i ] ;
139+ if ( elem . type === 'VariableDeclaration' && elem . kind === 'var' ) {
140+ declarations . push ( {
141+ 'text' : source . getText ( elem ) ,
142+ 'name' : elem . declarations [ 0 ] . id . name ,
143+ 'col' : elem . loc . start . column
144+ } ) ;
145+ if ( declarations . length === 1 ) {
146+ startRange = elem . range [ 0 ] - elem . loc . start . column ;
147+ }
148+ endRange = elem . range [ 1 ] ;
149+ }
150+ }
151+
152+ declarations . sort ( sortVars ) ;
153+
154+ for ( i = 0 ; i < declarations . length ; i ++ ) {
155+ for ( j = 0 ; j < declarations [ i ] . col ; j ++ ) {
156+ replacingText += '\t' ;
157+ }
158+ replacingText += declarations [ i ] . text ;
159+ if ( i !== declarations . length - 1 ) {
160+ replacingText += '\n' ;
161+ }
162+ }
163+
164+ return fixer . replaceTextRange ( [ startRange , endRange ] , replacingText ) ; // eslint-disable-line max-len
165+ }
97166 }
98167
99168 /**
@@ -117,7 +186,7 @@ function main( context ) {
117186 if ( elem . type === 'VariableDeclaration' && elem . kind === 'var' ) {
118187 name = elem . declarations [ 0 ] . id . name ;
119188 if ( prevLength && ! fun ( name . length , prevLength ) ) {
120- return report ( 'Variable declarations inside of function are not ordered by length (in ' + order + ' order)' , node . loc ) ;
189+ return report ( 'Variable declarations inside of function are not ordered by length (in ' + order + ' order)' , node ) ;
121190 }
122191 prevLength = name . length ;
123192 }
@@ -135,9 +204,11 @@ function main( context ) {
135204
136205rule = {
137206 'meta' : {
207+ 'type' : 'layout' ,
138208 'docs' : {
139209 'description' : 'require variable declarations inside of functions to be ordered by length'
140210 } ,
211+ 'fixable' : 'code' ,
141212 'schema' : [
142213 {
143214 'order' : [ 'increasing' , 'decreasing' ]
0 commit comments