-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule to enforce default import aliases
- Loading branch information
Showing
6 changed files
with
504 additions
and
2 deletions.
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
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,56 @@ | ||
# import/rename-default-import | ||
|
||
This rule will enforce a specific binding name for a default package import. | ||
Works for ES6 imports and CJS require. | ||
|
||
|
||
## Rule Details | ||
|
||
Given: | ||
|
||
There is a package `prop-types` with a default export | ||
|
||
and | ||
|
||
```json | ||
// .eslintrc | ||
{ | ||
"rules": { | ||
"import/rename-default-import": [ | ||
"warn", { | ||
"prop-types": "PropTypes", // key: name of the module, value: desired binding for default import | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
The following is considered valid: | ||
|
||
```js | ||
import {default as PropTypes} from 'prop-types' | ||
|
||
import PropTypes from 'prop-types' | ||
``` | ||
|
||
...and the following cases are reported: | ||
|
||
```js | ||
import propTypes from 'prop-types'; | ||
import {default as propTypes} from 'prop-types'; | ||
``` | ||
|
||
|
||
## When not to use it | ||
|
||
As long as you don't want to enforce specific naming for default imports. | ||
|
||
## Options | ||
|
||
This rule accepts an object which is a mapping | ||
between package name and the binding name that should be used for default imports. | ||
For example, a configuration like the one below | ||
|
||
`{'prop-types': 'PropTypes'}` | ||
|
||
specifies that default import for the package `prop-types` should be aliased to `PropTypes`. |
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,147 @@ | ||
/** | ||
* @fileoverview Rule to enforce aliases for default imports | ||
* @author Michał Kołodziejski | ||
*/ | ||
|
||
import docsUrl from '../docsUrl' | ||
|
||
|
||
function isDefaultImport(specifier) { | ||
if (specifier.type === 'ImportDefaultSpecifier') { | ||
return true | ||
} else if (specifier.type === 'ImportSpecifier' && specifier.imported.name === 'default') { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
function isCommonJSImport(declaration) { | ||
const variableInit = declaration.init | ||
if (variableInit.type === 'CallExpression') { | ||
return variableInit.callee.name === 'require' | ||
} | ||
return false | ||
} | ||
|
||
function handleImport(context, node, specifierOrDeclaration, packageName, importAlias) { | ||
const mappings = context.options[0] || {} | ||
const expectedAlias = mappings[packageName] | ||
|
||
if (expectedAlias === undefined || expectedAlias === importAlias) { | ||
return | ||
} | ||
|
||
let declaredVariables | ||
if (specifierOrDeclaration.type === 'VariableDeclarator') { | ||
declaredVariables = context.getDeclaredVariables(specifierOrDeclaration.parent)[0] | ||
} else { | ||
declaredVariables = context.getDeclaredVariables(specifierOrDeclaration)[0] | ||
} | ||
|
||
const references = declaredVariables.references | ||
const skipFixing = references.some((ref) => ref.identifier.parent.type === 'ExportSpecifier') | ||
|
||
context.report({ | ||
node: node, | ||
message: `Default import from '${packageName}' should be aliased to ` | ||
+ `${mappings[packageName]}, not ${importAlias}`, | ||
fix: skipFixing ? null : fixImportOrRequire(specifierOrDeclaration, mappings[packageName]), | ||
}) | ||
|
||
for (const variableReference of references) { | ||
if (specifierOrDeclaration.type === 'VariableDeclarator' && variableReference.init) { | ||
continue | ||
} | ||
|
||
context.report({ | ||
node: variableReference.identifier, | ||
message: `Using incorrect binding name '${variableReference.identifier.name}' ` | ||
+ `instead of ${mappings[packageName]} for ` | ||
+ `default import from package ${packageName}`, | ||
fix: fixer => { | ||
if (skipFixing) { | ||
return | ||
} | ||
|
||
return fixer.replaceText(variableReference.identifier, mappings[packageName]) | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
function fixImportOrRequire(node, text) { | ||
return function(fixer) { | ||
let newAlias = text | ||
let nodeOrToken | ||
if (node.type === 'VariableDeclarator') { | ||
nodeOrToken = node.id | ||
newAlias = text | ||
} else { | ||
nodeOrToken = node | ||
if (node.imported && node.imported.name === 'default') { | ||
newAlias = `default as ${text}` | ||
} else { | ||
newAlias = text | ||
} | ||
} | ||
|
||
return fixer.replaceText(nodeOrToken, newAlias) | ||
} | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: docsUrl('rename-default-import'), | ||
recommended: false, | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
}, | ||
], | ||
fixable: 'code', | ||
}, | ||
create: function(context) { | ||
return { | ||
'ImportDeclaration': function(node) { | ||
const {source, specifiers} = node | ||
const {options} = context | ||
|
||
if (options.length === 0 || Object.keys(options[0]).length === 0) { | ||
return | ||
} | ||
|
||
for (const specifier of specifiers) { | ||
if (!isDefaultImport(specifier)) { | ||
continue | ||
} | ||
|
||
handleImport(context, source, specifier, source.value, specifier.local.name) | ||
} | ||
}, | ||
'VariableDeclaration': function(node) { | ||
const {declarations} = node | ||
const {options} = context | ||
|
||
if (options.length === 0 || Object.keys(options[0]).length === 0) { | ||
return | ||
} | ||
|
||
for (const declaration of declarations) { | ||
if (!isCommonJSImport(declaration)) { | ||
continue | ||
} | ||
|
||
handleImport( | ||
context, | ||
node, | ||
declaration, | ||
declaration.init.arguments[0].value, | ||
declaration.id.name | ||
) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
Oops, something went wrong.