@@ -8,11 +8,11 @@ import {VFile} from 'vfile'
8
8
// Expose a frozen processor.
9
9
export const unified = base ( ) . freeze ( )
10
10
11
- var slice = [ ] . slice
12
- var own = { } . hasOwnProperty
11
+ const slice = [ ] . slice
12
+ const own = { } . hasOwnProperty
13
13
14
14
// Process pipeline.
15
- var pipeline = trough ( )
15
+ const pipeline = trough ( )
16
16
. use ( pipelineParse )
17
17
. use ( pipelineRun )
18
18
. use ( pipelineStringify )
@@ -36,7 +36,7 @@ function pipelineRun(p, ctx, next) {
36
36
}
37
37
38
38
function pipelineStringify ( p , ctx ) {
39
- var result = p . stringify ( ctx . tree , ctx . file )
39
+ const result = p . stringify ( ctx . tree , ctx . file )
40
40
41
41
if ( result === undefined || result === null ) {
42
42
// Empty.
@@ -49,11 +49,11 @@ function pipelineStringify(p, ctx) {
49
49
50
50
// Function to create the first processor.
51
51
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
57
57
58
58
// Data management.
59
59
processor . data = data
@@ -78,8 +78,8 @@ function base() {
78
78
79
79
// Create a new processor based on the processor in the current scope.
80
80
function processor ( ) {
81
- var destination = base ( )
82
- var index = - 1
81
+ const destination = base ( )
82
+ let index = - 1
83
83
84
84
while ( ++ index < attachers . length ) {
85
85
destination . use . apply ( null , attachers [ index ] )
@@ -94,19 +94,16 @@ function base() {
94
94
//
95
95
// For example, take unified itself: it’s frozen.
96
96
// 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.
98
98
//
99
- // In essence, always invoke this when exporting a processor.
99
+ // In essence, always call this when exporting a processor.
100
100
function freeze ( ) {
101
- var values
102
- var transformer
103
-
104
101
if ( frozen ) {
105
102
return processor
106
103
}
107
104
108
105
while ( ++ freezeIndex < attachers . length ) {
109
- values = attachers [ freezeIndex ]
106
+ const values = attachers [ freezeIndex ]
110
107
111
108
if ( values [ 1 ] === false ) {
112
109
continue
@@ -116,15 +113,15 @@ function base() {
116
113
values [ 1 ] = undefined
117
114
}
118
115
119
- transformer = values [ 0 ] . apply ( processor , values . slice ( 1 ) )
116
+ const transformer = values [ 0 ] . apply ( processor , values . slice ( 1 ) )
120
117
121
118
if ( typeof transformer === 'function' ) {
122
119
transformers . use ( transformer )
123
120
}
124
121
}
125
122
126
123
frozen = true
127
- freezeIndex = Infinity
124
+ freezeIndex = Number . POSITIVE_INFINITY
128
125
129
126
return processor
130
127
}
@@ -163,7 +160,7 @@ function base() {
163
160
// * a list of presets, attachers, and arguments (list of attachers and
164
161
// options).
165
162
function use ( value ) {
166
- var settings
163
+ let settings
167
164
168
165
assertUnfrozen ( 'use' , frozen )
169
166
@@ -172,13 +169,13 @@ function base() {
172
169
} else if ( typeof value === 'function' ) {
173
170
addPlugin ( ...arguments )
174
171
} else if ( typeof value === 'object' ) {
175
- if ( 'length' in value ) {
172
+ if ( Array . isArray ( value ) ) {
176
173
addList ( value )
177
174
} else {
178
175
addPreset ( value )
179
176
}
180
177
} else {
181
- throw new Error ( 'Expected usable value, not `' + value + '`' )
178
+ throw new TypeError ( 'Expected usable value, not `' + value + '`' )
182
179
}
183
180
184
181
if ( settings ) {
@@ -199,32 +196,32 @@ function base() {
199
196
if ( typeof value === 'function' ) {
200
197
addPlugin ( value )
201
198
} else if ( typeof value === 'object' ) {
202
- if ( 'length' in value ) {
199
+ if ( Array . isArray ( value ) ) {
203
200
addPlugin ( ...value )
204
201
} else {
205
202
addPreset ( value )
206
203
}
207
204
} else {
208
- throw new Error ( 'Expected usable value, not `' + value + '`' )
205
+ throw new TypeError ( 'Expected usable value, not `' + value + '`' )
209
206
}
210
207
}
211
208
212
209
function addList ( plugins ) {
213
- var index = - 1
210
+ let index = - 1
214
211
215
212
if ( plugins === null || plugins === undefined ) {
216
213
// Empty.
217
- } else if ( typeof plugins === 'object' && 'length' in plugins ) {
214
+ } else if ( Array . isArray ( plugins ) ) {
218
215
while ( ++ index < plugins . length ) {
219
216
add ( plugins [ index ] )
220
217
}
221
218
} else {
222
- throw new Error ( 'Expected a list of plugins, not `' + plugins + '`' )
219
+ throw new TypeError ( 'Expected a list of plugins, not `' + plugins + '`' )
223
220
}
224
221
}
225
222
226
223
function addPlugin ( plugin , value ) {
227
- var entry = find ( plugin )
224
+ const entry = find ( plugin )
228
225
229
226
if ( entry ) {
230
227
if ( isPlainObj ( entry [ 1 ] ) && isPlainObj ( value ) ) {
@@ -239,7 +236,7 @@ function base() {
239
236
}
240
237
241
238
function find ( plugin ) {
242
- var index = - 1
239
+ let index = - 1
243
240
244
241
while ( ++ index < attachers . length ) {
245
242
if ( attachers [ index ] [ 0 ] === plugin ) {
@@ -251,11 +248,9 @@ function base() {
251
248
// Parse a file (in string or vfile representation) into a unist node using
252
249
// the `Parser` on the processor.
253
250
function parse ( doc ) {
254
- var file = vfile ( doc )
255
- var Parser
256
-
257
251
freeze ( )
258
- Parser = processor . Parser
252
+ const file = vfile ( doc )
253
+ const Parser = processor . Parser
259
254
assertParser ( 'parse' , Parser )
260
255
261
256
if ( newable ( Parser , 'parse' ) ) {
@@ -301,8 +296,8 @@ function base() {
301
296
// Run transforms on a unist node representation of a file (in string or
302
297
// vfile representation), sync.
303
298
function runSync ( node , file ) {
304
- var result
305
- var complete
299
+ let result
300
+ let complete
306
301
307
302
run ( node , file , done )
308
303
@@ -311,20 +306,18 @@ function base() {
311
306
return result
312
307
313
308
function done ( error , tree ) {
314
- complete = true
315
309
result = tree
310
+ complete = true
316
311
bail ( error )
317
312
}
318
313
}
319
314
320
315
// Stringify a unist node representation of a file (in string or vfile
321
316
// representation) into a string using the `Compiler` on the processor.
322
317
function stringify ( node , doc ) {
323
- var file = vfile ( doc )
324
- var Compiler
325
-
326
318
freeze ( )
327
- Compiler = processor . Compiler
319
+ const file = vfile ( doc )
320
+ const Compiler = processor . Compiler
328
321
assertCompiler ( 'stringify' , Compiler )
329
322
assertNode ( node )
330
323
@@ -351,7 +344,7 @@ function base() {
351
344
executor ( null , cb )
352
345
353
346
function executor ( resolve , reject ) {
354
- var file = vfile ( doc )
347
+ const file = vfile ( doc )
355
348
356
349
pipeline . run ( processor , { file} , done )
357
350
@@ -369,13 +362,11 @@ function base() {
369
362
370
363
// Process the given document (in string or vfile representation), sync.
371
364
function processSync ( doc ) {
372
- var file
373
- var complete
374
-
375
365
freeze ( )
376
366
assertParser ( 'processSync' , processor . Parser )
377
367
assertCompiler ( 'processSync' , processor . Compiler )
378
- file = vfile ( doc )
368
+ let complete
369
+ const file = vfile ( doc )
379
370
380
371
process ( file , done )
381
372
@@ -404,9 +395,12 @@ function newable(value, name) {
404
395
405
396
// Check if `value` is an object with keys.
406
397
function keys ( value ) {
407
- var key
398
+ let key
399
+
408
400
for ( key in value ) {
409
- return true
401
+ if ( own . call ( value , key ) ) {
402
+ return true
403
+ }
410
404
}
411
405
412
406
return false
@@ -415,32 +409,32 @@ function keys(value) {
415
409
// Assert a parser is available.
416
410
function assertParser ( name , Parser ) {
417
411
if ( typeof Parser !== 'function' ) {
418
- throw new Error ( 'Cannot `' + name + '` without `Parser`' )
412
+ throw new TypeError ( 'Cannot `' + name + '` without `Parser`' )
419
413
}
420
414
}
421
415
422
416
// Assert a compiler is available.
423
417
function assertCompiler ( name , Compiler ) {
424
418
if ( typeof Compiler !== 'function' ) {
425
- throw new Error ( 'Cannot `' + name + '` without `Compiler`' )
419
+ throw new TypeError ( 'Cannot `' + name + '` without `Compiler`' )
426
420
}
427
421
}
428
422
429
423
// Assert the processor is not frozen.
430
424
function assertUnfrozen ( name , frozen ) {
431
425
if ( frozen ) {
432
426
throw new Error (
433
- 'Cannot invoke `' +
427
+ 'Cannot call `' +
434
428
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`.'
436
430
)
437
431
}
438
432
}
439
433
440
434
// Assert `node` is a unist node.
441
435
function assertNode ( node ) {
442
436
if ( ! node || typeof node . type !== 'string' ) {
443
- throw new Error ( 'Expected node, got `' + node + '`' )
437
+ throw new TypeError ( 'Expected node, got `' + node + '`' )
444
438
}
445
439
}
446
440
0 commit comments