Skip to content

Commit 1c8ca97

Browse files
GMartignysindresorhus
authored andcommitted
Prevent the no-import-test rule to crash when imported path is not a string (#228)
1 parent 9ec0565 commit 1c8ca97

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

rules/no-import-test-files.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ function getProjectInfo() {
2828

2929
function createImportValidator(context, files, projectInfo, filename) {
3030
return (node, importPath) => {
31+
if (!importPath || typeof importPath !== 'string') {
32+
return;
33+
}
34+
3135
const isImportingExternalModule = isExternalModule(importPath);
3236
if (isImportingExternalModule) {
3337
return;
@@ -70,7 +74,7 @@ const create = context => {
7074
return;
7175
}
7276

73-
if (node.arguments[0] && node.arguments[0].type === 'Literal') {
77+
if (node.arguments[0]) {
7478
validateImportPath(node, node.arguments[0].value);
7579
}
7680
}

test/no-import-test-files.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ ruleTester.run('no-import-test-files', rule, {
3333
{
3434
code: 'var highlight = require(\'highlight.js\')',
3535
filename: toPath('test/index.js')
36-
}
36+
},
37+
'const value = require(true);',
38+
'const value = require();'
3739
],
3840
invalid: [
3941
{

0 commit comments

Comments
 (0)