feat(complex-matcher): execute() and toString() expects current node as context

This commit is contained in:
Julien Fontanet
2016-05-19 18:10:34 +02:00
parent 2d75b6086f
commit d612598bd0
3 changed files with 15 additions and 7 deletions

View File

@@ -13,6 +13,6 @@ export default ({ benchmark }) => {
})
benchmark('toString', () => {
toString(ast)
ast::toString()
})
}

View File

@@ -246,9 +246,13 @@ export const execute = invoke(() => {
})
}
return (node, value) => visitors[node.type](node, value)
return function (value) {
return visitors[this.type](this, value)
}
})
// -------------------------------------------------------------------
export const toString = invoke(() => {
const toStringTerms = terms => map(terms, toString).join(' ')
const toStringGroup = terms => `(${toStringTerms(terms)})`
@@ -266,16 +270,20 @@ export const toString = invoke(() => {
const toString = node => visitors[node.type](node)
// Special case for a root “and”: do not add braces.
return node => node.type === 'and'
? toStringTerms(node.children)
: toString(node)
return function () {
return this.type === 'and'
? toStringTerms(this.children)
: toString(this)
}
})
// -------------------------------------------------------------------
export const create = pattern => {
pattern = parse(pattern)
if (!pattern) {
return
}
return value => execute(pattern, value)
return value => pattern::execute(value)
}

View File

@@ -14,5 +14,5 @@ test('parse', t => {
})
test('toString', t => {
t.is(pattern, toString(ast))
t.is(pattern, ast::toString())
})