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 8, 2016
1 parent bfdc2bb commit a32bde0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
## [Unreleased]
### Fixed
- attempt to fix crash in [`no-mutable-exports`]. ([#660])
- Add support for nested package.json [`no-extraneous-dependencies`]. ([#458])


## [2.2.0] - 2016-11-07
Expand Down Expand Up @@ -452,6 +453,7 @@ for info on changes for earlier releases.
[#416]: https://github.com/benmosher/eslint-plugin-import/issues/416
[#415]: https://github.com/benmosher/eslint-plugin-import/issues/415
[#402]: https://github.com/benmosher/eslint-plugin-import/issues/402
[#458]: https://github.com/benmosher/eslint-plugin-import/issues/458
[#386]: https://github.com/benmosher/eslint-plugin-import/issues/386
[#373]: https://github.com/benmosher/eslint-plugin-import/issues/373
[#370]: https://github.com/benmosher/eslint-plugin-import/issues/370
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -75,6 +75,7 @@
"eslint": "2.x - 3.x"
},
"dependencies": {
"app-root-path": "^2.0.1",
"builtin-modules": "^1.1.1",
"contains-path": "^0.1.0",
"debug": "^2.2.0",
Expand All @@ -83,8 +84,7 @@
"eslint-module-utils": "^2.0.0",
"has": "^1.0.1",
"lodash.cond": "^4.3.0",
"minimatch": "^3.0.3",
"pkg-up": "^1.0.0"
"minimatch": "^3.0.3"
},
"nyc": {
"require": [
Expand Down
18 changes: 14 additions & 4 deletions src/rules/no-extraneous-dependencies.js
@@ -1,12 +1,22 @@
import fs from 'fs'
import path from 'path'
import pkgUp from 'pkg-up'
import rootPath from 'app-root-path'
import minimatch from 'minimatch'
import importType from '../core/importType'
import isStaticRequire from '../core/staticRequire'

function getDependencies(context) {
const filepath = pkgUp.sync(context.getFilename())
function getRootPkg() {
const rootPkg = rootPath + '/package.json'

if (fs.existsSync(rootPkg)) {
return rootPkg
}

return null
}

function getDependencies() {
const filepath = getRootPkg()
if (!filepath) {
return null
}
Expand Down Expand Up @@ -105,7 +115,7 @@ module.exports = {
create: function (context) {
const options = context.options[0] || {}
const filename = context.getFilename()
const deps = getDependencies(context)
const deps = getDependencies()

if (!deps) {
return {}
Expand Down
4 changes: 2 additions & 2 deletions tests/files/package.json
Expand Up @@ -9,9 +9,9 @@
},
"dependencies": {
"@scope/core": "^1.0.0",
"app-root-path": "^2.0.1",
"jquery": "^3.1.0",
"lodash.cond": "^4.3.0",
"pkg-up": "^1.0.0"
"lodash.cond": "^4.3.0"
},
"optionalDependencies": {
"lodash.isarray": "^4.0.0"
Expand Down
8 changes: 4 additions & 4 deletions tests/src/rules/no-extraneous-dependencies.js
Expand Up @@ -9,15 +9,15 @@ const ruleTester = new RuleTester()
ruleTester.run('no-extraneous-dependencies', rule, {
valid: [
test({ code: 'import "lodash.cond"'}),
test({ code: 'import "pkg-up"'}),
test({ code: 'import "app-root-path"'}),
test({ code: 'import foo, { bar } from "lodash.cond"'}),
test({ code: 'import foo, { bar } from "pkg-up"'}),
test({ code: 'import foo, { bar } from "app-root-path"'}),
test({ code: 'import "eslint"'}),
test({ code: 'import "eslint/lib/api"'}),
test({ code: 'require("lodash.cond")'}),
test({ code: 'require("pkg-up")'}),
test({ code: 'require("app-root-path")'}),
test({ code: 'var foo = require("lodash.cond")'}),
test({ code: 'var foo = require("pkg-up")'}),
test({ code: 'var foo = require("app-root-path")'}),
test({ code: 'import "fs"'}),
test({ code: 'import "./foo"'}),
test({ code: 'import "lodash.isarray"'}),
Expand Down

0 comments on commit a32bde0

Please sign in to comment.