clojurescript

witek 2025-07-01T11:45:08.231699Z

Hello. How to write a macro which emits code prefixed with js/?

witek 2025-07-01T11:47:28.302469Z

This code

(defmacro log [text]
`(js/console.log ~text))
causes this compiler error:
Error in phase :compile-syntax-check
RuntimeException: No such namespace: js
	clojure.lang.Util.runtimeException (Util.java:221)
	clojure.lang.Compiler.resolveIn (Compiler.java:7913)
    ...

p-himik 2025-07-01T11:53:08.905059Z

How are you using that macro? Is it in a .cljc file, by any chance?

markaddleman 2025-07-01T11:53:10.767849Z

I haven’t tried but can you manually construct the list using something like (list 'js/console.log ~text)

p-himik 2025-07-01T11:53:32.439579Z

It's the same thing.

markaddleman 2025-07-01T11:54:10.882309Z

then how about manually constructing the symbol ?

p-himik 2025-07-01T11:54:30.138449Z

It is the same thing. It's not about how the symbol is constructed.

👍 1
witek 2025-07-01T12:12:32.709989Z

.clj file

witek 2025-07-01T12:26:01.446139Z

I have created an empty js.clj file in my src folder. Compilation ok. But then I get runtime error:

TypeError: (intermediate value).log is not a function

witek 2025-07-01T12:31:51.371139Z

@p-himik What would be the solution for a macro in a .cljc file?

schadocalex 2025-07-01T13:51:03.798109Z

Just follow thheller instructions: https://code.thheller.com/blog/shadow-cljs/2019/10/12/clojurescript-macros.html

witek 2025-07-01T13:53:02.429469Z

Solution:

`(~'js/console.log ~text)
and not calling the macro from a .clj or .cljc file.

schadocalex 2025-07-01T14:02:55.428899Z

the solution is not the "new" syntax, it's the same as your initial one

schadocalex 2025-07-01T14:03:46.520339Z

it's about how you declare and use the macro