From 4b975ee9297ed19aae44a87947a610713853dec8 Mon Sep 17 00:00:00 2001 From: Greg Price Date: Wed, 30 Nov 2022 13:45:00 -0800 Subject: [PATCH] [Fix] no-cycle: Accept `import typeof`, like `import type` Fixes #2607. --- CHANGELOG.md | 3 +++ src/ExportMap.js | 10 ++++++---- tests/files/cycles/flow-typeof.js | 4 ++++ tests/src/rules/no-cycle.js | 4 ++++ 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 tests/files/cycles/flow-typeof.js diff --git a/CHANGELOG.md b/CHANGELOG.md index f87541e9c0..d9975966d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange - [`order`]: require with member expression could not be fixed if alphabetize.order was used ([#2490], thanks [@msvab]) - [`order`]: leave more space in rankings for consecutive path groups ([#2506], thanks [@Pearce-Ropion]) - [`no-cycle`]: add ExportNamedDeclaration statements to dependencies ([#2511], thanks [@BenoitZugmeyer]) +- [`no-cycle`]: accept Flow `typeof` imports, just like `type` ([#2607], thanks [@gnprice]) - [`dynamic-import-chunkname`]: prevent false report on a valid webpack magic comment ([#2330], thanks [@mhmadhamster]) - [`export`]: do not error on TS export overloads ([#1590], thanks [@ljharb]) - [`no-unresolved`], [`extensions`]: ignore type only exports ([#2436], thanks [@Lukas-Kullmann]) @@ -1342,6 +1343,7 @@ for info on changes for earlier releases. [#211]: https://github.com/import-js/eslint-plugin-import/pull/211 [#164]: https://github.com/import-js/eslint-plugin-import/pull/164 [#157]: https://github.com/import-js/eslint-plugin-import/pull/157 +[#2607]: https://github.com/import-js/eslint-plugin-import/issues/2607 [#2444]: https://github.com/import-js/eslint-plugin-import/issues/2444 [#2412]: https://github.com/import-js/eslint-plugin-import/issues/2412 [#2392]: https://github.com/import-js/eslint-plugin-import/issues/2392 @@ -1624,6 +1626,7 @@ for info on changes for earlier releases. [@gavriguy]: https://github.com/gavriguy [@georeith]: https://github.com/georeith [@giodamelio]: https://github.com/giodamelio +[@gnprice]: https://github.com/gnprice [@golergka]: https://github.com/golergka [@golopot]: https://github.com/golopot [@GoodForOneFare]: https://github.com/GoodForOneFare diff --git a/src/ExportMap.js b/src/ExportMap.js index a39434bc94..b88ff262b8 100644 --- a/src/ExportMap.js +++ b/src/ExportMap.js @@ -502,8 +502,8 @@ ExportMap.parse = function (path, content, context) { } function captureDependencyWithSpecifiers(n) { - // import type { Foo } (TS and Flow) - const declarationIsType = n.importKind === 'type'; + // import type { Foo } (TS and Flow); import typeof { Foo } (Flow) + const declarationIsType = n.importKind === 'type' || n.importKind === 'typeof'; // import './foo' or import {} from './foo' (both 0 specifiers) is a side effect and // shouldn't be considered to be just importing types let specifiersOnlyImportingTypes = n.specifiers.length > 0; @@ -515,8 +515,10 @@ ExportMap.parse = function (path, content, context) { importedSpecifiers.add(specifier.type); } - // import { type Foo } (Flow) - specifiersOnlyImportingTypes = specifiersOnlyImportingTypes && specifier.importKind === 'type'; + // import { type Foo } (Flow); import { typeof Foo } (Flow) + specifiersOnlyImportingTypes = + specifiersOnlyImportingTypes + && (specifier.importKind === 'type' || specifier.importKind === 'typeof'); }); captureDependency(n, declarationIsType || specifiersOnlyImportingTypes, importedSpecifiers); } diff --git a/tests/files/cycles/flow-typeof.js b/tests/files/cycles/flow-typeof.js new file mode 100644 index 0000000000..7c63f9ab76 --- /dev/null +++ b/tests/files/cycles/flow-typeof.js @@ -0,0 +1,4 @@ +// @flow +import typeof Foo from './depth-zero'; +import { typeof Bar } from './depth-zero'; +import typeof { Bar } from './depth-zero'; diff --git a/tests/src/rules/no-cycle.js b/tests/src/rules/no-cycle.js index de0083f563..155f257b71 100644 --- a/tests/src/rules/no-cycle.js +++ b/tests/src/rules/no-cycle.js @@ -111,6 +111,10 @@ ruleTester.run('no-cycle', rule, { code: 'import { bar } from "./flow-types-only-importing-multiple-types"', parser: parsers.BABEL_OLD, }), + test({ + code: 'import { bar } from "./flow-typeof"', + parser: parsers.BABEL_OLD, + }), ), invalid: [].concat(