Rule based macros? That's sweet.js!
macro keywords, define a few patterns and then whenever you invoke the name you created in your project the pattern will be matched and expanded. This allows for some really powerful stuff, with very simple syntax.
Just to give a basic idea for what one of these might look like, here is a simple identity macro:
All this says is that when you use the keyword
myFirstMacro that you just defined, the sweet.js compiler will look for a pattern following that keyword to match
$x which is the way sweet.js says to just expand that pattern to itself. I will show an invocation followed by what the compiler would output. Invocation:
Now this may not seem very useful from this simple example but let me tweak the example just a little bit to see how this can be used.
Now if we are to invoke this macro here is the invocation and output:
Let me know what you think of this article on twitter @ or leave a comment below!