What is a JavaScript Macro?

javascript, macros, sweet.js

Solution

As has been posted in comments it's a macro system for javascript.

It's a way of defining text replacements in the "pre-processing phase". So you would define a macro and use it in your code, then run them both through sweet.js and the output would be code with text replacements.

example:

macro swap {
  rule { ($a, $b) } => {
    var tmp = $a;
    $a = $b;
    $b = tmp;
  }
}

var a = 10;
var b = 20;

swap (a, b)

After running this through sweet.js we get the expanded code:

var a$1 = 10;
var b$2 = 20;

var tmp$3 = a$1;
a$1 = b$2;
b$2 = tmp$3; 

Problem

Recently I have seen people talk about the using of macros in JavaScript. I have no idea what that means and after looking up documentation on MDN I came up without any answer. So that leads me to my question … What are JavaScript macros? How/why are they used? Is it a form of meta-programming? Answers with examples and example code would be appreciated greatly.

Original source

Related problems