-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
no-extraneous-dependencies
: add support/option for `peerDependencie…
…s` (#423)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Forbid the use of extraneous packages | ||
|
||
Forbid the import of external modules that are not declared in the `package.json`'s `dependencies` or `devDependencies`. | ||
Forbid the import of external modules that are not declared in the `package.json`'s `dependencies`, `devDependencies`, `optionalDependencies` or `peerDependencies`. | ||
The closest parent `package.json` will be used. If no `package.json` is found, the rule will not lint anything. | ||
|
||
### Options | ||
|
@@ -9,11 +9,12 @@ This rule supports the following options: | |
|
||
`devDependencies`: If set to `false`, then the rule will show an error when `devDependencies` are imported. Defaults to `true`. | ||
`optionalDependencies`: If set to `false`, then the rule will show an error when `optionalDependencies` are imported. Defaults to `true`. | ||
`peerDependencies`: If set to `false`, then the rule will show an error when `peerDependencies` are imported. Defaults to `false`. | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jfmengels
Author
Collaborator
|
||
|
||
You can set the options like this: | ||
|
||
```js | ||
"import/no-extraneous-dependencies": ["error", {"devDependencies": false, "optionalDependencies": false}] | ||
"import/no-extraneous-dependencies": ["error", {"devDependencies": false, "optionalDependencies": false, "peerDependencies": false}] | ||
``` | ||
|
||
|
||
|
@@ -38,6 +39,9 @@ Given the following `package.json`: | |
}, | ||
"optionalDependencies": { | ||
"lodash.isarray": "^4.0.0" | ||
}, | ||
"peerDependencies": { | ||
"react": ">=15.0.0 <16.0.0" | ||
} | ||
} | ||
``` | ||
|
@@ -49,6 +53,8 @@ Given the following `package.json`: | |
var _ = require('lodash'); | ||
import _ from 'lodash'; | ||
|
||
import react from 'react'; | ||
|
||
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": false}] */ | ||
import test from 'ava'; | ||
var test = require('ava'); | ||
|
@@ -69,6 +75,9 @@ var foo = require('./foo'); | |
import test from 'ava'; | ||
import find from 'lodash.find'; | ||
import find from 'lodash.isarray'; | ||
|
||
/* eslint import/no-extraneous-dependencies: ["error", {"peerDependencies": true}] */ | ||
import react from 'react'; | ||
``` | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ function getDependencies(context) { | |
dependencies: packageContent.dependencies || {}, | ||
devDependencies: packageContent.devDependencies || {}, | ||
optionalDependencies: packageContent.optionalDependencies || {}, | ||
peerDependencies: packageContent.peerDependencies || {}, | ||
} | ||
} catch (e) { | ||
return null | ||
|
@@ -35,7 +36,7 @@ function optDepErrorMessage(packageName) { | |
`not optionalDependencies.` | ||
} | ||
|
||
function reportIfMissing(context, deps, allowDevDeps, allowOptDeps, node, name) { | ||
function reportIfMissing(context, deps, depsOptions, node, name) { | ||
if (importType(name, context) !== 'external') { | ||
return | ||
} | ||
|
@@ -47,20 +48,22 @@ function reportIfMissing(context, deps, allowDevDeps, allowOptDeps, node, name) | |
const isInDeps = deps.dependencies[packageName] !== undefined | ||
const isInDevDeps = deps.devDependencies[packageName] !== undefined | ||
const isInOptDeps = deps.optionalDependencies[packageName] !== undefined | ||
const isInPeerDeps = deps.peerDependencies[packageName] === undefined | ||
This comment has been minimized.
Sorry, something went wrong.
benmosher
Member
|
||
|
||
if (isInDeps || | ||
(allowDevDeps && isInDevDeps) || | ||
(allowOptDeps && isInOptDeps) | ||
(depsOptions.allowDevDeps && isInDevDeps) || | ||
(depsOptions.allowPeerDeps && isInPeerDeps) || | ||
(depsOptions.allowOptDeps && isInOptDeps) | ||
) { | ||
return | ||
} | ||
|
||
if (isInDevDeps && !allowDevDeps) { | ||
if (isInDevDeps && !depsOptions.allowDevDeps) { | ||
context.report(node, devDepErrorMessage(packageName)) | ||
return | ||
} | ||
|
||
if (isInOptDeps && !allowOptDeps) { | ||
if (isInOptDeps && !depsOptions.allowOptDeps) { | ||
context.report(node, optDepErrorMessage(packageName)) | ||
return | ||
} | ||
|
@@ -70,22 +73,26 @@ function reportIfMissing(context, deps, allowDevDeps, allowOptDeps, node, name) | |
|
||
module.exports = function (context) { | ||
const options = context.options[0] || {} | ||
const allowDevDeps = options.devDependencies !== false | ||
const allowOptDeps = options.optionalDependencies !== false | ||
const deps = getDependencies(context) | ||
|
||
if (!deps) { | ||
return {} | ||
} | ||
|
||
const depsOptions = { | ||
allowDevDeps: options.devDependencies !== false, | ||
allowOptDeps: options.optionalDependencies !== false, | ||
allowPeerDeps: options.peerDependencies === true, | ||
} | ||
|
||
// todo: use module visitor from module-utils core | ||
return { | ||
ImportDeclaration: function (node) { | ||
reportIfMissing(context, deps, allowDevDeps, allowOptDeps, node, node.source.value) | ||
reportIfMissing(context, deps, depsOptions, node, node.source.value) | ||
}, | ||
CallExpression: function handleRequires(node) { | ||
if (isStaticRequire(node)) { | ||
reportIfMissing(context, deps, allowDevDeps, allowOptDeps, node, node.arguments[0].value) | ||
reportIfMissing(context, deps, depsOptions, node, node.arguments[0].value) | ||
} | ||
}, | ||
} | ||
|
@@ -97,6 +104,7 @@ module.exports.schema = [ | |
'properties': { | ||
'devDependencies': { 'type': 'boolean' }, | ||
'optionalDependencies': { 'type': 'boolean' }, | ||
'peerDependencies': { 'type': 'boolean' }, | ||
}, | ||
'additionalProperties': false, | ||
}, | ||
|
I'm having second thoughts on defaulting this to
false
, since the other two are defaulted totrue
. Probably makes sense to default totrue
for consistency.What do you think?