Skip to content

Commit

Permalink
fix: handle null values for dependency objects
Browse files Browse the repository at this point in the history
the current GitHub npm package registry returns packuments
where the various dependency objects are set to a null literal
which causes the destructuring defaults to not apply, so we
should guard against that for a bit of added safety here.
  • Loading branch information
nlf committed Mar 26, 2021
1 parent b969466 commit 234a120
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/get-dep-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = (mani, name) => {
peerDependencies: peerDeps = {},
} = mani

return typeof deps[name] === 'string' ? deps[name]
: typeof optDeps[name] === 'string' ? optDeps[name]
: typeof peerDeps[name] === 'string' ? peerDeps[name]
return deps && typeof deps[name] === 'string' ? deps[name]
: optDeps && typeof optDeps[name] === 'string' ? optDeps[name]
: peerDeps && typeof peerDeps[name] === 'string' ? peerDeps[name]
: null
}
34 changes: 29 additions & 5 deletions test/get-dep-spec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
const t = require('tap')
const getDepSpec = require('../lib/get-dep-spec.js')

t.equal(getDepSpec({ dependencies: { dep: '1' } }, 'dep'), '1')
t.equal(getDepSpec({ optionalDependencies: { dep: '1' } }, 'dep'), '1')
t.equal(getDepSpec({ peerDependencies: { dep: '1' } }, 'dep'), '1')
t.equal(getDepSpec({
dependencies: { dep: '1' }
}, 'dep'), '1')

t.equal(getDepSpec({
optionalDependencies: { dep: '1' }
}, 'dep'), '1')

t.equal(getDepSpec({
dependencies: null,
optionalDependencies: { dep: '1' }
}, 'dep'), '1', 'allows dependencies to be null')

t.equal(getDepSpec({
peerDependencies: { dep: '1' }
}, 'dep'), '1')

t.equal(getDepSpec({
dependencies: null,
optioanlDependencies: null,
peerDependencies: { dep: '1' }
}, 'dep'), '1', 'allows optionalDependencies to be null')

t.equal(getDepSpec({
dependencies: { dep: '1' },
optionalDependencies: { dep: '2' },
}, 'dep'), '1', 'prefer prod deps over optional')

t.equal(getDepSpec({
dependencies: { dep: '1' },
peerDependencies: { dep: '2' },
}, 'dep'), '1', 'prefer prod deps over peer')

t.equal(getDepSpec({
optionalDependencies: { dep: '1' },
peerDependencies: { dep: '2' },
}, 'dep'), '1', 'prefer optional deps over peer')
t.equal(getDepSpec({ devDependencies: { dep: '1' } }, 'dep'), null,
'ignore dev')

t.equal(getDepSpec({
devDependencies: { dep: '1' }
}, 'dep'), null, 'ignore dev')

0 comments on commit 234a120

Please sign in to comment.