Skip to content

Commit

Permalink
[New] no-cycle: allow maxDepth option to be "∞"
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jun 22, 2020
1 parent 4d6c539 commit bfc50b7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
## [Unreleased]
### Added
- [`no-unused-modules`]: consider exported TypeScript interfaces, types and enums ([#1819], thanks [@nicolashenry])
- [`no-cycle`]: allow `maxDepth` option to be `"∞"` (thanks [@ljharb])

### Fixed
- [`order`]/TypeScript: properly support `import = object` expressions ([#1823], thanks [@manuth])
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-cycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Ensures that there is no resolvable path back to this module via its dependencies.

This includes cycles of depth 1 (imported module imports me) to `Infinity`, if the
This includes cycles of depth 1 (imported module imports me) to `"∞"` (or `Infinity`), if the
[`maxDepth`](#maxdepth) option is not set.

```js
Expand Down
18 changes: 13 additions & 5 deletions src/rules/no-cycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ module.exports = {
type: 'suggestion',
docs: { url: docsUrl('no-cycle') },
schema: [makeOptionsSchema({
maxDepth:{
description: 'maximum dependency depth to traverse',
type: 'integer',
minimum: 1,
maxDepth: {
oneOf: [
{
description: 'maximum dependency depth to traverse',
type: 'integer',
minimum: 1,
},
{
enum: ['∞'],
type: 'string',
},
],
},
ignoreExternal: {
description: 'ignore external modules',
Expand All @@ -32,7 +40,7 @@ module.exports = {
if (myPath === '<text>') return {} // can't cycle-check a non-file

const options = context.options[0] || {}
const maxDepth = options.maxDepth || Infinity
const maxDepth = typeof options.maxDepth === 'number' ? options.maxDepth : Infinity
const ignoreModule = (name) => options.ignoreExternal ? isExternalModule(name) : false

function checkSourceValue(sourceNode, importer) {
Expand Down
10 changes: 10 additions & 0 deletions tests/src/rules/no-cycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ ruleTester.run('no-cycle', rule, {
parser: require.resolve('babel-eslint'),
errors: [error(`Dependency cycle via ./flow-types-depth-two:4=>./depth-one:1`)],
}),
test({
code: 'import { foo } from "./depth-two"',
options: [{ maxDepth: Infinity }],
errors: [error(`Dependency cycle via ./depth-one:1`)],
}),
test({
code: 'import { foo } from "./depth-two"',
options: [{ maxDepth: '∞' }],
errors: [error(`Dependency cycle via ./depth-one:1`)],
}),
],
})
// })

0 comments on commit bfc50b7

Please sign in to comment.