Skip to content

Commit 5a37196

Browse files
gnpriceljharb
authored andcommitted
[Fix] no-cycle: Accept import typeof, like import type
Fixes #2607.
1 parent 922819f commit 5a37196

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
3131
- [`no-unresolved`], [`extensions`]: ignore type only exports ([#2436], thanks [@Lukas-Kullmann])
3232
- `ExportMap`: add missing param to function ([#2589], thanks [@Fdawgs])
3333
- [`no-unused-modules`]: `checkPkgFieldObject` filters boolean fields from checks ([#2598], thanks [@mpint])
34+
- [`no-cycle`]: accept Flow `typeof` imports, just like `type` ([#2608], thanks [@gnprice])
3435

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

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

1029+
[#2608]: https://github.com/import-js/eslint-plugin-import/pull/2608
10281030
[#2605]: https://github.com/import-js/eslint-plugin-import/pull/2605
10291031
[#2602]: https://github.com/import-js/eslint-plugin-import/pull/2602
10301032
[#2598]: https://github.com/import-js/eslint-plugin-import/pull/2598
@@ -1626,6 +1628,7 @@ for info on changes for earlier releases.
16261628
[@gavriguy]: https://github.com/gavriguy
16271629
[@georeith]: https://github.com/georeith
16281630
[@giodamelio]: https://github.com/giodamelio
1631+
[@gnprice]: https://github.com/gnprice
16291632
[@golergka]: https://github.com/golergka
16301633
[@golopot]: https://github.com/golopot
16311634
[@GoodForOneFare]: https://github.com/GoodForOneFare

docs/rules/no-cycle.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { b } from './dep-b.js' // reported: Dependency cycle detected.
2222
This rule does _not_ detect imports that resolve directly to the linted module;
2323
for that, see [`no-self-import`].
2424

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

2628
## Rule Details
2729

src/ExportMap.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ ExportMap.parse = function (path, content, context) {
502502
}
503503

504504
function captureDependencyWithSpecifiers(n) {
505-
// import type { Foo } (TS and Flow)
506-
const declarationIsType = n.importKind === 'type';
505+
// import type { Foo } (TS and Flow); import typeof { Foo } (Flow)
506+
const declarationIsType = n.importKind === 'type' || n.importKind === 'typeof';
507507
// import './foo' or import {} from './foo' (both 0 specifiers) is a side effect and
508508
// shouldn't be considered to be just importing types
509509
let specifiersOnlyImportingTypes = n.specifiers.length > 0;
@@ -515,8 +515,9 @@ ExportMap.parse = function (path, content, context) {
515515
importedSpecifiers.add(specifier.type);
516516
}
517517

518-
// import { type Foo } (Flow)
519-
specifiersOnlyImportingTypes = specifiersOnlyImportingTypes && specifier.importKind === 'type';
518+
// import { type Foo } (Flow); import { typeof Foo } (Flow)
519+
specifiersOnlyImportingTypes = specifiersOnlyImportingTypes
520+
&& (specifier.importKind === 'type' || specifier.importKind === 'typeof');
520521
});
521522
captureDependency(n, declarationIsType || specifiersOnlyImportingTypes, importedSpecifiers);
522523
}

tests/files/cycles/flow-typeof.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @flow
2+
import typeof Foo from './depth-zero';
3+
import { typeof Bar } from './depth-zero';
4+
import typeof { Bar } from './depth-zero';

tests/src/rules/no-cycle.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ ruleTester.run('no-cycle', rule, {
111111
code: 'import { bar } from "./flow-types-only-importing-multiple-types"',
112112
parser: parsers.BABEL_OLD,
113113
}),
114+
test({
115+
code: 'import { bar } from "./flow-typeof"',
116+
parser: parsers.BABEL_OLD,
117+
}),
114118
),
115119

116120
invalid: [].concat(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy