From de1daa088896d004f556c8a1b2db6254680f74d0 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 23 Nov 2024 06:21:28 +0000 Subject: [PATCH 1/9] feat: add fixer function --- .../eslint/rules/vars-order/lib/main.js | 51 +++++++++++++------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js index fedde217d02e..48c8ce20080b 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js @@ -81,21 +81,6 @@ function main( context ) { break; } - /** - * Reports the error message. - * - * @private - * @param {string} msg - error message - * @param {Object} loc - lines of code (object with `start` and `end` properties) - */ - function report( msg, loc ) { - context.report({ - 'node': null, - 'message': msg, - 'loc': loc - }); - } - /** * Checks whether the variable declarations inside of the supplied node are ordered. * @@ -122,6 +107,42 @@ function main( context ) { prevLength = name.length; } } + + /** + * Reports the error message. + * + * @private + * @param {string} msg - error message + * @param {Object} loc - lines of code (object with `start` and `end` properties) + */ + function report( msg, loc ) { + context.report({ + 'node': null, + 'message': msg, + 'loc': loc, + 'fix': fix + }); + } + + /** + * Fixes the lint error by reordering the variable declarations inside of the function. + * + * @private + * @param {Function} fixer - ESLint fixer + * @returns {(Object|null)} fix or null + */ + function fix(fixer) { + var replacingText; + var prevElem; + var source; + + source = context.getSourceCode(); + prevElem = body[ i - 1 ]; + + replacingText = source.getText( elem, 4 ) + '\n' + source.getText( prevElem, 4 ); + + return fixer.replaceTextRange( [ prevElem.range[0] -prevElem.loc.start.column, elem.range[1] ], replacingText ); // eslint-disable-line max-len + } } return { From b4fc4323eb558cf5e55fc936d0c4d2bfa2dea712 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 23 Nov 2024 11:29:10 +0500 Subject: [PATCH 2/9] chore: apply review suggestion Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- .../@stdlib/_tools/eslint/rules/vars-order/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js index 48c8ce20080b..38d60c4ce115 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js @@ -141,7 +141,7 @@ function main( context ) { replacingText = source.getText( elem, 4 ) + '\n' + source.getText( prevElem, 4 ); - return fixer.replaceTextRange( [ prevElem.range[0] -prevElem.loc.start.column, elem.range[1] ], replacingText ); // eslint-disable-line max-len + return fixer.replaceTextRange( [ prevElem.range[0] - prevElem.loc.start.column, elem.range[1] ], replacingText ); // eslint-disable-line max-len } } From b8aa40b2e06b263e6e3cbd69c2e0129e7efda963 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 23 Nov 2024 09:06:11 +0000 Subject: [PATCH 3/9] refactor: make it fixable --- .../@stdlib/_tools/eslint/rules/vars-order/lib/main.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js index 48c8ce20080b..4556236e4027 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js @@ -156,9 +156,11 @@ function main( context ) { rule = { 'meta': { + 'type': 'layout', 'docs': { 'description': 'require variable declarations inside of functions to be ordered by length' }, + 'fixable': 'code', 'schema': [ { 'order': [ 'increasing', 'decreasing' ] From 5d98153deada89094b649bb6a55405eaa95a4170 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 23 Nov 2024 20:02:27 +0000 Subject: [PATCH 4/9] fix: update fixer function --- .../@stdlib/_tools/eslint/rules/vars-order/lib/main.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js index c05d8f516942..3d5c3ed02592 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js @@ -135,11 +135,12 @@ function main( context ) { var replacingText; var prevElem; var source; + var tabs; source = context.getSourceCode(); prevElem = body[ i - 1 ]; - - replacingText = source.getText( elem, 4 ) + '\n' + source.getText( prevElem, 4 ); + tabs = '\t'; + replacingText = tabs + source.getText( elem ) + '\n' + tabs + source.getText( prevElem ); return fixer.replaceTextRange( [ prevElem.range[0] - prevElem.loc.start.column, elem.range[1] ], replacingText ); // eslint-disable-line max-len } From 653dd9679bde06061c050ea841d9ceeb97c81556 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 24 Nov 2024 05:01:57 +0000 Subject: [PATCH 5/9] test: update test fixtures --- .../rules/vars-order/test/fixtures/invalid.js | 85 +++++++++++++++++-- 1 file changed, 80 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js index 6de001b4661e..47584577b9ca 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js @@ -38,7 +38,18 @@ test = { 'message': 'Variable declarations inside of function are not ordered by length (in decreasing order)', 'type': null } - ] + ], + 'output': [ + 'function fizzBuzz() {', + '\tvar out;', + '\tvar i;', + '', + ' for ( i = 1; i <= 100; i++ ) {', + ' out = ( i % 5 === 0 ) ? "Buzz" : ( i % 3 === 0 ) ? "Fizz" : i;', + ' console.log( out );', + ' }', + '}' + ].join( '\n' ) }; invalid.push( test ); @@ -55,7 +66,14 @@ test = { 'message': 'Variable declarations inside of function are not ordered by length (in decreasing order)', 'type': null } - ] + ], + 'output': [ + 'function addFour( y ) {', + '\tvar out = x + y;', + '\tvar x = 4.0;', + ' return out;', + '}' + ].join( '\n' ) }; invalid.push( test ); @@ -90,7 +108,32 @@ test = { 'message': 'Variable declarations inside of function are not ordered by length (in decreasing order)', 'type': null } - ] + ], + 'output': [ + '/**', + '* Returns a pseudorandom number drawn from a triangular distribution with minimum support `a`, maximum support `b` and mode `c`.', + '*', + '* @private', + '* @param {Function} rand - PRNG for generating uniformly distributed numbers', + '* @param {number} a - minimum support', + '* @param {number} b - maximum support', + '* @param {number} c - mode', + '* @returns {number} pseudorandom number', + '*/', + 'function triangular( rand, a, b, c ) {', + '\tvar fc;', + '\tvar x;', + ' var u;', + ' fc = (c - a) / (b - a);', + ' u = rand();', + ' if ( u < fc ) {', + ' x = (b - a) * (c - a);', + ' return a + sqrt( x * u );', + ' }', + ' x = (b - a) * (b - c);', + ' return b - sqrt( x * (1.0 - u) );', + '}' + ].join( '\n' ) }; invalid.push( test ); @@ -110,7 +153,14 @@ test = { 'message': 'Variable declarations inside of function are not ordered by length (in decreasing order)', 'type': null } - ] + ], + 'output': [ + 'function addFour( y ) {', + '\tvar out = x + y;', + '\tvar x = 4.0;', + ' return out;', + '}' + ].join( '\n' ) }; invalid.push( test ); @@ -148,7 +198,32 @@ test = { 'message': 'Variable declarations inside of function are not ordered by length (in increasing order)', 'type': null } - ] + ], + 'output': [ + '/**', + '* Returns a pseudorandom number drawn from a triangular distribution with minimum support `a`, maximum support `b` and mode `c`.', + '*', + '* @private', + '* @param {Function} rand - PRNG for generating uniformly distributed numbers', + '* @param {number} a - minimum support', + '* @param {number} b - maximum support', + '* @param {number} c - mode', + '* @returns {number} pseudorandom number', + '*/', + 'function triangular( rand, a, b, c ) {', + '\tvar x;', + '\tvar u;', + '\tvar fc;', + ' fc = (c - a) / (b - a);', + ' u = rand();', + ' if ( u < fc ) {', + ' x = (b - a) * (c - a);', + ' return a + sqrt( x * u );', + ' }', + ' x = (b - a) * (b - c);', + ' return b - sqrt( x * (1.0 - u) );', + '}' + ].join( '\n' ) }; invalid.push( test ); From ce7ab31d1e4c168743f663510399936990d4085a Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 27 Nov 2024 01:39:38 +0500 Subject: [PATCH 6/9] refactor: use single-pass and update test fixtures --- .../eslint/rules/vars-order/lib/main.js | 59 ++++- .../rules/vars-order/test/fixtures/invalid.js | 215 ++++++++++++------ 2 files changed, 199 insertions(+), 75 deletions(-) diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js index 3d5c3ed02592..fd056e5ef94a 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js @@ -131,18 +131,63 @@ function main( context ) { * @param {Function} fixer - ESLint fixer * @returns {(Object|null)} fix or null */ - function fix(fixer) { + function fix( fixer ) { var replacingText; - var prevElem; + var declarations; + var startRange; + var endRange; + var tabSize; var source; - var tabs; + var elem; + var j; + declarations = []; + replacingText = ''; + tabSize = 4; source = context.getSourceCode(); - prevElem = body[ i - 1 ]; - tabs = '\t'; - replacingText = tabs + source.getText( elem ) + '\n' + tabs + source.getText( prevElem ); - return fixer.replaceTextRange( [ prevElem.range[0] - prevElem.loc.start.column, elem.range[1] ], replacingText ); // eslint-disable-line max-len + for ( i = 0; i < body.length; i++ ) { + elem = body[ i ]; + if ( elem.type === 'VariableDeclaration' && elem.kind === 'var' ) { + declarations.push({ + 'text': source.getText( elem ), + 'col': elem.loc.start.column + }); + if ( declarations.length === 1 ) { + startRange = elem.range[ 0 ] - elem.loc.start.column; + } + endRange = elem.range[ 1 ]; + } + } + + declarations.sort( sortVars ); + + for ( i = 0; i < declarations.length; i++ ) { + for ( j = 0; j < declarations[ i ].col/tabSize; j++ ) { + replacingText += '\t'; + } + replacingText += declarations[ i ].text; + if ( i !== declarations.length -1 ) { + replacingText += '\n'; + } + } + + return fixer.replaceTextRange( [ startRange, endRange ], replacingText ); // eslint-disable-line max-len + + /** + * Sorts the variable declarations by name length. + * + * @private + * @param {Object} a - input object + * @param {Object} b - comparison object + * @returns {number} number indicating sort order + */ + function sortVars( a, b ) { + if ( fun( a.text.length, b.text.length ) ) { + return 1; + } + return -1; + } } } diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js index 47584577b9ca..b9f18370a421 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js @@ -24,13 +24,13 @@ var test; test = { 'code': [ 'function fizzBuzz() {', - ' var i;', - ' var out;', + ' var i;', + ' var out;', '', - ' for ( i = 1; i <= 100; i++ ) {', - ' out = ( i % 5 === 0 ) ? "Buzz" : ( i % 3 === 0 ) ? "Fizz" : i;', - ' console.log( out );', - ' }', + ' for ( i = 1; i <= 100; i++ ) {', + ' out = ( i % 5 === 0 ) ? "Buzz" : ( i % 3 === 0 ) ? "Fizz" : i;', + ' console.log( out );', + ' }', '}' ].join( '\n' ), 'errors': [ @@ -41,13 +41,13 @@ test = { ], 'output': [ 'function fizzBuzz() {', - '\tvar out;', - '\tvar i;', + ' var out;', + ' var i;', '', - ' for ( i = 1; i <= 100; i++ ) {', - ' out = ( i % 5 === 0 ) ? "Buzz" : ( i % 3 === 0 ) ? "Fizz" : i;', - ' console.log( out );', - ' }', + ' for ( i = 1; i <= 100; i++ ) {', + ' out = ( i % 5 === 0 ) ? "Buzz" : ( i % 3 === 0 ) ? "Fizz" : i;', + ' console.log( out );', + ' }', '}' ].join( '\n' ) }; @@ -56,9 +56,9 @@ invalid.push( test ); test = { 'code': [ 'function addFour( y ) {', - ' var x = 4.0;', - ' var out = x + y;', - ' return out;', + ' var x = 4.0;', + ' var out = x + y;', + ' return out;', '}' ].join( '\n' ), 'errors': [ @@ -69,9 +69,9 @@ test = { ], 'output': [ 'function addFour( y ) {', - '\tvar out = x + y;', - '\tvar x = 4.0;', - ' return out;', + ' var out = x + y;', + ' var x = 4.0;', + ' return out;', '}' ].join( '\n' ) }; @@ -90,17 +90,17 @@ test = { '* @returns {number} pseudorandom number', '*/', 'function triangular( rand, a, b, c ) {', - ' var x;', - ' var fc;', - ' var u;', - ' fc = (c - a) / (b - a);', - ' u = rand();', - ' if ( u < fc ) {', - ' x = (b - a) * (c - a);', - ' return a + sqrt( x * u );', - ' }', - ' x = (b - a) * (b - c);', - ' return b - sqrt( x * (1.0 - u) );', + ' var x;', + ' var fc;', + ' var u;', + ' fc = (c - a) / (b - a);', + ' u = rand();', + ' if ( u < fc ) {', + ' x = (b - a) * (c - a);', + ' return a + sqrt( x * u );', + ' }', + ' x = (b - a) * (b - c);', + ' return b - sqrt( x * (1.0 - u) );', '}' ].join( '\n' ), 'errors': [ @@ -121,17 +121,17 @@ test = { '* @returns {number} pseudorandom number', '*/', 'function triangular( rand, a, b, c ) {', - '\tvar fc;', - '\tvar x;', - ' var u;', - ' fc = (c - a) / (b - a);', - ' u = rand();', - ' if ( u < fc ) {', - ' x = (b - a) * (c - a);', - ' return a + sqrt( x * u );', - ' }', - ' x = (b - a) * (b - c);', - ' return b - sqrt( x * (1.0 - u) );', + ' var fc;', + ' var x;', + ' var u;', + ' fc = (c - a) / (b - a);', + ' u = rand();', + ' if ( u < fc ) {', + ' x = (b - a) * (c - a);', + ' return a + sqrt( x * u );', + ' }', + ' x = (b - a) * (b - c);', + ' return b - sqrt( x * (1.0 - u) );', '}' ].join( '\n' ) }; @@ -140,9 +140,9 @@ invalid.push( test ); test = { 'code': [ 'function addFour( y ) {', - ' var x = 4.0;', - ' var out = x + y;', - ' return out;', + ' var x = 4.0;', + ' var out = x + y;', + ' return out;', '}' ].join( '\n' ), 'options': [{ @@ -156,9 +156,9 @@ test = { ], 'output': [ 'function addFour( y ) {', - '\tvar out = x + y;', - '\tvar x = 4.0;', - ' return out;', + ' var out = x + y;', + ' var x = 4.0;', + ' return out;', '}' ].join( '\n' ) }; @@ -177,17 +177,17 @@ test = { '* @returns {number} pseudorandom number', '*/', 'function triangular( rand, a, b, c ) {', - ' var fc;', - ' var x;', - ' var u;', - ' fc = (c - a) / (b - a);', - ' u = rand();', - ' if ( u < fc ) {', - ' x = (b - a) * (c - a);', - ' return a + sqrt( x * u );', - ' }', - ' x = (b - a) * (b - c);', - ' return b - sqrt( x * (1.0 - u) );', + ' var fc;', + ' var x;', + ' var u;', + ' fc = (c - a) / (b - a);', + ' u = rand();', + ' if ( u < fc ) {', + ' x = (b - a) * (c - a);', + ' return a + sqrt( x * u );', + ' }', + ' x = (b - a) * (b - c);', + ' return b - sqrt( x * (1.0 - u) );', '}' ].join( '\n' ), 'options': [{ @@ -211,17 +211,96 @@ test = { '* @returns {number} pseudorandom number', '*/', 'function triangular( rand, a, b, c ) {', - '\tvar x;', - '\tvar u;', - '\tvar fc;', - ' fc = (c - a) / (b - a);', - ' u = rand();', - ' if ( u < fc ) {', - ' x = (b - a) * (c - a);', - ' return a + sqrt( x * u );', - ' }', - ' x = (b - a) * (b - c);', - ' return b - sqrt( x * (1.0 - u) );', + ' var x;', + ' var u;', + ' var fc;', + ' fc = (c - a) / (b - a);', + ' u = rand();', + ' if ( u < fc ) {', + ' x = (b - a) * (c - a);', + ' return a + sqrt( x * u );', + ' }', + ' x = (b - a) * (b - c);', + ' return b - sqrt( x * (1.0 - u) );', + '}' + ].join( '\n' ) +}; +invalid.push( test ); + +test = { + 'code': [ + 'function outer() {', + ' var x;', + ' var xyz;', + ' function inner() {', + ' var a = 10;', + ' var abc = 5;', + ' var ab = 5;', + ' return a + b + c;', + ' }', + ' xyz = inner() + x;', + ' return xyz;', + '}' + ].join( '\n' ), + 'errors': [ + { + 'message': 'Variable declarations inside of function are not ordered by length (in decreasing order)', + 'type': null + } + ], + 'output': [ + 'function outer() {', + ' var xyz;', + ' var x;', + ' function inner() {', + ' var abc = 5;', + ' var ab = 5;', + ' var a = 10;', + ' return a + b + c;', + ' }', + ' xyz = inner() + x;', + ' return xyz;', + '}' + ].join( '\n' ) +}; +invalid.push( test ); + +test = { + 'code': [ + 'function outer() {', + ' var xyz;', + ' var x;', + ' function inner() {', + ' var abc = 5;', + ' var ab = 5;', + ' var a = 10;', + ' return a + b + c;', + ' }', + ' xyz = inner() + x;', + ' return xyz;', + '}' + ].join( '\n' ), + 'options': [{ + 'order': 'increasing' + }], + 'errors': [ + { + 'message': 'Variable declarations inside of function are not ordered by length (in increasing order)', + 'type': null + } + ], + 'output': [ + 'function outer() {', + ' var x;', + ' var xyz;', + ' function inner() {', + ' var a = 10;', + ' var ab = 5;', + ' var abc = 5;', + ' return a + b + c;', + ' }', + ' xyz = inner() + x;', + ' return xyz;', '}' ].join( '\n' ) }; From ce340a3c1351e9dc7526035a88a5bcdeeb0b040a Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 27 Nov 2024 02:15:46 +0500 Subject: [PATCH 7/9] fix: update indent logic --- .../@stdlib/_tools/eslint/rules/vars-order/lib/main.js | 7 +++---- .../eslint/rules/vars-order/test/fixtures/invalid.js | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js index fd056e5ef94a..fa61f7ed4f6f 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js @@ -136,14 +136,12 @@ function main( context ) { var declarations; var startRange; var endRange; - var tabSize; var source; var elem; var j; declarations = []; replacingText = ''; - tabSize = 4; source = context.getSourceCode(); for ( i = 0; i < body.length; i++ ) { @@ -151,6 +149,7 @@ function main( context ) { if ( elem.type === 'VariableDeclaration' && elem.kind === 'var' ) { declarations.push({ 'text': source.getText( elem ), + 'name': elem.declarations[ 0 ].id.name, 'col': elem.loc.start.column }); if ( declarations.length === 1 ) { @@ -163,7 +162,7 @@ function main( context ) { declarations.sort( sortVars ); for ( i = 0; i < declarations.length; i++ ) { - for ( j = 0; j < declarations[ i ].col/tabSize; j++ ) { + for ( j = 0; j < declarations[ i ].col; j++ ) { replacingText += '\t'; } replacingText += declarations[ i ].text; @@ -183,7 +182,7 @@ function main( context ) { * @returns {number} number indicating sort order */ function sortVars( a, b ) { - if ( fun( a.text.length, b.text.length ) ) { + if ( fun( a.name.length, b.name.length ) ) { return 1; } return -1; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js index b9f18370a421..264ab01c6f0a 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js @@ -230,8 +230,8 @@ invalid.push( test ); test = { 'code': [ 'function outer() {', - ' var x;', ' var xyz;', + ' var x;', ' function inner() {', ' var a = 10;', ' var abc = 5;', @@ -268,8 +268,8 @@ invalid.push( test ); test = { 'code': [ 'function outer() {', - ' var xyz;', ' var x;', + ' var xyz;', ' function inner() {', ' var abc = 5;', ' var ab = 5;', From 1843e4bb1e9a4ecb80c7f67fa1ca6278afc47774 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 27 Nov 2024 14:51:45 +0500 Subject: [PATCH 8/9] refactor: move functions to a higher scope --- .../eslint/rules/vars-order/lib/main.js | 99 ++++++++++--------- 1 file changed, 51 insertions(+), 48 deletions(-) diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js index fa61f7ed4f6f..9f54ca98a843 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/lib/main.js @@ -82,47 +82,34 @@ function main( context ) { } /** - * Checks whether the variable declarations inside of the supplied node are ordered. + * Sorts the variable declarations by name length. * * @private - * @param {ASTNode} node - node to examine - * @returns {void} + * @param {Object} a - input object + * @param {Object} b - comparison object + * @returns {number} number indicating sort order */ - function validate( node ) { - var prevLength; - var body; - var elem; - var name; - var i; - - body = node.body.body; - prevLength = null; - for ( i = 0; i < body.length; i++ ) { - elem = body[ i ]; - if ( elem.type === 'VariableDeclaration' && elem.kind === 'var' ) { - name = elem.declarations[ 0 ].id.name; - if ( prevLength && !fun( name.length, prevLength ) ) { - return report( 'Variable declarations inside of function are not ordered by length (in '+ order +' order)', node.loc ); - } - prevLength = name.length; - } + function sortVars( a, b ) { + if ( fun( a.name.length, b.name.length ) ) { + return 1; } + return -1; + } - /** - * Reports the error message. - * - * @private - * @param {string} msg - error message - * @param {Object} loc - lines of code (object with `start` and `end` properties) - */ - function report( msg, loc ) { - context.report({ - 'node': null, - 'message': msg, - 'loc': loc, - 'fix': fix - }); - } + /** + * Reports the error message. + * + * @private + * @param {string} msg - error message + * @param {ASTNode} node - node to fix + */ + function report( msg, node ) { + context.report({ + 'node': null, + 'message': msg, + 'loc': node.loc, + 'fix': fix + }); /** * Fixes the lint error by reordering the variable declarations inside of the function. @@ -138,10 +125,13 @@ function main( context ) { var endRange; var source; var elem; + var body; + var i; var j; declarations = []; replacingText = ''; + body = node.body.body; source = context.getSourceCode(); for ( i = 0; i < body.length; i++ ) { @@ -172,20 +162,33 @@ function main( context ) { } return fixer.replaceTextRange( [ startRange, endRange ], replacingText ); // eslint-disable-line max-len + } + } - /** - * Sorts the variable declarations by name length. - * - * @private - * @param {Object} a - input object - * @param {Object} b - comparison object - * @returns {number} number indicating sort order - */ - function sortVars( a, b ) { - if ( fun( a.name.length, b.name.length ) ) { - return 1; + /** + * Checks whether the variable declarations inside of the supplied node are ordered. + * + * @private + * @param {ASTNode} node - node to examine + * @returns {void} + */ + function validate( node ) { + var prevLength; + var body; + var elem; + var name; + var i; + + body = node.body.body; + prevLength = null; + for ( i = 0; i < body.length; i++ ) { + elem = body[ i ]; + if ( elem.type === 'VariableDeclaration' && elem.kind === 'var' ) { + name = elem.declarations[ 0 ].id.name; + if ( prevLength && !fun( name.length, prevLength ) ) { + return report( 'Variable declarations inside of function are not ordered by length (in '+ order +' order)', node ); } - return -1; + prevLength = name.length; } } } From a37ecd87317ea8e0a4e23b6a23bbc8f83ca2b6f2 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Fri, 29 Nov 2024 10:26:39 -0500 Subject: [PATCH 9/9] test: add case where both inner and outer var declarations are in wrong order --- .../_tools/eslint/rules/vars-order/test/fixtures/invalid.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js index 264ab01c6f0a..bb09069ba350 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/vars-order/test/fixtures/invalid.js @@ -268,8 +268,8 @@ invalid.push( test ); test = { 'code': [ 'function outer() {', - ' var x;', ' var xyz;', + ' var x;', ' function inner() {', ' var abc = 5;', ' var ab = 5;', @@ -284,6 +284,10 @@ test = { 'order': 'increasing' }], 'errors': [ + { + 'message': 'Variable declarations inside of function are not ordered by length (in increasing order)', + 'type': null + }, { 'message': 'Variable declarations inside of function are not ordered by length (in increasing order)', 'type': null