|
7 | 7 | ; You must not remove this notice, or any other, from this software. |
8 | 8 |
|
9 | 9 | (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] |
12 | 12 | [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 |
16 | 15 | CompilerOptions SourceFile JsAst CommandLineRunner] |
17 | 16 | [com.google.javascript.jscomp.parsing Config$JsDocParsing] |
18 | 17 | [com.google.javascript.rhino |
19 | | - Node Token JSTypeExpression])) |
| 18 | + Node Token JSTypeExpression] |
| 19 | + [java.util.logging Level])) |
20 | 20 |
|
21 | 21 | (def ^:dynamic *ignore-var* false) |
22 | 22 | (def ^:dynamic *source-file* nil) |
|
30 | 30 | (into [] (butlast props)) |
31 | 31 | (with-meta (last props) ty)))) |
32 | 32 |
|
33 | | -(defn get-type* [^JSTypeExpression texpr] |
| 33 | +(defn get-tag [^JSTypeExpression texpr] |
34 | 34 | (when-let [root (.getRoot texpr)] |
35 | 35 | (if (.isString root) |
36 | 36 | (symbol (.getString root)) |
37 | 37 | (if-let [child (.. root getFirstChild)] |
38 | 38 | (if (.isString child) |
39 | 39 | (symbol (.. child getString))))))) |
40 | 40 |
|
41 | | -(defn get-type [^Node node] |
| 41 | +(defn get-var-info [^Node node] |
42 | 42 | (when node |
43 | 43 | (let [info (.getJSDocInfo node)] |
44 | 44 | (when info |
45 | 45 | (merge |
46 | 46 | (if-let [^JSTypeExpression ty (.getType info)] |
47 | | - {:tag (get-type* ty)} |
| 47 | + {:tag (get-tag ty)} |
48 | 48 | (if (or (.isConstructor info) (.isInterface info)) |
49 | 49 | (let [qname (symbol (.. node getFirstChild getQualifiedName))] |
50 | 50 | (cond-> {:tag 'Function} |
51 | 51 | (.isConstructor info) (merge {:ctor qname}) |
52 | 52 | (.isInterface info) (merge {:iface qname}))) |
53 | 53 | (if (.hasReturnType info) |
54 | 54 | {:tag 'Function |
55 | | - :ret-tag (get-type* (.getReturnType info)) |
| 55 | + :ret-tag (get-tag (.getReturnType info)) |
56 | 56 | :arglists (list (into [] (map symbol (.getParameterNames info))))}))) |
57 | 57 | {:file *source-file* |
58 | 58 | :line (.getLineno node)} |
|
65 | 65 |
|
66 | 66 | (defmethod parse-extern-node Token/VAR [node] |
67 | 67 | (when (> (.getChildCount node) 0) |
68 | | - (let [ty (get-type node)] |
| 68 | + (let [ty (get-var-info node)] |
69 | 69 | (cond-> (parse-extern-node (.getFirstChild node)) |
70 | 70 | ty (-> first (annotate ty) vector))))) |
71 | 71 |
|
|
75 | 75 |
|
76 | 76 | (defmethod parse-extern-node Token/ASSIGN [node] |
77 | 77 | (when (> (.getChildCount node) 0) |
78 | | - (let [ty (get-type node) |
| 78 | + (let [ty (get-var-info node) |
79 | 79 | lhs (cond-> (first (parse-extern-node (.getFirstChild node))) |
80 | 80 | ty (annotate ty))] |
81 | 81 | (if (> (.getChildCount node) 1) |
|
97 | 97 | (defmethod parse-extern-node Token/GETPROP [node] |
98 | 98 | (when-not *ignore-var* |
99 | 99 | (let [props (map symbol (string/split (.getQualifiedName node) #"\."))] |
100 | | - [(if-let [ty (get-type node)] |
| 100 | + [(if-let [ty (get-var-info node)] |
101 | 101 | (annotate props ty) |
102 | 102 | props)]))) |
103 | 103 |
|
|
171 | 171 | externs (index-externs (parse-externs externs-file)))) |
172 | 172 | defaults sources)))) |
173 | 173 |
|
| 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 | + |
174 | 181 | (comment |
| 182 | + |
| 183 | + (analyze-goog-file "goog/string/string.js") |
| 184 | + |
175 | 185 | (require '[clojure.java.io :as io] |
176 | 186 | '[cljs.closure :as closure] |
177 | 187 | '[clojure.pprint :refer [pprint]] |
|
0 commit comments