Skip to content

Commit 1cd7650

Browse files
frenchy64swannodette
authored andcommitted
CLJS-2803: Remove :record-value op and add record literal tests
1 parent 5aaf6db commit 1cd7650

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3572,23 +3572,24 @@
35723572
:children [:items]
35733573
:tag 'array}))))
35743574

3575+
(defn record-ns+name [x]
3576+
(map symbol
3577+
#?(:clj
3578+
((juxt (comp #(string/join "." %) butlast) last)
3579+
(string/split (.getName ^Class (type x)) #"\."))
3580+
:cljs
3581+
(string/split (pr-str (type x)) #"/"))))
3582+
35753583
(defn analyze-record
35763584
[env x]
3577-
(let [items (disallowing-recur
3585+
(let [;; register constansts
3586+
_items_ (disallowing-recur
35783587
(analyze (assoc env :context :expr) (into {} x)))
3579-
[ns name] (map symbol
3580-
#?(:clj
3581-
((juxt (comp #(string/join "." %) butlast) last)
3582-
(string/split (.getName ^Class (type x)) #"\."))
3583-
:cljs
3584-
(string/split (pr-str (type x)) #"/")))]
3585-
{:op :record-value
3586-
:ns ns
3587-
:name name
3588+
[ns name] (record-ns+name x)]
3589+
{:op :const
3590+
:val x
35883591
:env env
35893592
:form x
3590-
:items items
3591-
:children [:items]
35923593
:tag (symbol (str ns) (str name))}))
35933594

35943595
(defn elide-reader-meta [m]

src/main/clojure/cljs/compiler.cljc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@
233233
(defmulti emit-constant* type))
234234

235235
(declare emit-map emit-list emit-vector emit-set emit-js-object emit-js-array
236-
emit-with-meta emit-constants-comma-sep emit-constant)
236+
emit-with-meta emit-constants-comma-sep emit-constant emit-record-value)
237237

238238
(defn all-distinct? [xs]
239239
(apply distinct? xs))
@@ -242,6 +242,8 @@
242242
(defn emit-constant-no-meta [x]
243243
(cond
244244
(seq? x) (emit-list x emit-constants-comma-sep)
245+
(record? x) (let [[ns name] (ana/record-ns+name x)]
246+
(emit-record-value ns name #(emit-constant (into {} x))))
245247
(map? x) (emit-map (keys x) (vals x) emit-constants-comma-sep all-distinct?)
246248
(vector? x) (emit-vector x emit-constants-comma-sep)
247249
(set? x) (emit-set x emit-constants-comma-sep all-distinct?)
@@ -250,6 +252,8 @@
250252
(defn emit-constant-no-meta [x]
251253
(cond
252254
(ana/cljs-seq? x) (emit-list x emit-constants-comma-sep)
255+
(record? x) (let [[ns name] (ana/record-ns+name x)]
256+
(emit-record-value ns name #(emit-constant (into {} x))))
253257
(ana/cljs-map? x) (emit-map (keys x) (vals x) emit-constants-comma-sep all-distinct?)
254258
(ana/cljs-vector? x) (emit-vector x emit-constants-comma-sep)
255259
(ana/cljs-set? x) (emit-set x emit-constants-comma-sep all-distinct?)
@@ -554,10 +558,9 @@
554558
(emit-wrap env
555559
(emit-js-array items comma-sep)))
556560

557-
(defmethod emit* :record-value
558-
[{:keys [items ns name items env]}]
559-
(emit-wrap env
560-
(emits ns ".map__GT_" name "(" items ")")))
561+
(defn emit-record-value
562+
[ns name items]
563+
(emits ns ".map__GT_" name "(" items ")"))
561564

562565
(defmethod emit* :quote
563566
[{:keys [expr]}]

src/test/cljs/data_readers_test/records.cljc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,24 @@
33

44
(defrecord Foo [a b])
55

6-
(assert (= (Foo. 1 2) #data_readers_test.records.Foo{:a 1 :b 2}))
6+
(assert (= (Foo. 1 2) #data_readers_test.records.Foo{:a 1 :b 2}))
7+
(assert (= (Foo. 1 2)
8+
'#data_readers_test.records.Foo{:a 1 :b 2}))
9+
(assert (= (Foo. 1 2)
10+
(second ''#data_readers_test.records.Foo{:a 1 :b 2})))
11+
(assert (= (Foo. 'a 'b)
12+
(let [a 1
13+
b 2]
14+
#data_readers_test.records.Foo{:a a :b b}))
15+
(pr-str
16+
(let [a 1
17+
b 2]
18+
#data_readers_test.records.Foo{:a a :b b})))
19+
(assert (= (Foo. 'a 'b)
20+
(let [a 1
21+
b 2]
22+
'#data_readers_test.records.Foo{:a a :b b}))
23+
(pr-str
24+
(let [a 1
25+
b 2]
26+
'#data_readers_test.records.Foo{:a a :b b})))

0 commit comments

Comments
 (0)