Selecting function declarations
Functions can be declared in one of a few ways:
function x(a, b) {} // a named function declaration
(function x(a,b) {}) // a named function expression
(function(a, b) {}) // an anonymous function expression
((a, b) => {}) // an arrow expression
(fun)
can be used to select a function declaration (or expression) and has the following signature:
(fun [name] [args] [kind])
Names
name
restricts the selection to functions declared with a matching identifier:
(fun x) ; function x() {}
(fun /x/) ; function axb() {}
This parameter is ignored when matching against anonymous function or arrow expressions.
Arguments
args
accepts an (arg)
selector to match against the shape of an argument. Refer to the section on call expression selection for an explanation of the (arg)
selector. Unlike in call expressions, the arguments to a function declaration are not values so the selector would be matching against their shape instead; namely, identifiers and object or array patterns (destructuring.)
For example, to select a function that accepts an object parameter with a count
property:
(fun _ (arg _ (obj (prop count))))
Which would match against function x({ count }) {}
but not function x(count) {}
. Additionally, you can use a logical operator to match against more than argument:
(fun _ (:and (arg 1 (obj))
(arg 2)))
Which would match a function that receives two arguments, the first being an object, such as function x({ count }, callback) {}
.
Pattern selection is covered in more depth in the next chapter.
Async and generator functions
kind
restricts the selection to async or generator functions through the (is)
selector:
(fun _ _ (is async)) ; async function x() {}
(fun _ _ (is generator)) ; function *x() {}