Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make no-extraneous-deps lint only dependencies that can be resolved #943

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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