Skip to content

Commit 14fdf06

Browse files
author
Steven Hargrove
committed
moved package reducer to rule, to allow pkg locator to be more generic
1 parent c40029f commit 14fdf06

3 files changed

Lines changed: 64 additions & 35 deletions

File tree

src/core/CachedPackageLocator.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,12 @@ import path from 'path'
22
import os from 'os'
33
import fs from 'fs'
44

5-
function notEmpty(obj) {
6-
return Object.keys(obj).length
7-
}
8-
9-
function reducePackage({
10-
dependencies = {},
11-
devDependencies = {},
12-
peerDependencies = {},
13-
optionalDependencies = {},
14-
} = {}) {
15-
if ([dependencies, devDependencies, peerDependencies, optionalDependencies].some(notEmpty)) {
16-
return { dependencies, devDependencies, peerDependencies, optionalDependencies }
17-
}
18-
19-
return null
20-
}
215
export default class CachedPackageLocator {
226
constructor() {
237
this.store = {}
248
}
259

26-
readUpSync(context, dirname, immediate) {
10+
readUpSync(context, dirname, immediate, reduce) {
2711
const locations = []
2812
do {
2913
const location = path.join(dirname, 'package.json')
@@ -38,7 +22,7 @@ export default class CachedPackageLocator {
3822
}
3923

4024
try {
41-
this.store[location] = reducePackage(
25+
this.store[location] = reduce(
4226
JSON.parse(fs.readFileSync(location, 'utf8'))
4327
)
4428

src/rules/no-extraneous-dependencies.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ import docsUrl from '../docsUrl'
88

99
const packageLocator = new CachedPackageLocator()
1010

11+
function notEmpty(obj) {
12+
return Object.keys(obj).length
13+
}
14+
15+
function reducePackage({
16+
dependencies = {},
17+
devDependencies = {},
18+
peerDependencies = {},
19+
optionalDependencies = {},
20+
} = {}) {
21+
if ([dependencies, devDependencies, peerDependencies, optionalDependencies].some(notEmpty)) {
22+
return { dependencies, devDependencies, peerDependencies, optionalDependencies }
23+
}
24+
25+
return null
26+
}
27+
1128
function missingErrorMessage(packageName) {
1229
return `'${packageName}' should be listed in the project's dependencies. ` +
1330
`Run 'npm i -S ${packageName}' to add it`
@@ -104,7 +121,8 @@ module.exports = {
104121
const deps = packageLocator.readUpSync(
105122
context,
106123
options.packageDir || path.dirname(context.getFilename()),
107-
typeof options.packageDir !== 'undefined'
124+
typeof options.packageDir !== 'undefined',
125+
reducePackage
108126
)
109127

110128
if (!deps) {

tests/src/core/CachedPackageLocator.js

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ import { expect } from 'chai'
44

55
import CachedPackageLocator from '../../../src/core/CachedPackageLocator'
66

7+
function notEmpty(obj) {
8+
return Object.keys(obj).length
9+
}
10+
11+
function reduce({
12+
dependencies = {},
13+
devDependencies = {},
14+
peerDependencies = {},
15+
optionalDependencies = {},
16+
} = {}) {
17+
if ([dependencies, devDependencies, peerDependencies, optionalDependencies].some(notEmpty)) {
18+
return { dependencies, devDependencies, peerDependencies, optionalDependencies }
19+
}
20+
21+
return null
22+
}
23+
724
describe('CachedPackageLocator.readUpSync()', function () {
825
let sandbox
926
let packageLocator
@@ -49,18 +66,20 @@ describe('CachedPackageLocator.readUpSync()', function () {
4966
it('should not repeat fs.readFileSync on stored locations', function () {
5067
fs.readFileSync.withArgs('/a/package.json').returns(withDepsStr)
5168

52-
expect(packageLocator.readUpSync(context, '/a/b')).to.deep.equal(withDeps)
69+
expect(packageLocator.readUpSync(context, '/a/b', false, reduce))
70+
.to.deep.equal(withDeps)
5371
sinon.assert.callCount(fs.readFileSync, 2)
54-
expect(packageLocator.readUpSync(context, '/a')).to.deep.equal(withDeps)
72+
expect(packageLocator.readUpSync(context, '/a', false, reduce))
73+
.to.deep.equal(withDeps)
5574
sinon.assert.callCount(fs.readFileSync, 2)
5675
expect(packageLocator.store).to.deep.equal({
5776
'/a/b/package.json': null,
5877
'/a/package.json': withDeps,
5978
})
6079

61-
expect(packageLocator.readUpSync(context, '/x')).to.be.undefined
80+
expect(packageLocator.readUpSync(context, '/x', false, reduce)).to.be.undefined
6281
sinon.assert.callCount(fs.readFileSync, 4)
63-
expect(packageLocator.readUpSync(context, '/x')).to.be.undefined
82+
expect(packageLocator.readUpSync(context, '/x', false, reduce)).to.be.undefined
6483
sinon.assert.callCount(fs.readFileSync, 4)
6584
expect(packageLocator.store).to.deep.equal({
6685
'/x/package.json': null,
@@ -69,9 +88,9 @@ describe('CachedPackageLocator.readUpSync()', function () {
6988
'/package.json': null,
7089
})
7190

72-
expect(packageLocator.readUpSync(context, '/x/y/z')).to.be.undefined
91+
expect(packageLocator.readUpSync(context, '/x/y/z', false, reduce)).to.be.undefined
7392
sinon.assert.callCount(fs.readFileSync, 6)
74-
expect(packageLocator.readUpSync(context, '/x/y/z')).to.be.undefined
93+
expect(packageLocator.readUpSync(context, '/x/y/z', false, reduce)).to.be.undefined
7594
sinon.assert.callCount(fs.readFileSync, 6)
7695
expect(packageLocator.store).to.deep.equal({
7796
'/x/y/z/package.json': null,
@@ -82,9 +101,9 @@ describe('CachedPackageLocator.readUpSync()', function () {
82101
'/package.json': null,
83102
})
84103

85-
expect(packageLocator.readUpSync(context, '/x/w')).to.be.undefined
104+
expect(packageLocator.readUpSync(context, '/x/w', false, reduce)).to.be.undefined
86105
sinon.assert.callCount(fs.readFileSync, 7)
87-
expect(packageLocator.readUpSync(context, '/x/w')).to.be.undefined
106+
expect(packageLocator.readUpSync(context, '/x/w', false, reduce)).to.be.undefined
88107
sinon.assert.callCount(fs.readFileSync, 7)
89108

90109
expect(packageLocator.store).to.deep.equal({
@@ -100,7 +119,8 @@ describe('CachedPackageLocator.readUpSync()', function () {
100119

101120
it('should only store and return dependency fields', function () {
102121
fs.readFileSync.withArgs('/package.json').returns(withDepsExtraFieldsStr)
103-
expect(packageLocator.readUpSync(context, '/')).to.deep.equal(withDeps)
122+
expect(packageLocator.readUpSync(context, '/', false, reduce))
123+
.to.deep.equal(withDeps)
104124
expect(packageLocator.store).to.deep.equal({
105125
'/package.json': withDeps,
106126
})
@@ -109,7 +129,8 @@ describe('CachedPackageLocator.readUpSync()', function () {
109129

110130
it('should locate first available', function () {
111131
fs.readFileSync.withArgs('/a/b/package.json').returns(withDepsStr)
112-
expect(packageLocator.readUpSync(context, '/a/b')).to.deep.equal(withDeps)
132+
expect(packageLocator.readUpSync(context, '/a/b', false, reduce))
133+
.to.deep.equal(withDeps)
113134
expect(packageLocator.store).to.deep.equal({
114135
'/a/b/package.json': withDeps,
115136
})
@@ -118,7 +139,8 @@ describe('CachedPackageLocator.readUpSync()', function () {
118139

119140
it('should locate last available', function () {
120141
fs.readFileSync.withArgs('/package.json').returns(withDepsStr)
121-
expect(packageLocator.readUpSync(context, '/a/b/c/d/e/f')).to.deep.equal(withDeps)
142+
expect(packageLocator.readUpSync(context, '/a/b/c/d/e/f', false, reduce))
143+
.to.deep.equal(withDeps)
122144
expect(packageLocator.store).to.deep.equal({
123145
'/a/b/c/d/e/f/package.json': null,
124146
'/a/b/c/d/e/package.json': null,
@@ -133,7 +155,8 @@ describe('CachedPackageLocator.readUpSync()', function () {
133155

134156
it('should store package.json with empty deps as null', function () {
135157
fs.readFileSync.withArgs('/package.json').returns('{}')
136-
expect(packageLocator.readUpSync(context, '/')).to.be.undefined
158+
expect(packageLocator.readUpSync(context, '/', false, reduce))
159+
.to.be.undefined
137160
expect(packageLocator.store).to.deep.equal({
138161
'/package.json': null,
139162
})
@@ -145,9 +168,11 @@ describe('CachedPackageLocator.readUpSync()', function () {
145168
.withArgs('/package.json').returns(withDepsStr)
146169
.withArgs('/a/package.json').returns(withUnexpectedTokenStr)
147170
.withArgs('/a/b/package.json').returns(withUnexpectedEndStr)
148-
expect(packageLocator.readUpSync(context, '/a')).to.be.undefined
171+
expect(packageLocator.readUpSync(context, '/a', false))
172+
.to.be.undefined
149173
expect(packageLocator.store).to.be.empty
150-
expect(packageLocator.readUpSync(context, '/a/b/c/d')).to.be.undefined
174+
expect(packageLocator.readUpSync(context, '/a/b/c/d', false))
175+
.to.be.undefined
151176
expect(packageLocator.store).to.deep.equal({
152177
'/a/b/c/d/package.json': null,
153178
'/a/b/c/package.json': null,
@@ -156,7 +181,8 @@ describe('CachedPackageLocator.readUpSync()', function () {
156181
})
157182

158183
it('should store failed locations as null', function () {
159-
expect(packageLocator.readUpSync(context, '/does/not/exist')).to.be.undefined
184+
expect(packageLocator.readUpSync(context, '/does/not/exist', false))
185+
.to.be.undefined
160186
expect(packageLocator.store).to.deep.equal({
161187
'/does/not/exist/package.json': null,
162188
'/does/not/package.json': null,
@@ -167,7 +193,8 @@ describe('CachedPackageLocator.readUpSync()', function () {
167193
})
168194

169195
it('immediate=true should halt on first failed location', function () {
170-
expect(packageLocator.readUpSync(context, '/does/not/exist', true)).to.be.undefined
196+
expect(packageLocator.readUpSync(context, '/does/not/exist', true))
197+
.to.be.undefined
171198
expect(packageLocator.store).to.deep.equal({
172199
'/does/not/exist/package.json': null
173200
})

0 commit comments

Comments
 (0)