Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.

Commit 1221575

Browse files
committed
def
1 parent 3248872 commit 1221575

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

cherry/corpus/foo.cljs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
(ns foo)
22

3-
(js/console.log "hello")
4-
(js/console.log (+ 1 2 3))
3+
(def log js/console.log)
54

6-
(let [x (do (js/console.log "in do")
7-
12)]
8-
(js/console.log "x + 1 =" (inc x)))
5+
(log "hello")
6+
(log (+ 1 2 3))
7+
8+
(let [y (let [x (do (log "in do")
9+
12)]
10+
(log "x + 1 =" (inc x))
11+
(+ x 13))]
12+
(log "y =" y))

cherry/corpus/foo.mjs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
console.log("hello");
2-
console.log((1 + 2 + 3));
3-
(function () { const x = (function () { console.log("in do");
1+
const log = console.log;
2+
;
3+
log("hello");
4+
log((1 + 2 + 3));
5+
(function () { const y = (function () { const x = (function () { log("in do");
46
return 12; })();
5-
return (function () { return console.log("x + 1 =", (x + 1)); })(); })();
7+
return (function () { log("x + 1 =", (x + 1));
8+
return (x + 13); })(); })();
9+
return (function () { return log("y =", y); })(); })();

cherry/src/cherry/transpiler.clj

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@
7373
(str (name expr)))
7474

7575
(defmethod emit clojure.lang.Symbol [expr]
76-
(when-not (valid-symbol? (str expr))
77-
(#' throwf "%s is not a valid javascript symbol" expr))
78-
(str expr))
76+
(let [expr (if (and (qualified-symbol? expr)
77+
(= "js" (namespace expr)))
78+
(name expr)
79+
expr)]
80+
(when-not (valid-symbol? (str expr))
81+
(#' throwf "%s is not a valid javascript symbol" expr))
82+
(str expr)))
7983

8084
(defmethod emit java.util.regex.Pattern [expr]
8185
(str \/ expr \/))
@@ -87,7 +91,7 @@
8791
'return 'delete 'new 'do 'aget 'while 'doseq
8892
'str 'inc! 'dec! 'dec 'inc 'defined? 'and 'or
8993
'? 'try 'break
90-
'await 'const 'defn 'let 'ns]))
94+
'await 'const 'defn 'let 'ns 'def]))
9195

9296
(def prefix-unary-operators (set ['!]))
9397

@@ -138,12 +142,18 @@
138142
(partition 2 more))
139143
(repeat statement-separator))))
140144

141-
(defmethod emit-special 'const [_type [const & more]]
145+
(defn emit-const [more]
142146
(apply str (interleave (map (fn [[name expr]]
143147
(str "const " (emit name) " = " (emit expr)))
144148
(partition 2 more))
145149
(repeat statement-separator))))
146150

151+
(defmethod emit-special 'const [_type [_const & more]]
152+
(emit-const more))
153+
154+
(defmethod emit-special 'def [_type [_const & more]]
155+
(emit-const more))
156+
147157
(declare emit-do)
148158

149159
(defn wrap-await [s]

0 commit comments

Comments
 (0)