Skip to content

Commit a32bde0

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

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

CHANGELOG.md

+2
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
### Fixed
88
- attempt to fix crash in [`no-mutable-exports`]. ([#660])
9+
- Add support for nested package.json [`no-extraneous-dependencies`]. ([#458])
910

1011

1112
## [2.2.0] - 2016-11-07
@@ -452,6 +453,7 @@ for info on changes for earlier releases.
452453
[#416]: https://github.com/benmosher/eslint-plugin-import/issues/416
453454
[#415]: https://github.com/benmosher/eslint-plugin-import/issues/415
454455
[#402]: https://github.com/benmosher/eslint-plugin-import/issues/402
456+
[#458]: https://github.com/benmosher/eslint-plugin-import/issues/458
455457
[#386]: https://github.com/benmosher/eslint-plugin-import/issues/386
456458
[#373]: https://github.com/benmosher/eslint-plugin-import/issues/373
457459
[#370]: https://github.com/benmosher/eslint-plugin-import/issues/370

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"eslint": "2.x - 3.x"
7676
},
7777
"dependencies": {
78+
"app-root-path": "^2.0.1",
7879
"builtin-modules": "^1.1.1",
7980
"contains-path": "^0.1.0",
8081
"debug": "^2.2.0",
@@ -83,8 +84,7 @@
8384
"eslint-module-utils": "^2.0.0",
8485
"has": "^1.0.1",
8586
"lodash.cond": "^4.3.0",
86-
"minimatch": "^3.0.3",
87-
"pkg-up": "^1.0.0"
87+
"minimatch": "^3.0.3"
8888
},
8989
"nyc": {
9090
"require": [

src/rules/no-extraneous-dependencies.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import fs from 'fs'
22
import path from 'path'
3-
import pkgUp from 'pkg-up'
3+
import rootPath from 'app-root-path'
44
import minimatch from 'minimatch'
55
import importType from '../core/importType'
66
import isStaticRequire from '../core/staticRequire'
77

8-
function getDependencies(context) {
9-
const filepath = pkgUp.sync(context.getFilename())
8+
function getRootPkg() {
9+
const rootPkg = rootPath + '/package.json'
10+
11+
if (fs.existsSync(rootPkg)) {
12+
return rootPkg
13+
}
14+
15+
return null
16+
}
17+
18+
function getDependencies() {
19+
const filepath = getRootPkg()
1020
if (!filepath) {
1121
return null
1222
}
@@ -105,7 +115,7 @@ module.exports = {
105115
create: function (context) {
106116
const options = context.options[0] || {}
107117
const filename = context.getFilename()
108-
const deps = getDependencies(context)
118+
const deps = getDependencies()
109119

110120
if (!deps) {
111121
return {}

tests/files/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
},
1010
"dependencies": {
1111
"@scope/core": "^1.0.0",
12+
"app-root-path": "^2.0.1",
1213
"jquery": "^3.1.0",
13-
"lodash.cond": "^4.3.0",
14-
"pkg-up": "^1.0.0"
14+
"lodash.cond": "^4.3.0"
1515
},
1616
"optionalDependencies": {
1717
"lodash.isarray": "^4.0.0"

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ const ruleTester = new RuleTester()
99
ruleTester.run('no-extraneous-dependencies', rule, {
1010
valid: [
1111
test({ code: 'import "lodash.cond"'}),
12-
test({ code: 'import "pkg-up"'}),
12+
test({ code: 'import "app-root-path"'}),
1313
test({ code: 'import foo, { bar } from "lodash.cond"'}),
14-
test({ code: 'import foo, { bar } from "pkg-up"'}),
14+
test({ code: 'import foo, { bar } from "app-root-path"'}),
1515
test({ code: 'import "eslint"'}),
1616
test({ code: 'import "eslint/lib/api"'}),
1717
test({ code: 'require("lodash.cond")'}),
18-
test({ code: 'require("pkg-up")'}),
18+
test({ code: 'require("app-root-path")'}),
1919
test({ code: 'var foo = require("lodash.cond")'}),
20-
test({ code: 'var foo = require("pkg-up")'}),
20+
test({ code: 'var foo = require("app-root-path")'}),
2121
test({ code: 'import "fs"'}),
2222
test({ code: 'import "./foo"'}),
2323
test({ code: 'import "lodash.isarray"'}),

0 commit comments

Comments
 (0)