clojurific 0.12.1 → 0.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/cljf/launcher.js +15 -8
- package/package.json +2 -2
- package/compiler/deps.edn +0 -9
- package/compiler/src/main/cljs/cljs/analyzer/passes/and_or.cljc +0 -118
- package/compiler/src/main/cljs/cljs/analyzer/passes/lite.cljc +0 -32
- package/compiler/src/main/cljs/cljs/analyzer/passes.cljc +0 -32
- package/compiler/src/main/cljs/cljs/bootstrap_nodejs.js +0 -154
- package/compiler/src/main/cljs/cljs/core/specs/alpha.cljc +0 -272
- package/compiler/src/main/cljs/cljs/core/specs/alpha.cljs +0 -10
- package/compiler/src/main/cljs/cljs/core.cljs +0 -13194
- package/compiler/src/main/cljs/cljs/esm/goog/array.js +0 -61
- package/compiler/src/main/cljs/cljs/esm/goog/math.js +0 -1846
- package/compiler/src/main/cljs/cljs/esm/goog/object.js +0 -80
- package/compiler/src/main/cljs/cljs/esm/goog/string.js +0 -192
- package/compiler/src/main/cljs/cljs/esm/goog/userAgent/product.js +0 -10
- package/compiler/src/main/cljs/cljs/esm/goog.js +0 -90
- package/compiler/src/main/cljs/cljs/esm/karma.cljs +0 -103
- package/compiler/src/main/cljs/cljs/esm/lazy.clj +0 -51
- package/compiler/src/main/cljs/cljs/esm/lazy.cljs +0 -49
- package/compiler/src/main/cljs/cljs/esm/node_test.cljs +0 -21
- package/compiler/src/main/cljs/cljs/esm/repl_runtime.cljs +0 -353
- package/compiler/src/main/cljs/cljs/externs.js +0 -27
- package/compiler/src/main/cljs/cljs/imul.js +0 -11
- package/compiler/src/main/cljs/cljs/js.clj +0 -24
- package/compiler/src/main/cljs/cljs/js.cljs +0 -1432
- package/compiler/src/main/cljs/cljs/loader.cljs +0 -107
- package/compiler/src/main/cljs/cljs/main.clj +0 -67
- package/compiler/src/main/cljs/cljs/math.cljs +0 -869
- package/compiler/src/main/cljs/cljs/module_deps.js +0 -250
- package/compiler/src/main/cljs/cljs/nodejs.cljs +0 -27
- package/compiler/src/main/cljs/cljs/nodejs_externs.js +0 -3
- package/compiler/src/main/cljs/cljs/nodejscli.cljs +0 -22
- package/compiler/src/main/cljs/cljs/pprint.cljc +0 -167
- package/compiler/src/main/cljs/cljs/pprint.cljs +0 -3325
- package/compiler/src/main/cljs/cljs/proxy/impl.cljs +0 -32
- package/compiler/src/main/cljs/cljs/proxy.cljs +0 -184
- package/compiler/src/main/cljs/cljs/reader.clj +0 -18
- package/compiler/src/main/cljs/cljs/reader.cljs +0 -214
- package/compiler/src/main/cljs/cljs/repl.cljs +0 -206
- package/compiler/src/main/cljs/cljs/source_map/base64.cljs +0 -25
- package/compiler/src/main/cljs/cljs/source_map/base64_vlq.cljs +0 -102
- package/compiler/src/main/cljs/cljs/source_map.cljs +0 -305
- package/compiler/src/main/cljs/cljs/spec/alpha.cljc +0 -564
- package/compiler/src/main/cljs/cljs/spec/alpha.cljs +0 -1510
- package/compiler/src/main/cljs/cljs/spec/gen/alpha.cljc +0 -68
- package/compiler/src/main/cljs/cljs/spec/gen/alpha.cljs +0 -183
- package/compiler/src/main/cljs/cljs/spec/test/alpha.cljc +0 -329
- package/compiler/src/main/cljs/cljs/spec/test/alpha.cljs +0 -380
- package/compiler/src/main/cljs/cljs/stacktrace.cljc +0 -578
- package/compiler/src/main/cljs/cljs/test.cljc +0 -458
- package/compiler/src/main/cljs/cljs/test.cljs +0 -606
- package/compiler/src/main/cljs/clojure/browser/dom.cljs +0 -152
- package/compiler/src/main/cljs/clojure/browser/event.cljs +0 -101
- package/compiler/src/main/cljs/clojure/browser/net.cljs +0 -181
- package/compiler/src/main/cljs/clojure/browser/repl/preload.cljs +0 -13
- package/compiler/src/main/cljs/clojure/browser/repl.cljs +0 -283
- package/compiler/src/main/cljs/clojure/core/protocols.cljs +0 -29
- package/compiler/src/main/cljs/clojure/core/reducers.cljs +0 -312
- package/compiler/src/main/cljs/clojure/data.cljs +0 -162
- package/compiler/src/main/cljs/clojure/datafy.cljs +0 -63
- package/compiler/src/main/cljs/clojure/edn.cljs +0 -55
- package/compiler/src/main/cljs/clojure/reflect.cljs +0 -57
- package/compiler/src/main/cljs/clojure/set.cljs +0 -165
- package/compiler/src/main/cljs/clojure/string.cljs +0 -290
- package/compiler/src/main/cljs/clojure/walk.cljs +0 -98
- package/compiler/src/main/cljs/clojure/zip.cljs +0 -291
- package/compiler/src/main/cljs/process/env.cljs +0 -12
- package/compiler/src/main/cljs/shadow/resource.clj +0 -60
- package/compiler/src/main/cljs/shadow/resource.cljs +0 -10
- package/compiler/src/main/clojure/cljs/analyzer/api.cljc +0 -299
- package/compiler/src/main/clojure/cljs/analyzer/impl/namespaces.cljc +0 -68
- package/compiler/src/main/clojure/cljs/analyzer/impl.cljc +0 -59
- package/compiler/src/main/clojure/cljs/analyzer/macros.clj +0 -57
- package/compiler/src/main/clojure/cljs/analyzer/utils.clj +0 -36
- package/compiler/src/main/clojure/cljs/analyzer.cljc +0 -5292
- package/compiler/src/main/clojure/cljs/build/api.clj +0 -309
- package/compiler/src/main/clojure/cljs/cli.clj +0 -812
- package/compiler/src/main/clojure/cljs/closure.clj +0 -3440
- package/compiler/src/main/clojure/cljs/compiler/api.clj +0 -101
- package/compiler/src/main/clojure/cljs/compiler/macros.clj +0 -16
- package/compiler/src/main/clojure/cljs/compiler.cljc +0 -2748
- package/compiler/src/main/clojure/cljs/core/macros.clj +0 -43
- package/compiler/src/main/clojure/cljs/core/server.clj +0 -167
- package/compiler/src/main/clojure/cljs/core.cljc +0 -3719
- package/compiler/src/main/clojure/cljs/env/macros.clj +0 -37
- package/compiler/src/main/clojure/cljs/env.cljc +0 -102
- package/compiler/src/main/clojure/cljs/esm/repl/nrepl.clj +0 -151
- package/compiler/src/main/clojure/cljs/esm/repl.clj +0 -878
- package/compiler/src/main/clojure/cljs/esm.clj +0 -987
- package/compiler/src/main/clojure/cljs/externs/default.edn +0 -1549
- package/compiler/src/main/clojure/cljs/externs.clj +0 -479
- package/compiler/src/main/clojure/cljs/foreign/node.clj +0 -207
- package/compiler/src/main/clojure/cljs/instant.clj +0 -54
- package/compiler/src/main/clojure/cljs/js_deps.cljc +0 -412
- package/compiler/src/main/clojure/cljs/module_graph.cljc +0 -402
- package/compiler/src/main/clojure/cljs/repl/bootstrap.clj +0 -53
- package/compiler/src/main/clojure/cljs/repl/browser.clj +0 -540
- package/compiler/src/main/clojure/cljs/repl/node.clj +0 -253
- package/compiler/src/main/clojure/cljs/repl/node_repl.js +0 -114
- package/compiler/src/main/clojure/cljs/repl/reflect.clj +0 -85
- package/compiler/src/main/clojure/cljs/repl/server.clj +0 -236
- package/compiler/src/main/clojure/cljs/repl.cljc +0 -1615
- package/compiler/src/main/clojure/cljs/server/browser.clj +0 -54
- package/compiler/src/main/clojure/cljs/server/node.clj +0 -54
- package/compiler/src/main/clojure/cljs/source_map/base64.clj +0 -28
- package/compiler/src/main/clojure/cljs/source_map/base64_vlq.clj +0 -104
- package/compiler/src/main/clojure/cljs/source_map.clj +0 -369
- package/compiler/src/main/clojure/cljs/support.cljc +0 -18
- package/compiler/src/main/clojure/cljs/tagged_literals.cljc +0 -93
- package/compiler/src/main/clojure/cljs/util.cljc +0 -420
- package/compiler/src/main/clojure/cljs/vendor/bridge.clj +0 -42
- package/compiler/src/main/clojure/cljs/vendor/clojure/data/json.clj +0 -809
- package/compiler/src/main/clojure/cljs/vendor/clojure/tools/reader/default_data_readers.clj +0 -303
- package/compiler/src/main/clojure/cljs/vendor/clojure/tools/reader/edn.clj +0 -440
- package/compiler/src/main/clojure/cljs/vendor/clojure/tools/reader/impl/commons.clj +0 -131
- package/compiler/src/main/clojure/cljs/vendor/clojure/tools/reader/impl/errors.clj +0 -214
- package/compiler/src/main/clojure/cljs/vendor/clojure/tools/reader/impl/inspect.clj +0 -91
- package/compiler/src/main/clojure/cljs/vendor/clojure/tools/reader/impl/utils.clj +0 -127
- package/compiler/src/main/clojure/cljs/vendor/clojure/tools/reader/reader_types.clj +0 -431
- package/compiler/src/main/clojure/cljs/vendor/clojure/tools/reader.clj +0 -1035
- package/compiler/src/main/clojure/cljs/vendor/cognitect/transit.clj +0 -479
- /package/{compiler/no-clojurescript → no-clojurescript}/deps.edn +0 -0
|
@@ -1,479 +0,0 @@
|
|
|
1
|
-
; Copyright (c) Rich Hickey. All rights reserved.
|
|
2
|
-
; The use and distribution terms for this software are covered by the
|
|
3
|
-
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
|
4
|
-
; which can be found in the file epl-v10.html at the root of this distribution.
|
|
5
|
-
; By using this software in any fashion, you are agreeing to be bound by
|
|
6
|
-
; the terms of this license.
|
|
7
|
-
; You must not remove this notice, or any other, from this software.
|
|
8
|
-
|
|
9
|
-
(ns cljs.externs
|
|
10
|
-
(:require [cljs.util :as util]
|
|
11
|
-
[cljs.js-deps :as js-deps]
|
|
12
|
-
[clojure.java.io :as io]
|
|
13
|
-
[clojure.string :as string])
|
|
14
|
-
(:import [com.google.javascript.jscomp
|
|
15
|
-
CompilerOptions CompilerOptions$Environment SourceFile CompilerInput CommandLineRunner]
|
|
16
|
-
[com.google.javascript.jscomp.parsing Config$JsDocParsing]
|
|
17
|
-
[com.google.javascript.rhino
|
|
18
|
-
Node Token JSTypeExpression JSDocInfo JSDocInfo$Visibility]
|
|
19
|
-
[java.util.logging Level]
|
|
20
|
-
[java.net URL]))
|
|
21
|
-
|
|
22
|
-
(def ^:dynamic *ignore-var* false)
|
|
23
|
-
(def ^:dynamic *source-file* nil)
|
|
24
|
-
(def ^:dynamic *goog-ns* nil)
|
|
25
|
-
|
|
26
|
-
(defn default-externs []
|
|
27
|
-
(CommandLineRunner/getBuiltinExterns CompilerOptions$Environment/BROWSER))
|
|
28
|
-
|
|
29
|
-
;; ------------------------------------------------------------------------------
|
|
30
|
-
;; Externs Parsing
|
|
31
|
-
|
|
32
|
-
(defn annotate
|
|
33
|
-
"Given a sequential list of properties [foo core baz] representing segments
|
|
34
|
-
of the namespace, annotate the last symbol with the type information."
|
|
35
|
-
[props ty]
|
|
36
|
-
(when (seq props)
|
|
37
|
-
(conj
|
|
38
|
-
(into [] (butlast props))
|
|
39
|
-
(with-meta (last props) ty))))
|
|
40
|
-
|
|
41
|
-
(def token->kw
|
|
42
|
-
{Token/BANG :bang
|
|
43
|
-
Token/BLOCK :block
|
|
44
|
-
Token/PIPE :pipe
|
|
45
|
-
Token/STRINGLIT :string-lit
|
|
46
|
-
Token/QMARK :qmark
|
|
47
|
-
Token/STAR :star})
|
|
48
|
-
|
|
49
|
-
(defn parse-texpr [^Node root]
|
|
50
|
-
(when-let [token (get token->kw (.getToken root))]
|
|
51
|
-
(let [children (.children root)]
|
|
52
|
-
(merge
|
|
53
|
-
{:type token}
|
|
54
|
-
(when-not (empty? children)
|
|
55
|
-
{:children (vec (map parse-texpr (.children root)))})
|
|
56
|
-
(when (= :string-lit token)
|
|
57
|
-
{:value (.getString root)})))))
|
|
58
|
-
|
|
59
|
-
(defn undefined?
|
|
60
|
-
[{:keys [type value] :as texpr}]
|
|
61
|
-
(and (= type :string-lit)
|
|
62
|
-
(= "undefined" value)))
|
|
63
|
-
|
|
64
|
-
(defn add-prefix
|
|
65
|
-
"Externs inference uses :prefix meta to both resolve externs as well as generate
|
|
66
|
-
missing externs information. Google Closure Compiler default externs includes
|
|
67
|
-
nested types like webCrypto.Crypto. Add prefix information to the returned symbol to
|
|
68
|
-
simplify resolution later."
|
|
69
|
-
[type-str]
|
|
70
|
-
(with-meta (symbol type-str)
|
|
71
|
-
{:prefix (->> (string/split (name type-str) #"\.")
|
|
72
|
-
(map symbol) vec)}))
|
|
73
|
-
|
|
74
|
-
(defn simplify-texpr
|
|
75
|
-
[texpr]
|
|
76
|
-
(case (:type texpr)
|
|
77
|
-
:string-lit (-> texpr :value add-prefix)
|
|
78
|
-
:star 'any
|
|
79
|
-
;; TODO: qmark should probably be #{nil T}
|
|
80
|
-
(:qmark :bang) (simplify-texpr (-> texpr :children first))
|
|
81
|
-
:pipe (let [[x y] (:children texpr)]
|
|
82
|
-
(if (undefined? y)
|
|
83
|
-
(simplify-texpr x)
|
|
84
|
-
'any))
|
|
85
|
-
'any))
|
|
86
|
-
|
|
87
|
-
(defn get-tag [^JSTypeExpression texpr]
|
|
88
|
-
(some-> (.getRoot texpr) parse-texpr simplify-texpr))
|
|
89
|
-
|
|
90
|
-
(defn params->method-params [xs]
|
|
91
|
-
(let [not-opt? (complement :optional?)
|
|
92
|
-
required (into [] (map :name (take-while not-opt? xs)))
|
|
93
|
-
opts (map :name (drop-while not-opt? xs))]
|
|
94
|
-
(loop [ret [required] opts opts]
|
|
95
|
-
(if-let [opt (first opts)]
|
|
96
|
-
(recur (conj ret (conj (last ret) opt)) (drop 1 opts))
|
|
97
|
-
(seq ret)))))
|
|
98
|
-
|
|
99
|
-
(defn generic? [t]
|
|
100
|
-
(let [s (name t)]
|
|
101
|
-
(boolean (re-matches #"[A-Z]" s))))
|
|
102
|
-
|
|
103
|
-
(defn gtype->cljs-type [t]
|
|
104
|
-
(when t
|
|
105
|
-
(cond
|
|
106
|
-
(generic? t) 'any
|
|
107
|
-
(= t 'Array) 'array
|
|
108
|
-
:else t)))
|
|
109
|
-
|
|
110
|
-
(defn get-params
|
|
111
|
-
"Return param information in JSDoc appearance order. GCL is relatively
|
|
112
|
-
civilized, so this isn't really a problem."
|
|
113
|
-
[^JSDocInfo info]
|
|
114
|
-
(map
|
|
115
|
-
(fn [n]
|
|
116
|
-
(let [t (.getParameterType info n)]
|
|
117
|
-
{:name (symbol n)
|
|
118
|
-
:optional? (.isOptionalArg t)
|
|
119
|
-
:var-args? (.isVarArgs t)}))
|
|
120
|
-
(.getParameterNames info)))
|
|
121
|
-
|
|
122
|
-
(defn get-var-info [^Node node]
|
|
123
|
-
(when node
|
|
124
|
-
(let [info (.getJSDocInfo node)]
|
|
125
|
-
(when info
|
|
126
|
-
(merge
|
|
127
|
-
(if-let [^JSTypeExpression ty (.getType info)]
|
|
128
|
-
{:tag (get-tag ty)}
|
|
129
|
-
(if (or (.isConstructor info) (.isInterface info))
|
|
130
|
-
(let [qname (symbol (.. node getFirstChild getQualifiedName))]
|
|
131
|
-
(cond-> {:tag 'Function}
|
|
132
|
-
(.isConstructor info) (merge {:ctor qname})
|
|
133
|
-
(.isInterface info) (merge {:iface qname})
|
|
134
|
-
(.hasBaseType info) (merge {:super (get-tag (.getBaseType info))})))
|
|
135
|
-
(if (or (.hasReturnType info)
|
|
136
|
-
(as-> (.getParameterCount info) c
|
|
137
|
-
(and c (pos? c))))
|
|
138
|
-
(let [arglist (get-params info)
|
|
139
|
-
arglists (params->method-params arglist)]
|
|
140
|
-
{:tag 'Function
|
|
141
|
-
:js-fn-var true
|
|
142
|
-
:ret-tag (or (some-> (.getReturnType info)
|
|
143
|
-
get-tag gtype->cljs-type)
|
|
144
|
-
'clj-nil)
|
|
145
|
-
:variadic? (boolean (some :var-args? arglist))
|
|
146
|
-
:max-fixed-arity (count (take-while (complement :var-args?) arglist))
|
|
147
|
-
:method-params arglists
|
|
148
|
-
:arglists arglists}))))
|
|
149
|
-
{:file *source-file*
|
|
150
|
-
:line (.getLineno node)}
|
|
151
|
-
(when-let [doc (.getOriginalCommentString info)]
|
|
152
|
-
{:doc doc})
|
|
153
|
-
(when (= JSDocInfo$Visibility/PRIVATE (.getVisibility info))
|
|
154
|
-
{:private true}))))))
|
|
155
|
-
|
|
156
|
-
(defmulti parse-extern-node
|
|
157
|
-
(fn [^Node node]
|
|
158
|
-
(.getToken node)))
|
|
159
|
-
|
|
160
|
-
;; handle named function case (i.e. goog.modules)
|
|
161
|
-
;; function foo {}, the entire function is the node
|
|
162
|
-
(defmethod parse-extern-node Token/FUNCTION [^Node node]
|
|
163
|
-
(when (> (.getChildCount node) 0)
|
|
164
|
-
(let [ty (get-var-info node)]
|
|
165
|
-
(doto
|
|
166
|
-
(cond-> (parse-extern-node (.getFirstChild node))
|
|
167
|
-
ty (-> first (annotate ty) vector))))))
|
|
168
|
-
|
|
169
|
-
(defmethod parse-extern-node Token/VAR [^Node node]
|
|
170
|
-
(when (> (.getChildCount node) 0)
|
|
171
|
-
(let [ty (get-var-info node)]
|
|
172
|
-
(cond-> (parse-extern-node (.getFirstChild node))
|
|
173
|
-
ty (-> first (annotate ty) vector)))))
|
|
174
|
-
|
|
175
|
-
(defmethod parse-extern-node Token/EXPR_RESULT [^Node node]
|
|
176
|
-
(when (> (.getChildCount node) 0)
|
|
177
|
-
(parse-extern-node (.getFirstChild node))))
|
|
178
|
-
|
|
179
|
-
(defmethod parse-extern-node Token/ASSIGN [^Node node]
|
|
180
|
-
(when (> (.getChildCount node) 0)
|
|
181
|
-
(let [ty (get-var-info node)
|
|
182
|
-
lhs (cond-> (first (parse-extern-node (.getFirstChild node)))
|
|
183
|
-
ty (annotate ty))]
|
|
184
|
-
(if (> (.getChildCount node) 1)
|
|
185
|
-
(let [externs
|
|
186
|
-
(binding [*ignore-var* true]
|
|
187
|
-
(parse-extern-node (.getChildAtIndex node 1)))]
|
|
188
|
-
(conj (map (fn [ext] (concat lhs ext)) externs)
|
|
189
|
-
lhs))
|
|
190
|
-
[lhs]))))
|
|
191
|
-
|
|
192
|
-
;; JavaScript name
|
|
193
|
-
;; function foo {}, in this case the `foo` name node
|
|
194
|
-
;; {"foo": bar}, in this case the `bar` name node
|
|
195
|
-
(defmethod parse-extern-node Token/NAME [^Node node]
|
|
196
|
-
(if (= Token/STRING_KEY (-> node .getParent .getToken))
|
|
197
|
-
;; if we are inside an object literal we are done
|
|
198
|
-
[]
|
|
199
|
-
;; also check .getString - goog.module defs won't have qualified names
|
|
200
|
-
(let [name (or (.getQualifiedName node) (.getString node))
|
|
201
|
-
lhs (when-not (string/blank? name)
|
|
202
|
-
(map symbol (string/split name #"\.")))]
|
|
203
|
-
(if (seq lhs)
|
|
204
|
-
(if (> (.getChildCount node) 0)
|
|
205
|
-
(let [externs (parse-extern-node (.getFirstChild node))]
|
|
206
|
-
(conj (map (fn [ext] (concat lhs ext)) externs)
|
|
207
|
-
lhs))
|
|
208
|
-
[lhs])
|
|
209
|
-
[]))))
|
|
210
|
-
|
|
211
|
-
(defmethod parse-extern-node Token/GETPROP [^Node node]
|
|
212
|
-
(when-not *ignore-var*
|
|
213
|
-
(let [props (map symbol (string/split (.getQualifiedName node) #"\."))]
|
|
214
|
-
[(if-let [ty (get-var-info node)]
|
|
215
|
-
(annotate props ty)
|
|
216
|
-
props)])))
|
|
217
|
-
|
|
218
|
-
;; JavaScript Object literal
|
|
219
|
-
;; { ... }
|
|
220
|
-
(defmethod parse-extern-node Token/OBJECTLIT [^Node node]
|
|
221
|
-
(when (> (.getChildCount node) 0)
|
|
222
|
-
(loop [nodes (.children node)
|
|
223
|
-
externs []]
|
|
224
|
-
(if (empty? nodes)
|
|
225
|
-
externs
|
|
226
|
-
(recur (rest nodes)
|
|
227
|
-
(concat externs (parse-extern-node (first nodes))))))))
|
|
228
|
-
|
|
229
|
-
;; Object literal string key node
|
|
230
|
-
;; {"foo": bar} - the key and value together
|
|
231
|
-
(defmethod parse-extern-node Token/STRING_KEY [^Node node]
|
|
232
|
-
(let [lhs [(-> node .getString symbol)]]
|
|
233
|
-
(if (> (.getChildCount node) 0)
|
|
234
|
-
(let [externs (parse-extern-node (.getFirstChild node))]
|
|
235
|
-
(conj (map (fn [ext] (concat lhs ext)) externs)
|
|
236
|
-
lhs))
|
|
237
|
-
[lhs])))
|
|
238
|
-
|
|
239
|
-
(defmethod parse-extern-node :default [node])
|
|
240
|
-
|
|
241
|
-
(defn parse-externs
|
|
242
|
-
"Returns a sequential collection of the form:
|
|
243
|
-
|
|
244
|
-
[[foo core first]
|
|
245
|
-
[foo core next]
|
|
246
|
-
[foo core baz last] ...]
|
|
247
|
-
|
|
248
|
-
Where the last symbol is annotated with var info via metadata. This simple
|
|
249
|
-
structure captures the nested form of Closure namespaces and aids
|
|
250
|
-
direct indexing."
|
|
251
|
-
[^SourceFile source-file]
|
|
252
|
-
(binding [*source-file* (.getName source-file)]
|
|
253
|
-
(let [^CompilerOptions compiler-options
|
|
254
|
-
(doto (CompilerOptions.)
|
|
255
|
-
(.setParseJsDocDocumentation
|
|
256
|
-
Config$JsDocParsing/INCLUDE_DESCRIPTIONS_WITH_WHITESPACE))
|
|
257
|
-
closure-compiler
|
|
258
|
-
(doto
|
|
259
|
-
(let [compiler (com.google.javascript.jscomp.Compiler.)]
|
|
260
|
-
(com.google.javascript.jscomp.Compiler/setLoggingLevel Level/WARNING)
|
|
261
|
-
compiler)
|
|
262
|
-
(.init (list source-file) '() compiler-options))
|
|
263
|
-
js-ast (CompilerInput. source-file)
|
|
264
|
-
^Node root (.getAstRoot js-ast closure-compiler)
|
|
265
|
-
;; TODO: switch to getFirstChild + getNext in the loop
|
|
266
|
-
nodes (.children root)]
|
|
267
|
-
(loop [nodes (cond-> nodes
|
|
268
|
-
;; handle goog.modules which won't have top-levels
|
|
269
|
-
;; need to look at internal children
|
|
270
|
-
(= Token/MODULE_BODY (some-> nodes ^Node (first) .getToken))
|
|
271
|
-
(-> ^Node (first) .children))
|
|
272
|
-
externs []]
|
|
273
|
-
(if (empty? nodes)
|
|
274
|
-
externs
|
|
275
|
-
(let [node (first nodes)
|
|
276
|
-
new-extern (parse-extern-node node)]
|
|
277
|
-
(recur (rest nodes) (concat externs new-extern))))))))
|
|
278
|
-
|
|
279
|
-
(defn index-externs [externs]
|
|
280
|
-
(reduce
|
|
281
|
-
(fn [m xs]
|
|
282
|
-
(cond-> m
|
|
283
|
-
(seq xs) (update-in xs merge {})))
|
|
284
|
-
{} externs))
|
|
285
|
-
|
|
286
|
-
(defn externs-map*
|
|
287
|
-
([]
|
|
288
|
-
(externs-map* (default-externs)))
|
|
289
|
-
([sources]
|
|
290
|
-
(externs-map* sources
|
|
291
|
-
'{eval {}
|
|
292
|
-
global {}
|
|
293
|
-
goog {nodeGlobalRequire {}}
|
|
294
|
-
COMPILED {}
|
|
295
|
-
TypeError {}
|
|
296
|
-
Error {prototype {number {} columnNumber {}}}
|
|
297
|
-
ReferenceError {}}))
|
|
298
|
-
([sources defaults]
|
|
299
|
-
(let [sources (if-not (empty? sources)
|
|
300
|
-
sources
|
|
301
|
-
(default-externs))]
|
|
302
|
-
(reduce
|
|
303
|
-
(fn [externs externs-file]
|
|
304
|
-
(util/map-merge
|
|
305
|
-
externs (index-externs (parse-externs externs-file))))
|
|
306
|
-
defaults sources))))
|
|
307
|
-
|
|
308
|
-
(def externs-map (memoize externs-map*))
|
|
309
|
-
|
|
310
|
-
(defn ns-match? [ns-segs var-segs]
|
|
311
|
-
(or
|
|
312
|
-
;; exact match (i.e. ctors)
|
|
313
|
-
(= ns-segs var-segs)
|
|
314
|
-
(and
|
|
315
|
-
(= (inc (count ns-segs)) (count var-segs))
|
|
316
|
-
(= ns-segs (take (count ns-segs) var-segs)))))
|
|
317
|
-
|
|
318
|
-
(defmulti parsed->defs (fn [_ module-type] module-type))
|
|
319
|
-
|
|
320
|
-
(defmethod parsed->defs :goog
|
|
321
|
-
([externs _]
|
|
322
|
-
(let [grouped (group-by #(= 'exports (first %)) externs)
|
|
323
|
-
exports (->> (get grouped true)
|
|
324
|
-
(map (comp vec rest))
|
|
325
|
-
(remove empty?)
|
|
326
|
-
set)
|
|
327
|
-
exported (filter exports (get grouped false))]
|
|
328
|
-
(reduce
|
|
329
|
-
(fn [m xs]
|
|
330
|
-
(let [sym (last xs)]
|
|
331
|
-
(cond-> m
|
|
332
|
-
(seq xs) (assoc sym (merge (meta sym) {:ns *goog-ns* :name sym})))))
|
|
333
|
-
{} exported))))
|
|
334
|
-
|
|
335
|
-
(defmethod parsed->defs :default
|
|
336
|
-
([externs _]
|
|
337
|
-
(let [ns-segs (into [] (map symbol (string/split (str *goog-ns*) #"\.")))]
|
|
338
|
-
(reduce
|
|
339
|
-
(fn [m xs]
|
|
340
|
-
;; ignore definitions from other provided namespaces not under consideration
|
|
341
|
-
(if (ns-match? ns-segs xs)
|
|
342
|
-
(let [sym (last xs)]
|
|
343
|
-
(cond-> m
|
|
344
|
-
(seq xs) (assoc sym (merge (meta sym) {:ns *goog-ns* :name sym}))))
|
|
345
|
-
m))
|
|
346
|
-
{} externs))))
|
|
347
|
-
|
|
348
|
-
(defn resource->source-file
|
|
349
|
-
[^URL resource]
|
|
350
|
-
(-> (SourceFile/builder)
|
|
351
|
-
(.withPath (.getPath resource))
|
|
352
|
-
(.withContent (io/input-stream resource))
|
|
353
|
-
(.build)))
|
|
354
|
-
|
|
355
|
-
(defn analyze-goog-file
|
|
356
|
-
([f]
|
|
357
|
-
(analyze-goog-file f nil))
|
|
358
|
-
([f ns]
|
|
359
|
-
(let [rsrc (io/resource f)
|
|
360
|
-
desc (js-deps/parse-js-ns (line-seq (io/reader rsrc)))
|
|
361
|
-
ns (or ns (-> (:provides desc) first symbol))]
|
|
362
|
-
(binding [*goog-ns* ns]
|
|
363
|
-
{:name ns
|
|
364
|
-
:defs (parsed->defs
|
|
365
|
-
(parse-externs (resource->source-file rsrc))
|
|
366
|
-
(:module desc))}))))
|
|
367
|
-
|
|
368
|
-
(defn info
|
|
369
|
-
"Helper for grabbing var info from an externs map.
|
|
370
|
-
Example:
|
|
371
|
-
(info externs '[Number isNaN])
|
|
372
|
-
See `externs-map`"
|
|
373
|
-
[externs props]
|
|
374
|
-
(-> externs
|
|
375
|
-
(get-in (butlast props))
|
|
376
|
-
(find (last props))
|
|
377
|
-
first meta))
|
|
378
|
-
|
|
379
|
-
(defn filtered-externs [f]
|
|
380
|
-
(->>
|
|
381
|
-
(filter
|
|
382
|
-
#(= f (.getName %))
|
|
383
|
-
(default-externs))
|
|
384
|
-
first parse-externs index-externs))
|
|
385
|
-
|
|
386
|
-
(comment
|
|
387
|
-
(require '[clojure.java.io :as io]
|
|
388
|
-
'[cljs.closure :as closure]
|
|
389
|
-
'[clojure.pprint :refer [pprint]]
|
|
390
|
-
'[cljs.js-deps :as js-deps])
|
|
391
|
-
|
|
392
|
-
(resource->source-file (io/resource "goog/dom/dom.js"))
|
|
393
|
-
|
|
394
|
-
(pprint
|
|
395
|
-
(get-in (analyze-goog-file "goog/dom/dom.js")
|
|
396
|
-
[:defs 'setTextContent]))
|
|
397
|
-
|
|
398
|
-
(pprint (analyze-goog-file "goog/string/string.js"))
|
|
399
|
-
|
|
400
|
-
(get (js-deps/js-dependency-index {}) "goog.string")
|
|
401
|
-
|
|
402
|
-
;; {:tag Function :ret-tag boolean}
|
|
403
|
-
(->
|
|
404
|
-
(nth
|
|
405
|
-
(parse-externs
|
|
406
|
-
(closure/js-source-file "goog/string/string.js"
|
|
407
|
-
(io/input-stream (io/resource "goog/string/string.js"))))
|
|
408
|
-
2)
|
|
409
|
-
last meta)
|
|
410
|
-
|
|
411
|
-
(parse-externs
|
|
412
|
-
(closure/js-source-file "goog/string/string.js"
|
|
413
|
-
(io/input-stream (io/resource "goog/string/string.js"))))
|
|
414
|
-
|
|
415
|
-
(-> (externs-map
|
|
416
|
-
[(closure/js-source-file "goog/string/string.js"
|
|
417
|
-
(io/input-stream (io/resource "goog/string/string.js")))]
|
|
418
|
-
{})
|
|
419
|
-
(get-in '[goog string])
|
|
420
|
-
(find 'numberAwareCompare_)
|
|
421
|
-
first meta)
|
|
422
|
-
|
|
423
|
-
(-> (externs-map
|
|
424
|
-
[(closure/js-source-file "goog/date/date.js"
|
|
425
|
-
(io/input-stream (io/resource "goog/date/date.js")))]
|
|
426
|
-
{})
|
|
427
|
-
(get-in '[goog date month])
|
|
428
|
-
)
|
|
429
|
-
|
|
430
|
-
(pprint (analyze-goog-file "goog/date/date.js" 'goog.date.month))
|
|
431
|
-
|
|
432
|
-
(externs-map)
|
|
433
|
-
|
|
434
|
-
(-> (externs-map)
|
|
435
|
-
(find 'console) first meta)
|
|
436
|
-
|
|
437
|
-
(get (externs-map) 'Function)
|
|
438
|
-
|
|
439
|
-
(get (externs-map) 'Error)
|
|
440
|
-
|
|
441
|
-
;; values are not on the prototype
|
|
442
|
-
(get (externs-map) 'Symbol)
|
|
443
|
-
(get (externs-map) 'Number)
|
|
444
|
-
|
|
445
|
-
(-> (get-in (externs-map) '[Window prototype])
|
|
446
|
-
(find 'performance) first meta)
|
|
447
|
-
|
|
448
|
-
;; webkit_dom.js defines Console and Window.prototype.console
|
|
449
|
-
(filter
|
|
450
|
-
(fn [s]
|
|
451
|
-
(let [m (-> s parse-externs index-externs)]
|
|
452
|
-
(get-in m '[Window prototype console])))
|
|
453
|
-
(default-externs))
|
|
454
|
-
|
|
455
|
-
(->
|
|
456
|
-
(filter
|
|
457
|
-
(fn [s]
|
|
458
|
-
(= "externs.zip//webkit_dom.js" (.getName s)))
|
|
459
|
-
(default-externs))
|
|
460
|
-
first parse-externs index-externs
|
|
461
|
-
(find 'console) first meta)
|
|
462
|
-
|
|
463
|
-
(->
|
|
464
|
-
(filter
|
|
465
|
-
(fn [s]
|
|
466
|
-
(= "externs.zip//webkit_dom.js" (.getName s)))
|
|
467
|
-
(default-externs))
|
|
468
|
-
first parse-externs index-externs
|
|
469
|
-
(get-in '[Console prototype])
|
|
470
|
-
(find 'log) first meta)
|
|
471
|
-
|
|
472
|
-
(require '[clojure.java.io :as io]
|
|
473
|
-
'[cljs.closure :as cc])
|
|
474
|
-
|
|
475
|
-
(-> (cc/js-source-file nil (io/file "react.ext.js"))
|
|
476
|
-
parse-externs index-externs
|
|
477
|
-
(get 'React)
|
|
478
|
-
(find 'Component) first meta)
|
|
479
|
-
)
|
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
(ns cljs.foreign.node
|
|
2
|
-
(:require [cljs.vendor.clojure.data.json :as json]
|
|
3
|
-
[clojure.java.io :as io]
|
|
4
|
-
[clojure.string :as string]))
|
|
5
|
-
|
|
6
|
-
(defn package-json-entries
|
|
7
|
-
"Takes options and returns a sequence with the desired order of package.json
|
|
8
|
-
entries for the given :package-json-resolution mode. If no mode is provided,
|
|
9
|
-
defaults to :webpack (if no target is set) and :nodejs (if the target is
|
|
10
|
-
:nodejs)."
|
|
11
|
-
[opts]
|
|
12
|
-
{:pre [(or (= (:package-json-resolution opts) :webpack)
|
|
13
|
-
(= (:package-json-resolution opts) :nodejs)
|
|
14
|
-
(and (sequential? (:package-json-resolution opts))
|
|
15
|
-
(every? string? (:package-json-resolution opts)))
|
|
16
|
-
(not (contains? opts :package-json-resolution)))]}
|
|
17
|
-
(let [modes {:nodejs ["main"]
|
|
18
|
-
:webpack ["browser" "module" "main"]}]
|
|
19
|
-
(if-let [mode (:package-json-resolution opts)]
|
|
20
|
-
(if (sequential? mode) mode (get modes mode))
|
|
21
|
-
(case (:target opts)
|
|
22
|
-
:nodejs (:nodejs modes)
|
|
23
|
-
(:webpack modes)))))
|
|
24
|
-
|
|
25
|
-
(comment
|
|
26
|
-
(= (package-json-entries {}) ["browser" "module" "main"])
|
|
27
|
-
(= (package-json-entries {:package-json-resolution :nodejs}) ["main"])
|
|
28
|
-
(= (package-json-entries {:package-json-resolution :webpack}) ["browser" "module" "main"])
|
|
29
|
-
(= (package-json-entries {:package-json-resolution ["foo" "bar" "baz"]}) ["foo" "bar" "baz"])
|
|
30
|
-
(= (package-json-entries {:target :nodejs}) ["main"])
|
|
31
|
-
(= (package-json-entries {:target :nodejs :package-json-resolution :nodejs}) ["main"])
|
|
32
|
-
(= (package-json-entries {:target :nodejs :package-json-resolution :webpack}) ["browser" "module" "main"])
|
|
33
|
-
(= (package-json-entries {:target :nodejs :package-json-resolution ["foo" "bar"]}) ["foo" "bar"]))
|
|
34
|
-
|
|
35
|
-
(defn- package-json? [path]
|
|
36
|
-
(= "package.json" (.getName (io/file path))))
|
|
37
|
-
|
|
38
|
-
(defn- top-level-package-json? [path]
|
|
39
|
-
(boolean (re-find #"node_modules[/\\](@[^/\\]+?[/\\])?[^/\\]+?[/\\]package\.json$" path)))
|
|
40
|
-
|
|
41
|
-
;; the path sans the package.json part
|
|
42
|
-
;; i.e. some_lib/package.json -> some_lib
|
|
43
|
-
(defn- trim-package-json [s]
|
|
44
|
-
(if (string/ends-with? s "package.json")
|
|
45
|
-
(subs s 0 (- (count s) 12))
|
|
46
|
-
s))
|
|
47
|
-
|
|
48
|
-
(defn- trim-relative [path]
|
|
49
|
-
(cond-> path
|
|
50
|
-
(string/starts-with? path "./")
|
|
51
|
-
(subs 2)))
|
|
52
|
-
|
|
53
|
-
(defn- ->export-pkg-json [package-path export]
|
|
54
|
-
(io/file
|
|
55
|
-
(trim-package-json package-path)
|
|
56
|
-
(trim-relative export)
|
|
57
|
-
"package.json"))
|
|
58
|
-
|
|
59
|
-
(defn resolve-export
|
|
60
|
-
"Given an export value, find the entry point based on the
|
|
61
|
-
:package-json-resolution value, defaults to :nodejs. Returns nil
|
|
62
|
-
if we couldn't resolve it."
|
|
63
|
-
[export opts]
|
|
64
|
-
(if (string? export)
|
|
65
|
-
export
|
|
66
|
-
;; we check for require to attempt to filter out
|
|
67
|
-
;; strange cases, i.e. import but no require etc.
|
|
68
|
-
(when (and (map? export) (contains? export "require"))
|
|
69
|
-
(let [resolve (:package-json-resolution opts :nodejs)
|
|
70
|
-
lookup (if (sequential? resolve)
|
|
71
|
-
(or (some #{"import" "require"} resolve) "require")
|
|
72
|
-
({:webpack "import" :nodejs "require"} resolve))
|
|
73
|
-
entry (get export lookup)]
|
|
74
|
-
(if (map? entry)
|
|
75
|
-
(get entry "default")
|
|
76
|
-
entry)))))
|
|
77
|
-
|
|
78
|
-
(defn- export-subpaths
|
|
79
|
-
"Examine the export subpaths to compute subpackages. Add them to pkg-json
|
|
80
|
-
parameter (this is a reduce-kv helper)."
|
|
81
|
-
[pkg-jsons export-subpath export package-path pkg-name opts]
|
|
82
|
-
;; NOTE: ignore "." exports for now
|
|
83
|
-
(if (= "." export-subpath)
|
|
84
|
-
(if-let [resolved (resolve-export export opts)]
|
|
85
|
-
(assoc-in pkg-jsons [package-path "main"] resolved)
|
|
86
|
-
pkg-jsons)
|
|
87
|
-
;; technically the following logic is a bit brittle since `exports` is
|
|
88
|
-
;; supposed to be used to hide the package structure.
|
|
89
|
-
;; instead, here we assume the export subpath does match the library structure
|
|
90
|
-
;; on disk, if we find a package.json we add it to pkg-jsons map
|
|
91
|
-
;; and we synthesize "name" key based on subpath
|
|
92
|
-
(let [export-pkg-json-file (->export-pkg-json package-path export-subpath)]
|
|
93
|
-
;; note this will ignore export wildcards etc.
|
|
94
|
-
(cond-> pkg-jsons
|
|
95
|
-
(.exists export-pkg-json-file)
|
|
96
|
-
(-> (assoc
|
|
97
|
-
(.getAbsolutePath export-pkg-json-file)
|
|
98
|
-
(merge
|
|
99
|
-
(json/read-str (slurp export-pkg-json-file))
|
|
100
|
-
;; add the name field so that path->main-name works later
|
|
101
|
-
(when (and (map? export)
|
|
102
|
-
(contains? export "require"))
|
|
103
|
-
{"name" (str pkg-name (string/replace export-subpath "./" "/"))}))))))))
|
|
104
|
-
|
|
105
|
-
(defn- add-exports
|
|
106
|
-
"Given a list of pkg-jsons examine them for the `exports` field. `exports`
|
|
107
|
-
is now the preferred way to declare an entrypoint to a Node.js library. However,
|
|
108
|
-
for backwards compatibility it is often combined with `main`.
|
|
109
|
-
|
|
110
|
-
`export` can also be a JS object - if so, it can define subpaths. `.` points
|
|
111
|
-
to main and other subpaths can be defined relative to that.
|
|
112
|
-
|
|
113
|
-
See https://nodejs.org/api/packages.html#main-entry-point-export for more
|
|
114
|
-
detailed information."
|
|
115
|
-
[pkg-jsons opts]
|
|
116
|
-
(reduce-kv
|
|
117
|
-
(fn [pkg-jsons package-path {:strs [exports] :as pkg-json}]
|
|
118
|
-
(if (string? exports)
|
|
119
|
-
pkg-jsons
|
|
120
|
-
;; map case
|
|
121
|
-
(reduce-kv
|
|
122
|
-
(fn [pkg-jsons export-subpath export]
|
|
123
|
-
(export-subpaths pkg-jsons export-subpath
|
|
124
|
-
export package-path (get pkg-json "name") opts))
|
|
125
|
-
pkg-jsons exports)))
|
|
126
|
-
pkg-jsons pkg-jsons))
|
|
127
|
-
|
|
128
|
-
(defn path->main-name
|
|
129
|
-
"Determine whether a path is a main entrypoint in the provided package.json.
|
|
130
|
-
If so return the name entry provided in the package.json file."
|
|
131
|
-
[path [pkg-json-path {:as pkg-json :strs [name]}] opts]
|
|
132
|
-
(let [entries (package-json-entries opts)
|
|
133
|
-
entry (first (keep (partial get pkg-json) entries))]
|
|
134
|
-
(when-not (nil? entry)
|
|
135
|
-
;; should be the only edge case in
|
|
136
|
-
;; the package.json main field - Antonio
|
|
137
|
-
(let [entry (trim-relative entry)
|
|
138
|
-
entry-path (-> pkg-json-path (string/replace \\ \/)
|
|
139
|
-
trim-package-json (str entry))]
|
|
140
|
-
;; find a package.json entry point that matches
|
|
141
|
-
;; the `path`
|
|
142
|
-
(some (fn [candidate]
|
|
143
|
-
(when (= candidate (string/replace path \\ \/)) name))
|
|
144
|
-
(cond-> [entry-path]
|
|
145
|
-
;; if we have an entry point that doesn't end in .js or .json
|
|
146
|
-
;; try to match some alternatives
|
|
147
|
-
(not (or (string/ends-with? entry-path ".js")
|
|
148
|
-
(string/ends-with? entry-path ".json")))
|
|
149
|
-
(into [(str entry-path ".js") (str entry-path "/index.js") (str entry-path ".json")
|
|
150
|
-
(string/replace entry-path #"\.cjs$" ".js")])))))))
|
|
151
|
-
|
|
152
|
-
(defn- path->rel-name [path]
|
|
153
|
-
(-> (subs path (.lastIndexOf path "node_modules"))
|
|
154
|
-
(string/replace \\ \/)
|
|
155
|
-
(string/replace #"node_modules[\\\/]" "")))
|
|
156
|
-
|
|
157
|
-
(defn path->provides
|
|
158
|
-
"For a given path in node_modules, determine what namespaces that file would
|
|
159
|
-
provide to ClojureScript. Note it is assumed that we *already* processed all
|
|
160
|
-
package.json files and they are present via pkg-jsons parameter as we need them
|
|
161
|
-
to figure out the provides."
|
|
162
|
-
[path pkg-jsons opts]
|
|
163
|
-
(merge
|
|
164
|
-
{:file path
|
|
165
|
-
:module-type :es6}
|
|
166
|
-
;; if the file is *not* a package.json, then compute what
|
|
167
|
-
;; namespaces it :provides to ClojureScript
|
|
168
|
-
(when-not (package-json? path)
|
|
169
|
-
;; given some path search the package.json to determine whether it is a
|
|
170
|
-
;; main entry point or not
|
|
171
|
-
(let [pkg-json-main (some #(path->main-name path % opts) pkg-jsons)]
|
|
172
|
-
{:provides (let [module-rel-name (path->rel-name path)
|
|
173
|
-
provides (cond-> [module-rel-name (string/replace module-rel-name #"\.js(on)?$" "")]
|
|
174
|
-
(some? pkg-json-main) (conj pkg-json-main))
|
|
175
|
-
index-replaced (string/replace module-rel-name #"[\\\/]index\.js(on)?$" "")]
|
|
176
|
-
(cond-> provides
|
|
177
|
-
(and (boolean (re-find #"[\\\/]index\.js(on)?$" module-rel-name))
|
|
178
|
-
(not (some #{index-replaced} provides)))
|
|
179
|
-
(conj index-replaced)))}))))
|
|
180
|
-
|
|
181
|
-
(defn get-pkg-jsons
|
|
182
|
-
"Given all a seq of files in node_modules return a map of all package.json
|
|
183
|
-
files indexed by path. Includes any `export` package.json files as well"
|
|
184
|
-
([module-fseq]
|
|
185
|
-
(get-pkg-jsons module-fseq nil))
|
|
186
|
-
([module-fseq opts]
|
|
187
|
-
(add-exports
|
|
188
|
-
(into {}
|
|
189
|
-
(comp (map #(.getAbsolutePath %))
|
|
190
|
-
(filter top-level-package-json?)
|
|
191
|
-
(map (fn [path] [path (json/read-str (slurp path))])))
|
|
192
|
-
module-fseq) opts)))
|
|
193
|
-
|
|
194
|
-
(defn node-file-seq->libs-spec*
|
|
195
|
-
"Given a sequence of non-nested node_module paths where the extension ends in
|
|
196
|
-
`.js/.json`, return lib-spec maps for each path containing at least :file,
|
|
197
|
-
:module-type, and :provides."
|
|
198
|
-
[module-fseq opts]
|
|
199
|
-
(let [;; a map of all the *top-level* package.json paths and their exports
|
|
200
|
-
;; to the package.json contents as EDN
|
|
201
|
-
pkg-jsons (get-pkg-jsons module-fseq opts)]
|
|
202
|
-
(into []
|
|
203
|
-
(comp
|
|
204
|
-
(map #(.getAbsolutePath %))
|
|
205
|
-
;; for each file, figure out what it will provide to ClojureScript
|
|
206
|
-
(map #(path->provides % pkg-jsons opts)))
|
|
207
|
-
module-fseq)))
|