Skip to content

Commit

Permalink
Add documentation for no-extraneous-dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Apr 18, 2016
1 parent 8481315 commit ceea5dc
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
## [Unreleased]
### Added
- [`no-named-as-default-member`] to `warnings` canned config
- add [`no-extraneous-dependencies`] rule

## [1.5.0] - 2016-04-18
### Added
Expand Down
68 changes: 68 additions & 0 deletions docs/rules/no-extraneous-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Forbid the use of extraneous packages

Forbid the import of external modules that are not declared in the `package.json`'s `dependencies` or `devDependencies`.
The closest parent `package.json` will be used. If no `package.json` is found, the rule will not lint anything.

### Options

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`.

You can set the options like this:

```js
"import/no-extraneous-dependencies": ["error", {"devDependencies": false}]
```


## Rule Details

Given the following `package.json`:
```json
{
"name": "my-project",
"...": "...",
"dependencies": {
"builtin-modules": "^1.1.1",
"lodash.cond": "^4.2.0",
"lodash.find": "^4.2.0",
"pkg-up": "^1.0.0"
},
"devDependencies": {
"ava": "^0.13.0",
"eslint": "^2.4.0",
"eslint-plugin-ava": "^1.3.0",
"xo": "^0.13.0"
}
}
```


## Fail

```js
var _ = require('lodash');
import _ from 'lodash';

/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": false}] */
import test from 'ava';
var test = require('ava');
```


## Pass

```js
// Builtin and internal modules are fine
var path = require('path');
var foo = require('./foo');

import test from 'ava';
import find from 'lodash.find';
```


## When Not To Use It

If you do not have a `package.json` file in your project.
24 changes: 12 additions & 12 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import isStaticRequire from '../core/staticRequire'

function getDependencies(context) {
const filepath = pkgUp.sync(context.getFilename())
if (!filepath) {
return null
}
if (!filepath) {
return null
}

try {
const packageContent = JSON.parse(fs.readFileSync(filepath, 'utf8'))
return {
try {
const packageContent = JSON.parse(fs.readFileSync(filepath, 'utf8'))
return {
dependencies: packageContent.dependencies || {},
devDependencies: packageContent.devDependencies || {},
}
} catch (e) {
return null
}
} catch (e) {
return null
}
}

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

function devDepErrorMessage(packageName) {
return `'${packageName}' is not listed in the project's dependencies, not devDependencies.`
return `'${packageName}' is not listed in the project's dependencies, not devDependencies.`
}

function reportIfMissing(context, deps, allowDevDeps, node, name) {
Expand Down

0 comments on commit ceea5dc

Please sign in to comment.