feat(complex-matcher): execute() and toString() expects current node as context
This commit is contained in:
@@ -13,6 +13,6 @@ export default ({ benchmark }) => {
|
||||
})
|
||||
|
||||
benchmark('toString', () => {
|
||||
toString(ast)
|
||||
ast::toString()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -14,5 +14,5 @@ test('parse', t => {
|
||||
})
|
||||
|
||||
test('toString', t => {
|
||||
t.is(pattern, toString(ast))
|
||||
t.is(pattern, ast::toString())
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user