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

Improve monorepo support for no-extraneous-dependencies rule #1016

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/rules/no-extraneous-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ Also there is one more option called `packageDir`, this option is to specify the
"import/no-extraneous-dependencies": ["error", {"packageDir": './some-dir/'}]
```

Or, as an array - for when you need this rule to lookup multiple package.json files (for ex, if you are using a monnorepo).
```js
"import/no-extraneous-dependencies": ["error", {"packageDir": ['./some-dir/', './some/other/dir/']}]
```

## Rule Details

Given the following `package.json`:
Expand Down
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'))
)
Object.keys(manifest).forEach(depType => {
Object.assign(packageContent[depType], manifest[depType])
})
}
}

if (!packageContent) {
if (!Object.keys(packageContent).some(depType => hasKeys(packageContent[depType]))) {
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