Skip to content

Commit

Permalink
Add support for using ES classes to parse, compile
Browse files Browse the repository at this point in the history
Closes GH-48.
Closes GH-51.

Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
  • Loading branch information
wooorm authored Jun 14, 2019
1 parent cbe18d1 commit 4d96f98
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function unified() {
Parser = processor.Parser
assertParser('parse', Parser)

if (newable(Parser)) {
if (newable(Parser, 'parse')) {
return new Parser(String(file), file).parse()
}

Expand Down Expand Up @@ -337,7 +337,7 @@ function unified() {
assertCompiler('stringify', Compiler)
assertNode(node)

if (newable(Compiler)) {
if (newable(Compiler, 'compile')) {
return new Compiler(node, file).compile()
}

Expand Down Expand Up @@ -400,8 +400,14 @@ function unified() {
}

// Check if `value` is a constructor.
function newable(value) {
return typeof value === 'function' && keys(value.prototype)
function newable(value, name) {
return (
typeof value === 'function' &&
// 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.
(keys(value.prototype) || name in value.prototype)
)
}

// Check if `value` is an object with keys.
Expand Down
22 changes: 21 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(8)
t.plan(12)

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

class ESParser {
constructor(doc, file) {
t.equal(typeof doc, 'string', 'should pass a document')
t.ok('message' in file, 'should pass a file')
}

parse() {
t.equal(arguments.length, 0, 'should not pass anything to `parse`')
return n
}
}

p.Parser = ESParser

t.equal(
p.parse('charlie'),
n,
'should return the result `Parser#parse` returns on an ES class'
)
})
22 changes: 21 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(9)
t.plan(13)

t.throws(
function() {
Expand Down Expand Up @@ -60,4 +60,24 @@ test('stringify(node[, file])', function(t) {
/Expected node, got `undefined`/,
'should throw without node'
)

class ESCompiler {
constructor(node, file) {
t.equal(node, n, 'should pass a node')
t.ok('message' in file, 'should pass a file')
}

compile() {
t.equal(arguments.length, 0, 'should not pass anything to `compile`')
return 'echo'
}
}

p.Compiler = ESCompiler

t.equal(
p.stringify(n, f),
'echo',
'should return the result `Compiler#compile` returns on an ES class'
)
})

0 comments on commit 4d96f98

Please sign in to comment.