Skip to content

Commit f456871

Browse files
committed
Add support to specify the package.json
1 parent 790dd66 commit f456871

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1010

1111
### Changed
1212
- [`no-extraneous-dependencies`]: use `read-pkg-up` to simplify finding + loading `package.json` ([#680], thanks [@wtgtybhertgeghgtwtg])
13+
- Add support to specify the package.json [`no-extraneous-dependencies`] ([#685], thanks [@ramasilveyra])
1314

1415
### Fixed
1516
- attempt to fix crash in [`no-mutable-exports`]. ([#660])
@@ -383,6 +384,7 @@ for info on changes for earlier releases.
383384
[`no-anonymous-default-export`]: ./docs/rules/no-anonymous-default-export.md
384385

385386
[#712]: https://github.com/benmosher/eslint-plugin-import/pull/712
387+
[#685]: https://github.com/benmosher/eslint-plugin-import/pull/685
386388
[#680]: https://github.com/benmosher/eslint-plugin-import/pull/680
387389
[#654]: https://github.com/benmosher/eslint-plugin-import/pull/654
388390
[#639]: https://github.com/benmosher/eslint-plugin-import/pull/639
@@ -571,3 +573,4 @@ for info on changes for earlier releases.
571573
[@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg
572574
[@duncanbeevers]: https://github.com/duncanbeevers
573575
[@giodamelio]: https://github.com/giodamelio
576+
[@ramasilveyra]: https://github.com/ramasilveyra

docs/rules/no-extraneous-dependencies.md

+7-1
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 `packageDir`.
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 `packageDir`, this option is to specify the path to the folder containing package.json and is relative to the current working directory.
31+
32+
```js
33+
"import/no-extraneous-dependencies": ["error", {"packageDir": './some-dir/'}]
34+
```
35+
3036
## Rule Details
3137

3238
Given the following `package.json`:

src/rules/no-extraneous-dependencies.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
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, packageDir) {
89
try {
9-
const pkg = readPkgUp.sync({cwd: context.getFilename(), normalize: false})
10-
if (!pkg || !pkg.pkg) {
10+
const packageContent = packageDir
11+
? JSON.parse(fs.readFileSync(path.join(packageDir, 'package.json'), 'utf8'))
12+
: readPkgUp.sync({cwd: context.getFilename(), normalize: false}).pkg
13+
14+
if (!packageContent) {
1115
return null
1216
}
13-
const packageContent = pkg.pkg
17+
1418
return {
1519
dependencies: packageContent.dependencies || {},
1620
devDependencies: packageContent.devDependencies || {},
1721
optionalDependencies: packageContent.optionalDependencies || {},
1822
peerDependencies: packageContent.peerDependencies || {},
1923
}
2024
} catch (e) {
25+
if (packageDir && e.code === 'ENOENT') {
26+
context.report({
27+
message: 'The package.json file could not be found.',
28+
loc: { line: 0, column: 0 },
29+
})
30+
}
31+
if (e.name === 'JSONError' || e instanceof SyntaxError) {
32+
context.report({
33+
message: 'The package.json file could not be parsed: ' + e.message,
34+
loc: { line: 0, column: 0 },
35+
})
36+
}
37+
2138
return null
2239
}
2340
}
@@ -93,6 +110,7 @@ module.exports = {
93110
'devDependencies': { 'type': ['boolean', 'array'] },
94111
'optionalDependencies': { 'type': ['boolean', 'array'] },
95112
'peerDependencies': { 'type': ['boolean', 'array'] },
113+
'packageDir': { 'type': 'string' },
96114
},
97115
'additionalProperties': false,
98116
},
@@ -102,7 +120,7 @@ module.exports = {
102120
create: function (context) {
103121
const options = context.options[0] || {}
104122
const filename = context.getFilename()
105-
const deps = getDependencies(context)
123+
const deps = getDependencies(context, options.packageDir)
106124

107125
if (!deps) {
108126
return {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ "name": "with-syntax-error" }

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

+28
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ ruleTester.run('no-extraneous-dependencies', rule, {
5656
filename: path.join(process.cwd(), 'foo.spec.js'),
5757
}),
5858
test({ code: 'require(6)' }),
59+
test({
60+
code: 'import "doctrine"',
61+
options: [{packageDir: path.join(__dirname, '../../../')}],
62+
}),
5963
],
6064
invalid: [
6165
test({
@@ -154,5 +158,29 @@ ruleTester.run('no-extraneous-dependencies', rule, {
154158
message: '\'lodash.isarray\' should be listed in the project\'s dependencies, not optionalDependencies.',
155159
}],
156160
}),
161+
test({
162+
code: 'import "not-a-dependency"',
163+
options: [{packageDir: path.join(__dirname, '../../../')}],
164+
errors: [{
165+
ruleId: 'no-extraneous-dependencies',
166+
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
167+
}],
168+
}),
169+
test({
170+
code: 'import "bar"',
171+
options: [{packageDir: path.join(__dirname, './doesn-exist/')}],
172+
errors: [{
173+
ruleId: 'no-extraneous-dependencies',
174+
message: 'The package.json file could not be found.',
175+
}],
176+
}),
177+
test({
178+
code: 'import foo from "foo"',
179+
options: [{packageDir: path.join(__dirname, '../../files/with-syntax-error')}],
180+
errors: [{
181+
ruleId: 'no-extraneous-dependencies',
182+
message: 'The package.json file could not be parsed: Unexpected token { in JSON at position 1',
183+
}],
184+
}),
157185
],
158186
})

0 commit comments

Comments
 (0)