Skip to content

Commit ae37318

Browse files
panrafalljharb
authored andcommitted
[patch] make no-extraneous-dependencies include only paths that can be resolved,
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.
1 parent ee51581 commit ae37318

File tree

8 files changed

+14
-7
lines changed

8 files changed

+14
-7
lines changed

docs/rules/no-extraneous-dependencies.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Forbid the import of external modules that are not declared in the `package.json`'s `dependencies`, `devDependencies`, `optionalDependencies` or `peerDependencies`.
44
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`.
55

6+
Modules have to be installed for this rule to work.
7+
68
### Options
79

810
This rule supports the following options:

src/rules/no-extraneous-dependencies.js

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'path'
22
import fs from 'fs'
33
import readPkgUp from 'read-pkg-up'
44
import minimatch from 'minimatch'
5+
import resolve from 'eslint-module-utils/resolve'
56
import importType from '../core/importType'
67
import isStaticRequire from '../core/staticRequire'
78

@@ -57,6 +58,10 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
5758
if (importType(name, context) !== 'external') {
5859
return
5960
}
61+
const resolved = resolve(name, context)
62+
if (!resolved) {
63+
return
64+
}
6065
const splitName = name.split('/')
6166
const packageName = splitName[0][0] === '@'
6267
? splitName.slice(0, 2).join('/')

tests/files/node_modules/@org/not-a-dependency/foo.js

Whitespace-only changes.

tests/files/node_modules/@org/not-a-dependency/index.js

Whitespace-only changes.

tests/files/node_modules/chai/index.js

Whitespace-only changes.

tests/files/node_modules/not-a-dependency/index.js

Whitespace-only changes.

tests/files/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"eslint": "2.x"
99
},
1010
"dependencies": {
11-
"@scope/core": "^1.0.0",
11+
"@org/package": "^1.0.0",
1212
"jquery": "^3.1.0",
1313
"lodash.cond": "^4.3.0",
1414
"pkg-up": "^1.0.0"

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ruleTester.run('no-extraneous-dependencies', rule, {
3030
test({ code: 'import "fs"'}),
3131
test({ code: 'import "./foo"'}),
3232
test({ code: 'import "lodash.isarray"'}),
33-
test({ code: 'import "@scope/core"'}),
33+
test({ code: 'import "@org/package"'}),
3434

3535
test({ code: 'import "electron"', settings: { 'import/core-modules': ['electron'] } }),
3636
test({ code: 'import "eslint"' }),
@@ -57,7 +57,7 @@ ruleTester.run('no-extraneous-dependencies', rule, {
5757
test({
5858
code: 'import chai from "chai"',
5959
options: [{devDependencies: ['*.test.js', '*.spec.js']}],
60-
filename: 'foo.spec.js',
60+
filename: path.join(process.cwd(), 'foo.spec.js'),
6161
}),
6262
test({
6363
code: 'import chai from "chai"',
@@ -79,17 +79,17 @@ ruleTester.run('no-extraneous-dependencies', rule, {
7979
}],
8080
}),
8181
test({
82-
code: 'var donthaveit = require("@scope/donthaveit")',
82+
code: 'var donthaveit = require("@org/not-a-dependency")',
8383
errors: [{
8484
ruleId: 'no-extraneous-dependencies',
85-
message: '\'@scope/donthaveit\' should be listed in the project\'s dependencies. Run \'npm i -S @scope/donthaveit\' to add it',
85+
message: '\'@org/not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S @org/not-a-dependency\' to add it',
8686
}],
8787
}),
8888
test({
89-
code: 'var donthaveit = require("@scope/donthaveit/lib/foo")',
89+
code: 'var donthaveit = require("@org/not-a-dependency/foo")',
9090
errors: [{
9191
ruleId: 'no-extraneous-dependencies',
92-
message: '\'@scope/donthaveit\' should be listed in the project\'s dependencies. Run \'npm i -S @scope/donthaveit\' to add it',
92+
message: '\'@org/not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S @org/not-a-dependency\' to add it',
9393
}],
9494
}),
9595
test({

0 commit comments

Comments
 (0)