Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# import/max-dependencies | ||
|
||
Forbid importing into nested folders of a module. | ||
|
||
This is a useful rule as import from packages you should stick to the public API (which is typically exported from the root of the package). Depending on specific folders in a package that are not public API exposes you to a greater potential of breaking changes. | ||
|
||
### Options | ||
|
||
This rule takes the following options: | ||
|
||
#### `max` | ||
`max`: The maximum number of nested directories allowed. Anything over will trigger the rule. **Default is 0** if the rule is enabled and no `max` is specified. | ||
|
||
You can set the option like this: | ||
|
||
```js | ||
"import/max-depth": ["error", {"max": 2}] | ||
``` | ||
|
||
#### `overridePackages` | ||
`overridePackages`: This allows you to override this rule for specific packages. This can be useful if the packages public API is expected to be imported from nested directories such as `lodash`. This options is declared as an object with the key being set to the name of the package and the value is set to the `max` for that package only. | ||
|
||
You can set the option like this: | ||
|
||
```js | ||
"import/max-depth": ["error", {"overridePackages": {'lodash': 2}}] | ||
``` | ||
|
||
## Example | ||
|
||
Given a max value of `{"max": 0}`: | ||
|
||
### Fail | ||
|
||
```js | ||
import map from "lodash/map" | ||
const map = require("lodash/map") | ||
``` | ||
|
||
### Pass | ||
|
||
```js | ||
import "lodash" | ||
import _ from "lodash" | ||
const _ = require("lodash") | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't care how about imports deeply nested into packages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import isStaticRequire from '../core/staticRequire' | ||
import docsUrl from '../docsUrl' | ||
|
||
const DEFAULT_MAX = 0 | ||
|
||
const createError = (sourcePath, maxDepth, actualDepth) => ( | ||
`Import '${sourcePath}' exceeds max nesting depth of ${maxDepth} (actual: ${actualDepth}).` | ||
) | ||
|
||
const checkDepth = (sourceValue, node, context) => { | ||
const {max = DEFAULT_MAX, overridePackages = {}} = context.options[0] || { } | ||
let packageMax = max | ||
|
||
if (/^\./.test(sourceValue)) { | ||
// skip relative paths | ||
return | ||
} | ||
|
||
let [pkgName, ...dirs] = sourceValue.split('/') | ||
|
||
// update variables for scoped packages | ||
if (/^@/.test(pkgName)) { | ||
pkgName = `${pkgName}/${dirs[0]}` | ||
dirs = dirs.slice(1) | ||
} | ||
|
||
if (pkgName in overridePackages) { | ||
packageMax = overridePackages[pkgName] | ||
} | ||
|
||
if (dirs.length > packageMax) { | ||
context.report({ | ||
node, | ||
message: createError(sourceValue, packageMax, dirs.length), | ||
}) | ||
} | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: docsUrl('max-depth'), | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
overridePackages: { | ||
type: 'object', | ||
patternProperties: { | ||
'^[a-zA-Z-_@/]+$': { | ||
type: 'integer', | ||
minimum: 0, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
max: { | ||
type: 'integer', | ||
minimum: 0, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
|
||
create: context => { | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
checkDepth(node.source.value, node.source, context) | ||
}, | ||
CallExpression(node) { | ||
if (isStaticRequire(node)) { | ||
const [ requirePath ] = node.arguments | ||
checkDepth(requirePath.value, node, context) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { test } from '../utils' | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester() | ||
, rule = require('rules/max-depth') | ||
|
||
ruleTester.run('max-depth', rule, { | ||
valid: [ | ||
test({code: 'import "./foo.js"'}), | ||
test({code: 'import "../bar/baz.js"'}), | ||
test({code: 'import a from "./foo.js"'}), | ||
test({code: 'import b from "../bar/baz.js"'}), | ||
test({code: 'const a = require("./foo.js")'}), | ||
test({code: 'const b = require("../bar/baz.js")'}), | ||
test({code: 'import "@scope/foo"'}), | ||
test({code: 'import "@scope/bar-baz"'}), | ||
test({code: 'import "lodash"'}), | ||
test({code: 'import _ from "lodash"'}), | ||
test({code: 'const _ = require("lodash")'}), | ||
test({ | ||
code: 'import map from "lodash/map"', | ||
options: [{ | ||
overridePackages: {'lodash': 1}, | ||
}], | ||
}), | ||
test({ | ||
code: 'import map from "lodash/map"', | ||
options: [{ | ||
max: 1, | ||
}], | ||
}), | ||
], | ||
invalid: [ | ||
test({ | ||
code: 'import map from "lodash/map"', | ||
errors: [ | ||
'Import \'lodash/map\' exceeds max nesting depth of 0 (actual: 1).', | ||
], | ||
}), | ||
test({ | ||
code: 'import map from "lodash/fp/map"', | ||
options: [{ | ||
max: 1, | ||
}], | ||
errors: [ | ||
'Import \'lodash/fp/map\' exceeds max nesting depth of 1 (actual: 2).', | ||
], | ||
}), | ||
test({ | ||
code: 'import map from "lodash/fp/map"', | ||
options: [{ | ||
overridePackages: {'lodash': 1}, | ||
}], | ||
errors: [ | ||
'Import \'lodash/fp/map\' exceeds max nesting depth of 1 (actual: 2).', | ||
], | ||
}), | ||
test({ | ||
code: 'import "@scope/foo/file"', | ||
errors: [ | ||
'Import \'@scope/foo/file\' exceeds max nesting depth of 0 (actual: 1).', | ||
], | ||
}), | ||
test({ | ||
code: 'import "@scope/bar-baz/nested/directory"', | ||
errors: [ | ||
'Import \'@scope/bar-baz/nested/directory\' exceeds max nesting depth of 0 (actual: 2).', | ||
], | ||
}), | ||
], | ||
}) |