Skip to content

Commit

Permalink
Add support for nested package.json. Fix #458
Browse files Browse the repository at this point in the history
  • Loading branch information
ramasilveyra committed Dec 24, 2016
1 parent c975742 commit e2fb13b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
4 changes: 4 additions & 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]
### Changed
- [`no-extraneous-dependencies`]: use `read-pkg-up` to simplify finding + loading `package.json` ([#680], thanks [@wtgtybhertgeghgtwtg])
- Add support for nested package.json [`no-extraneous-dependencies`] ([#458] + [#685], thanks [@ramasilveyra])

### Fixed
- attempt to fix crash in [`no-mutable-exports`]. ([#660])
Expand Down Expand Up @@ -377,6 +378,7 @@ for info on changes for earlier releases.
[`no-unassigned-import`]: ./docs/rules/no-unassigned-import.md
[`unambiguous`]: ./docs/rules/unambiguous.md

[#685]: https://github.com/benmosher/eslint-plugin-import/pull/685
[#680]: https://github.com/benmosher/eslint-plugin-import/pull/680
[#654]: https://github.com/benmosher/eslint-plugin-import/pull/654
[#639]: https://github.com/benmosher/eslint-plugin-import/pull/639
Expand Down Expand Up @@ -448,6 +450,7 @@ for info on changes for earlier releases.
[#507]: https://github.com/benmosher/eslint-plugin-import/issues/507
[#484]: https://github.com/benmosher/eslint-plugin-import/issues/484
[#478]: https://github.com/benmosher/eslint-plugin-import/issues/478
[#458]: https://github.com/benmosher/eslint-plugin-import/issues/458
[#456]: https://github.com/benmosher/eslint-plugin-import/issues/456
[#453]: https://github.com/benmosher/eslint-plugin-import/issues/453
[#452]: https://github.com/benmosher/eslint-plugin-import/issues/452
Expand Down Expand Up @@ -561,3 +564,4 @@ for info on changes for earlier releases.
[@ntdb]: https://github.com/ntdb
[@jakubsta]: https://github.com/jakubsta
[@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg
[@ramasilveyra]: https://github.com/ramasilveyra
8 changes: 7 additions & 1 deletion docs/rules/no-extraneous-dependencies.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Forbid the use of extraneous packages

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.
The closest parent `package.json` will be used. If no `package.json` is found, the rule will not lint anything. This behaviour can be changed with the rule option `packagePath`.

### Options

Expand All @@ -27,6 +27,12 @@ You can also use an array of globs instead of literal booleans:

When using an array of globs, the setting will be activated if the name of the file being linted matches a single glob in the array.

Also there is one more option called `packagePath`, this option is to specify the path of the package.json.

```js
"import/no-extraneous-dependencies": ["error", {"packagePath": './package.json'}]
```

## Rule Details

Given the following `package.json`:
Expand Down
17 changes: 12 additions & 5 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import path from 'path'
import fs from 'fs'
import readPkgUp from 'read-pkg-up'
import minimatch from 'minimatch'
import importType from '../core/importType'
import isStaticRequire from '../core/staticRequire'

function getDependencies(context) {
function getDependencies(context, packagePath) {
try {
const pkg = readPkgUp.sync({cwd: context.getFilename(), normalize: false})
if (!pkg || !pkg.pkg) {
const pkg = packagePath
? JSON.parse(fs.readFileSync(packagePath, 'utf8'))
: readPkgUp.sync({cwd: context.getFilename(), normalize: false}).pkg

if (!pkg) {
return null
}
const packageContent = pkg.pkg

const packageContent = pkg

return {
dependencies: packageContent.dependencies || {},
devDependencies: packageContent.devDependencies || {},
Expand Down Expand Up @@ -93,6 +99,7 @@ module.exports = {
'devDependencies': { 'type': ['boolean', 'array'] },
'optionalDependencies': { 'type': ['boolean', 'array'] },
'peerDependencies': { 'type': ['boolean', 'array'] },
'packagePath': { 'type': 'string' },
},
'additionalProperties': false,
},
Expand All @@ -102,7 +109,7 @@ module.exports = {
create: function (context) {
const options = context.options[0] || {}
const filename = context.getFilename()
const deps = getDependencies(context)
const deps = getDependencies(context, options.packagePath)

if (!deps) {
return {}
Expand Down
14 changes: 14 additions & 0 deletions tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ ruleTester.run('no-extraneous-dependencies', rule, {
filename: path.join(process.cwd(), 'foo.spec.js'),
}),
test({ code: 'require(6)' }),

test({
code: 'import "doctrine"',
options: [{packagePath: path.join(__dirname, '../../../package.json')}],
}),
],
invalid: [
test({
Expand Down Expand Up @@ -154,5 +159,14 @@ ruleTester.run('no-extraneous-dependencies', rule, {
message: '\'lodash.isarray\' should be listed in the project\'s dependencies, not optionalDependencies.',
}],
}),

test({
code: 'import "not-a-dependency"',
options: [{packagePath: path.join(__dirname, '../../../package.json')}],
errors: [{
ruleId: 'no-extraneous-dependencies',
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
}],
}),
],
})

0 comments on commit e2fb13b

Please sign in to comment.