@@ -81,19 +81,88 @@ function main( context ) {
81
81
break ;
82
82
}
83
83
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
+
84
99
/**
85
100
* Reports the error message.
86
101
*
87
102
* @private
88
103
* @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
90
105
*/
91
- function report ( msg , loc ) {
106
+ function report ( msg , node ) {
92
107
context . report ( {
93
108
'node' : null ,
94
109
'message' : msg ,
95
- 'loc' : loc
110
+ 'loc' : node . loc ,
111
+ 'fix' : fix
96
112
} ) ;
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
+ }
97
166
}
98
167
99
168
/**
@@ -117,7 +186,7 @@ function main( context ) {
117
186
if ( elem . type === 'VariableDeclaration' && elem . kind === 'var' ) {
118
187
name = elem . declarations [ 0 ] . id . name ;
119
188
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 ) ;
121
190
}
122
191
prevLength = name . length ;
123
192
}
@@ -135,9 +204,11 @@ function main( context ) {
135
204
136
205
rule = {
137
206
'meta' : {
207
+ 'type' : 'layout' ,
138
208
'docs' : {
139
209
'description' : 'require variable declarations inside of functions to be ordered by length'
140
210
} ,
211
+ 'fixable' : 'code' ,
141
212
'schema' : [
142
213
{
143
214
'order' : [ 'increasing' , 'decreasing' ]
0 commit comments