Skip to content

Commit

Permalink
[fix] named: ignore Flow import typeof and export type
Browse files Browse the repository at this point in the history
  • Loading branch information
loganfsmyth committed Apr 25, 2019
1 parent c17dd73 commit 4620185
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## [Unreleased]

- [`named`]: ignore Flow `typeof` imports and `type` exports ([#1345], thanks [@loganfsmyth])

## [2.17.2] - 2019-04-16

### Fixed
Expand Down Expand Up @@ -553,6 +555,7 @@ for info on changes for earlier releases.

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

[#1345]: https://github.com/benmosher/eslint-plugin-import/pull/1345
[#1331]: https://github.com/benmosher/eslint-plugin-import/pull/1331
[#1330]: https://github.com/benmosher/eslint-plugin-import/pull/1330
[#1320]: https://github.com/benmosher/eslint-plugin-import/pull/1320
Expand Down Expand Up @@ -887,3 +890,4 @@ for info on changes for earlier releases.
[@bradzacher]: https://github.com/bradzacher
[@feychenie]: https://github.com/feychenie
[@kiwka]: https://github.com/kiwka
[@loganfsmyth]: https://github.com/loganfsmyth
2 changes: 1 addition & 1 deletion docs/rules/named.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Note: for packages, the plugin will find exported names
from [`jsnext:main`], if present in `package.json`.
Redux's npm module includes this key, and thereby is lintable, for example.

A module path that is [ignored] or not [unambiguously an ES module] will not be reported when imported. Note that type imports, as used by [Flow], are always ignored.
A module path that is [ignored] or not [unambiguously an ES module] will not be reported when imported. Note that type imports and exports, as used by [Flow], are always ignored.

[ignored]: ../../README.md#importignore
[unambiguously an ES module]: https://github.com/bmeck/UnambiguousJavaScriptGrammar
Expand Down
9 changes: 6 additions & 3 deletions src/rules/named.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ module.exports = {

create: function (context) {
function checkSpecifiers(key, type, node) {
// ignore local exports and type imports
if (node.source == null || node.importKind === 'type') return
// ignore local exports and type imports/exports
if (node.source == null || node.importKind === 'type' ||
node.importKind === 'typeof' || node.exportKind === 'type') {
return
}

if (!node.specifiers
.some(function (im) { return im.type === type })) {
Expand All @@ -32,7 +35,7 @@ module.exports = {
if (im.type !== type) return

// ignore type imports
if (im.importKind === 'type') return
if (im.importKind === 'type' || im.importKind === 'typeof') return

const deepLookup = imports.hasDeep(im[key].name)

Expand Down
32 changes: 30 additions & 2 deletions tests/src/rules/named.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,45 @@ ruleTester.run('named', rule, {
test({ code: 'import { deepProp } from "./named-exports"' }),
test({ code: 'import { deepSparseElement } from "./named-exports"' }),

// should ignore imported flow types, even if they don’t exist
// should ignore imported/exported flow types, even if they don’t exist
test({
code: 'import type { MissingType } from "./flowtypes"',
parser: 'babel-eslint',
}),
test({
code: 'import typeof { MissingType } from "./flowtypes"',
parser: 'babel-eslint',
}),
test({
code: 'import type { MyOpaqueType } from "./flowtypes"',
parser: 'babel-eslint',
}),
test({
code: 'import { type MyOpaqueType, MyClass } from "./flowtypes"',
code: 'import typeof { MyOpaqueType } from "./flowtypes"',
parser: 'babel-eslint',
}),
test({
code: 'import { type MyOpaqueType, MyClass } from "./flowtypes"',
parser: 'babel-eslint',
}),
test({
code: 'import { typeof MyOpaqueType, MyClass } from "./flowtypes"',
parser: 'babel-eslint',
}),
test({
code: 'import typeof MissingType from "./flowtypes"',
parser: 'babel-eslint',
}),
test({
code: 'import typeof * as MissingType from "./flowtypes"',
parser: 'babel-eslint',
}),
test({
code: 'export type { MissingType } from "./flowtypes"',
parser: 'babel-eslint',
}),
test({
code: 'export type { MyOpaqueType } from "./flowtypes"',
parser: 'babel-eslint',
}),

Expand Down

0 comments on commit 4620185

Please sign in to comment.