jlongster / es6-macros

A collection of sweet.js macros that implement ES6 features for ES5
BSD 2-Clause "Simplified" License
237 stars 18 forks source link

default arguments as a macro #7

Open Raynos opened 10 years ago

Raynos commented 10 years ago

It would be nice to figure out how to do default arguments in function declarations.

jlongster commented 10 years ago

This won't be too hard! I'm going to start adding more stuff in the next week or two.

nmn commented 10 years ago

Any updates? I'm struggling with the sweet.js syntax these days, or I'd just send some PRs your way.

jlongster commented 10 years ago

I've been using this project so far happily and have been working on a few other things. But I can come back and implement some of this stuff in the next week or so :)

ponychicken commented 9 years ago

See also: https://github.com/mozilla/sweet.js/issues/37

ponychicken commented 9 years ago

Here's a halfway working thing:

let function = macro {
  // Regular named functions
  rule { $name ($params ...) { $body }  } => {
    function $name split_params_comma ($params ...)  { split_params_space $params ... $body }
  }
  // Anon functions
  rule { ($params ...) { $body }  } => {
    function split_params_comma ($params ...)  { split_params_space $params ... $body }
  }
  // Bodyless functions
  rule { $name ($params ...) { }  } => {
    function $name split_params_comma ($params ...)  {}
  }
}

macro split_params_comma {
  rule { $param ... , $rest ... } => {
    remove_param_value $param ... , split_params_comma $rest ...
  }
  rule { $param ... } => {
    remove_param_value $param ...
  }
}

macro split_params_space {
  rule { $param ... , $rest ... } => {
    param_value_to_if $param ...  split_params_space $rest ...
  }
  rule { $param ... } => {
    param_value_to_if $param ...
  }
}

macro remove_param_value {
  rule { $name=$val } => { $name }
  rule { $name } => { $name }
}

macro param_value_to_if {
  rule { $name=$val } => { if (typeof $name == 'undefined') $name = $val; }
  rule { $name } => {  }
}

function Something(Test = 8) { a }

function Something(Test) { a }

function Something(Test, na = 9, ca = 9) { a }

function Something(Test, na = 9, ca = 9) {  }

Can someone figure out why anom fn are failing?