-
-
Notifications
You must be signed in to change notification settings - Fork 908
feat: add a fixer
function to eslint/rules/require-order
#3323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
ab42666
a3b73c6
08c8449
a45d93c
de2e42c
2b74e90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,21 @@ function main( context ) { | |
} | ||
} | ||
|
||
/** | ||
* Sorts `require` statements based on their ranks. | ||
* | ||
* @private | ||
* @param {Object} a - first require statement | ||
* @param {Object} b - second require statement | ||
* @returns {number} number indicating sort order | ||
*/ | ||
function sortRequires( a, b ) { | ||
if ( a.rank < b.rank ) { | ||
return -1; | ||
} | ||
return 1; | ||
} | ||
|
||
/** | ||
* Reports the error message. | ||
* | ||
|
@@ -141,9 +156,65 @@ function main( context ) { | |
context.report({ | ||
'node': null, | ||
'message': msg, | ||
'loc': curr.node.loc | ||
'loc': curr.node.loc, | ||
'fix': fix | ||
}); | ||
} | ||
|
||
/** | ||
* Fixes the lint error by reordering the require statements. | ||
* | ||
* @private | ||
* @param {Function} fixer - ESLint fixer | ||
* @returns {(Object|Null)} fix or null | ||
*/ | ||
function fix(fixer) { | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
var requireDeclarations; | ||
var replacingText; | ||
var startRange; | ||
var endRange; | ||
var source; | ||
var elem; | ||
var txt; | ||
var i; | ||
|
||
replacingText = ''; | ||
requireDeclarations = []; | ||
source = context.getSourceCode(); | ||
|
||
// Get the text and rank for the require statements: | ||
for ( i = 0; i < requires.length; i++ ) { | ||
elem = requires[i].node.parent.parent; | ||
txt = source.getText(elem); | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if ( i === 0 ) { | ||
startRange = elem.range[ 0 ] - elem.loc.start.column; | ||
} | ||
endRange = elem.range[ 1 ]; | ||
requireDeclarations.push( { | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
'text': txt, | ||
'rank': requires[i].rank | ||
}); | ||
} | ||
|
||
// Sort the require statements: | ||
requireDeclarations.sort( sortRequires ); | ||
|
||
// Build the replacement text: | ||
for ( i = 0; i < requireDeclarations.length; i++ ) { | ||
txt = requireDeclarations[ i ].text; | ||
if ( !txt.startsWith( 'var' ) ) { | ||
txt = 'var '+txt; | ||
} | ||
if ( !txt.endsWith( ';' ) ) { | ||
txt += ';'; | ||
} | ||
replacingText += txt; | ||
|
||
if ( i < requireDeclarations.length-1 ) { | ||
replacingText += '\n'; | ||
} | ||
} | ||
return fixer.replaceTextRange( [ startRange, endRange ], replacingText );// eslint-disable-line max-len | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -196,9 +267,11 @@ function main( context ) { | |
|
||
rule = { | ||
'meta': { | ||
'type': 'layout', | ||
'docs': { | ||
'description': 'enforce that `require()` calls follow a specified order' | ||
}, | ||
'fixable': 'code', | ||
'schema': [ | ||
{ | ||
'type': 'object', | ||
|
Uh oh!
There was an error while loading. Please reload this page.