workaround RegExp formatting bugs (#3720)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 15 Feb 2020 05:26:48 +0000 (05:26 +0000)
committerGitHub <noreply@github.com>
Sat, 15 Feb 2020 05:26:48 +0000 (05:26 +0000)
lib/output.js
test/compress/regexp.js

index 86c0931..f05c93d 100644 (file)
@@ -1354,11 +1354,18 @@ function OutputStream(options) {
     DEFPRINT(AST_RegExp, function(self, output) {
         var regexp = self.value;
         var str = regexp.toString();
+        var end = str.lastIndexOf("/");
         if (regexp.raw_source) {
-            str = "/" + regexp.raw_source + str.slice(str.lastIndexOf("/"));
+            str = "/" + regexp.raw_source + str.slice(end);
+        } else if (end == 1) {
+            str = "/(?:)" + str.slice(end);
+        } else if (str.indexOf("/", 1) < end) {
+            str = "/" + str.slice(1, end).replace(/\\\\|[^/]?\//g, function(match) {
+                return match[0] == "\\" ? match : match.slice(0, -1) + "\\/";
+            }) + str.slice(end);
         }
-        output.print(output.to_utf8(str).replace(/\\(?:\0(?![0-9])|[^\0])/g, function(seq) {
-            switch (seq[1]) {
+        output.print(output.to_utf8(str).replace(/\\(?:\0(?![0-9])|[^\0])/g, function(match) {
+            switch (match[1]) {
               case "\n": return "\\n";
               case "\r": return "\\r";
               case "\t": return "\t";
@@ -1368,7 +1375,7 @@ function OutputStream(options) {
               case "\x0B": return "\v";
               case "\u2028": return "\\u2028";
               case "\u2029": return "\\u2029";
-              default: return seq;
+              default: return match;
             }
         }).replace(/[\n\r\u2028\u2029]/g, function(c) {
             switch (c) {
index 460ff50..defc007 100644 (file)
@@ -186,3 +186,72 @@ issue_3434_3: {
         /\nfo\n[\n]o\bbb/;
     }
 }
+
+issue_3434_4: {
+    options = {
+        evaluate: true,
+        unsafe: true,
+    }
+    input: {
+        [
+            [ "", RegExp("") ],
+            [ "/", RegExp("/") ],
+            [ "//", RegExp("//") ],
+            [ "\/", RegExp("\\/") ],
+            [ "///", RegExp("///") ],
+            [ "/\/", RegExp("/\\/") ],
+            [ "\//", RegExp("\\//") ],
+            [ "\\/", RegExp("\\\\/") ],
+            [ "////", RegExp("////") ],
+            [ "//\/", RegExp("//\\/") ],
+            [ "/\//", RegExp("/\\//") ],
+            [ "/\\/", RegExp("/\\\\/") ],
+            [ "\///", RegExp("\\///") ],
+            [ "\/\/", RegExp("\\/\\/") ],
+            [ "\\//", RegExp("\\\\//") ],
+            [ "\\\/", RegExp("\\\\\\/") ],
+        ].forEach(function(test) {
+            console.log(test[1].test("\\"), test[1].test(test[0]));
+        });
+    }
+    expect: {
+        [
+            [ "", /(?:)/ ],
+            [ "/", /\// ],
+            [ "//", /\/\// ],
+            [ "/", /\// ],
+            [ "///", /\/\/\// ],
+            [ "//", /\/\// ],
+            [ "//", /\/\// ],
+            [ "\\/", /\\\// ],
+            [ "////", /\/\/\/\// ],
+            [ "///", /\/\/\// ],
+            [ "///", /\/\/\// ],
+            [ "/\\/", /\/\\\// ],
+            [ "///", /\/\/\// ],
+            [ "//", /\/\// ],
+            [ "\\//", /\\\/\// ],
+            [ "\\/", /\\\// ],
+        ].forEach(function(test) {
+            console.log(test[1].test("\\"), test[1].test(test[0]));
+        });
+    }
+    expect_stdout: [
+        "true true",
+        "false true",
+        "false true",
+        "false true",
+        "false true",
+        "false true",
+        "false true",
+        "false true",
+        "false true",
+        "false true",
+        "false true",
+        "false true",
+        "false true",
+        "false true",
+        "false true",
+        "false true",
+    ]
+}