Skip to content

Commit

Permalink
Ensure @scoped-packages are ignored in no-import-test-files
Browse files Browse the repository at this point in the history
Only attempt to classify imports that are file paths. Fixes #253.
  • Loading branch information
novemberborn committed Jun 2, 2019
1 parent c8ddcc3 commit 67c27b4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
9 changes: 3 additions & 6 deletions rules/no-import-test-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
const path = require('path');
const util = require('../util');

const externalModuleRegExp = /^\w/;
function isExternalModule(name) {
return externalModuleRegExp.test(name);
}
// Assume absolute paths can be classified by AVA.
const isFileImport = name => path.isAbsolute(name) || name.startsWith('./') || name.startsWith('../');

const create = context => {
const filename = context.getFilename();
Expand All @@ -24,8 +22,7 @@ const create = context => {
return;
}

const isImportingExternalModule = isExternalModule(importPath);
if (isImportingExternalModule) {
if (!isFileImport(importPath)) {
return;
}

Expand Down
11 changes: 11 additions & 0 deletions test/no-import-test-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ util.loadAvaHelper = () => ({
switch (importPath) {
case toPath('lib/foo.test.js'):
return {isHelper: false, isSource: false, isTest: true};
case toPath('../foo.test.js'):
return {isHelper: false, isSource: false, isTest: true};
case toPath('@foo/bar'): // Regression test for https://github.com/avajs/eslint-plugin-ava/issues/253
return {isHelper: false, isSource: false, isTest: true};
default:
return {isHelper: false, isSource: false, isTest: false};
}
Expand All @@ -36,6 +40,8 @@ const errors = [
ruleTester.run('no-import-test-files', rule, {
valid: [
'import test from \'ava\';',
'import foo from \'@foo/bar\';',
'import foo from \'/foo/bar\';', // Classfied as not a test.
'const test = require(\'ava\');',
'console.log()',
'const value = require(somePath);',
Expand All @@ -52,6 +58,11 @@ ruleTester.run('no-import-test-files', rule, {
code: 'const test = require(\'./foo.test.js\');',
filename: toPath('lib/foo.js'),
errors
},
{
code: 'const test = require(\'../foo.test.js\');',
filename: toPath('foo.js'),
errors
}
]
});

0 comments on commit 67c27b4

Please sign in to comment.