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-cycle: Accept import typeof, like import type #2608

Merged
merged 1 commit into from
Dec 1, 2022
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [`no-unresolved`], [`extensions`]: ignore type only exports ([#2436], thanks [@Lukas-Kullmann])
- `ExportMap`: add missing param to function ([#2589], thanks [@Fdawgs])
- [`no-unused-modules`]: `checkPkgFieldObject` filters boolean fields from checks ([#2598], thanks [@mpint])
- [`no-cycle`]: accept Flow `typeof` imports, just like `type` ([#2608], thanks [@gnprice])

### Changed
- [Tests] [`named`]: Run all TypeScript test ([#2427], thanks [@ProdigySim])
Expand Down Expand Up @@ -1025,6 +1026,7 @@ for info on changes for earlier releases.

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

[#2608]: https://github.com/import-js/eslint-plugin-import/pull/2608
[#2605]: https://github.com/import-js/eslint-plugin-import/pull/2605
[#2602]: https://github.com/import-js/eslint-plugin-import/pull/2602
[#2598]: https://github.com/import-js/eslint-plugin-import/pull/2598
Expand Down Expand Up @@ -1626,6 +1628,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
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-cycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { b } from './dep-b.js' // reported: Dependency cycle detected.
This rule does _not_ detect imports that resolve directly to the linted module;
for that, see [`no-self-import`].

This rule ignores type-only imports in Flow and TypeScript syntax (`import type` and `import typeof`), which have no runtime effect.


## Rule Details

Expand Down
9 changes: 5 additions & 4 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -515,8 +515,9 @@ 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);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/files/cycles/flow-typeof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
import typeof Foo from './depth-zero';
import { typeof Bar } from './depth-zero';
import typeof { Bar } from './depth-zero';
4 changes: 4 additions & 0 deletions tests/src/rules/no-cycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down