Skip to content

Commit 32abf7c

Browse files
committed
Refactor code style
* Use `TypeError`s instead of generic `Error`s where applicable * Use `Array.isArray` instead of `'length' in` check * Reword the complex `invoke` to the more clear `call` in error message * `const` and `let`
1 parent dc46bc5 commit 32abf7c

File tree

14 files changed

+324
-342
lines changed

14 files changed

+324
-342
lines changed

index.js

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {VFile} from 'vfile'
88
// Expose a frozen processor.
99
export const unified = base().freeze()
1010

11-
var slice = [].slice
12-
var own = {}.hasOwnProperty
11+
const slice = [].slice
12+
const own = {}.hasOwnProperty
1313

1414
// Process pipeline.
15-
var pipeline = trough()
15+
const pipeline = trough()
1616
.use(pipelineParse)
1717
.use(pipelineRun)
1818
.use(pipelineStringify)
@@ -36,7 +36,7 @@ function pipelineRun(p, ctx, next) {
3636
}
3737

3838
function pipelineStringify(p, ctx) {
39-
var result = p.stringify(ctx.tree, ctx.file)
39+
const result = p.stringify(ctx.tree, ctx.file)
4040

4141
if (result === undefined || result === null) {
4242
// Empty.
@@ -49,11 +49,11 @@ function pipelineStringify(p, ctx) {
4949

5050
// Function to create the first processor.
5151
function base() {
52-
var attachers = []
53-
var transformers = trough()
54-
var namespace = {}
55-
var freezeIndex = -1
56-
var frozen
52+
const attachers = []
53+
const transformers = trough()
54+
let namespace = {}
55+
let freezeIndex = -1
56+
let frozen
5757

5858
// Data management.
5959
processor.data = data
@@ -78,8 +78,8 @@ function base() {
7878

7979
// Create a new processor based on the processor in the current scope.
8080
function processor() {
81-
var destination = base()
82-
var index = -1
81+
const destination = base()
82+
let index = -1
8383

8484
while (++index < attachers.length) {
8585
destination.use.apply(null, attachers[index])
@@ -94,19 +94,16 @@ function base() {
9494
//
9595
// For example, take unified itself: it’s frozen.
9696
// Plugins should not be added to it.
97-
// Rather, it should be extended, by invoking it, before modifying it.
97+
// Rather, it should be extended, by calling it, before modifying it.
9898
//
99-
// In essence, always invoke this when exporting a processor.
99+
// In essence, always call this when exporting a processor.
100100
function freeze() {
101-
var values
102-
var transformer
103-
104101
if (frozen) {
105102
return processor
106103
}
107104

108105
while (++freezeIndex < attachers.length) {
109-
values = attachers[freezeIndex]
106+
const values = attachers[freezeIndex]
110107

111108
if (values[1] === false) {
112109
continue
@@ -116,15 +113,15 @@ function base() {
116113
values[1] = undefined
117114
}
118115

119-
transformer = values[0].apply(processor, values.slice(1))
116+
const transformer = values[0].apply(processor, values.slice(1))
120117

121118
if (typeof transformer === 'function') {
122119
transformers.use(transformer)
123120
}
124121
}
125122

126123
frozen = true
127-
freezeIndex = Infinity
124+
freezeIndex = Number.POSITIVE_INFINITY
128125

129126
return processor
130127
}
@@ -163,7 +160,7 @@ function base() {
163160
// * a list of presets, attachers, and arguments (list of attachers and
164161
// options).
165162
function use(value) {
166-
var settings
163+
let settings
167164

168165
assertUnfrozen('use', frozen)
169166

@@ -172,13 +169,13 @@ function base() {
172169
} else if (typeof value === 'function') {
173170
addPlugin(...arguments)
174171
} else if (typeof value === 'object') {
175-
if ('length' in value) {
172+
if (Array.isArray(value)) {
176173
addList(value)
177174
} else {
178175
addPreset(value)
179176
}
180177
} else {
181-
throw new Error('Expected usable value, not `' + value + '`')
178+
throw new TypeError('Expected usable value, not `' + value + '`')
182179
}
183180

184181
if (settings) {
@@ -199,32 +196,32 @@ function base() {
199196
if (typeof value === 'function') {
200197
addPlugin(value)
201198
} else if (typeof value === 'object') {
202-
if ('length' in value) {
199+
if (Array.isArray(value)) {
203200
addPlugin(...value)
204201
} else {
205202
addPreset(value)
206203
}
207204
} else {
208-
throw new Error('Expected usable value, not `' + value + '`')
205+
throw new TypeError('Expected usable value, not `' + value + '`')
209206
}
210207
}
211208

212209
function addList(plugins) {
213-
var index = -1
210+
let index = -1
214211

215212
if (plugins === null || plugins === undefined) {
216213
// Empty.
217-
} else if (typeof plugins === 'object' && 'length' in plugins) {
214+
} else if (Array.isArray(plugins)) {
218215
while (++index < plugins.length) {
219216
add(plugins[index])
220217
}
221218
} else {
222-
throw new Error('Expected a list of plugins, not `' + plugins + '`')
219+
throw new TypeError('Expected a list of plugins, not `' + plugins + '`')
223220
}
224221
}
225222

226223
function addPlugin(plugin, value) {
227-
var entry = find(plugin)
224+
const entry = find(plugin)
228225

229226
if (entry) {
230227
if (isPlainObj(entry[1]) && isPlainObj(value)) {
@@ -239,7 +236,7 @@ function base() {
239236
}
240237

241238
function find(plugin) {
242-
var index = -1
239+
let index = -1
243240

244241
while (++index < attachers.length) {
245242
if (attachers[index][0] === plugin) {
@@ -251,11 +248,9 @@ function base() {
251248
// Parse a file (in string or vfile representation) into a unist node using
252249
// the `Parser` on the processor.
253250
function parse(doc) {
254-
var file = vfile(doc)
255-
var Parser
256-
257251
freeze()
258-
Parser = processor.Parser
252+
const file = vfile(doc)
253+
const Parser = processor.Parser
259254
assertParser('parse', Parser)
260255

261256
if (newable(Parser, 'parse')) {
@@ -301,8 +296,8 @@ function base() {
301296
// Run transforms on a unist node representation of a file (in string or
302297
// vfile representation), sync.
303298
function runSync(node, file) {
304-
var result
305-
var complete
299+
let result
300+
let complete
306301

307302
run(node, file, done)
308303

@@ -311,20 +306,18 @@ function base() {
311306
return result
312307

313308
function done(error, tree) {
314-
complete = true
315309
result = tree
310+
complete = true
316311
bail(error)
317312
}
318313
}
319314

320315
// Stringify a unist node representation of a file (in string or vfile
321316
// representation) into a string using the `Compiler` on the processor.
322317
function stringify(node, doc) {
323-
var file = vfile(doc)
324-
var Compiler
325-
326318
freeze()
327-
Compiler = processor.Compiler
319+
const file = vfile(doc)
320+
const Compiler = processor.Compiler
328321
assertCompiler('stringify', Compiler)
329322
assertNode(node)
330323

@@ -351,7 +344,7 @@ function base() {
351344
executor(null, cb)
352345

353346
function executor(resolve, reject) {
354-
var file = vfile(doc)
347+
const file = vfile(doc)
355348

356349
pipeline.run(processor, {file}, done)
357350

@@ -369,13 +362,11 @@ function base() {
369362

370363
// Process the given document (in string or vfile representation), sync.
371364
function processSync(doc) {
372-
var file
373-
var complete
374-
375365
freeze()
376366
assertParser('processSync', processor.Parser)
377367
assertCompiler('processSync', processor.Compiler)
378-
file = vfile(doc)
368+
let complete
369+
const file = vfile(doc)
379370

380371
process(file, done)
381372

@@ -404,9 +395,12 @@ function newable(value, name) {
404395

405396
// Check if `value` is an object with keys.
406397
function keys(value) {
407-
var key
398+
let key
399+
408400
for (key in value) {
409-
return true
401+
if (own.call(value, key)) {
402+
return true
403+
}
410404
}
411405

412406
return false
@@ -415,32 +409,32 @@ function keys(value) {
415409
// Assert a parser is available.
416410
function assertParser(name, Parser) {
417411
if (typeof Parser !== 'function') {
418-
throw new Error('Cannot `' + name + '` without `Parser`')
412+
throw new TypeError('Cannot `' + name + '` without `Parser`')
419413
}
420414
}
421415

422416
// Assert a compiler is available.
423417
function assertCompiler(name, Compiler) {
424418
if (typeof Compiler !== 'function') {
425-
throw new Error('Cannot `' + name + '` without `Compiler`')
419+
throw new TypeError('Cannot `' + name + '` without `Compiler`')
426420
}
427421
}
428422

429423
// Assert the processor is not frozen.
430424
function assertUnfrozen(name, frozen) {
431425
if (frozen) {
432426
throw new Error(
433-
'Cannot invoke `' +
427+
'Cannot call `' +
434428
name +
435-
'` on a frozen processor.\nCreate a new processor first, by invoking it: use `processor()` instead of `processor`.'
429+
'` on a frozen processor.\nCreate a new processor first, by calling it: use `processor()` instead of `processor`.'
436430
)
437431
}
438432
}
439433

440434
// Assert `node` is a unist node.
441435
function assertNode(node) {
442436
if (!node || typeof node.type !== 'string') {
443-
throw new Error('Expected node, got `' + node + '`')
437+
throw new TypeError('Expected node, got `' + node + '`')
444438
}
445439
}
446440

package.json

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,6 @@
8888
},
8989
"xo": {
9090
"prettier": true,
91-
"rules": {
92-
"no-var": "off",
93-
"prefer-arrow-callback": "off",
94-
"guard-for-in": "off",
95-
"no-unreachable-loop": "off",
96-
"unicorn/prefer-number-properties": "off",
97-
"unicorn/prefer-optional-catch-binding": "off",
98-
"unicorn/prefer-reflect-apply": "off",
99-
"unicorn/prefer-type-error": "off"
100-
},
10191
"ignores": [
10292
"types/"
10393
]

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ Yields:
938938
throw new Error(
939939
^
940940
941-
Error: Cannot invoke `use` on a frozen processor.
941+
Error: Cannot call `use` on a frozen processor.
942942
Create a new processor first, by invoking it: use `processor()` instead of `processor`.
943943
at assertUnfrozen (~/node_modules/unified/index.js:440:11)
944944
at Function.use (~/node_modules/unified/index.js:172:5)

test/async-function.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import test from 'tape'
22
import {VFile} from 'vfile'
33
import {unified} from '../index.js'
44

5-
test('async function transformer () {}', function (t) {
6-
var givenFile = new VFile('alpha')
7-
var givenNode = {type: 'bravo'}
8-
var modifiedNode = {type: 'charlie'}
5+
test('async function transformer () {}', (t) => {
6+
const givenFile = new VFile('alpha')
7+
const givenNode = {type: 'bravo'}
8+
const modifiedNode = {type: 'charlie'}
99

1010
t.plan(5)
1111

1212
unified()
1313
.use(plugin)
14-
.run(givenNode, givenFile, function (error, tree, file) {
14+
.run(givenNode, givenFile, (error, tree, file) => {
1515
t.error(error, 'should’t fail')
1616
t.equal(tree, modifiedNode, 'passes given tree to `done`')
1717
t.equal(file, givenFile, 'passes given file to `done`')

test/core.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import test from 'tape'
22
import {unified} from '../index.js'
33

4-
test('unified()', function (t) {
5-
var count
6-
var processor
7-
var otherProcessor
4+
test('unified()', (t) => {
5+
let count
86

97
t.throws(
10-
function () {
8+
() => {
119
unified.use(Function.prototype)
1210
},
13-
/Cannot invoke `use` on a frozen processor/,
11+
/Cannot call `use` on a frozen processor/,
1412
'should be frozen'
1513
)
1614

17-
processor = unified()
15+
const processor = unified()
1816

1917
t.equal(typeof processor, 'function', 'should return a function')
2018

@@ -24,18 +22,18 @@ test('unified()', function (t) {
2422
})
2523

2624
count = 0
27-
otherProcessor = processor().freeze()
25+
const otherProcessor = processor().freeze()
2826

2927
t.equal(
3028
count,
3129
1,
32-
'should create a new processor implementing the ancestral processor when invoked (#1)'
30+
'should create a new processor implementing the ancestral processor when called (#1)'
3331
)
3432

3533
t.equal(
3634
otherProcessor.data('foo'),
3735
'bar',
38-
'should create a new processor implementing the ancestral processor when invoked (#2)'
36+
'should create a new processor implementing the ancestral processor when called (#2)'
3937
)
4038

4139
t.end()

test/data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import test from 'tape'
22
import {unified} from '../index.js'
33

4-
test('data(key[, value])', function (t) {
5-
var processor = unified()
4+
test('data(key[, value])', (t) => {
5+
const processor = unified()
66

77
t.equal(
88
processor.data('foo', 'bar'),

0 commit comments

Comments
 (0)