Skip to content

Commit 11de795

Browse files
committed
cljs.externs: organize requires, better names - get-type* -> get-tag,
get-type -> get-var-info
1 parent 2931216 commit 11de795

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/main/clojure/cljs/externs.clj

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
; You must not remove this notice, or any other, from this software.
88

99
(ns cljs.externs
10-
(:require [clojure.string :as string]
11-
[cljs.util :as util]
10+
(:require [cljs.util :as util]
11+
[cljs.js-deps :as js-deps]
1212
[clojure.java.io :as io]
13-
[cljs.js-deps :as js-deps])
14-
(:import [java.util.logging Level]
15-
[com.google.javascript.jscomp
13+
[clojure.string :as string])
14+
(:import [com.google.javascript.jscomp
1615
CompilerOptions SourceFile JsAst CommandLineRunner]
1716
[com.google.javascript.jscomp.parsing Config$JsDocParsing]
1817
[com.google.javascript.rhino
19-
Node Token JSTypeExpression]))
18+
Node Token JSTypeExpression]
19+
[java.util.logging Level]))
2020

2121
(def ^:dynamic *ignore-var* false)
2222
(def ^:dynamic *source-file* nil)
@@ -30,29 +30,29 @@
3030
(into [] (butlast props))
3131
(with-meta (last props) ty))))
3232

33-
(defn get-type* [^JSTypeExpression texpr]
33+
(defn get-tag [^JSTypeExpression texpr]
3434
(when-let [root (.getRoot texpr)]
3535
(if (.isString root)
3636
(symbol (.getString root))
3737
(if-let [child (.. root getFirstChild)]
3838
(if (.isString child)
3939
(symbol (.. child getString)))))))
4040

41-
(defn get-type [^Node node]
41+
(defn get-var-info [^Node node]
4242
(when node
4343
(let [info (.getJSDocInfo node)]
4444
(when info
4545
(merge
4646
(if-let [^JSTypeExpression ty (.getType info)]
47-
{:tag (get-type* ty)}
47+
{:tag (get-tag ty)}
4848
(if (or (.isConstructor info) (.isInterface info))
4949
(let [qname (symbol (.. node getFirstChild getQualifiedName))]
5050
(cond-> {:tag 'Function}
5151
(.isConstructor info) (merge {:ctor qname})
5252
(.isInterface info) (merge {:iface qname})))
5353
(if (.hasReturnType info)
5454
{:tag 'Function
55-
:ret-tag (get-type* (.getReturnType info))
55+
:ret-tag (get-tag (.getReturnType info))
5656
:arglists (list (into [] (map symbol (.getParameterNames info))))})))
5757
{:file *source-file*
5858
:line (.getLineno node)}
@@ -65,7 +65,7 @@
6565

6666
(defmethod parse-extern-node Token/VAR [node]
6767
(when (> (.getChildCount node) 0)
68-
(let [ty (get-type node)]
68+
(let [ty (get-var-info node)]
6969
(cond-> (parse-extern-node (.getFirstChild node))
7070
ty (-> first (annotate ty) vector)))))
7171

@@ -75,7 +75,7 @@
7575

7676
(defmethod parse-extern-node Token/ASSIGN [node]
7777
(when (> (.getChildCount node) 0)
78-
(let [ty (get-type node)
78+
(let [ty (get-var-info node)
7979
lhs (cond-> (first (parse-extern-node (.getFirstChild node)))
8080
ty (annotate ty))]
8181
(if (> (.getChildCount node) 1)
@@ -97,7 +97,7 @@
9797
(defmethod parse-extern-node Token/GETPROP [node]
9898
(when-not *ignore-var*
9999
(let [props (map symbol (string/split (.getQualifiedName node) #"\."))]
100-
[(if-let [ty (get-type node)]
100+
[(if-let [ty (get-var-info node)]
101101
(annotate props ty)
102102
props)])))
103103

@@ -171,7 +171,17 @@
171171
externs (index-externs (parse-externs externs-file))))
172172
defaults sources))))
173173

174+
(defn analyze-goog-file [f]
175+
(let [rsrc (io/resource f)
176+
desc (js-deps/parse-js-ns (line-seq (io/reader rsrc)))]
177+
;; TODO: figure out what to do about other provides
178+
[(first (:provides desc))
179+
]))
180+
174181
(comment
182+
183+
(analyze-goog-file "goog/string/string.js")
184+
175185
(require '[clojure.java.io :as io]
176186
'[cljs.closure :as closure]
177187
'[clojure.pprint :refer [pprint]]

0 commit comments

Comments
 (0)