Skip to content

Commit 38fa4c2

Browse files
authored
CLJS-3408: Handle @extends in externs (#218)
* externs: parse @extends into :super * add test case * check base type when checking externs * add CLJS-3408 test, verifying reported issue is resolved
1 parent 0c5ecd7 commit 38fa4c2

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

src/main/clojure/cljs/analyzer.cljc

+4-1
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,10 @@
10481048
xmeta (meta x')]
10491049
(if (and (= 'Function (:tag xmeta)) (:ctor xmeta))
10501050
(or (has-extern?* (into '[prototype] (next pre)) externs' top)
1051-
(has-extern?* (next pre) externs' top))
1051+
(has-extern?* (next pre) externs' top)
1052+
;; check base type if it exists
1053+
(when-let [super (:super xmeta)]
1054+
(has-extern?* (into [super] (next pre)) externs top)))
10521055
(recur (next pre) externs' top))))))))
10531056

10541057
(defn has-extern?

src/main/clojure/cljs/externs.clj

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@
7373
{:tag (get-tag ty)}
7474
(if (or (.isConstructor info) (.isInterface info))
7575
(let [qname (symbol (.. node getFirstChild getQualifiedName))]
76-
(cond-> {:tag 'Function}
76+
(cond-> (merge {:tag 'Function}
77+
(when (.hasBaseType info)
78+
{:super (get-tag (.getBaseType info))}))
7779
(.isConstructor info) (merge {:ctor qname})
7880
(.isInterface info) (merge {:iface qname})))
7981
(if (or (.hasReturnType info)

src/test/clojure/cljs/externs_infer_tests.clj

+11
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,17 @@
475475
["Object.renderInline;"])
476476
res)))))
477477

478+
(deftest test-cljs-3408
479+
(testing "inheritance of JS Types is inferred"
480+
(let [ws (atom [])
481+
res (infer-test-helper
482+
{:forms '[(ns foo.core)
483+
(.querySelectorAll js/document "div")]
484+
:warnings ws
485+
:warn true
486+
:with-core? true})]
487+
(is (empty? @ws)))))
488+
478489
(comment
479490
(binding [ana/*cljs-ns* ana/*cljs-ns*]
480491
(ana/no-warn

src/test/clojure/cljs/externs_parsing_tests.clj

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
(:require [cljs.closure :as closure]
1111
[cljs.externs :as externs]
1212
[clojure.java.io :as io]
13-
[clojure.test :as test :refer [deftest is]]))
13+
[clojure.test :as test :refer [deftest is]])
14+
(:import [com.google.javascript.jscomp CommandLineRunner]))
1415

1516
(deftest cljs-3121
1617
(let [externs (externs/parse-externs
@@ -34,6 +35,16 @@
3435
(is (= 'any (get-in ns [:defs 'get :ret-tag])))
3536
(is (= 'array (get-in ns [:defs 'getKeys :ret-tag])))))
3637

38+
(deftest test-parse-super
39+
(let [info (->
40+
(filter
41+
(fn [s]
42+
(= "externs.zip//w3c_dom2.js" (.getName s)))
43+
(CommandLineRunner/getDefaultExterns))
44+
first externs/parse-externs externs/index-externs
45+
(find 'HTMLDocument) first meta)]
46+
(is (= 'Document (:super info)))))
47+
3748
(comment
3849

3950
(externs/parse-externs

0 commit comments

Comments
 (0)