Skip to content
Permalink
Browse files

CLJS-2868: Add ^string hints

Added type hint to subs, munge-str, clojure.string/reverse, clojure.string/replace, clojure.string/replace-first, clojure.string/join, clojure.string/upper-case, clojure.string/lower-case, clojure.string/capitalize, clojure.string/trim, clojure.string/triml, clojure.string/trimr, clojure.string/trim-newline and clojure.string/escape .
  • Loading branch information...
Martin Kučera authored and mfikes committed Feb 20, 2019
1 parent 73272a2 commit 95b13de8300123c3c984b80410475d5acd92af6f
Showing with 50 additions and 16 deletions.
  1. +3 −3 src/main/cljs/cljs/core.cljs
  2. +13 −13 src/main/cljs/clojure/string.cljs
  3. +34 −0 src/test/clojure/cljs/analyzer_tests.clj
@@ -2970,8 +2970,8 @@ reduces them without incurring seq initialization"
(defn subs
"Returns the substring of s beginning at start inclusive, and ending
at end (defaults to length of string), exclusive."
([s start] (.substring s start))
([s start end] (.substring s start end)))
([s start] ^string (.substring s start))
([s start end] ^string (.substring s start end)))

(declare map name)

@@ -11475,7 +11475,7 @@ reduces them without incurring seq initialization"
(str ret "|\\$"))))))
DEMUNGE_PATTERN)

(defn- munge-str [name]
(defn- ^string munge-str [name]
(let [sb (StringBuffer.)]
(loop [i 0]
(if (< i (. name -length))
@@ -18,7 +18,7 @@
(def ^:private re-surrogate-pair
(js/RegExp. "([\\uD800-\\uDBFF])([\\uDC00-\\uDFFF])" "g"))

(defn reverse
(defn ^string reverse
"Returns s with its characters reversed."
[s]
(-> (.replace s re-surrogate-pair "$2$1")
@@ -41,7 +41,7 @@
(f (first matches))
(f (vec matches))))))

(defn replace
(defn ^string replace
"Replaces all instance of match with replacement in s.
match/replacement can be:
@@ -73,7 +73,7 @@

:else (throw (str "Invalid match arg: " match))))

(defn replace-first
(defn ^string replace-first
"Replaces the first instance of match with replacement in s.
match/replacement can be:
@@ -104,7 +104,7 @@
(loop [sb (StringBuffer.) coll (seq coll)]
(if-not (nil? coll)
(recur (. sb (append (str (first coll)))) (next coll))
(.toString sb))))
^string (.toString sb))))
([separator coll]
(loop [sb (StringBuffer.) coll (seq coll)]
(if-not (nil? coll)
@@ -114,19 +114,19 @@
(when-not (nil? coll)
(. sb (append separator)))
(recur sb coll)))
(.toString sb)))))
^string (.toString sb)))))

(defn upper-case
(defn ^string upper-case
"Converts string to all upper-case."
[s]
(.toUpperCase s))

(defn lower-case
(defn ^string lower-case
"Converts string to all lower-case."
[s]
(.toLowerCase s))

(defn capitalize
(defn ^string capitalize
"Converts first character of the string to upper-case, all other
characters to lower-case."
[s]
@@ -193,22 +193,22 @@
[s]
(split s #"\n|\r\n"))

(defn trim
(defn ^string trim
"Removes whitespace from both ends of string."
[s]
(gstring/trim s))

(defn triml
(defn ^string triml
"Removes whitespace from the left side of string."
[s]
(gstring/trimLeft s))

(defn trimr
(defn ^string trimr
"Removes whitespace from the right side of string."
[s]
(gstring/trimRight s))

(defn trim-newline
(defn ^string trim-newline
"Removes all trailing newline \\n or return \\r characters from
string. Similar to Perl's chomp."
[s]
@@ -226,7 +226,7 @@
[s]
(gstring/isEmptySafe s))

(defn escape
(defn ^string escape
"Return a new string, using cmap to escape each character ch
from s as follows:
@@ -1977,3 +1977,37 @@
(recur 1)
(+ 3 x)))))
(is (zero? (count @ws)))))

(deftest test-cljs-2868
(is (= 'string
(e/with-compiler-env test-cenv
(:tag (analyze test-env '(subs "duck" 1 1))))))
(is (= 'string
(e/with-compiler-env test-cenv
(:tag (analyze test-env '(subs "duck" 1))))))

(is (= 'string
(e/with-compiler-env test-cenv
(:tag (analyze test-env '(str))))))
(is (= 'string
(e/with-compiler-env test-cenv
(:tag (analyze test-env '(str 1))))))
(is (= 'string
(e/with-compiler-env test-cenv
(:tag (analyze test-env '(str 1 2))))))

(is (= 'string
(e/with-compiler-env test-cenv
(:tag (analyze test-env '(pr-str 0))))))
(is (= 'string
(e/with-compiler-env test-cenv
(:tag (analyze test-env '(prn-str 0))))))
(is (= 'string
(e/with-compiler-env test-cenv
(:tag (analyze test-env '(print-str 0))))))
(is (= 'string
(e/with-compiler-env test-cenv
(:tag (analyze test-env '(munge-str ""))))))
(is (= 'string
(e/with-compiler-env test-cenv
(:tag (analyze test-env '(demunge-str "")))))))

0 comments on commit 95b13de

Please sign in to comment.
You can’t perform that action at this time.