Skip to content

Commit 307fc2c

Browse files
committed
fix: linting
1 parent 4ceacc3 commit 307fc2c

File tree

2 files changed

+54
-42
lines changed

2 files changed

+54
-42
lines changed

lib/index.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {promisify} = require('util')
1+
const { promisify } = require('util')
22
const fs = require('fs')
33
const readFile = promisify(fs.readFile)
44
const lstat = promisify(fs.lstat)
@@ -18,12 +18,14 @@ const normalizePackageBin = require('npm-normalize-package-bin')
1818

1919
// load the directories.bin folder as a 'bin' object
2020
const readBinDir = async (path, data) => {
21-
if (data.bin)
21+
if (data.bin) {
2222
return data
23+
}
2324

2425
const m = data.directories && data.directories.bin
25-
if (!m || typeof m !== 'string')
26+
if (!m || typeof m !== 'string') {
2627
return data
28+
}
2729

2830
// cut off any monkey business, like setting directories.bin
2931
// to ../../../etc/passwd or /etc/passwd or something like that.
@@ -36,30 +38,33 @@ const readBinDir = async (path, data) => {
3638
const walkBinDir = async (root, dir, obj) => {
3739
const entries = await readdir(resolve(root, dir)).catch(() => [])
3840
for (const entry of entries) {
39-
if (entry.charAt(0) === '.')
41+
if (entry.charAt(0) === '.') {
4042
continue
43+
}
4144
const f = resolve(root, dir, entry)
4245
// ignore stat errors, weird file types, symlinks, etc.
4346
const st = await lstat(f).catch(() => null)
44-
if (!st)
47+
if (!st) {
4548
continue
46-
else if (st.isFile())
49+
} else if (st.isFile()) {
4750
obj[entry] = relative(root, f)
48-
else if (st.isDirectory())
51+
} else if (st.isDirectory()) {
4952
await walkBinDir(root, join(dir, entry), obj)
53+
}
5054
}
5155
return obj
5256
}
5357

5458
// do not preserve _fields set in files, they are sus
5559
const stripUnderscores = data => {
56-
for (const key of Object.keys(data).filter(k => /^_/.test(k)))
60+
for (const key of Object.keys(data).filter(k => /^_/.test(k))) {
5761
delete data[key]
62+
}
5863
return data
5964
}
6065

6166
const normalize = data => {
62-
add_id(data)
67+
addId(data)
6368
fixBundled(data)
6469
pruneRepeatedOptionals(data)
6570
fixScripts(data)
@@ -70,9 +75,10 @@ const normalize = data => {
7075

7176
rpj.normalize = normalize
7277

73-
const add_id = data => {
74-
if (data.name && data.version)
78+
const addId = data => {
79+
if (data.name && data.version) {
7580
data._id = `${data.name}@${data.version}`
81+
}
7682
return data
7783
}
7884

@@ -88,8 +94,9 @@ const pruneRepeatedOptionals = data => {
8894
delete dd[name]
8995
}
9096
}
91-
if (Object.keys(dd).length === 0)
97+
if (Object.keys(dd).length === 0) {
9298
delete data.dependencies
99+
}
93100
return data
94101
}
95102

@@ -98,17 +105,19 @@ const fixBundled = data => {
98105
const bd = data.bundleDependencies === undefined ? bdd
99106
: data.bundleDependencies
100107

101-
if (bd === false)
108+
if (bd === false) {
102109
data.bundleDependencies = []
103-
else if (bd === true)
110+
} else if (bd === true) {
104111
data.bundleDependencies = Object.keys(data.dependencies || {})
105-
else if (bd && typeof bd === 'object') {
106-
if (!Array.isArray(bd))
112+
} else if (bd && typeof bd === 'object') {
113+
if (!Array.isArray(bd)) {
107114
data.bundleDependencies = Object.keys(bd)
108-
else
115+
} else {
109116
data.bundleDependencies = bd
110-
} else
117+
}
118+
} else {
111119
delete data.bundleDependencies
120+
}
112121

113122
delete data.bundledDependencies
114123
return data
@@ -121,15 +130,17 @@ const fixScripts = data => {
121130
}
122131

123132
for (const [name, script] of Object.entries(data.scripts)) {
124-
if (typeof script !== 'string')
133+
if (typeof script !== 'string') {
125134
delete data.scripts[name]
135+
}
126136
}
127137
return data
128138
}
129139

130140
const fixFunding = data => {
131-
if (data.funding && typeof data.funding === 'string')
141+
if (data.funding && typeof data.funding === 'string') {
132142
data.funding = { url: data.funding }
143+
}
133144
return data
134145
}
135146

test/basic.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ t.test('clean up bundleddddddDependencies', async t => {
4646
}),
4747
}) + '/package.json'), { bundleDependencies: [] }))
4848

