Skip to content

Commit

Permalink
no-extraneous-dependencies includes only paths that can be resolved,
Browse files Browse the repository at this point in the history
as only resolved paths can be reliably tested if they live in a `node_modules` folder.

Up until now, this rule assumed, that every package that lives in a `node_modules` folder
or *cannot be resolved* is an external package.
This happens because of [that line](https://github.com/benmosher/eslint-plugin-import/blob/master/src/core/importType.js#L32).

If project uses aliasing, or transforms imports, this assumption is wrong,
as they may not be resolvable by import plugin, but also, they should not be defined in package.json.

Changing `isExternalPath` will make any ordering lints unstable
(order may change depending if you ran `npm install` or not).

Using `import/ignore` setting does not make sense too, as it's used to ignore imports unknown to the plugin,
like .css files. But still, you may import a css file from a dependency, and this rule should check that.
  • Loading branch information
panrafal committed Nov 2, 2017
1 parent c9dd91d commit e80a989
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docs/rules/no-extraneous-dependencies.md
Expand Up @@ -3,6 +3,8 @@
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. This behaviour can be changed with the rule option `packageDir`.

Modules have to be installed for this rule to work.

### Options

This rule supports the following options:
Expand Down
5 changes: 5 additions & 0 deletions src/rules/no-extraneous-dependencies.js
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import fs from 'fs'
import readPkgUp from 'read-pkg-up'
import minimatch from 'minimatch'
import resolve from 'eslint-module-utils/resolve'
import importType from '../core/importType'
import isStaticRequire from '../core/staticRequire'

Expand Down Expand Up @@ -57,6 +58,10 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
if (importType(name, context) !== 'external') {
return
}
const resolved = resolve(name, context)
if (!resolved) {
return
}
const splitName = name.split('/')
const packageName = splitName[0][0] === '@'
? splitName.slice(0, 2).join('/')
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion tests/files/package.json
Expand Up @@ -8,7 +8,7 @@
"eslint": "2.x"
},
"dependencies": {
"@scope/core": "^1.0.0",
"@org/package": "^1.0.0",
"jquery": "^3.1.0",
"lodash.cond": "^4.3.0",
"pkg-up": "^1.0.0"
Expand Down
12 changes: 6 additions & 6 deletions tests/src/rules/no-extraneous-dependencies.js
Expand Up @@ -30,7 +30,7 @@ ruleTester.run('no-extraneous-dependencies', rule, {
test({ code: 'import "fs"'}),
test({ code: 'import "./foo"'}),
test({ code: 'import "lodash.isarray"'}),
test({ code: 'import "@scope/core"'}),
test({ code: 'import "@org/package"'}),

test({ code: 'import "electron"', settings: { 'import/core-modules': ['electron'] } }),
test({ code: 'import "eslint"' }),
Expand All @@ -57,7 +57,7 @@ ruleTester.run('no-extraneous-dependencies', rule, {
test({
code: 'import chai from "chai"',
options: [{devDependencies: ['*.test.js', '*.spec.js']}],
filename: 'foo.spec.js',
filename: path.join(process.cwd(), 'foo.spec.js'),
}),
test({
code: 'import chai from "chai"',
Expand All @@ -79,17 +79,17 @@ ruleTester.run('no-extraneous-dependencies', rule, {
}],
}),
test({
code: 'var donthaveit = require("@scope/donthaveit")',
code: 'var donthaveit = require("@org/not-a-dependency")',
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',
message: '\'@org/not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S @org/not-a-dependency\' to add it',
}],
}),
test({
code: 'var donthaveit = require("@scope/donthaveit/lib/foo")',
code: 'var donthaveit = require("@org/not-a-dependency/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',
message: '\'@org/not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S @org/not-a-dependency\' to add it',
}],
}),
test({
Expand Down

0 comments on commit e80a989

Please sign in to comment.