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

[Fix] no-named-default: ignore Flow import type and typeof #1983

Merged
merged 1 commit into from Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Expand Up @@ -23,6 +23,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`first`]: fix handling of `import = require` ([#1963], thanks [@MatthiasKunnen])
- [`no-cycle`]/[`extensions`]: fix isExternalModule usage ([#1696], thanks [@paztis])
- [`extensions`]/[`no-cycle`]/[`no-extraneous-dependencies`]: Correct module real path resolution ([#1696], thanks [@paztis])
- [`no-named-default`]: ignore Flow import type and typeof ([#1983], thanks [@christianvuerings])

### Changed
- [Generic Import Callback] Make callback for all imports once in rules ([#1237], thanks [@ljqx])
Expand Down Expand Up @@ -757,6 +758,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#1983]: https://github.com/benmosher/eslint-plugin-import/pull/1983
[#1974]: https://github.com/benmosher/eslint-plugin-import/pull/1974
[#1958]: https://github.com/benmosher/eslint-plugin-import/pull/1958
[#1948]: https://github.com/benmosher/eslint-plugin-import/pull/1948
Expand Down Expand Up @@ -1334,4 +1336,5 @@ for info on changes for earlier releases.
[@guillaumewuip]: https://github.com/guillaumewuip
[@tapayne88]: https://github.com/tapayne88
[@panrafal]: https://github.com/panrafal
[@ttmarek]: https://github.com/ttmarek
[@ttmarek]: https://github.com/ttmarek
[@christianvuerings]: https://github.com/christianvuerings
4 changes: 4 additions & 0 deletions docs/rules/no-named-default.md
Expand Up @@ -4,6 +4,10 @@ Reports use of a default export as a locally named import.

Rationale: the syntax exists to import default exports expressively, let's use it.

Note that type imports, as used by [Flow], are always ignored.

[Flow]: https://flow.org/

## Rule Details

Given:
Expand Down
4 changes: 4 additions & 0 deletions src/rules/no-named-default.js
Expand Up @@ -13,6 +13,10 @@ module.exports = {
return {
'ImportDeclaration': function (node) {
node.specifiers.forEach(function (im) {
if (im.importKind === 'type' || im.importKind === 'typeof') {
return;
}

if (im.type === 'ImportSpecifier' && im.imported.name === 'default') {
context.report({
node: im.local,
Expand Down
10 changes: 10 additions & 0 deletions tests/src/rules/no-named-default.js
Expand Up @@ -9,6 +9,16 @@ ruleTester.run('no-named-default', rule, {
test({ code: 'import bar from "./bar";' }),
test({ code: 'import bar, { foo } from "./bar";' }),

// Should ignore imported flow types
test({
code: 'import { type default as Foo } from "./bar";',
parser: require.resolve('babel-eslint'),
}),
test({
code: 'import { typeof default as Foo } from "./bar";',
parser: require.resolve('babel-eslint'),
}),

...SYNTAX_CASES,
],

Expand Down