Skip to content

Commit dca338e

Browse files
committed
* don't return raised js/Foo types for boolean, number, string
* remove ^boolean hint from array? * fix tests * add test inference test case for array?
1 parent 7c7fca7 commit dca338e

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@
243243
[x]
244244
(coercive-= x nil))
245245

246-
(defn ^boolean array?
246+
(defn array?
247247
"Returns true if x is a JavaScript array."
248248
[x]
249249
(if (identical? *target* "nodejs")

src/main/clojure/cljs/analyzer.cljc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,12 @@
11221122
(js-tag pre tag-type externs externs))
11231123
([pre tag-type externs top]
11241124
(when-let [tag (get-in (resolve-extern pre externs) [:info tag-type])]
1125-
(lift-tag-to-js tag))))
1125+
(case tag
1126+
;; don't lift these, analyze-dot will raise them for analysis
1127+
;; representing these types as js/Foo is a hassle as it widens the
1128+
;; return types unnecessarily i.e. #{boolean js/Boolean}
1129+
(boolean number string) tag
1130+
(lift-tag-to-js tag)))))
11261131

11271132
(defn dotted-symbol? [sym]
11281133
(let [s (str sym)]

src/test/clojure/cljs/externs_infer_tests.clj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
(is (= 'js/Console (ana/js-tag '[console] :tag externs)))
107107
(is (= 'js/Function (ana/js-tag '[console log] :tag externs)))
108108
(is (= 'js/undefined (ana/js-tag '[console log] :ret-tag externs)))
109-
(is (= 'js/Boolean (ana/js-tag '[Number isNaN] :ret-tag externs)))
109+
(is (= 'boolean (ana/js-tag '[Number isNaN] :ret-tag externs)))
110110
(is (= 'js/Foo (ana/js-tag '[baz] :ret-tag externs)))))
111111

112112
(comment
@@ -148,23 +148,23 @@
148148
(get @test-cenv ::ana/namespaces))))))))))))
149149

150150
(deftest test-externs-type-infer
151-
(is (= 'js/Boolean
151+
(is (= 'boolean
152152
(-> (binding [ana/*cljs-ns* ana/*cljs-ns*]
153153
(env/with-compiler-env (env/default-compiler-env)
154154
(analyze (ana/empty-env) '(.isNaN js/Number 1))))
155155
:tag)))
156-
(is (= 'js/Boolean
156+
(is (= 'boolean
157157
(-> (binding [ana/*cljs-ns* ana/*cljs-ns*]
158158
(env/with-compiler-env (env/default-compiler-env)
159159
(analyze (ana/empty-env) '(js/Number.isNaN 1))))
160160
:tag)))
161-
(is (= 'js/Boolean
161+
(is (= 'boolean
162162
(-> (binding [ana/*cljs-ns* ana/*cljs-ns*]
163163
(env/with-compiler-env (env/default-compiler-env)
164164
(analyze (ana/empty-env) '(let [x js/Number]
165165
(.isNaN x 1)))))
166166
:tag)))
167-
(is (= 'js/Boolean
167+
(is (= 'boolean
168168
(-> (binding [ana/*cljs-ns* ana/*cljs-ns*]
169169
(env/with-compiler-env (env/default-compiler-env)
170170
(analyze (ana/empty-env) '(js/isNaN 1))))
@@ -174,12 +174,12 @@
174174
(env/with-compiler-env (env/default-compiler-env)
175175
(analyze (ana/empty-env) '(.generateKey js/crypto.subtle))))
176176
:tag)))
177-
(is (= 'js/String
177+
(is (= 'string
178178
(-> (binding [ana/*cljs-ns* ana/*cljs-ns*]
179179
(env/with-compiler-env (env/default-compiler-env)
180180
(analyze (ana/empty-env) '(.toUpperCase "foo"))))
181181
:tag)))
182-
(is (= 'js/Boolean
182+
(is (= 'boolean
183183
(-> (binding [ana/*cljs-ns* ana/*cljs-ns*]
184184
(env/with-compiler-env (env/default-compiler-env)
185185
(analyze (ana/empty-env) '(.isArray js/Array (array)))))

src/test/clojure/cljs/type_inference_tests.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@
313313
; 'clj))
314314
)
315315

316+
(deftest lib-inference-extern-call
317+
(testing "Test return type inference for core fns whose
318+
internal implementation uses standard JS APIs"
319+
(is (= 'boolean
320+
(env/with-compiler-env test-cenv
321+
(:tag (analyze test-env '(array? (array)))))))))
322+
316323
(deftest test-always-true-if
317324
(is (= (env/with-compiler-env test-cenv
318325
(:tag (analyze test-env '(if 1 2 "foo"))))

0 commit comments

Comments
 (0)