Skip to content

Commit

Permalink
CLJS-471: prevent empty regexps from causing compiler errors
Browse files Browse the repository at this point in the history
Depending on the JS vm being used, empty regexes will print as
either #"" or #"(?:)" (the latter being emitted by those with a
webkit heritage AFAICT).
  • Loading branch information
cemerick authored and swannodette committed Oct 24, 2013
1 parent 0d6d293 commit acf3a6d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/clj/cljs/compiler.clj
Expand Up @@ -151,8 +151,10 @@
(emits (wrap-in-double-quotes (escape-char x)))) (emits (wrap-in-double-quotes (escape-char x))))


(defmethod emit-constant java.util.regex.Pattern [x] (defmethod emit-constant java.util.regex.Pattern [x]
(let [[_ flags pattern] (re-find #"^(?:\(\?([idmsux]*)\))?(.*)" (str x))] (if (= "" (str x))
(emits \/ (.replaceAll (re-matcher #"/" pattern) "\\\\/") \/ flags))) (emits "(new RegExp(\"\"))")
(let [[_ flags pattern] (re-find #"^(?:\(\?([idmsux]*)\))?(.*)" (str x))]
(emits \/ (.replaceAll (re-matcher #"/" pattern) "\\\\/") \/ flags))))


(def ^:const goog-hash-max 0x100000000) (def ^:const goog-hash-max 0x100000000)


Expand Down
2 changes: 2 additions & 0 deletions test/cljs/cljs/core_test.cljs
Expand Up @@ -628,6 +628,8 @@
(assert (= (re-seq (re-pattern "foo") "foo bar foo baz foo zot") (list "foo" "foo" "foo"))) (assert (= (re-seq (re-pattern "foo") "foo bar foo baz foo zot") (list "foo" "foo" "foo")))
(assert (= (re-seq (re-pattern "f(.)o") "foo bar foo baz foo zot") (list ["foo" "o"] ["foo" "o"] ["foo" "o"]))) (assert (= (re-seq (re-pattern "f(.)o") "foo bar foo baz foo zot") (list ["foo" "o"] ["foo" "o"] ["foo" "o"])))
(assert (= (re-matches (re-pattern "(?i)foo") "Foo") "Foo")) (assert (= (re-matches (re-pattern "(?i)foo") "Foo") "Foo"))
; new RegExp("").source => "(?:)" on webkit-family envs, "" elsewhere
(assert (#{"#\"\"" "#\"(?:)\""} (pr-str #"")))


;; destructuring ;; destructuring
(assert (= [2 1] (let [[a b] [1 2]] [b a]))) (assert (= [2 1] (let [[a b] [1 2]] [b a])))
Expand Down

0 comments on commit acf3a6d

Please sign in to comment.