Skip to content

Commit 4063f46

Browse files
committed
feat: add fixer function
1 parent 6e86cea commit 4063f46

File tree

2 files changed

+35
-3
lines changed
  • lib/node_modules/@stdlib/_tools/eslint/rules/no-internal-require

2 files changed

+35
-3
lines changed

lib/node_modules/@stdlib/_tools/eslint/rules/no-internal-require/lib/main.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,27 @@ function main( context ) {
4848
function report( node ) {
4949
context.report({
5050
'node': node,
51-
'message': 'require() call contains a path which loads a module internal to a stdlib package, and, thus, should be considered private'
51+
'message': 'require() call contains a path which loads a module internal to a stdlib package, and, thus, should be considered private',
52+
'fix': fix
5253
});
54+
55+
/**
56+
* Fixes the lint error by removing internal path from the require statement.
57+
*
58+
* @private
59+
* @param {Object} fixer - ESLint fixer
60+
* @returns {Object} fix - fix object
61+
*/
62+
function fix( fixer ) {
63+
var replacingText;
64+
var statement;
65+
var source;
66+
67+
source = context.getSourceCode();
68+
statement = source.getText( node.arguments[ 0 ] );
69+
replacingText = statement.replace(/\/lib\/.*$/, '\'');
70+
return fixer.replaceTextRange( node.arguments[ 0 ].range, replacingText ); // eslint-disable-line max-len
71+
}
5372
}
5473

5574
/**
@@ -83,9 +102,11 @@ function main( context ) {
83102

84103
rule = {
85104
'meta': {
105+
'type': 'suggestion',
86106
'docs': {
87107
'description': 'disallow require() calls into internals of other stdlib packages'
88108
},
109+
'fixable': 'code',
89110
'schema': []
90111
},
91112
'create': main

lib/node_modules/@stdlib/_tools/eslint/rules/no-internal-require/test/fixtures/invalid.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ test = {
3232
'message': 'require() call contains a path which loads a module internal to a stdlib package, and, thus, should be considered private',
3333
'type': 'CallExpression'
3434
}
35-
]
35+
],
36+
'output': [
37+
'// MODULES //',
38+
'',
39+
'var betainc = require( \'@stdlib/math/base/special/betainc\' );'
40+
].join( '\n' )
3641
};
3742
invalid.push( test );
3843

@@ -48,7 +53,13 @@ test = {
4853
'message': 'require() call contains a path which loads a module internal to a stdlib package, and, thus, should be considered private',
4954
'type': 'CallExpression'
5055
}
51-
]
56+
],
57+
'output': [
58+
'// MODULES //',
59+
'',
60+
'var betaincinv = require( \'@stdlib/math/base/special/betaincinv\' )',
61+
'var kernelBetainc = require( \'@stdlib/math/base/special/kernel-betainc\' )'
62+
].join( '\n' )
5263
};
5364
invalid.push( test );
5465

0 commit comments

Comments
 (0)