Browse files

CLJS-471: prevent empty regexps from causing compiler errors

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...
1 parent 0d6d293 commit acf3a6dda7c5808f5210a59ecf6d6f3bdca1d63f @cemerick cemerick committed with swannodette Oct 22, 2013
Showing with 6 additions and 2 deletions.
  1. +4 −2 src/clj/cljs/compiler.clj
  2. +2 −0 test/cljs/cljs/core_test.cljs
View
6 src/clj/cljs/compiler.clj
@@ -151,8 +151,10 @@
(emits (wrap-in-double-quotes (escape-char x))))
(defmethod emit-constant java.util.regex.Pattern [x]
- (let [[_ flags pattern] (re-find #"^(?:\(\?([idmsux]*)\))?(.*)" (str x))]
- (emits \/ (.replaceAll (re-matcher #"/" pattern) "\\\\/") \/ flags)))
+ (if (= "" (str x))
+ (emits "(new RegExp(\"\"))")
+ (let [[_ flags pattern] (re-find #"^(?:\(\?([idmsux]*)\))?(.*)" (str x))]
+ (emits \/ (.replaceAll (re-matcher #"/" pattern) "\\\\/") \/ flags))))
(def ^:const goog-hash-max 0x100000000)
View
2 test/cljs/cljs/core_test.cljs
@@ -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 "f(.)o") "foo bar foo baz foo zot") (list ["foo" "o"] ["foo" "o"] ["foo" "o"])))
(assert (= (re-matches (re-pattern "(?i)foo") "Foo") "Foo"))
+ ; new RegExp("").source => "(?:)" on webkit-family envs, "" elsewhere
+ (assert (#{"#\"\"" "#\"(?:)\""} (pr-str #"")))
;; destructuring
(assert (= [2 1] (let [[a b] [1 2]] [b a])))

0 comments on commit acf3a6d

Please sign in to comment.