Skip to content

Commit e2fb13b

Browse files
committed
Add support for nested package.json. Fix #458
1 parent c975742 commit e2fb13b

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
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
### Changed
88
- [`no-extraneous-dependencies`]: use `read-pkg-up` to simplify finding + loading `package.json` ([#680], thanks [@wtgtybhertgeghgtwtg])
9+
- Add support for nested package.json [`no-extraneous-dependencies`] ([#458] + [#685], thanks [@ramasilveyra])
910

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

381+
[#685]: https://github.com/benmosher/eslint-plugin-import/pull/685
380382
[#680]: https://github.com/benmosher/eslint-plugin-import/pull/680
381383
[#654]: https://github.com/benmosher/eslint-plugin-import/pull/654
382384
[#639]: https://github.com/benmosher/eslint-plugin-import/pull/639
@@ -448,6 +450,7 @@ for info on changes for earlier releases.
448450
[#507]: https://github.com/benmosher/eslint-plugin-import/issues/507
449451
[#484]: https://github.com/benmosher/eslint-plugin-import/issues/484
450452
[#478]: https://github.com/benmosher/eslint-plugin-import/issues/478
453+
[#458]: https://github.com/benmosher/eslint-plugin-import/issues/458
451454
[#456]: https://github.com/benmosher/eslint-plugin-import/issues/456
452455
[#453]: https://github.com/benmosher/eslint-plugin-import/issues/453
453456
[#452]: https://github.com/benmosher/eslint-plugin-import/issues/452
@@ -561,3 +564,4 @@ for info on changes for earlier releases.
561564
[@ntdb]: https://github.com/ntdb
562565
[@jakubsta]: https://github.com/jakubsta
563566
[@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg
567+
[@ramasilveyra]: https://github.com/ramasilveyra

docs/rules/no-extraneous-dependencies.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Forbid the use of extraneous packages
22

33
Forbid the import of external modules that are not declared in the `package.json`'s `dependencies`, `devDependencies`, `optionalDependencies` or `peerDependencies`.
4-
The closest parent `package.json` will be used. If no `package.json` is found, the rule will not lint anything.
4+
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`.
55

66
### Options
77

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

2828
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.
2929

30+
Also there is one more option called `packagePath`, this option is to specify the path of the package.json.
31+
32+
```js
33+
"import/no-extraneous-dependencies": ["error", {"packagePath": './package.json'}]
34+
```
35+
3036
## Rule Details
3137

3238
Given the following `package.json`:

src/rules/no-extraneous-dependencies.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import path from 'path'
2+
import fs from 'fs'
23
import readPkgUp from 'read-pkg-up'
34
import minimatch from 'minimatch'
45
import importType from '../core/importType'
56
import isStaticRequire from '../core/staticRequire'
67

7-
function getDependencies(context) {
8+
function getDependencies(context, packagePath) {
89
try {
9-
const pkg = readPkgUp.sync({cwd: context.getFilename(), normalize: false})
10-
if (!pkg || !pkg.pkg) {
10+
const pkg = packagePath
11+
? JSON.parse(fs.readFileSync(packagePath, 'utf8'))
12+
: readPkgUp.sync({cwd: context.getFilename(), normalize: false}).pkg
13+
14+
if (!pkg) {
1115
return null
1216
}
13-
const packageContent = pkg.pkg
17+
18+
const packageContent = pkg
19+
1420
return {
1521
dependencies: packageContent.dependencies || {},
1622
devDependencies: packageContent.devDependencies || {},
@@ -93,6 +99,7 @@ module.exports = {
9399
'devDependencies': { 'type': ['boolean', 'array'] },
94100
'optionalDependencies': { 'type': ['boolean', 'array'] },
95101
'peerDependencies': { 'type': ['boolean', 'array'] },
102+
'packagePath': { 'type': 'string' },
96103
},
97104
'additionalProperties': false,
98105
},
@@ -102,7 +109,7 @@ module.exports = {
102109
create: function (context) {
103110
const options = context.options[0] || {}
104111
const filename = context.getFilename()
105-
const deps = getDependencies(context)
112+
const deps = getDependencies(context, options.packagePath)
106113

107114
if (!deps) {
108115
return {}

tests/src/rules/no-extraneous-dependencies.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ ruleTester.run('no-extraneous-dependencies', rule, {
5656
filename: path.join(process.cwd(), 'foo.spec.js'),
5757
}),
5858
test({ code: 'require(6)' }),
59+
60+
test({
61+
code: 'import "doctrine"',
62+
options: [{packagePath: path.join(__dirname, '../../../package.json')}],
63+
}),
5964
],
6065
invalid: [
6166
test({
@@ -154,5 +159,14 @@ ruleTester.run('no-extraneous-dependencies', rule, {
154159
message: '\'lodash.isarray\' should be listed in the project\'s dependencies, not optionalDependencies.',
155160
}],
156161
}),
162+
163+
test({
164+
code: 'import "not-a-dependency"',
165+
options: [{packagePath: path.join(__dirname, '../../../package.json')}],
166+
errors: [{
167+
ruleId: 'no-extraneous-dependencies',
168+
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
169+
}],
170+
}),
157171
],
158172
})

0 commit comments

Comments
 (0)