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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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