Skip to content

Commit f3a51da

Browse files
committed
build: handle external scoped packages correctly
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 23bddcf commit f3a51da

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

lib/node_modules/@stdlib/_tools/eslint/rules/require-file-extensions/lib/main.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ function main( context ) {
102102
*/
103103
function validate( node ) {
104104
var requirePath;
105+
var slashIndex;
105106
var filename;
106107
var resolved;
107108
var parts;
@@ -111,11 +112,24 @@ function main( context ) {
111112
if ( node.callee.name === 'require' ) {
112113
requirePath = node.arguments[ 0 ].value;
113114
if ( isString( requirePath ) ) {
114-
filename = context.getFilename();
115-
dir = dirname( filename );
116115
if ( startsWith( requirePath, '.' ) ) {
117116
requirePath = path.resolve( dir, requirePath );
118117
}
118+
else if ( !startsWith( requirePath, '@stdlib' ) ) {
119+
// Allow simple package names without slashes (e.g., 'tape', 'glob'):
120+
if ( !contains( requirePath, '/' ) ) {
121+
return;
122+
}
123+
// Only skip top-level scoped packages ('@scope/pkg'); still enforce extensions on deeper imports like '@scope/pkg/file.js':
124+
if ( startsWith( requirePath, '@' ) ) {
125+
slashIndex = requirePath.indexOf( '/' );
126+
if ( slashIndex > 0 && requirePath.indexOf( '/', slashIndex + 1 ) === -1 ) {
127+
return;
128+
}
129+
}
130+
}
131+
filename = context.getFilename();
132+
dir = dirname( filename );
119133
resolved = tryResolve( requirePath, dir );
120134
parts = path.parse( requirePath );
121135

lib/node_modules/@stdlib/_tools/eslint/rules/require-file-extensions/test/fixtures/invalid.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ test = {
7676
};
7777
invalid.push( test );
7878

79+
test = {
80+
'code': [
81+
'// MODULES //',
82+
'',
83+
'var parser = require( \'@typescript-eslint/parser/dist/index\' );'
84+
].join( '\n' ),
85+
'errors': [
86+
{
87+
'message': 'require statement of file is missing a file extension. Value: @typescript-eslint/parser/dist/index',
88+
'type': 'CallExpression'
89+
}
90+
],
91+
'filename': resolve( './invalid.js' )
92+
};
93+
invalid.push( test );
94+
7995

8096
// EXPORTS //
8197

lib/node_modules/@stdlib/_tools/eslint/rules/require-file-extensions/test/fixtures/valid.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ test = {
9696
};
9797
valid.push( test );
9898

99+
test = {
100+
'code': [
101+
'// MODULES //',
102+
'',
103+
'var parser = require( \'@typescript-eslint/parser\' );'
104+
].join( '\n' ),
105+
'filename': resolve( './valid.js' )
106+
};
107+
valid.push( test );
108+
99109

100110
// EXPORTS //
101111

0 commit comments

Comments
 (0)