From 21bf8c665f15647f8fa9651b61a5332b8c26cd83 Mon Sep 17 00:00:00 2001 From: Maxim Malov Date: Sat, 5 Oct 2019 14:08:07 +0600 Subject: [PATCH] [Fix] `no-cycle`: should not warn for Flow imports --- CHANGELOG.md | 3 +++ src/ExportMap.js | 12 ++++++++++-- tests/files/cycles/flow-types-depth-one.js | 6 ++++++ tests/files/cycles/flow-types-depth-two.js | 1 + tests/files/cycles/flow-types.js | 6 ++++++ tests/src/rules/no-cycle.js | 9 +++++++++ 6 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/files/cycles/flow-types-depth-one.js create mode 100644 tests/files/cycles/flow-types-depth-two.js create mode 100644 tests/files/cycles/flow-types.js diff --git a/CHANGELOG.md b/CHANGELOG.md index dbbc991db..e6995d173 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel - [`prefer-default-export`]: fix false positive with type export ([#1506], thanks [@golopot]) - [`extensions`]: Fix `ignorePackages` to produce errors ([#1521], thanks [@saschanaz]) - [`no-unused-modules`]: fix crash due to `export *` ([#1496], thanks [@Taranys]) +- [`no-cycle`]: should not warn for Flow imports ([#1494], thanks [@maxmalov]) ### Docs - [`no-useless-path-segments`]: add docs for option `commonjs` ([#1507], thanks [@golopot]) @@ -629,6 +630,7 @@ for info on changes for earlier releases. [#1506]: https://github.com/benmosher/eslint-plugin-import/pull/1506 [#1496]: https://github.com/benmosher/eslint-plugin-import/pull/1496 [#1495]: https://github.com/benmosher/eslint-plugin-import/pull/1495 +[#1494]: https://github.com/benmosher/eslint-plugin-import/pull/1494 [#1472]: https://github.com/benmosher/eslint-plugin-import/pull/1472 [#1470]: https://github.com/benmosher/eslint-plugin-import/pull/1470 [#1436]: https://github.com/benmosher/eslint-plugin-import/pull/1436 @@ -1019,3 +1021,4 @@ for info on changes for earlier releases. [@saschanaz]: https://github.com/saschanaz [@brettz9]: https://github.com/brettz9 [@Taranys]: https://github.com/Taranys +[@maxmalov]: https://github.com/maxmalov diff --git a/src/ExportMap.js b/src/ExportMap.js index 8009f8b83..ba455e368 100644 --- a/src/ExportMap.js +++ b/src/ExportMap.js @@ -404,19 +404,27 @@ ExportMap.parse = function (path, content, context) { function captureDependency(declaration) { if (declaration.source == null) return null + if (declaration.importKind === 'type') return null // skip Flow type imports const importedSpecifiers = new Set() const supportedTypes = new Set(['ImportDefaultSpecifier', 'ImportNamespaceSpecifier']) + let hasImportedType = false if (declaration.specifiers) { declaration.specifiers.forEach(specifier => { - if (supportedTypes.has(specifier.type)) { + const isType = specifier.importKind === 'type' + hasImportedType = hasImportedType || isType + + if (supportedTypes.has(specifier.type) && !isType) { importedSpecifiers.add(specifier.type) } - if (specifier.type === 'ImportSpecifier') { + if (specifier.type === 'ImportSpecifier' && !isType) { importedSpecifiers.add(specifier.imported.name) } }) } + // only Flow types were imported + if (hasImportedType && importedSpecifiers.size === 0) return null + const p = remotePath(declaration.source.value) if (p == null) return null const existing = m.imports.get(p) diff --git a/tests/files/cycles/flow-types-depth-one.js b/tests/files/cycles/flow-types-depth-one.js new file mode 100644 index 000000000..f8a7a4b47 --- /dev/null +++ b/tests/files/cycles/flow-types-depth-one.js @@ -0,0 +1,6 @@ +// @flow + +import type { FooType } from './flow-types-depth-two'; +import { type BarType, bar } from './flow-types-depth-two'; + +export { bar } diff --git a/tests/files/cycles/flow-types-depth-two.js b/tests/files/cycles/flow-types-depth-two.js new file mode 100644 index 000000000..9058840ac --- /dev/null +++ b/tests/files/cycles/flow-types-depth-two.js @@ -0,0 +1 @@ +import { foo } from './depth-one' diff --git a/tests/files/cycles/flow-types.js b/tests/files/cycles/flow-types.js new file mode 100644 index 000000000..fbfb69f30 --- /dev/null +++ b/tests/files/cycles/flow-types.js @@ -0,0 +1,6 @@ +// @flow + +import type { FooType } from './flow-types-depth-two'; +import { type BarType } from './flow-types-depth-two'; + +export const bar = 1; diff --git a/tests/src/rules/no-cycle.js b/tests/src/rules/no-cycle.js index 18fe88af1..df1e6d143 100644 --- a/tests/src/rules/no-cycle.js +++ b/tests/src/rules/no-cycle.js @@ -53,6 +53,10 @@ ruleTester.run('no-cycle', rule, { code: 'import type { FooType, BarType } from "./depth-one"', parser: require.resolve('babel-eslint'), }), + test({ + code: 'import { bar } from "./flow-types"', + parser: require.resolve('babel-eslint'), + }), ], invalid: [ test({ @@ -120,6 +124,11 @@ ruleTester.run('no-cycle', rule, { errors: [error(`Dependency cycle via ./depth-two:1=>./depth-one:1`)], parser: require.resolve('babel-eslint'), }), + test({ + code: 'import { bar } from "./flow-types-depth-one"', + parser: require.resolve('babel-eslint'), + errors: [error(`Dependency cycle via ./flow-types-depth-two:4=>./depth-one:1`)], + }), ], }) // })