Skip to content

Commit ceea5dc

Browse files
committed
Add documentation for no-extraneous-dependencies
1 parent 8481315 commit ceea5dc

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
66
## [Unreleased]
77
### Added
88
- [`no-named-as-default-member`] to `warnings` canned config
9+
- add [`no-extraneous-dependencies`] rule
910

1011
## [1.5.0] - 2016-04-18
1112
### Added
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Forbid the use of extraneous packages
2+
3+
Forbid the import of external modules that are not declared in the `package.json`'s `dependencies` or `devDependencies`.
4+
The closest parent `package.json` will be used. If no `package.json` is found, the rule will not lint anything.
5+
6+
### Options
7+
8+
This rule supports the following options:
9+
10+
`devDependencies`: If set to `false`, then the rule will show an error when `devDependencies` are imported. Defaults to `true`.
11+
12+
You can set the options like this:
13+
14+
```js
15+
"import/no-extraneous-dependencies": ["error", {"devDependencies": false}]
16+
```
17+
18+
19+
## Rule Details
20+
21+
Given the following `package.json`:
22+
```json
23+
{
24+
"name": "my-project",
25+
"...": "...",
26+
"dependencies": {
27+
"builtin-modules": "^1.1.1",
28+
"lodash.cond": "^4.2.0",
29+
"lodash.find": "^4.2.0",
30+
"pkg-up": "^1.0.0"
31+
},
32+
"devDependencies": {
33+
"ava": "^0.13.0",
34+
"eslint": "^2.4.0",
35+
"eslint-plugin-ava": "^1.3.0",
36+
"xo": "^0.13.0"
37+
}
38+
}
39+
```
40+
41+
42+
## Fail
43+
44+
```js
45+
var _ = require('lodash');
46+
import _ from 'lodash';
47+
48+
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": false}] */
49+
import test from 'ava';
50+
var test = require('ava');
51+
```
52+
53+
54+
## Pass
55+
56+
```js
57+
// Builtin and internal modules are fine
58+
var path = require('path');
59+
var foo = require('./foo');
60+
61+
import test from 'ava';
62+
import find from 'lodash.find';
63+
```
64+
65+
66+
## When Not To Use It
67+
68+
If you do not have a `package.json` file in your project.

src/rules/no-extraneous-dependencies.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ import isStaticRequire from '../core/staticRequire'
55

66
function getDependencies(context) {
77
const filepath = pkgUp.sync(context.getFilename())
8-
if (!filepath) {
9-
return null
10-
}
8+
if (!filepath) {
9+
return null
10+
}
1111

12-
try {
13-
const packageContent = JSON.parse(fs.readFileSync(filepath, 'utf8'))
14-
return {
12+
try {
13+
const packageContent = JSON.parse(fs.readFileSync(filepath, 'utf8'))
14+
return {
1515
dependencies: packageContent.dependencies || {},
1616
devDependencies: packageContent.devDependencies || {},
1717
}
18-
} catch (e) {
19-
return null
20-
}
18+
} catch (e) {
19+
return null
20+
}
2121
}
2222

2323
function missingErrorMessage(packageName) {
24-
return `'${packageName}' is not listed in the project's dependencies. ` +
25-
`Run 'npm i -S ${packageName}' to add it`
24+
return `'${packageName}' is not listed in the project's dependencies. ` +
25+
`Run 'npm i -S ${packageName}' to add it`
2626
}
2727

2828
function devDepErrorMessage(packageName) {
29-
return `'${packageName}' is not listed in the project's dependencies, not devDependencies.`
29+
return `'${packageName}' is not listed in the project's dependencies, not devDependencies.`
3030
}
3131

3232
function reportIfMissing(context, deps, allowDevDeps, node, name) {

0 commit comments

Comments
 (0)