Skip to content

Commit aec61c1

Browse files
committed
CLJS-3438: Inference for goog.object/containsKey returns any, not boolean
- fix desugar-dotted-expr, generated malformed AST in the case of goog.module var - compiler test for goog.object/containsKey - uncomment test now passes ... but compiler-tests are failing due to warning about incorrect usage of goog.object/get, but that's because we don't parse options params yet also need to resolve to the original goog.module var name and not the local thing in the namespace
1 parent aa5e751 commit aec61c1

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,9 +1214,11 @@
12141214

12151215
(defmethod resolve* :goog-module
12161216
[env sym full-ns current-ns]
1217-
{:name (symbol (str current-ns) (str (munge-goog-module-lib full-ns) "." (name sym)))
1218-
:ns current-ns
1219-
:op :var})
1217+
(let [sym-ast (gets @env/*compiler* ::namespaces full-ns :defs (symbol (name sym)))]
1218+
(merge sym-ast
1219+
{:name (symbol (str current-ns) (str (munge-goog-module-lib full-ns) "." (name sym)))
1220+
:ns current-ns
1221+
:op :var})))
12201222

12211223
(defmethod resolve* :global
12221224
[env sym full-ns current-ns]
@@ -3946,14 +3948,20 @@
39463948
{:op :host-field
39473949
:env (:env expr)
39483950
:form (list '. prefix field)
3949-
:target (desugar-dotted-expr (-> expr
3951+
;; goog.module vars get converted to the form of
3952+
;; current.ns/goog$module.theDef, we need to dissoc
3953+
;; actual extern var info so we get something well-formed
3954+
:target (desugar-dotted-expr (-> (dissoc expr :info)
39503955
(assoc :name prefix
39513956
:form prefix)
39523957
(dissoc :tag)
39533958
(assoc-in [:info :name] prefix)
39543959
(assoc-in [:env :context] :expr)))
39553960
:field field
39563961
:tag (:tag expr)
3962+
;; in the case of goog.module var if there is :info,
3963+
;; we need to adopt it now as this is where :ret-tag info lives
3964+
:info (:info expr)
39573965
:children [:target]})
39583966
expr)
39593967
;:var

src/test/clojure/cljs/compiler_tests.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,16 @@
391391
(if (gstring/contains "foobar" "foo") true false)]))]
392392
(is (nil? (re-find #"truth_" code))))))
393393

394+
(deftest test-goog-module-infer-cljs-3438
395+
(testing "goog.object/containKey requires correct handling of vars from
396+
goog.module namespace"
397+
(let [code (env/with-compiler-env (env/default-compiler-env)
398+
(compile-form-seq
399+
'[(ns test.foo
400+
(:require [goog.object :as gobject]))
401+
(if (gobject/containsKey nil nil) true false)]))]
402+
(is (nil? (re-find #"truth_" code))))))
403+
394404
;; CLJS-1225
395405

396406
(comment

src/test/clojure/cljs/type_inference_tests.clj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,11 @@
403403
(:require [goog.string :as gstring]))
404404
(gstring/contains "foobar" "foo")]
405405
{} true)))))
406-
;; FIXME: infers any instead of boolean, nothing wrong w/ the externs parsing
407-
;; but this definitely does not work at the moment
408-
#_(is (= 'boolean
406+
(is (= 'boolean
409407
(:tag
410408
(env/with-compiler-env (env/default-compiler-env)
411409
(ana/analyze-form-seq
412410
'[(ns test.foo
413411
(:require [goog.object :as gobject]))
414412
(gobject/containsKey (js-object) "foo")]
415413
{} true))))))
416-

0 commit comments

Comments
 (0)