Skip to content

Commit d33d69d

Browse files
author
JEROMEH
committed
correct no-extraneous-dependencies rule: get the real name from the resolved package.json. If not aliased imports (alias/react for example) will not be correctly interpreted
1 parent 075af94 commit d33d69d

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

Diff for: src/core/packagePath.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import {dirname} from 'path'
22
import findUp from 'find-up'
3+
import readPkgUp from 'read-pkg-up'
34

45

56
export function getContextPackagePath(context) {
67
return getFilePackagePath(context.getFilename())
78
}
89

910
export function getFilePackagePath(filePath) {
10-
const fp = findUp.sync('package.json', {cwd: filePath, normalize: false})
11+
const fp = findUp.sync('package.json', {cwd: filePath})
1112
return dirname(fp)
1213
}
14+
15+
export function getFilePackageName(filePath) {
16+
const pkg = readPkgUp.sync({cwd: filePath, normalize: false}).pkg
17+
return pkg && pkg.name
18+
}

Diff for: src/rules/no-extraneous-dependencies.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import readPkgUp from 'read-pkg-up'
44
import minimatch from 'minimatch'
55
import resolve from 'eslint-module-utils/resolve'
66
import importType from '../core/importType'
7+
import { getFilePackageName } from '../core/packagePath'
78
import isStaticRequire from '../core/staticRequire'
89
import docsUrl from '../docsUrl'
910

@@ -109,6 +110,17 @@ function optDepErrorMessage(packageName) {
109110
`not optionalDependencies.`
110111
}
111112

113+
function getModuleOriginalName(name) {
114+
const splitName = name.split('/')
115+
return splitName[0][0] === '@'
116+
? splitName.slice(0, 2).join('/')
117+
: splitName[0]
118+
}
119+
120+
function getModuleRealName(resolved) {
121+
return getFilePackageName(resolved)
122+
}
123+
112124
function reportIfMissing(context, deps, depsOptions, node, name) {
113125
// Do not report when importing types
114126
if (node.importKind === 'type') {
@@ -122,10 +134,11 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
122134
const resolved = resolve(name, context)
123135
if (!resolved) { return }
124136

125-
const splitName = name.split('/')
126-
const packageName = splitName[0][0] === '@'
127-
? splitName.slice(0, 2).join('/')
128-
: splitName[0]
137+
// get the real name from the resolved package.json
138+
// if not aliased imports (alias/react for example) will not be correctly interpreted
139+
// fallback on original name in case no package.json found
140+
const packageName = getModuleRealName(resolved) || getModuleOriginalName(name)
141+
129142
const isInDeps = deps.dependencies[packageName] !== undefined
130143
const isInDevDeps = deps.devDependencies[packageName] !== undefined
131144
const isInOptDeps = deps.optionalDependencies[packageName] !== undefined

Diff for: tests/files/webpack.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ module.exports = {
22
resolve: {
33
extensions: ['', '.js', '.jsx'],
44
root: __dirname,
5+
alias: {
6+
'alias/chai$': 'chai' // alias for no-extraneous-dependencies tests
7+
}
58
},
69
}

Diff for: tests/src/rules/no-extraneous-dependencies.js

+9
Original file line numberDiff line numberDiff line change
@@ -330,5 +330,14 @@ ruleTester.run('no-extraneous-dependencies', rule, {
330330
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
331331
}],
332332
}),
333+
test({
334+
code: 'import chai from "alias/chai";',
335+
settings: { 'import/resolver': 'webpack' },
336+
errors: [{
337+
ruleId: 'no-extraneous-dependencies',
338+
// missing dependency is chai not alias
339+
message: "'chai' should be listed in the project's dependencies. Run 'npm i -S chai' to add it",
340+
}],
341+
}),
333342
],
334343
})

0 commit comments

Comments
 (0)