From: Alex Lam S.L Date: Sat, 15 Feb 2020 05:26:48 +0000 (+0000) Subject: workaround RegExp formatting bugs (#3720) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=0d820e4c0a7a1b1eeee25fb632b9496a9780b28a;p=UglifyJS.git workaround RegExp formatting bugs (#3720) --- diff --git a/lib/output.js b/lib/output.js index 86c0931e..f05c93df 100644 --- a/lib/output.js +++ b/lib/output.js @@ -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) { diff --git a/test/compress/regexp.js b/test/compress/regexp.js index 460ff504..defc0079 100644 --- a/test/compress/regexp.js +++ b/test/compress/regexp.js @@ -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", + ] +}