Skip to content

Instantly share code, notes, and snippets.

@mfikes
Last active February 23, 2016 20:59
Show Gist options
  • Save mfikes/faea7393d04b92a43388 to your computer and use it in GitHub Desktop.
Save mfikes/faea7393d04b92a43388 to your computer and use it in GitHub Desktop.
Using Planck to desugar ClojureScript ns forms

If you do (doc ns) you'll see a description of how :refer-macros and :include-macros can be use in ns forms as inline macro specification sugar, along with an example and the desugared result.

This desugaring is done within the ClojureScript compiler, and in regular (JVM-based) ClojureScript, the associated desugaring algorithms are invoked as Clojure.

But, now that we have bootstrapped ClojureScript, it is easy to call directly upon the compiler to see what any given ns form will desugar into.

Here is an example showing how to do so within Planck:

First import the analyzer namespace and define a desugar function that wraps the underlying capability:

(require '[cljs.analyzer :as ana])

(defn desugar [[_ ns & specs]]
  (list* 'ns ns (ana/desugar-ns-specs specs)))

With this in place, you can easily produce the example that appears in (doc ns):

(desugar '(ns testme.core
  (:require [foo.core :as foo :refer [foo-fn] :include-macros true]
            [woz.core :as woz :refer [woz-fn] :refer-macros [app jx]])))

will produce the following (wrapped for readability)

(ns testme.core 
  (:require (foo.core :as foo :refer [foo-fn]) 
            (woz.core :as woz :refer [woz-fn])) 
  (:require-macros (foo.core :as foo) 
                   (woz.core :as woz :refer [app jx])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment