explaingit

sweet-js/sweet-core

4,560JavaScriptAudience · developerComplexity · 3/5Setup · moderate

TLDR

Sweet.js lets you add custom syntax to JavaScript using macros, you define new keywords that expand into regular JavaScript before your code runs, similar to how Rust and Lisp work. Experimental, not production-ready.

Mindmap

mindmap
  root((sweet-core))
    What it does
      JS macros
      Custom syntax
      Compile-time expansion
    Key Concept
      Hygienic macros
      No name collisions
      Pattern matching
    Workflow
      Write macro
      Use in code
      Compile with sjs
    Status
      Experimental
      Not for production
      Active redevelopment
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Define a custom keyword in JavaScript that expands to a reusable code pattern to reduce repetition in your codebase

USE CASE 2

Experiment with new syntax ideas for JavaScript without waiting for official language proposals

USE CASE 3

Learn how compile-time code transformation and macro systems work by writing simple macros in a real JS project

Tech stack

JavaScriptNode.jsnpm

Getting it running

Difficulty · moderate Time to first run · 30min

In plain English

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.

Copy-paste prompts

Prompt 1
Show me how to write a simple Sweet.js macro called hi that expands to a console.log statement, then compile it using the sjs command
Prompt 2
How do I install sweet-js globally via npm and use the sjs compiler to transform a JavaScript file that uses a custom macro?
Prompt 3
Explain how hygienic macros in Sweet.js prevent variable name collisions when a macro introduces new variable names into surrounding code
Prompt 4
Show me how to define a Sweet.js macro that creates a shorter syntax for a common JavaScript pattern like a for-of loop with an index counter
Open on GitHub → Explain another repo

← sweet-js on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.