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 import-style rule #232

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/rules/no-direct-import.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Do not import modules directly

Do not import the core Node.js modules directly.
rahgurung marked this conversation as resolved.
Show resolved Hide resolved

## Fail

```js
const util = require('util');

import util from 'util';

import * as util from 'util';
```


## Pass

```js
const {promisify} = require('util');

import {promisify} from 'util';
```
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = {
'unicorn/prefer-add-event-listener': 'error',
'unicorn/prefer-exponentiation-operator': 'error',
'unicorn/no-console-spaces': 'error',
'unicorn/no-direct-import': 'error',
rahgurung marked this conversation as resolved.
Show resolved Hide resolved
'unicorn/no-unreadable-array-destructuring': 'error',
'unicorn/no-unused-properties': 'off',
'unicorn/prefer-node-append': 'error',
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Configure it in `package.json`.
"unicorn/no-unsafe-regex": "off",
"unicorn/prefer-add-event-listener": "error",
"unicorn/no-console-spaces": "error",
"unicorn/no-direct-import": "error",
rahgurung marked this conversation as resolved.
Show resolved Hide resolved
"unicorn/no-unreadable-array-destructuring": "error",
"unicorn/no-unused-properties": "off",
"unicorn/prefer-node-append": "error",
Expand Down Expand Up @@ -104,6 +105,7 @@ Configure it in `package.json`.
- [prefer-add-event-listener](docs/rules/prefer-add-event-listener.md) - Prefer `addEventListener` over `on`-functions. *(fixable)*
- [prefer-exponentiation-operator](docs/rules/prefer-exponentiation-operator.md) - Prefer the exponentiation operator over `Math.pow()` *(fixable)*
- [no-console-spaces](docs/rules/no-console-spaces.md) - Do not use leading/trailing space between `console.log` parameters. *(fixable)*
- [no-direct-import](docs/rules/no-direct-import.md) - Do not import the core Node.js modules directly.
rahgurung marked this conversation as resolved.
Show resolved Hide resolved
- [no-unreadable-array-destructuring](docs/rules/no-unreadable-array-destructuring.md) - Disallow unreadable array destructuring.
- [no-unused-properties](docs/rules/no-unused-properties.md) - Disallow unused object properties.
- [prefer-node-append](docs/rules/prefer-node-append.md) - Prefer `append` over `appendChild`. *(fixable)*
Expand Down
77 changes: 77 additions & 0 deletions rules/no-direct-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';
const getDocsUrl = require('./utils/get-docs-url');

const defaults = [
'lodash',
'underscore',
'util'
];

// This part handles the VariableDeclarator
const isObjectPattern = node => node.id.type === 'ObjectPattern';

const isImportingDirect = callExpression => {
if (callExpression.callee.name === 'require') {
if (defaults.includes(callExpression.arguments[0].value)) {
return true;
}

return false;
}

return false;
};

const importDirectVariable = (context, node, callExpression) => {
if (isObjectPattern(node)) {
return true;
}

if (isImportingDirect(callExpression)) {
context.report({
node,
message: 'Do not reference directly'
});
}
};

// This part handles the ImportSpecifiers
const isImportSpecifier = node => node.specifiers[0].type === 'ImportSpecifier';

const isImportRestricted = node => {
if (defaults.includes(node.source.value)) {
return true;
}

return false;
};

const importDirectDeclaration = (context, node) => {
if (isImportSpecifier(node)) {
return true;
}

if (isImportRestricted(node)) {
context.report({
node,
message: 'Do not reference directly'
});
}
};

const create = context => {
return {
VariableDeclarator: node => importDirectVariable(context, node, node.init),
ImportDeclaration: node => importDirectDeclaration(context, node)
};
};

module.exports = {
create,
meta: {
docs: {
url: getDocsUrl(__filename)
},
fixable: 'code'
}
};
38 changes: 38 additions & 0 deletions test/no-direct-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import rule from '../rules/no-direct-import';

const ruleTester = avaRuleTester(test, {
parserOptions: {
sourceType: 'module'
}
});

const error = {
ruleId: 'no-direct-import',
message: 'Do not reference directly'
};

ruleTester.run('no-direct-import', rule, {
valid: [
'const {promisify} = require(\'util\');',
'import {promisify} from \'util\';',
'const file = require(\'unrestricted\')',
'import file from \'unrestricted\'',
'const util = myFunction(\'util\')'
],
invalid: [
{
code: 'const util = require(\'util\');',
errors: [error]
},
{
code: 'import util from \'util\';',
errors: [error]
},
{
code: 'import * as util from \'util\';',
errors: [error]
}
]
});
rahgurung marked this conversation as resolved.
Show resolved Hide resolved