49-
5049
t.test('handle bundleDependencies: false', t =>
5150
t.resolveMatch(rpj(t.testdir({
5251
'package.json': JSON.stringify({
@@ -77,29 +76,29 @@ t.test('clean up scripts', async t => {
7776
'package.json': JSON.stringify({
7877
scripts: {
7978
foo: 'bar',
80-
bar: [ 'baz' ],
79+
bar: ['baz'],
8180
baz: { bar: { foo: 'barbaz' } },
8281
},
83-
})
82+
}),
8483
}) + '/package.json'), {
8584
scripts: {
8685
foo: 'bar',
8786
bar: undefined,
8887
baz: undefined,
89-
}
88+
},
9089
}))
9190
})
9291

9392
t.test('convert funding string to object', t =>
9493
t.resolveMatch(rpj(t.testdir({
95-
'package.json': JSON.stringify({ funding: 'hello' })
94+
'package.json': JSON.stringify({ funding: 'hello' }),
9695
}) + '/package.json'), { funding: { url: 'hello' } }))
9796

9897
t.test('cleanup bins', async t => {
9998
t.test('handle string when a name is set', t =>
10099
t.resolveMatch(rpj(t.testdir({
101100
'package.json': JSON.stringify({ name: 'x', bin: 'y' }),
102-
}) + '/package.json'), { bin: { x: 'y' }}))
101+
}) + '/package.json'), { bin: { x: 'y' } }))
103102

104103
t.test('delete string bin when no name', t =>
105104
t.resolveMatch(rpj(t.testdir({
@@ -117,49 +116,49 @@ t.test('cleanup bins', async t => {
117116
x: 'y',
118117
y: 1234,
119118
z: { a: 'b' },
120-
}}),
121-
}) + '/package.json'), { bin: {x:'y', y: undefined, z: undefined }}))
119+
} }),
120+
}) + '/package.json'), { bin: { x: 'y', y: undefined, z: undefined } }))
122121
})
123122

124123
t.test('dedupe optional deps out of regular deps', async t => {
125124
t.test('choose optional deps in conflict', t =>
126125
t.resolveMatch(rpj(t.testdir({
127126
'package.json': JSON.stringify({
128127
optionalDependencies: {
129-
whowins: '1.2.3-optional'
128+
whowins: '1.2.3-optional',
130129
},
131130
dependencies: {
132-
whowins: '1.2.3-prod'
133-
}
131+
whowins: '1.2.3-prod',
132+
},
134133
}),
135134
}) + '/package.json'), {
136135
optionalDependencies: {
137-
whowins: '1.2.3-optional'
136+
whowins: '1.2.3-optional',
138137
},
139138
}))
140139

141140
t.test('do not create regular deps if only optional specified', t =>
142141
t.resolveMatch(rpj(t.testdir({
143142
'package.json': JSON.stringify({
144143
optionalDependencies: {
145-
whowins: '1.2.3-optional'
144+
whowins: '1.2.3-optional',
146145
},
147146
}),
148147
}) + '/package.json'), {
149148
optionalDependencies: {
150-
whowins: '1.2.3-optional'
149+
whowins: '1.2.3-optional',
151150
},
152151
}))
153152
})
154153

155154
t.test('set _id if name and version set', t =>
156155
t.resolveMatch(rpj(t.testdir({
157-
'package.json': JSON.stringify({name:'a', version: '1.2.3'}),
156+
'package.json': JSON.stringify({ name: 'a', version: '1.2.3' }),
158157
}) + '/package.json'), { _id: '[email protected]' }))
159158

160159
t.test('exports the normalize function', async t =>
161-
t.same(rpj.normalize({ bundledDependencies: true, dependencies: {a:'1'}}),
162-
{ bundleDependencies: ['a'], dependencies: {a:'1'}}))
160+
t.same(rpj.normalize({ bundledDependencies: true, dependencies: { a: '1' } }),
161+
{ bundleDependencies: ['a'], dependencies: { a: '1' } }))
163162

164163
t.test('preserve indentation', async t => {
165164
const obj = {
@@ -278,20 +277,22 @@ t.test('strip _fields', async t => {
278277
t.test('load directories.bin', async t => {
279278
const { basename } = require('path')
280279
const fs = require('fs')
281-
const rpj = t.mock('../', {
280+
const rpjMock = t.mock('../', {
282281
fs: {
283282
...fs,
284283
lstat: (p, cb) => {
285-
if (basename(p) === 'staterror')
284+
if (basename(p) === 'staterror') {
286285
cb(new Error('stat error'))
287-
else
286+
} else {
288287
return fs.lstat(p, cb)
288+
}
289289
},
290290
readdir: (p, cb) => {
291291
if (basename(p) === 'readdirerror') {
292292
cb(new Error('readdir error'))
293-
} else
293+
} else {
294294
return fs.readdir(p, cb)
295+
}
295296
},
296297
},
297298
})
@@ -323,7 +324,7 @@ t.test('load directories.bin', async t => {
323324
},
324325
},
325326
})
326-
t.strictSame(await rpj(`${path}/package.json`), {
327+
t.strictSame(await rpjMock(`${path}/package.json`), {
327328
name: 'foo',
328329
version: '1.2.3',
329330

0 commit comments

Comments
 (0)