Skip to content

Commit

Permalink
Add ignoreImports option to import-index rule (#421)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
maximelkin and sindresorhus committed Nov 19, 2019
1 parent 9e0db0b commit bda7769
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 4 deletions.
22 changes: 22 additions & 0 deletions docs/rules/import-index.md
Expand Up @@ -61,3 +61,25 @@ const m = require('@foo/bar');
```js
import m from '.';
```


## Options

### ignoreImports

Type: `boolean`\
Default: `false`

Don't check `import` statements.

Can be useful if you're using native `import` in Node.js where the filename and extension is required.

```js
// eslint unicorn/import-index: ["error", {"ignoreImports": true}]
import m from './index'; // Passes
```

```js
// eslint unicorn/import-index: ["error", {"ignoreImports": false}]
import m from './index'; // Fails
```
25 changes: 22 additions & 3 deletions rules/import-index.js
Expand Up @@ -16,19 +16,38 @@ const importIndex = (context, node, argument) => {
};

const create = context => {
return {
'CallExpression[callee.name="require"]': node => importIndex(context, node, node.arguments[0]),
ImportDeclaration: node => importIndex(context, node, node.source)
const options = context.options[0] || {};

const rules = {
'CallExpression[callee.name="require"]': node => importIndex(context, node, node.arguments[0])
};

if (!options.ignoreImports) {
rules.ImportDeclaration = node => importIndex(context, node, node.source);
}

return rules;
};

const schema = [{
type: 'object',
properties: {
ignoreImports: {
type: 'boolean',
default: false
}
},
additionalProperties: false
}];

module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
url: getDocumentationUrl(__filename)
},
schema,
fixable: 'code'
}
};
58 changes: 57 additions & 1 deletion test/import-index.js
Expand Up @@ -16,10 +16,18 @@ const error = {
message: 'Do not reference the index file directly.'
};

const ignoreImportsOptions = [{
ignoreImports: true
}];

ruleTester.run('import-index', rule, {
valid: [
'const m = require()',
'const m = require(\'.\')',
{
code: 'const m = require(\'.\')',
options: ignoreImportsOptions
},
'const m = require(\'..\')',
'const m = require(\'../..\')',
'const m = require(\'./foobar\')',
Expand All @@ -38,11 +46,47 @@ ruleTester.run('import-index', rule, {
'import m from \'foobar\'',
'import m from \'index\'',
'import m from \'index.js\'',
{
code: 'import m from \'index.js\'',
options: ignoreImportsOptions
},
'import m from \'indexbar\'',
'import m from \'fooindex\'',
'import m from \'@index/foo\'',
'import m from \'@foo/index\'',
'import m from \'@foo/index.js\''
'import m from \'@foo/index.js\'',
{
code: 'import m from \'./\'',
options: ignoreImportsOptions
},
{
code: 'import m from \'./index\'',
options: ignoreImportsOptions
},
{
code: 'import m from \'./index.js\'',
options: ignoreImportsOptions
},
{
code: 'import m from \'../../index\'',
options: ignoreImportsOptions
},
{
code: 'import m from \'./foo/index.js\'',
options: ignoreImportsOptions
},
{
code: 'import m from \'./foobar/\'',
options: ignoreImportsOptions
},
{
code: 'import m from \'@foo/index/index\'',
options: ignoreImportsOptions
},
{
code: 'import m from \'@foo/index/index.js\'',
options: ignoreImportsOptions
}
],
invalid: [
{
Expand All @@ -60,11 +104,23 @@ ruleTester.run('import-index', rule, {
errors: [error],
output: 'const m = require(\'.\')'
},
{
code: 'const m = require(\'./index\')',
errors: [error],
output: 'const m = require(\'.\')',
options: ignoreImportsOptions
},
{
code: 'const m = require(\'./index.js\')',
errors: [error],
output: 'const m = require(\'.\')'
},
{
code: 'const m = require(\'./index.js\')',
errors: [error],
output: 'const m = require(\'.\')',
options: ignoreImportsOptions
},
{
code: 'const m = require(\'../../index.js\')',
errors: [error],
Expand Down

0 comments on commit bda7769

Please sign in to comment.