Define a custom keyword in JavaScript that expands to a reusable code pattern to reduce repetition in your codebase
Experiment with new syntax ideas for JavaScript without waiting for official language proposals
Learn how compile-time code transformation and macro systems work by writing simple macros in a real JS project
Sweet.js is a tool that adds macros to JavaScript. A macro in this context is a way to define new syntax: you write a rule that tells the compiler what to do when it sees a particular pattern in your code, and the compiler expands it into regular JavaScript before running or bundling the file. Languages like Lisp and Rust have this capability built in, Sweet.js brings a version of it to JavaScript. The macros are hygienic, which means they avoid a common problem where a macro accidentally captures or collides with variable names in the surrounding code. You write a macro definition using a special syntax keyword, give it a name, and then use it in your code like any other keyword. When you compile the file with the included command-line tool, the macro calls are replaced with the JavaScript they expand to. The README shows a simple example: a macro named hi that, when used, produces a console.log statement. You install the command-line tool via npm, write your code in a .js file using any macros you have defined, and run the sjs command to compile it into standard JavaScript. The project was described in its README as experimental and under heavy re-development, and the README notes it was not recommended for production use at the time it was written. The README is brief and links out to a tutorial and reference documentation on the project's website for full details. Community discussion was hosted on Google Groups, IRC, and Gitter.
← sweet-js on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.