Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLJS-3373: Externs Inference issue with vars invoked from foreign libs #173

Merged
merged 1 commit into from May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/main/clojure/cljs/analyzer.cljc
Expand Up @@ -1514,15 +1514,31 @@
else-tag #{else-tag})]
(into then-tag else-tag))))))))

(defn infer-invoke [env {f :fn :keys [args] :as e}]
(let [me (assoc (find-matching-method f args) :op :fn-method)]
(defn js-var? [ast]
(= :js-var (:op ast)))

(defn js-var-fn? [fn-ast]
(js-var? (:info fn-ast)))

(defn fn-ast->tag
[{:keys [info] :as fn-ast}]
(cond
;; ClojureScript Fn
(:fn-var info) (:ret-tag info)
;; Global foreign JS Fn inferred via externs
(:js-fn-var info) (:ret-tag info)
;; Node foreign JS *var*, we cannot distinguish between properties
;; and functions from such libs at this time, we cannot possibly
;; know the returns so break the leading prefix (start with raw 'js tag)
(js-var-fn? fn-ast) 'js
:else (when (= 'js (:ns info)) 'js)))

(defn infer-invoke [env {fn-ast :fn :keys [args] :as e}]
(let [me (assoc (find-matching-method fn-ast args) :op :fn-method)]
(if-some [ret-tag (infer-tag env me)]
ret-tag
(let [{:keys [info]} f]
(if-some [ret-tag (if (or (true? (:fn-var info))
(true? (:js-fn-var info)))
(:ret-tag info)
(when (= 'js (:ns info)) 'js))]
(let []
(if-some [ret-tag (fn-ast->tag fn-ast)]
ret-tag
impl/ANY_SYM)))))

Expand Down
20 changes: 20 additions & 0 deletions src/test/clojure/cljs/externs_infer_tests.clj
Expand Up @@ -413,6 +413,26 @@
:with-core? true}))]
(is (empty? @ws))))

(deftest test-cljs-3373
(testing "var from foreign libraries that are invoked as fn should propagate 'js hints"
(let [ws (atom [])
res (infer-test-helper
{:js-dependency-index {"firebase" {:global-exports '{firebase Firebase}}}
:forms '[(ns foo.core
(:require [firebase :refer [getAuth]]))
(def auth
(doto (getAuth)
(.useDeviceLanguage)
(.onAuthStateChanged (fn [user]))))]
:warnings ws
:warn true
:with-core? false})]
(is (= (unsplit-lines
["Object.getAuth;"
"Object.useDeviceLanguage;"
"Object.onAuthStateChanged;"])
res)))))

(comment
(binding [ana/*cljs-ns* ana/*cljs-ns*]
(ana/no-warn
Expand Down