News for hackers. Stuff that matters.
Re:JavaScript Macros in Bun by goranmoomin on Friday June 30, 2023 @03:49PM (#36541152)
I was ready to be excited from the title, but was utterly disappointed :(

IMO these aren’t macros in the Lisp-sense of the word (or Rust, or even C); yeah they run code at compile time, but that’s where the common ends.

Macros should be able to apply syntactic transformation on the code. Lisp is famous for allowing that by representing code as lists. Rust has a compiler-level API to give tokens and run arbitrary code, then spit new tokens out. C macros operate on the tokens level, so with enough magic you can transform code to the shape you want.

This… isn’t any of that.

A pretty good example (and something I’m still sad that it didn’t take off) of macros in JS is Sweet.js[0]. Babel macros[1] are a bit higher level, where macros require the input to already be a valid AST, but that’s also something I’d call macros.

This… I’d say it’s more of a compile time code execution feature, not a macro feature.

[0]: https://www.sweetjs.org/ [1]: https://babeljs.io/blog/2017/09/11/zero-config-with-babel-ma...

Re:JavaScript Macros in Bun by Jarred on Friday June 30, 2023 @04:10PM (#36541503)
An earlier version of macros in Bun looked more like what you describe. We nerfed it because the API was too complicated. But we probably will revisit in the future.

https://gist.github.com/Jarred-Sumner/454da846d614f7bb4bcceb...

Re:JavaScript Macros in Bun by namuol on Friday June 30, 2023 @09:07PM (#36545084)
Came here to suggest renaming this feature for the reasons described here.

Ideas are cheap, and naming is hard, but I like Zig’s “comptime” term, which is closer to this than “macro”. “comptime” distinguishes it from C preprocessor stuff, but also describes what it actually does more clearly.

Re:JavaScript Macros in Bun by mhio on Friday June 30, 2023 @09:39PM (#36545371)
I've been using ts-morph for this recently, the accompanying AST viewer makes getting started easy.

https://github.com/dsherret/ts-morph

https://ts-ast-viewer.com

Re:JavaScript Macros in Bun by love2read on Saturday July 01, 2023 @12:02AM (#36546429)
Out of curiosity, what have you made?
Re:JavaScript Macros in Bun by mhio on Monday July 03, 2023 @09:55PM (#36581220)
It was mostly utilitarian, modifying the outputs of some code generation tools where maintaining a fork was going to be difficult. The most recent was munging some file upload details where the OpenAPI definition is incomplete (or only recently defined). Also adding some aspect oriented changes across the code (audit/log/debug).