From 708f08630aab833205dbb0fdabf34b0b27302daa Mon Sep 17 00:00:00 2001 From: Ramiro Silveyra d'Avila Date: Sat, 24 Dec 2016 02:13:50 -0300 Subject: [PATCH] Add support to specify the package.json --- CHANGELOG.md | 3 +++ docs/rules/no-extraneous-dependencies.md | 8 +++++++- src/rules/no-extraneous-dependencies.js | 15 ++++++++++----- tests/src/rules/no-extraneous-dependencies.js | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d1686fd31..c385953b2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ### Changed - [`no-extraneous-dependencies`]: use `read-pkg-up` to simplify finding + loading `package.json` ([#680], thanks [@wtgtybhertgeghgtwtg]) +- Add support to specify the package.json [`no-extraneous-dependencies`] ([#685], thanks [@ramasilveyra]) ### Fixed - attempt to fix crash in [`no-mutable-exports`]. ([#660]) @@ -383,6 +384,7 @@ for info on changes for earlier releases. [`no-anonymous-default-export`]: ./docs/rules/no-anonymous-default-export.md [#712]: https://github.com/benmosher/eslint-plugin-import/pull/712 +[#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 @@ -571,3 +573,4 @@ for info on changes for earlier releases. [@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg [@duncanbeevers]: https://github.com/duncanbeevers [@giodamelio]: https://github.com/giodamelio +[@ramasilveyra]: https://github.com/ramasilveyra diff --git a/docs/rules/no-extraneous-dependencies.md b/docs/rules/no-extraneous-dependencies.md index adb532e390..232955d746 100644 --- a/docs/rules/no-extraneous-dependencies.md +++ b/docs/rules/no-extraneous-dependencies.md @@ -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 @@ -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 and is relative to the current working directory. + +```js +"import/no-extraneous-dependencies": ["error", {"packagePath": './package.json'}] +``` + ## Rule Details Given the following `package.json`: diff --git a/src/rules/no-extraneous-dependencies.js b/src/rules/no-extraneous-dependencies.js index 166d4f843b..d2ed53cdb0 100644 --- a/src/rules/no-extraneous-dependencies.js +++ b/src/rules/no-extraneous-dependencies.js @@ -1,16 +1,20 @@ 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 packageContent = packagePath + ? JSON.parse(fs.readFileSync(packagePath, 'utf8')) + : readPkgUp.sync({cwd: context.getFilename(), normalize: false}).pkg + + if (!packageContent) { return null } - const packageContent = pkg.pkg + return { dependencies: packageContent.dependencies || {}, devDependencies: packageContent.devDependencies || {}, @@ -93,6 +97,7 @@ module.exports = { 'devDependencies': { 'type': ['boolean', 'array'] }, 'optionalDependencies': { 'type': ['boolean', 'array'] }, 'peerDependencies': { 'type': ['boolean', 'array'] }, + 'packagePath': { 'type': 'string' }, }, 'additionalProperties': false, }, @@ -102,7 +107,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 {} diff --git a/tests/src/rules/no-extraneous-dependencies.js b/tests/src/rules/no-extraneous-dependencies.js index 8bb632d017..b4883fe083 100644 --- a/tests/src/rules/no-extraneous-dependencies.js +++ b/tests/src/rules/no-extraneous-dependencies.js @@ -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({ @@ -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', + }], + }), ], })