Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignoreImports option to import-index rule #421

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'
}
fisker marked this conversation as resolved.
Show resolved Hide resolved
},
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(\'.\')',
fisker marked this conversation as resolved.
Show resolved Hide resolved
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