Skip to content

Commit

Permalink
no-extraneous-dependencies: Handle scoped packages. Fixes #316 (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels authored and benmosher committed May 9, 2016
1 parent f8301ea commit 90fefa3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`prefer-default-export`], new rule. ([#308], thanks [@gavriguy])

### Fixed
- ignore namespace / ES7 re-exports in [`no-mutable-exports`]. ([#317], fixed by [#322]. thanks [@borisyankov] + [@jfmengels])
- Ignore namespace / ES7 re-exports in [`no-mutable-exports`]. ([#317], fixed by [#322]. thanks [@borisyankov] + [@jfmengels])
- Make [`no-extraneous-dependencies`] handle scoped packages ([#316], thanks [@jfmengels])

## [1.7.0] - 2016-05-06
### Added
Expand All @@ -18,7 +19,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- add [`no-mutable-exports`] rule ([#290], thanks [@josh])
- [`import/extensions` setting]: a whitelist of file extensions to parse as modules
and search for `export`s. If unspecified, all extensions are considered valid (for now).
In v2, this will likely default to `['.js', MODULE_EXT]`,. ([#297], to fix [#267])
In v2, this will likely default to `['.js', MODULE_EXT]`. ([#297], to fix [#267])

### Fixed
- [`extensions`]: fallback to source path for extension enforcement if imported
Expand Down Expand Up @@ -210,6 +211,7 @@ for info on changes for earlier releases.
[`prefer-default-export`]: ./docs/rules/prefer-default-export.md

[#322]: https://github.com/benmosher/eslint-plugin-import/pull/322
[#316]: https://github.com/benmosher/eslint-plugin-import/pull/316
[#308]: https://github.com/benmosher/eslint-plugin-import/pull/308
[#298]: https://github.com/benmosher/eslint-plugin-import/pull/298
[#297]: https://github.com/benmosher/eslint-plugin-import/pull/297
Expand Down
5 changes: 4 additions & 1 deletion src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ function reportIfMissing(context, deps, allowDevDeps, allowOptDeps, node, name)
if (importType(name, context) !== 'external') {
return
}
const packageName = name.split('/')[0]
const splitName = name.split('/')
const packageName = splitName[0][0] === '@'
? splitName.slice(0, 2).join('/')
: splitName[0]

const isInDeps = deps.dependencies[packageName] !== undefined
const isInDevDeps = deps.devDependencies[packageName] !== undefined
Expand Down
1 change: 1 addition & 0 deletions tests/files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"eslint": "2.x"
},
"dependencies": {
"@scope/core": "^1.0.0",
"lodash.cond": "^4.3.0",
"pkg-up": "^1.0.0"
},
Expand Down
17 changes: 16 additions & 1 deletion tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ ruleTester.run('no-extraneous-dependencies', rule, {
test({ code: 'import "fs"'}),
test({ code: 'import "./foo"'}),
test({ code: 'import "lodash.isarray"'}),
test({ code: 'import "@scope/core"'}),

// 'project' type
test({
code: 'import "importType"',
settings: { "import/resolver": { node: { paths: [ path.join(__dirname, '../../files') ] } } },
settings: { 'import/resolver': { node: { paths: [ path.join(__dirname, '../../files') ] } } },
}),
],
invalid: [
Expand All @@ -36,6 +37,20 @@ ruleTester.run('no-extraneous-dependencies', rule, {
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
}],
}),
test({
code: 'var donthaveit = require("@scope/donthaveit")',
errors: [{
ruleId: 'no-extraneous-dependencies',
message: '\'@scope/donthaveit\' should be listed in the project\'s dependencies. Run \'npm i -S @scope/donthaveit\' to add it',
}],
}),
test({
code: 'var donthaveit = require("@scope/donthaveit/lib/foo")',
errors: [{
ruleId: 'no-extraneous-dependencies',
message: '\'@scope/donthaveit\' should be listed in the project\'s dependencies. Run \'npm i -S @scope/donthaveit\' to add it',
}],
}),
test({
code: 'import "eslint"',
options: [{devDependencies: false}],
Expand Down

0 comments on commit 90fefa3

Please sign in to comment.