Skip to content

Commit

Permalink
Add support for transformers as async functions
Browse files Browse the repository at this point in the history
Closes GH-35.
  • Loading branch information
wooorm committed May 2, 2018
1 parent 316fea3 commit 16b7607
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
15 changes: 7 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var bail = require('bail')
var vfile = require('vfile')
var trough = require('trough')
var string = require('x-is-string')
var func = require('x-is-function')
var plain = require('is-plain-obj')

/* Expose a frozen processor. */
Expand Down Expand Up @@ -123,7 +122,7 @@ function unified() {

transformer = plugin.apply(processor, values.slice(1))

if (func(transformer)) {
if (typeof transformer === 'function') {
transformers.use(transformer)
}
}
Expand Down Expand Up @@ -176,7 +175,7 @@ function unified() {

if (value === null || value === undefined) {
/* Empty */
} else if (func(value)) {
} else if (typeof value === 'function') {
addPlugin.apply(null, arguments)
} else if (typeof value === 'object') {
if ('length' in value) {
Expand All @@ -203,7 +202,7 @@ function unified() {
}

function add(value) {
if (func(value)) {
if (typeof value === 'function') {
addPlugin(value)
} else if (typeof value === 'object') {
if ('length' in value) {
Expand Down Expand Up @@ -287,7 +286,7 @@ function unified() {
assertNode(node)
freeze()

if (!cb && func(file)) {
if (!cb && typeof file === 'function') {
cb = file
file = null
}
Expand Down Expand Up @@ -411,7 +410,7 @@ function unified() {

/* Check if `func` is a constructor. */
function newable(value) {
return func(value) && keys(value.prototype)
return typeof value === 'function' && keys(value.prototype)
}

/* Check if `value` is an object with keys. */
Expand All @@ -425,14 +424,14 @@ function keys(value) {

/* Assert a parser is available. */
function assertParser(name, Parser) {
if (!func(Parser)) {
if (typeof Parser !== 'function') {
throw new Error('Cannot `' + name + '` without `Parser`')
}
}

/* Assert a compiler is available. */
function assertCompiler(name, Compiler) {
if (!func(Compiler)) {
if (typeof Compiler !== 'function') {
throw new Error('Cannot `' + name + '` without `Compiler`')
}
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"is-plain-obj": "^1.1.0",
"trough": "^1.0.0",
"vfile": "^2.0.0",
"x-is-function": "^1.0.4",
"x-is-string": "^0.1.0"
},
"devDependencies": {
Expand Down
35 changes: 35 additions & 0 deletions test/async-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

var test = require('tape')
var vfile = require('vfile')
var unified = require('..')

test('async function transformer () {}', function(t) {
var f
var n
var e

t.plan(5)

f = vfile('alpha')
n = {type: 'bravo'}
e = {type: 'charlie'}

unified()
.use(plugin)
.run(n, f, function(err, a, file) {
t.error(err, 'should’t fail')
t.equal(a, e, 'passes given tree to `done`')
t.equal(file, f, 'passes given file to `done`')
})

function plugin() {
return transformer
}

async function transformer(tree, file) {
t.equal(tree, n, 'passes correct tree to an async function')
t.equal(file, f, 'passes correct file to an async function')
return e
}
})
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ require('./parse')
require('./run')
require('./stringify')
require('./process')

var asyncfunctions = false

try {
eval('typeof async function() {}') // eslint-disable-line no-eval
asyncfunctions = true
} catch (err) {}

console.log('asyncfunctions: ', asyncfunctions)
if (asyncfunctions) {
require('./async-function')
}

0 comments on commit 16b7607

Please sign in to comment.