Skip to content

Commit

Permalink
Add support for parser, compiler as arrow function
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 13, 2019
1 parent 0cba11b commit 6043ef7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ function unified() {
function newable(value, name) {
return (
typeof value === 'function' &&
value.prototype &&
// A function with keys in its prototype is probably a constructor.
// Classes’ prototype methods are not enumerable, so we check if some value
// exists in the prototype.
Expand Down
14 changes: 13 additions & 1 deletion test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test('parse(file)', function(t) {
var p = unified()
var n

t.plan(12)
t.plan(15)

t.throws(
function() {
Expand Down Expand Up @@ -47,6 +47,18 @@ test('parse(file)', function(t) {
'should return the result `parser` returns if it’s not a constructor'
)

p.Parser = (doc, file) => {
t.equal(typeof doc, 'string', 'should pass a document')
t.ok('message' in file, 'should pass a file')
return n
}

t.equal(
p.parse('charlie'),
n,
'should return the result `parser` returns if it’s an arrow function'
)

class ESParser {
constructor(doc, file) {
t.equal(typeof doc, 'string', 'should pass a document')
Expand Down
14 changes: 13 additions & 1 deletion test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test('stringify(node[, file])', function(t) {
var f
var n

t.plan(13)
t.plan(16)

t.throws(
function() {
Expand Down Expand Up @@ -51,6 +51,18 @@ test('stringify(node[, file])', function(t) {
'should return the result `compiler` returns if it’s not a constructor'
)

p.Compiler = (node, file) => {
t.equal(node, n, 'should pass a node')
t.ok('message' in file, 'should pass a file')
return 'echo'
}

t.equal(
p.stringify(n, f),
'echo',
'should return the result `compiler` returns if it’s an arrow function'
)

p.Compiler = noop.Compiler

t.throws(
Expand Down

0 comments on commit 6043ef7

Please sign in to comment.