|
16 | 16 | [cljs.compiler :as comp] |
17 | 17 | [cljs.closure :as closure] |
18 | 18 | [cljs.externs :as externs] |
19 | | - [cljs.analyzer :as ana]) |
| 19 | + [cljs.analyzer :as ana] |
| 20 | + [clojure.string :as string]) |
20 | 21 | (:use clojure.test)) |
21 | 22 |
|
22 | 23 | (defn collecting-warning-handler [state] |
|
926 | 927 | (map (comp :externs second) |
927 | 928 | (get @test-cenv ::a/namespaces))))))))))) |
928 | 929 |
|
929 | | -(deftest test-type-hint-minimal-infer-unknown-method |
930 | | - (let [test-cenv (atom {::a/externs (externs/externs-map |
| 930 | +(deftest test-type-hint-infer-unknown-method-in-chain |
| 931 | + (let [ws (atom []) |
| 932 | + test-cenv (atom {::a/externs (externs/externs-map |
931 | 933 | (closure/load-externs |
932 | 934 | {:externs ["src/test/externs/test.js"]}))})] |
933 | | - (binding [a/*cljs-ns* a/*cljs-ns* |
934 | | - a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)] |
935 | | - (e/with-compiler-env test-cenv |
936 | | - (a/analyze-form-seq |
937 | | - '[(defn baz [^js/Foo a] |
938 | | - (.gozMethod a))]) |
939 | | - (is (= "Foo.prototype.gozMethod;\n" |
940 | | - (with-out-str |
941 | | - (comp/emit-externs |
942 | | - (reduce util/map-merge {} |
943 | | - (map (comp :externs second) |
944 | | - (get @test-cenv ::a/namespaces))))))))))) |
945 | | - |
946 | | -(comment |
947 | | - (require '[cljs.compiler :as cc]) |
948 | | - (require '[cljs.closure :as closure]) |
949 | | - |
950 | | - ;; FIXME: generates externs we know about including the one we don't |
951 | | - (let [test-cenv (atom {::a/externs (externs/externs-map |
952 | | - (closure/load-externs |
953 | | - {:externs ["src/test/externs/test.js"]}))})] |
954 | | - (binding [a/*cljs-ns* a/*cljs-ns* |
955 | | - a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)] |
956 | | - (e/with-compiler-env test-cenv |
957 | | - (a/analyze-form-seq |
958 | | - '[(defn afun [^js/Foo.Bar x] |
959 | | - (let [z (.baz x)] |
960 | | - (.wozz z)))]) |
961 | | - (cc/emit-externs |
962 | | - (reduce util/map-merge {} |
963 | | - (map (comp :externs second) |
964 | | - (get @test-cenv ::a/namespaces))))))) |
965 | | - |
966 | | - ;; works, generates extern |
967 | | - (let [test-cenv (atom {::a/externs (externs/externs-map |
968 | | - (closure/load-externs |
969 | | - {:externs ["src/test/externs/test.js"]}))})] |
970 | | - (binding [a/*cljs-ns* a/*cljs-ns* |
971 | | - a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)] |
972 | | - (e/with-compiler-env test-cenv |
973 | | - (a/analyze-form-seq |
974 | | - '[(defn baz [^js/Foo a] |
975 | | - (.gozMethod a))]) |
976 | | - (cc/emit-externs |
977 | | - (reduce util/map-merge {} |
978 | | - (map (comp :externs second) |
979 | | - (get @test-cenv ::a/namespaces))))))) |
980 | | - |
981 | | - ;; works, generates extern |
982 | | - (let [test-cenv (atom {::a/externs (externs/externs-map |
| 935 | + (a/with-warning-handlers [(collecting-warning-handler ws)] |
| 936 | + (binding [a/*cljs-ns* a/*cljs-ns* |
| 937 | + a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)] |
| 938 | + (e/with-compiler-env test-cenv |
| 939 | + (a/analyze-form-seq |
| 940 | + '[(defn afun [^js/Foo.Bar x] |
| 941 | + (let [z (.baz x)] |
| 942 | + (.wozz z)))]) |
| 943 | + (is (= "Foo.Boo.prototype.wozz;\n" |
| 944 | + (with-out-str |
| 945 | + (comp/emit-externs |
| 946 | + (reduce util/map-merge {} |
| 947 | + (map (comp :externs second) |
| 948 | + (get @test-cenv ::a/namespaces))))))) |
| 949 | + (is (= 1 (count @ws))) |
| 950 | + (is (string/starts-with? |
| 951 | + (first @ws) |
| 952 | + "Cannot resolve property wozz for inferred type js/Foo.Boo"))))))) |
| 953 | + |
| 954 | +(deftest test-type-hint-infer-unknown-property-in-chain |
| 955 | + (let [ws (atom []) |
| 956 | + test-cenv (atom {::a/externs (externs/externs-map |
983 | 957 | (closure/load-externs |
984 | 958 | {:externs ["src/test/externs/test.js"]}))})] |
985 | | - (binding [a/*cljs-ns* a/*cljs-ns* |
986 | | - a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)] |
987 | | - (e/with-compiler-env test-cenv |
988 | | - (a/analyze-form-seq |
989 | | - '[(.gozMethod (js/baz))]) |
990 | | - (cc/emit-externs |
991 | | - (reduce util/map-merge {} |
992 | | - (map (comp :externs second) |
993 | | - (get @test-cenv ::a/namespaces))))))) |
994 | | - |
995 | | - ;; known extern |
996 | | - (let [test-cenv (atom {::a/externs (externs/externs-map |
| 959 | + (a/with-warning-handlers [(collecting-warning-handler ws)] |
| 960 | + (binding [a/*cljs-ns* a/*cljs-ns* |
| 961 | + a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)] |
| 962 | + (e/with-compiler-env test-cenv |
| 963 | + (a/analyze-form-seq |
| 964 | + '[(defn afun [^js/Foo.Bar x] |
| 965 | + (let [z (.baz x)] |
| 966 | + (.-wozz z)))]) |
| 967 | + (is (= "Foo.Boo.prototype.wozz;\n" |
| 968 | + (with-out-str |
| 969 | + (comp/emit-externs |
| 970 | + (reduce util/map-merge {} |
| 971 | + (map (comp :externs second) |
| 972 | + (get @test-cenv ::a/namespaces))))))) |
| 973 | + (is (= 1 (count @ws))) |
| 974 | + (is (string/starts-with? |
| 975 | + (first @ws) |
| 976 | + "Cannot resolve property wozz for inferred type js/Foo.Boo"))))))) |
| 977 | + |
| 978 | +(deftest test-type-hint-infer-unknown-method |
| 979 | + (let [ws (atom []) |
| 980 | + test-cenv (atom {::a/externs (externs/externs-map |
997 | 981 | (closure/load-externs |
998 | 982 | {:externs ["src/test/externs/test.js"]}))})] |
999 | | - (binding [a/*cljs-ns* a/*cljs-ns* |
1000 | | - a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)] |
1001 | | - (e/with-compiler-env test-cenv |
1002 | | - (a/analyze-form-seq |
1003 | | - '[(.gozMethod (js/baz))]) |
1004 | | - (cc/emit-externs |
1005 | | - (reduce util/map-merge {} |
1006 | | - (map (comp :externs second) |
1007 | | - (get @test-cenv ::a/namespaces))))))) |
1008 | | - |
1009 | | - (let [test-cenv (atom {::a/externs (externs/externs-map |
| 983 | + (a/with-warning-handlers [(collecting-warning-handler ws)] |
| 984 | + (binding [a/*cljs-ns* a/*cljs-ns* |
| 985 | + a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)] |
| 986 | + (e/with-compiler-env test-cenv |
| 987 | + (a/analyze-form-seq |
| 988 | + '[(defn baz [^js/Foo a] |
| 989 | + (.gozMethod a))]) |
| 990 | + (is (= "Foo.prototype.gozMethod;\n" |
| 991 | + (with-out-str |
| 992 | + (comp/emit-externs |
| 993 | + (reduce util/map-merge {} |
| 994 | + (map (comp :externs second) |
| 995 | + (get @test-cenv ::a/namespaces))))))) |
| 996 | + (is (= 1 (count @ws))) |
| 997 | + (is (string/starts-with? |
| 998 | + (first @ws) |
| 999 | + "Cannot resolve property gozMethod for inferred type js/Foo"))))))) |
| 1000 | + |
| 1001 | +(deftest test-infer-unknown-method-from-externs |
| 1002 | + (let [ws (atom []) |
| 1003 | + test-cenv (atom {::a/externs (externs/externs-map |
1010 | 1004 | (closure/load-externs |
1011 | 1005 | {:externs ["src/test/externs/test.js"]}))})] |
1012 | | - (binding [a/*cljs-ns* a/*cljs-ns* |
1013 | | - a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)] |
1014 | | - (e/with-compiler-env test-cenv |
1015 | | - (a/analyze-form-seq |
1016 | | - '[(fn [^js/Foo.Bar x] |
1017 | | - (let [z (.baz x)] |
1018 | | - (.-wozz z)))]) |
1019 | | - (cc/emit-externs |
1020 | | - (reduce util/map-merge {} |
1021 | | - (map (comp :externs second) |
1022 | | - (get @test-cenv ::a/namespaces))))))) |
1023 | | - |
1024 | | - (let [test-cenv (atom {::a/externs (externs/externs-map |
| 1006 | + (a/with-warning-handlers [(collecting-warning-handler ws)] |
| 1007 | + (binding [a/*cljs-ns* a/*cljs-ns* |
| 1008 | + a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)] |
| 1009 | + (e/with-compiler-env test-cenv |
| 1010 | + (a/analyze-form-seq |
| 1011 | + '[(.gozMethod (js/baz))]) |
| 1012 | + (is (= "Foo.prototype.gozMethod;\n" |
| 1013 | + (with-out-str |
| 1014 | + (comp/emit-externs |
| 1015 | + (reduce util/map-merge {} |
| 1016 | + (map (comp :externs second) |
| 1017 | + (get @test-cenv ::a/namespaces))))))) |
| 1018 | + (is (= 1 (count @ws))) |
| 1019 | + (is (string/starts-with? |
| 1020 | + (first @ws) |
| 1021 | + "Cannot resolve property gozMethod for inferred type js/Foo"))))))) |
| 1022 | + |
| 1023 | +(deftest test-infer-js-require |
| 1024 | + (let [ws (atom []) |
| 1025 | + test-cenv (atom {::a/externs (externs/externs-map |
1025 | 1026 | (closure/load-externs |
1026 | 1027 | {:externs ["src/test/externs/test.js"]}))})] |
1027 | | - (binding [a/*cljs-ns* a/*cljs-ns* |
1028 | | - a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)] |
1029 | | - (e/with-compiler-env test-cenv |
1030 | | - (a/analyze-form-seq |
1031 | | - '[(ns foo.core) |
1032 | | - (def React (js/require "react")) |
1033 | | - (.log js/console (.-Component React))]) |
1034 | | - (cc/emit-externs |
1035 | | - (reduce util/map-merge {} |
1036 | | - (map (comp :externs second) |
1037 | | - (get @test-cenv ::a/namespaces))))))) |
1038 | | - |
1039 | | - ) |
| 1028 | + (a/with-warning-handlers [(collecting-warning-handler ws)] |
| 1029 | + (binding [a/*cljs-ns* a/*cljs-ns* |
| 1030 | + a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)] |
| 1031 | + (e/with-compiler-env test-cenv |
| 1032 | + (a/analyze-form-seq |
| 1033 | + '[(ns foo.core) |
| 1034 | + (def React (js/require "react")) |
| 1035 | + (.log js/console (.-Component React))]) |
| 1036 | + (is (= "var require;\nObject.Component;\n" |
| 1037 | + (with-out-str |
| 1038 | + (comp/emit-externs |
| 1039 | + (reduce util/map-merge {} |
| 1040 | + (map (comp :externs second) |
| 1041 | + (get @test-cenv ::a/namespaces))))))) |
| 1042 | + (is (= 1 (count @ws))) |
| 1043 | + (is (string/starts-with? |
| 1044 | + (first @ws) |
| 1045 | + "Adding extern to Object for property Component"))))))) |
0 commit comments