Search code examples
javascriptpreprocessorsweet.js

Implement multiline skinny arrow syntax in sweetjs


I'm playing around with sweetjs and for the life of me can't figure out why this rule for parameterless multiline skinny arrow syntax isn't matched

Code:

macro -> {
  rule infix { () | { $body ... $last:expr } } => {
    function( ) { 
      $body ...;
      return $last
    }
  }
}

var fn = () -> {
  var a = 1;
  a + 2;
};
expect(fn()).to.equal(3);

results in

SyntaxError: [macro] Macro `-` could not be matched with `> {} ; expect ()...`
10: var fn = () -> {
                ^

Solution

  • Try removing the semi-colon on the last line of the closure, for some reason the sweetjs compiler has trouble with $last and semi-colons.

    macro -> {
     rule infix { () | { $body ... $last:expr } } => {
        function() {
          $($body) ...
          return $last
        }
      }
    }
    
    var fn = () -> {
      var a = 1
      a + 2
    };