Fork me on GitHub

Sweeten Your JavaScript

Sweet.js brings the hygienic macros of languages like Scheme and Rust to JavaScript. Macros allow you to sweeten the syntax of JavaScript and craft the language you’ve always wanted.

Do you want to use class syntax but don’t want to wait for ES6? Add them yourself with just a couple lines of code!

// Define the class macro here...
macro class {

  rule {

    $className {
        constructor $cparams $cbody
        $($mname $mparams $mbody) ...
    }

  } => {

    function $className $cparams $cbody

    $($className.prototype.$mname
      = function $mname $mparams $mbody; ) ...

  }

}

// An now classes are in JavaScript!
class Person {
  constructor(name) {
    this.name = name;
  }

  say(msg) {
    console.log(this.name + " says: " + msg);
  }
}
var bob = new Person("Bob");
bob.say("Macros are sweet!");

To get a better sense of what macros can do, check out some example macros or play around with macros in the online editor.

Getting sweet.js

Install the sweet.js compiler via npm:

$ npm install -g sweet.js

Use the sjs binary to compile your sweet code:

$ sjs -o output.js my_sweet_code.js

Next Steps