Skip to content

Commit

Permalink
allow packageDir option to accept array
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Hargrove committed Apr 19, 2018
1 parent 4e37dbf commit 2780d7c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
65 changes: 52 additions & 13 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,61 @@ import importType from '../core/importType'
import isStaticRequire from '../core/staticRequire'
import docsUrl from '../docsUrl'

function hasKeys(obj) {
return Object.keys(obj).length > 0
}

function extractDependencies(manifest = {}) {
const {
dependencies = {},
devDependencies = {},
optionalDependencies = {},
peerDependencies = {},
} = manifest

return {
dependencies,
devDependencies,
optionalDependencies,
peerDependencies,
}
}

function getDependencies(context, packageDir) {
const packageDirs = Array.isArray(packageDir)
? packageDir
: (packageDir ? [packageDir] : [])
const packageContent = {
dependencies: {},
devDependencies: {},
optionalDependencies: {},
peerDependencies: {},
}

try {
const packageContent = packageDir
? JSON.parse(fs.readFileSync(path.join(packageDir, 'package.json'), 'utf8'))
: readPkgUp.sync({cwd: context.getFilename(), normalize: false}).pkg
if (!packageDirs.length) {
Object.assign(
packageContent,
extractDependencies(
readPkgUp.sync({cwd: context.getFilename(), normalize: false}).pkg
)
)
} else {
for (const dir of packageDirs) {
const manifest = extractDependencies(
JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'))
)
for (const [depType, deps] of Object.entries(manifest)) {
Object.assign(packageContent[depType], deps)
}
}
}

if (!packageContent) {
if (!Object.values(packageContent).some(hasKeys)) {
return null
}

return {
dependencies: packageContent.dependencies || {},
devDependencies: packageContent.devDependencies || {},
optionalDependencies: packageContent.optionalDependencies || {},
peerDependencies: packageContent.peerDependencies || {},
}
return packageContent
} catch (e) {
if (packageDir && e.code === 'ENOENT') {
context.report({
Expand All @@ -36,9 +75,9 @@ function getDependencies(context, packageDir) {
loc: { line: 0, column: 0 },
})
}

return null
}

return null
}

function missingErrorMessage(packageName) {
Expand Down Expand Up @@ -124,7 +163,7 @@ module.exports = {
'devDependencies': { 'type': ['boolean', 'array'] },
'optionalDependencies': { 'type': ['boolean', 'array'] },
'peerDependencies': { 'type': ['boolean', 'array'] },
'packageDir': { 'type': 'string' },
'packageDir': { 'type': ['string', 'array'] },
},
'additionalProperties': false,
},
Expand Down
4 changes: 4 additions & 0 deletions tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ ruleTester.run('no-extraneous-dependencies', rule, {
code: 'import "doctrine"',
options: [{packageDir: path.join(__dirname, '../../../')}],
}),
test({
code: 'import "chai"',
options: [{packageDir: [path.join(__dirname, '../../../')]}],
}),
test({
code: 'import type MyType from "myflowtyped";',
options: [{packageDir: packageDirWithFlowTyped}],
Expand Down

0 comments on commit 2780d7c

Please sign in to comment.