Skip to content
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

feat: add fixer function to eslint/rules/no-internal-require #3364

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,27 @@ function main( context ) {
function report( node ) {
context.report({
'node': node,
'message': 'require() call contains a path which loads a module internal to a stdlib package, and, thus, should be considered private'
'message': 'require() call contains a path which loads a module internal to a stdlib package, and, thus, should be considered private',
'fix': fix
});

/**
* Fixes the lint error by removing internal path from the require statement.
*
* @private
* @param {Object} fixer - ESLint fixer
* @returns {Object} fix - fix object
*/
function fix( fixer ) {
var replacingText;
var statement;
var source;

source = context.getSourceCode();
statement = source.getText( node.arguments[ 0 ] );
replacingText = statement.replace(/\/lib\/.*$/, '\'');
return fixer.replaceTextRange( node.arguments[ 0 ].range, replacingText ); // eslint-disable-line max-len
}
}

/**
Expand Down Expand Up @@ -83,9 +102,11 @@ function main( context ) {

rule = {
'meta': {
'type': 'suggestion',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up. Currently, the auto-fixing for suggestion type is not supported.

'docs': {
'description': 'disallow require() calls into internals of other stdlib packages'
},
'fixable': 'code',
'schema': []
},
'create': main
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ test = {
'message': 'require() call contains a path which loads a module internal to a stdlib package, and, thus, should be considered private',
'type': 'CallExpression'
}
]
],
'output': [
'// MODULES //',
'',
'var betainc = require( \'@stdlib/math/base/special/betainc\' );'
].join( '\n' )
};
invalid.push( test );

Expand All @@ -48,7 +53,13 @@ test = {
'message': 'require() call contains a path which loads a module internal to a stdlib package, and, thus, should be considered private',
'type': 'CallExpression'
}
]
],
'output': [
'// MODULES //',
'',
'var betaincinv = require( \'@stdlib/math/base/special/betaincinv\' )',
'var kernelBetainc = require( \'@stdlib/math/base/special/kernel-betainc\' )'
].join( '\n' )
};
invalid.push( test );

Expand Down
Loading