Skip to content

Commit

Permalink
correct no-extraneous-dependencies rule: get the real name from the r…
Browse files Browse the repository at this point in the history
…esolved package.json. If not aliased imports (alias/react for example) will not be correctly interpreted
  • Loading branch information
JEROMEH committed Mar 24, 2020
1 parent 075af94 commit d33d69d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/core/packagePath.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import {dirname} from 'path'
import findUp from 'find-up'
import readPkgUp from 'read-pkg-up'


export function getContextPackagePath(context) {
return getFilePackagePath(context.getFilename())
}

export function getFilePackagePath(filePath) {
const fp = findUp.sync('package.json', {cwd: filePath, normalize: false})
const fp = findUp.sync('package.json', {cwd: filePath})
return dirname(fp)
}

export function getFilePackageName(filePath) {
const pkg = readPkgUp.sync({cwd: filePath, normalize: false}).pkg
return pkg && pkg.name
}
21 changes: 17 additions & 4 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import readPkgUp from 'read-pkg-up'
import minimatch from 'minimatch'
import resolve from 'eslint-module-utils/resolve'
import importType from '../core/importType'
import { getFilePackageName } from '../core/packagePath'
import isStaticRequire from '../core/staticRequire'
import docsUrl from '../docsUrl'

Expand Down Expand Up @@ -109,6 +110,17 @@ function optDepErrorMessage(packageName) {
`not optionalDependencies.`
}

function getModuleOriginalName(name) {
const splitName = name.split('/')
return splitName[0][0] === '@'
? splitName.slice(0, 2).join('/')
: splitName[0]
}

function getModuleRealName(resolved) {
return getFilePackageName(resolved)
}

function reportIfMissing(context, deps, depsOptions, node, name) {
// Do not report when importing types
if (node.importKind === 'type') {
Expand All @@ -122,10 +134,11 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
const resolved = resolve(name, context)
if (!resolved) { return }

const splitName = name.split('/')
const packageName = splitName[0][0] === '@'
? splitName.slice(0, 2).join('/')
: splitName[0]
// get the real name from the resolved package.json
// if not aliased imports (alias/react for example) will not be correctly interpreted
// fallback on original name in case no package.json found
const packageName = getModuleRealName(resolved) || getModuleOriginalName(name)

const isInDeps = deps.dependencies[packageName] !== undefined
const isInDevDeps = deps.devDependencies[packageName] !== undefined
const isInOptDeps = deps.optionalDependencies[packageName] !== undefined
Expand Down
3 changes: 3 additions & 0 deletions tests/files/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ module.exports = {
resolve: {
extensions: ['', '.js', '.jsx'],
root: __dirname,
alias: {
'alias/chai$': 'chai' // alias for no-extraneous-dependencies tests
}
},
}
9 changes: 9 additions & 0 deletions tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,5 +330,14 @@ 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: 'import chai from "alias/chai";',
settings: { 'import/resolver': 'webpack' },
errors: [{
ruleId: 'no-extraneous-dependencies',
// missing dependency is chai not alias
message: "'chai' should be listed in the project's dependencies. Run 'npm i -S chai' to add it",
}],
}),
],
})

0 comments on commit d33d69d

Please sign in to comment.