feat(minifier): may add space before RegExpLiteral (#397)

This commit is contained in:
Wenzhe Wang 2023-05-29 23:11:40 +08:00 committed by GitHub
parent c01df484db
commit 3792b70dbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View file

@ -801,6 +801,14 @@ impl Gen for BigintLiteral {
impl Gen for RegExpLiteral {
fn gen(&self, p: &mut Printer) {
let last = p.peek_nth(0);
// Avoid forming a single-line comment or "</script" sequence
if Some('/') == last
|| (Some('<') == last
&& self.regex.pattern.as_str().to_lowercase().starts_with("script"))
{
p.print(b' ');
}
p.print(b'/');
p.print_str(self.regex.pattern.as_bytes());
p.print(b'/');

View file

@ -570,7 +570,7 @@ fn whitespace() {
// test("1 + -Infinity", "1+-1/0");
// test("1 - -Infinity", "1- -1/0;");
// test("/x/ / /y/", "/x// /y/");
test("/x/ / /y/", "/x// /y/");
test("/x/ + Foo", "/x/+Foo");
test("/x/ instanceof Foo", "/x/ instanceof Foo");
test("[x] instanceof Foo", "[x]instanceof Foo");
@ -693,8 +693,17 @@ fn jsx() {}
fn jsx_single_line() {}
#[test]
#[ignore]
fn avoid_slash_script() {}
fn avoid_slash_script() {
// Positive cases
test("x = 1 < /script/.exec(y).length", "x=1< /script/.exec(y).length");
test("x = 1 < /SCRIPT/.exec(y).length", "x=1< /SCRIPT/.exec(y).length");
test("x = 1 < /ScRiPt/.exec(y).length", "x=1< /ScRiPt/.exec(y).length");
test("x = 1 << /script/.exec(y).length", "x=1<< /script/.exec(y).length");
// Negative cases
test("x = 1 < / script/.exec(y).length", "x=1</ script/.exec(y).length");
test("x = 1 << / script/.exec(y).length", "x=1<</ script/.exec(y).length");
}
#[test]
fn binary_operator_visitor() {