-
-
Notifications
You must be signed in to change notification settings - Fork 293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
consider using sci for macro-expansion #811
Comments
Just adding this for reference: clojure-lsp uses a DSL to describe custom macros: https://github.com/snoe/clojure-lsp/#macro-defs |
@borkdude looks awesome! one question though, could the return of each entry be Clojure forms instead of a string? |
@wilkerlucio Which entry/string, where? Can you clarify? |
I mean, I'm wondering if there could be a way to be something like: {:macroexpand
{foo/weird-macro
(fn weird-macro [{:keys [:sexpr]}]
(let [[[sym val opts] & body] (rest sexpr)]
(when-not (and sym val)
(throw (ex-info "No sym and val provided " {})))
{:sexpr `(let [~sym ~val] ~@(cons opts body))}))}} But given the macro/runtime constraints, I'm not sure if its possible, but wonder if you did consider it. |
@wilkerlucio The reason the code is in a string is that not all code is valid EDN. This is part of the .clj-kondo/config.edn file. |
Gotcha, makes sense, thanks for the clarification. |
@wilkerlucio Your comment did get me thinking that large code strings in an EDN file aren't very ergonomic. Maybe I could allow {:macroexpand {foo/weird-macro "(load-file \".clj-kondo/macros/weird-macro.clj\")"}} Thoughts? |
Also see some elaboration here: https://clojureverse.org/t/feedback-wanted-on-new-clj-kondo-macroexpansion-feature/6043 |
Yeah, writing code on strings gets me bad memories from the past, hahaha. So yeah, I like the idea of having this being something externally loaded. |
@wilkerlucio Updated the example code using |
Hmm, one problem is that relative paths don't work, since the editor sometimes set the current working directory to the current file. |
@wilkerlucio Solved the problem as follows: if the code is a single line and doesn't start with a paren, it's interpreted as a file path relative to the |
If both :macroexpand and :lint-as are defined, what is the priority? |
@SevereOverfl0w Yet to be determined. What do you think? |
:macroexpand is explicit opting into the newer behavior, and is more likely to be accurate. Seems a reasonably priority to me. |
11:48 PM 11:48 PM |
@SevereOverfl0w Change: Example: |
Note (23rd of June 2020):
A blog post about this feature can be found here.
Documentation for this feature can be found here.
The original text of this issue
In the macros-sci branch, we added the following config option:
I.e. the
:macroexpand
option takes a map from fully qualified symbols to a string which represents Clojure code. The Clojure code should return a function which::sexpr
, the sexpr representing the macro invocation:sexpr
, the macroexpansion..clj-kondo
directoryThe reason we accept and return maps is for extensibility. The reason we expose the entire sexpr and not just the macro args to the function is that the function can copy location metadata from the original macro invocation to be used in lint warnings.
An example usage can be found in corpus/macroexpand.clj.
TODO:
require
. this allows for multiple macros from the same library to make a small lib with helpers functions that can be re-used over multiple macros. the sci ctx will be shared over all calls. We can add.clj-kondo/src
to the classpath of sci.{:hooks {:call {foo/bar ...}
?solution: we're going to use rewrite-clj directly. see next.
11:43 AM
Still, the sexpr can be used for some validations, when people don't "macroexpand"
.clj-kondo/config.edn
:findings
or withreg-finding!
The text was updated successfully, but these errors were encountered: