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

Use read-pkg-up over pkg-up. #680

Merged
merged 5 commits into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"has": "^1.0.1",
"lodash.cond": "^4.3.0",
"minimatch": "^3.0.3",
"pkg-up": "^1.0.0"
"read-pkg-up": "^2.0.0"
},
"nyc": {
"require": [
Expand Down
11 changes: 3 additions & 8 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import fs from 'fs'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this import, now that it is not used?

import path from 'path'
import pkgUp from 'pkg-up'
import readPkgUp from 'read-pkg-up'
import minimatch from 'minimatch'
import importType from '../core/importType'
import isStaticRequire from '../core/staticRequire'

function getDependencies(context) {
const filepath = pkgUp.sync(context.getFilename())
if (!filepath) {
return null
}

try {
const packageContent = JSON.parse(fs.readFileSync(filepath, 'utf8'))
const pkg = readPkgUp.sync({cwd: context.getFilename(), normalize: false})
const packageContent = JSON.parse(pkg.pkg)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem that makes the build fail is that getDependencies is now returning something else. pkg.pkg is now an object, and not a string that remains to be parsed. By attempting to parse it you are causing an error, which will be cached and make the function return null.

This works better:

    const pkg = readPkgUp.sync({cwd: context.getFilename(), normalize: false})
    if (!pkg || !pkg.pkg) {
      return null
    }

    const packageContent = pkg.pkg

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I don't know how I missed that.

return {
dependencies: packageContent.dependencies || {},
devDependencies: packageContent.devDependencies || {},
Expand Down Expand Up @@ -46,7 +42,6 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
const packageName = splitName[0][0] === '@'
? splitName.slice(0, 2).join('/')
: splitName[0]

const isInDeps = deps.dependencies[packageName] !== undefined
const isInDevDeps = deps.devDependencies[packageName] !== undefined
const isInOptDeps = deps.optionalDependencies[packageName] !== undefined
Expand Down