Skip to content

Commit

Permalink
[fix] no-named-default: ignore Flow import type and typeof
Browse files Browse the repository at this point in the history
  • Loading branch information
christianvuerings committed Feb 1, 2021
1 parent a45661b commit 0709fa9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
4 changes: 3 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 ([], thanks [@christianvuerings])

### Changed
- [Generic Import Callback] Make callback for all imports once in rules ([#1237], thanks [@ljqx])
Expand Down Expand Up @@ -1334,4 +1335,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

0 comments on commit 0709fa9

Please sign in to comment.