Skip to content

Commit f24177a

Browse files
committed
if we know some foreign js/foo is a fn from externs, annotate the AST with :js-fn-var
in infer-invoke, handle :js-fn-var case, in the case of an invoke we do not know anything about set the :tag to 'js
1 parent e15af2f commit f24177a

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

src/main/clojure/cljs/analyzer.cljc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,8 @@
850850
:ns 'js
851851
:tag (with-meta (or (js-tag pre) 'js) {:prefix pre})}
852852
(when-let [ret-tag (js-tag pre :ret-tag)]
853-
{:ret-tag ret-tag}))))
853+
{:js-fn-var true
854+
:ret-tag ret-tag}))))
854855
(let [s (str sym)
855856
lb (get locals sym)]
856857
(cond
@@ -1076,7 +1077,10 @@
10761077

10771078
(defn infer-invoke [env e]
10781079
(let [{info :info :as f} (:f e)]
1079-
(if-some [ret-tag (when (true? (:fn-var info)) (:ret-tag info))]
1080+
(if-some [ret-tag (if (or (true? (:fn-var info))
1081+
(true? (:js-fn-var info)))
1082+
(:ret-tag info)
1083+
(when (= 'js (:ns info)) 'js))]
10801084
ret-tag
10811085
(let [args (:args e)
10821086
me (assoc (find-matching-method f args) :op :method)]

src/test/clojure/cljs/analyzer_tests.clj

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,17 @@
659659
(-> (binding [a/*cljs-ns* a/*cljs-ns*]
660660
(e/with-compiler-env externs-cenv
661661
(a/analyze (a/empty-env) 'js/baz)))
662-
:info :ret-tag))))
662+
:info :ret-tag)))
663+
(is (= 'js/Foo
664+
(-> (binding [a/*cljs-ns* a/*cljs-ns*]
665+
(e/with-compiler-env externs-cenv
666+
(a/analyze (a/empty-env) '(js/baz))))
667+
:tag)))
668+
(is (= 'js
669+
(-> (binding [a/*cljs-ns* a/*cljs-ns*]
670+
(e/with-compiler-env externs-cenv
671+
(a/analyze (a/empty-env) '(js/woz))))
672+
:tag))))
663673

664674
(comment
665675
(require '[cljs.compiler :as cc])
@@ -691,8 +701,7 @@
691701
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
692702
(e/with-compiler-env test-cenv
693703
(a/analyze-form-seq
694-
'[(ns foo.core)
695-
(js/console.log (.wozMethod (js/baz)))]))
704+
'[(js/console.log (.wozMethod (js/baz)))]))
696705
(cc/emit-externs
697706
(reduce util/map-merge {}
698707
(map (comp :externs second)
@@ -706,8 +715,7 @@
706715
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
707716
(e/with-compiler-env test-cenv
708717
(a/analyze-form-seq
709-
'[(ns foo.core)
710-
(defn baz [^js/Foo x]
718+
'[(defn afun [^js/Foo x]
711719
(.wozMethod x))]))
712720
(cc/emit-externs
713721
(reduce util/map-merge {}
@@ -722,8 +730,7 @@
722730
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
723731
(e/with-compiler-env test-cenv
724732
(a/analyze-form-seq
725-
'[(ns foo.core)
726-
(defn baz [^js/Foo a]
733+
'[(defn baz [^js/Foo a]
727734
(.gozMethod a))]))
728735
(cc/emit-externs
729736
(reduce util/map-merge {}
@@ -738,8 +745,7 @@
738745
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
739746
(e/with-compiler-env test-cenv
740747
(a/analyze-form-seq
741-
'[(ns foo.core)
742-
(js/console.log (.gozMethod (js/baz)))]))
748+
'[(.gozMethod (js/baz))]))
743749
(cc/emit-externs
744750
(reduce util/map-merge {}
745751
(map (comp :externs second)

0 commit comments

Comments
 (0)