Last active
August 29, 2015 14:02
-
-
Save kl0tl/12d46f02303ff97484dd to your computer and use it in GitHub Desktop.
Shorthand get/set accessors notation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
``` | |
var foo = {}; | |
get foo.computed() { | |
... | |
} | |
set foo.computed() { | |
... | |
} | |
``` | |
*/ | |
macro function_params { | |
rule { ($param:ident (,) ...) } | |
} | |
macro concise_function_expression { | |
rule { $params:function_params { $body ... } } => { | |
function $params { $body ... } | |
} | |
rule { $params:function_params => $body:expr } => { | |
function $params { return $body } | |
} | |
} | |
// cf http://stackoverflow.com/a/22414263 | |
macro syntax2string { | |
rule { $syntax } => { | |
[makeValue(unwrapSyntax($syntax).toString(), $syntax)] | |
} | |
} | |
macro stringify { | |
case { _ ($syntax) } => { | |
letstx $string = syntax2string(#{$syntax}); | |
return #{$string}; | |
} | |
} | |
let get = macro { | |
rule { $name:ident $params:function_params => $body:expr } => { | |
get $name $params { return $body } | |
} | |
rule { $symbol:ident (.) ... $name:ident $function:concise_function_expression } => { | |
$symbol (.) ... . __defineGetter__(stringify($name), $function) | |
} | |
rule { $rest ... } => { get $rest ... } | |
} | |
let set = macro { | |
rule { $name:ident $params:function_params => $body:expr } => { | |
set $name $params { return $body } | |
} | |
rule { $symbol:ident (.) ... $name:ident $function:concise_function_expression } => { | |
$symbol (.) ... . __defineSetter__(stringify($name), $function) | |
} | |
rule { $rest ... } => { set $rest ... } | |
} | |
export get; | |
export set; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment