@@ -8,11 +8,11 @@ import {VFile} from 'vfile'
88// Expose a frozen processor.
99export 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
3838function 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.
5151function 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.
406397function 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.
416410function 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.
423417function 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.
430424function 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.
441435function 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
0 commit comments