Skip to content

Commit 1843e4b

Browse files
committed
refactor: move functions to a higher scope
1 parent ce340a3 commit 1843e4b

File tree

1 file changed

+51
-48
lines changed
  • lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib

1 file changed

+51
-48
lines changed

lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -82,47 +82,34 @@ function main( context ) {
8282
}
8383

8484
/**
85-
* Checks whether the variable declarations inside of the supplied node are ordered.
85+
* Sorts the variable declarations by name length.
8686
*
8787
* @private
88-
* @param {ASTNode} node - node to examine
89-
* @returns {void}
88+
* @param {Object} a - input object
89+
* @param {Object} b - comparison object
90+
* @returns {number} number indicating sort order
9091
*/
91-
function validate( node ) {
92-
var prevLength;
93-
var body;
94-
var elem;
95-
var name;
96-
var i;
97-
98-
body = node.body.body;
99-
prevLength = null;
100-
for ( i = 0; i < body.length; i++ ) {
101-
elem = body[ i ];
102-
if ( elem.type === 'VariableDeclaration' && elem.kind === 'var' ) {
103-
name = elem.declarations[ 0 ].id.name;
104-
if ( prevLength && !fun( name.length, prevLength ) ) {
105-
return report( 'Variable declarations inside of function are not ordered by length (in '+ order +' order)', node.loc );
106-
}
107-
prevLength = name.length;
108-
}
92+
function sortVars( a, b ) {
93+
if ( fun( a.name.length, b.name.length ) ) {
94+
return 1;
10995
}
96+
return -1;
97+
}
11098

111-
/**
112-
* Reports the error message.
113-
*
114-
* @private
115-
* @param {string} msg - error message
116-
* @param {Object} loc - lines of code (object with `start` and `end` properties)
117-
*/
118-
function report( msg, loc ) {
119-
context.report({
120-
'node': null,
121-
'message': msg,
122-
'loc': loc,
123-
'fix': fix
124-
});
125-
}
99+
/**
100+
* Reports the error message.
101+
*
102+
* @private
103+
* @param {string} msg - error message
104+
* @param {ASTNode} node - node to fix
105+
*/
106+
function report( msg, node ) {
107+
context.report({
108+
'node': null,
109+
'message': msg,
110+
'loc': node.loc,
111+
'fix': fix
112+
});
126113

127114
/**
128115
* Fixes the lint error by reordering the variable declarations inside of the function.
@@ -138,10 +125,13 @@ function main( context ) {
138125
var endRange;
139126
var source;
140127
var elem;
128+
var body;
129+
var i;
141130
var j;
142131

143132
declarations = [];
144133
replacingText = '';
134+
body = node.body.body;
145135
source = context.getSourceCode();
146136

147137
for ( i = 0; i < body.length; i++ ) {
@@ -172,20 +162,33 @@ function main( context ) {
172162
}
173163

174164
return fixer.replaceTextRange( [ startRange, endRange ], replacingText ); // eslint-disable-line max-len
165+
}
166+
}
175167

176-
/**
177-
* Sorts the variable declarations by name length.
178-
*
179-
* @private
180-
* @param {Object} a - input object
181-
* @param {Object} b - comparison object
182-
* @returns {number} number indicating sort order
183-
*/
184-
function sortVars( a, b ) {
185-
if ( fun( a.name.length, b.name.length ) ) {
186-
return 1;
168+
/**
169+
* Checks whether the variable declarations inside of the supplied node are ordered.
170+
*
171+
* @private
172+
* @param {ASTNode} node - node to examine
173+
* @returns {void}
174+
*/
175+
function validate( node ) {
176+
var prevLength;
177+
var body;
178+
var elem;
179+
var name;
180+
var i;
181+
182+
body = node.body.body;
183+
prevLength = null;
184+
for ( i = 0; i < body.length; i++ ) {
185+
elem = body[ i ];
186+
if ( elem.type === 'VariableDeclaration' && elem.kind === 'var' ) {
187+
name = elem.declarations[ 0 ].id.name;
188+
if ( prevLength && !fun( name.length, prevLength ) ) {
189+
return report( 'Variable declarations inside of function are not ordered by length (in '+ order +' order)', node );
187190
}
188-
return -1;
191+
prevLength = name.length;
189192
}
190193
}
191194
}

0 commit comments

Comments
 (0)