Skip to content

Commit

Permalink
macroexpand forms given to compile-form and add cases for let* and le…
Browse files Browse the repository at this point in the history
…tfn* special operators

body should be variable length

optimize do special operator

add test forms for do, let*, and letfn*

Remove austin

Require cljs.tagged_literals in compiler

Update ClojureScript

Version 0.3.2

Version 0.3.3-SNAPSHOT
  • Loading branch information
aamedina committed Feb 13, 2015
1 parent 0582d19 commit d8f9644
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
7 changes: 3 additions & 4 deletions project.clj
@@ -1,4 +1,4 @@
(defproject sablono "0.3.2-SNAPSHOT"
(defproject sablono "0.3.3-SNAPSHOT"
:description "Lisp style templating for Facebook's React."
:url "http://github.com/r0man/sablono"
:author "r0man"
Expand All @@ -7,7 +7,7 @@
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[cljsjs/react "0.12.2-5"]
[org.clojure/clojure "1.6.0"]
[org.clojure/clojurescript "0.0-2816" :scope "provided"]]
[org.clojure/clojurescript "0.0-2843" :scope "provided"]]
:aliases {"cleantest" ["do" "clean," "cljx" "once," "test," "cljsbuild" "test"]
"deploy" ["do" "clean," "cljx" "once," "deploy" "clojars"]}
:cljsbuild {:builds [{:id "dev"
Expand Down Expand Up @@ -45,14 +45,13 @@
:deploy-repositories [["releases" :clojars]]
:prep-tasks [["cljx" "once"]]
:profiles {:dev {:dependencies [[crate "0.2.5"]
[com.cemerick/piggieback "0.1.6-SNAPSHOT"]
[hickory "0.5.4"]
[org.clojure/tools.nrepl "0.2.7"]
[reagent "0.4.3"]]
:plugins [[com.keminglabs/cljx "0.5.0"]
[com.cemerick/austin "0.1.7-SNAPSHOT"]
[com.cemerick/clojurescript.test "0.3.3"]
[lein-cljsbuild "1.0.4"]]
:repl-options {:nrepl-middleware [cljx.repl-middleware/wrap-cljx]}
:resource-paths ["test-resources"]
:source-paths ["target/classes"]
:test-paths ["test" "target/test-classes"]}})
20 changes: 18 additions & 2 deletions src/sablono/compiler.clj
@@ -1,5 +1,6 @@
(ns sablono.compiler
(:require [sablono.util :refer :all])
(:require [cljs.tagged_literals]
[sablono.util :refer :all])
(:import cljs.tagged_literals.JSValue))

(defprotocol ICompile
Expand Down Expand Up @@ -94,6 +95,18 @@
{:private true}
form-name)

(defmethod compile-form "do"
[[_ & forms]]
`(do ~@(butlast forms) ~(compile-html (last forms))))

(defmethod compile-form "let*"
[[_ bindings & body]]
`(let* ~bindings ~@(butlast body) ~(compile-html (last body))))

(defmethod compile-form "letfn*"
[[_ bindings & body]]
`(letfn* ~bindings ~@(butlast body) ~(compile-html (last body))))

(defmethod compile-form "for"
[[_ bindings body]]
`(~'into-array (for ~bindings ~(compile-html body))))
Expand All @@ -104,7 +117,10 @@

(defmethod compile-form :default
[expr]
`(sablono.interpreter/interpret ~expr))
(let [mform (macroexpand expr)]
(if (= mform expr)
`(sablono.interpreter/interpret ~expr)
(compile-form mform))))

(defn- not-hint?
"True if x is not hinted to be the supplied type."
Expand Down
18 changes: 17 additions & 1 deletion test/sablono/compiler_test.clj
Expand Up @@ -258,7 +258,23 @@
(clojure.core/remove
clojure.core/nil?
(if (clojure.core/map? attrs)
[] [(sablono.interpreter/interpret attrs)]))))))
[] [(sablono.interpreter/interpret attrs)]))))
'(let [] (when true [:span "foo"]))
'(let* [] (if true (do (js/React.createElement "span" nil "foo"))))
'(letfn [(foo [] true)]
(when (foo)
[:span "bar"]))
'(letfn* [foo (clojure.core/fn foo [] true)]
(if (foo) (do (js/React.createElement "span" nil "bar"))))
'(do [:div "should not be optimized"]
(let []
(when true
[:div "should be optimized"])))
'(do [:div "should not be optimized"]
(let* []
(if true
(do (js/React.createElement "div" nil
"should be optimized")))))))
(testing "values are evaluated only once"
(let [times-called (atom 0)
foo #(swap! times-called inc)]
Expand Down

0 comments on commit d8f9644

Please sign in to comment.