Skip to content

Commit

Permalink
import-index rule add ignoreImports option
Browse files Browse the repository at this point in the history
  • Loading branch information
maxeljkin committed Oct 16, 2019
1 parent dbf4b79 commit a972f5b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
13 changes: 13 additions & 0 deletions docs/rules/import-index.md
Expand Up @@ -61,3 +61,16 @@ const m = require('@foo/bar');
```js
import m from '.';
```


### `ignoreImports`

The default behavior is to check for imports. If you want to disable this (for example, you are moving to nodejs esm), set `ignoreImports` to `true`:

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

// eslint unicorn/import-index: ["error", {"ignoreImports": false}]
import m from './index'; // fails
```
24 changes: 21 additions & 3 deletions rules/import-index.js
Expand Up @@ -16,19 +16,37 @@ 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'
}
},
additionalProperties: false
}];

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

const ignoreImportsOptions = [{
ignoreImports: true
}];

ruleTester.run('import-index', rule, {
valid: [
'const m = require(\'.\')',
{
code: 'const m = require(\'.\')',
options: ignoreImportsOptions
},
'const m = require(\'..\')',
'const m = require(\'../..\')',
'const m = require(\'./foobar\')',
Expand All @@ -37,11 +45,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 Down

0 comments on commit a972f5b

Please sign in to comment.