-
-
Notifications
You must be signed in to change notification settings - Fork 381
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6815b53
#211 - Add `no-direct-import` rule
rahgurung cdc9d76
fixed linting errors
rahgurung 79c8932
Merge branch 'master' into 211
rahgurung 764ff40
feedback commit
rahgurung 4cd9d7d
Merge branch 'master' into 211
rahgurung 296660d
removed old file
rahgurung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Whether to allow default imports or destructuring/named imports | ||
|
||
Sometimes a module contains unrelated functions like `util`, thus it is a good practice to enforce destructuring or named imports here, other times in modules like `path` it is good to do default import as they have similar functions, likely to be utilised. By default `path` and `chalk` are enforced to have default export and `util`, `lodash` and `underscore` are having named export. But you can easily override these properties by passing `false` for respective module. | ||
|
||
## 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'; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
'use strict'; | ||
const getDocsUrl = require('./utils/get-docs-url'); | ||
|
||
const declarationHandler = (context, node, options) => { | ||
const moduleName = getModuleName(node); | ||
const moduleImportType = getModuleImportType(node); | ||
|
||
for (const importStyle in options) { | ||
if ({}.hasOwnProperty.call(options, importStyle)) { | ||
for (const importFile in options[importStyle]) { | ||
if (importFile === moduleName && moduleImportType === 'namedImport' && importStyle === 'defaultExport' && options[importStyle][importFile] === true) { | ||
context.report({ | ||
node, | ||
message: `Do not make named import for ${moduleName}` | ||
}); | ||
} else if (importFile === moduleName && moduleImportType === 'defaultImport' && importStyle === 'namedExport' && options[importStyle][importFile] === true) { | ||
context.report({ | ||
node, | ||
message: `Do not make default import for ${moduleName}` | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
function getModuleName(node) { | ||
if (node.type === 'VariableDeclaration') { | ||
return node.declarations[0].init.arguments[0].value; | ||
} | ||
|
||
return node.source.value; | ||
} | ||
|
||
function getModuleImportType(node) { | ||
if (node.type === 'VariableDeclaration') { | ||
if (node.declarations[0].id.type === 'ObjectPattern') { | ||
return 'namedImport'; | ||
} | ||
|
||
return 'defaultImport'; | ||
} | ||
|
||
if (node.specifiers[0].type === 'ImportSpecifier') { | ||
return 'namedImport'; | ||
} | ||
|
||
return 'defaultImport'; | ||
} | ||
|
||
const create = context => { | ||
const options = { | ||
defaultExport: { | ||
path: true, | ||
chalk: true, | ||
...(context.options[0] && context.options[0].defaultExport) | ||
}, | ||
namedExport: { | ||
util: true, | ||
lodash: true, | ||
underscore: true, | ||
...(context.options[0] && context.options[0].namedExport) | ||
} | ||
}; | ||
return { | ||
VariableDeclaration: node => declarationHandler(context, node, options), | ||
ImportDeclaration: node => declarationHandler(context, node, options) | ||
}; | ||
}; | ||
|
||
const schema = [{ | ||
type: 'object', | ||
properties: { | ||
defaultExport: { | ||
type: 'object' | ||
}, | ||
namedExport: { | ||
type: 'object' | ||
} | ||
} | ||
}]; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocsUrl(__filename) | ||
}, | ||
fixable: 'code', | ||
schema | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/import-style'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
parserOptions: { | ||
ecmaVersion: 2019, | ||
sourceType: 'module' | ||
}, | ||
env: { | ||
es6: true | ||
} | ||
}); | ||
|
||
function buildError({moduleName, type}) { | ||
const error = { | ||
ruleId: 'import-style' | ||
}; | ||
|
||
if (type === 'defaultImport') { | ||
error.message = `Do not make default import for ${moduleName}`; | ||
return error; | ||
} | ||
|
||
error.message = `Do not make named import for ${moduleName}`; | ||
return error; | ||
} | ||
|
||
ruleTester.run('import-style', rule, { | ||
valid: [ | ||
{ | ||
code: 'const {promisify} = require(\'util\');', | ||
options: [ | ||
{ | ||
defaultExport: { | ||
path: false | ||
}, | ||
namedExport: { | ||
util: false | ||
} | ||
} | ||
] | ||
}, | ||
'import {promisify} from \'util\';', | ||
'const file = require(\'unrestricted\');', | ||
'import file from \'unrestricted\';', | ||
'import {promisify as foo} from \'util\';', | ||
'import {debuglog, promisify} from \'util\';', | ||
'const {promisify : foo} = require(\'util\');' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'const util = require(\'util\');', | ||
errors: [buildError({moduleName: 'util', type: 'defaultImport'})] | ||
}, | ||
{ | ||
code: 'import util from \'util\';', | ||
errors: [buildError({moduleName: 'util', type: 'defaultImport'})] | ||
}, | ||
{ | ||
code: 'import * as util from \'util\';', | ||
errors: [buildError({moduleName: 'util', type: 'defaultImport'})] | ||
}, | ||
{ | ||
code: 'import {something} from \'path\';', | ||
errors: [buildError({moduleName: 'path', type: 'namedImport'})] | ||
}, | ||
{ | ||
code: 'import util, {promisify} from \'util\';', | ||
errors: [buildError({moduleName: 'util', type: 'defaultImport'})] | ||
}, | ||
{ | ||
code: 'const foo = myFunction(\'util\')', | ||
errors: [buildError({moduleName: 'util', type: 'defaultImport'})] | ||
}, | ||
{ | ||
code: 'import {something} from \'restricted\';', | ||
options: [ | ||
{ | ||
defaultExport: { | ||
restricted: true | ||
} | ||
} | ||
], | ||
errors: [buildError({moduleName: 'restricted', type: 'namedImport'})] | ||
} | ||
] | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this is error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh! Thanks for the review. I was a little busy in academic work. Yeah, it shouldn't be error, I'll add the check to throw error only in case of
require()
.