0

I'm trying to write a rule for capturing the expression and arguments of a function call with sweet.js.

Here's my current macro (that fails to match):

macro foo {
    rule {
        ( $fn:expr ($args ...) )
    } => {
        $fn("stuff", $args ...) // pushes "stuff" to the beginning of the arguments
    }
}

And some inputs and expected outputs:

foo(somefn("bar"))

should output:

somefn("stuff", "bar")

and

foo(console.log("bar"))

should output:

console.log("stuff", "bar")

Any assistance would be greatly appreciated.

SimpleJ
  • 13,812
  • 13
  • 53
  • 93

1 Answers1

2

The :expr pattern class is greedy so $fn:expr is matching all of somefn("bar") (since that's an entire expression).

In the case, probably the easiest solution is to use ellipses:

macro foo {
    rule {
        ( $fn ... ($args ...) )
    } => {
        $fn ... ("stuff", $args ...) // pushes "stuff" to the beginning of the arguments
    }
}
timdisney
  • 5,287
  • 9
  • 35
  • 31