-
-
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
317 additions
and
0 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,61 @@ | ||
# import/rename-default-import | ||
|
||
This rule will enforce a specific binding name for a default package import. Only ES6 imports are processed. | ||
|
||
|
||
## Rule Details | ||
|
||
Given: | ||
|
||
```js | ||
// ./foo.js | ||
export default function () { return 'Foo' } | ||
``` | ||
|
||
and | ||
|
||
```json | ||
// .eslintrc | ||
{ | ||
"rules": { | ||
"import/rename-default-import": [ | ||
"warn", { | ||
"prop-types": "PropTypes", // key: name of the module, value: desired binding for default import | ||
"./foo": "Foo" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
The following is considered valid: | ||
|
||
```js | ||
import Foo from './foo' | ||
|
||
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,93 @@ | ||
/** | ||
* @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 | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
|
||
function handleImport(context, specifier, source) { | ||
const {value: packageName} = source | ||
const {local: {name: importAlias}} = specifier | ||
const mappings = context.options[0] | ||
|
||
if (Object.keys(mappings).indexOf(packageName) === -1) { | ||
return | ||
} | ||
|
||
if (mappings[packageName] !== importAlias) { | ||
context.report({ | ||
node: source, | ||
message: `Default import from '${packageName}' should be aliased to ` | ||
+ `${mappings[packageName]}, not ${importAlias}`, | ||
fix: fixer => { | ||
let newAlias = mappings[packageName] | ||
if (specifier.imported && specifier.imported.name === 'default') { | ||
newAlias = `default as ${mappings[packageName]}` | ||
} | ||
|
||
return fixer.replaceText(specifier, newAlias) | ||
}, | ||
}) | ||
|
||
const declaredVariable = context.getDeclaredVariables(specifier)[0] | ||
for (const variableReference of declaredVariable.references) { | ||
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 => { | ||
return fixer.replaceText(variableReference.identifier, mappings[packageName]) | ||
}, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
|
||
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.options | ||
|
||
if (options.length === 0 || Object.keys(options[0]).length === 0) { | ||
return | ||
} | ||
|
||
for (const specifier of specifiers) { | ||
if (!isDefaultImport(specifier)) { | ||
continue | ||
} | ||
|
||
handleImport(context, specifier, source) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
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,158 @@ | ||
import { test } from '../utils' | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester(), | ||
rule = require('rules/rename-default-import') | ||
|
||
ruleTester.run('rename-default-import', rule, { | ||
valid: [ | ||
test({ | ||
code: `import PropTypes from 'prop-types';`, | ||
options: [], | ||
}), | ||
test({ | ||
code: `import PropTypes from 'prop-types';`, | ||
options: [{'foo': 'Foo'}], | ||
}), | ||
test({ | ||
code: `import PropTypes from 'prop-types';`, | ||
options: [{'prop-types': 'PropTypes'}], | ||
}), | ||
test({ | ||
code: `import PropTypes, {Foo} from 'prop-types';`, | ||
options: [{'prop-types': 'PropTypes'}], | ||
}), | ||
test({ | ||
code: `import {default as PropTypes} from 'prop-types';`, | ||
options: [{'prop-types': 'PropTypes'}], | ||
}), | ||
test({ | ||
code: `import {Foo} from 'prop-types';`, | ||
options: [{'prop-types': 'PropTypes'}], | ||
}), | ||
test({ | ||
code: `import * as PropTypes from 'prop-types';`, | ||
options: [{'prop-types': 'PropTypes'}] | ||
}), | ||
], | ||
invalid: [ | ||
test({ | ||
code: `import propTypes from 'prop-types';`, | ||
options: [{'prop-types': 'PropTypes'}], | ||
output: `import PropTypes from 'prop-types';`, | ||
errors: [{ | ||
ruleId: 'rename-default-import', | ||
message: `Default import from 'prop-types' should be aliased to PropTypes, not propTypes`, | ||
}], | ||
}), | ||
test({ | ||
code: `import propTypes, {B} from 'prop-types';`, | ||
options: [{'prop-types': 'PropTypes'}], | ||
output: `import PropTypes, {B} from 'prop-types';`, | ||
errors: [{ | ||
ruleId: 'rename-default-import', | ||
message: `Default import from 'prop-types' should be aliased to PropTypes, not propTypes`, | ||
}], | ||
}), | ||
test({ | ||
code: `import {default as propTypes} from 'prop-types';`, | ||
options: [{'prop-types': 'PropTypes'}], | ||
output: `import {default as PropTypes} from 'prop-types';`, | ||
errors: [{ | ||
ruleId: 'rename-default-import', | ||
message: `Default import from 'prop-types' should be aliased to PropTypes, not propTypes`, | ||
}], | ||
}), | ||
test({ | ||
code: `import propTypes from 'prop-types';import foo from 'foo';`, | ||
options: [{'prop-types': 'PropTypes', 'foo': 'Foo'}], | ||
output: `import PropTypes from 'prop-types';import Foo from 'foo';`, | ||
errors: [{ | ||
ruleId: 'rename-default-import', | ||
message: `Default import from 'prop-types' should be aliased to PropTypes, not propTypes`, | ||
}, { | ||
ruleId: 'rename-default-import', | ||
message: `Default import from 'foo' should be aliased to Foo, not foo`, | ||
}], | ||
}), | ||
test({ | ||
code: ` | ||
import propTypes from 'prop-types'; | ||
const obj = { | ||
foo: propTypes.string | ||
} | ||
`, | ||
options: [{'prop-types': 'PropTypes'}], | ||
output: ` | ||
import PropTypes from 'prop-types'; | ||
const obj = { | ||
foo: PropTypes.string | ||
} | ||
`, | ||
errors: [{ | ||
ruleId: 'rename-default-import', | ||
message: `Default import from 'prop-types' should be aliased to PropTypes, not propTypes`, | ||
}, { | ||
ruleId: 'rename-default-import', | ||
message: `Using incorrect binding name 'propTypes' instead of PropTypes for default import from package prop-types` | ||
}], | ||
}), | ||
test({ | ||
code: ` | ||
import foo from 'bar'; | ||
const a = foo.foo(); | ||
const b = bar(foo); | ||
const c = (foo) => { | ||
foo(); | ||
}; | ||
c(foo) | ||
const d = (bar) => { | ||
bar(); | ||
}; | ||
d(foo); | ||
const e = () => { | ||
foo(); | ||
}; | ||
`, | ||
options: [{'bar': 'Foo'}], | ||
output: ` | ||
import Foo from 'bar'; | ||
const a = Foo.foo(); | ||
const b = bar(Foo); | ||
const c = (foo) => { | ||
foo(); | ||
}; | ||
c(Foo) | ||
const d = (bar) => { | ||
bar(); | ||
}; | ||
d(Foo); | ||
const e = () => { | ||
Foo(); | ||
}; | ||
`, | ||
errors: [{ | ||
ruleId: 'rename-default-import', | ||
message: `Default import from 'bar' should be aliased to Foo, not foo`, | ||
}, { | ||
ruleId: 'rename-default-import', | ||
message: `Using incorrect binding name 'foo' instead of Foo for default import from package bar`, | ||
}, { | ||
ruleId: 'rename-default-import', | ||
message: `Using incorrect binding name 'foo' instead of Foo for default import from package bar`, | ||
}, { | ||
ruleId: 'rename-default-import', | ||
message: `Using incorrect binding name 'foo' instead of Foo for default import from package bar`, | ||
}, { | ||
ruleId: 'rename-default-import', | ||
message: `Using incorrect binding name 'foo' instead of Foo for default import from package bar`, | ||
}, { | ||
ruleId: 'rename-default-import', | ||
message: `Using incorrect binding name 'foo' instead of Foo for default import from package bar`, | ||
}] | ||
}) | ||
] | ||
}) |