Skip to content

Commit

Permalink
JS: don't convert carriage returns (\r) to a literal inside template …
Browse files Browse the repository at this point in the history
…literals, fixes #708
  • Loading branch information
tdewolff committed Jun 7, 2024
1 parent bfacf27 commit 7930f01
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
9 changes: 5 additions & 4 deletions js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ func TestJS(t *testing.T) {
{`"string\a\c\'string"`, `"stringac'string"`},
{`"string\∀string"`, `"string∀string"`},
{`"string\0\uFFFFstring"`, "\"string\\0\uffffstring\""},
{`"string\x00\x55\x0A\x0D\x22\x27string"`, "`string\\x00U\n\r\"'string`"},
{`"string\000\12\015\042\47\411string"`, "`string\\0\n\r\"'!1string`"},
{`"string\x00\x55\x0A\x0D\x22\x27string"`, "`string\\x00U\n\\r\"'string`"},
{`"string\000\12\015\042\47\411string"`, "`string\\0\n\\r\"'!1string`"},
{`"\x005"`, `"\x005"`},
{"'string\\n\\rstring'", "`string\n\rstring`"},
{"`\\r`", "`\\r`"},
{"'string\\n\\rstring'", "`string\n\\rstring`"},
{"'string\\\r\nstring\\\nstring\\\rstring\\\u2028string\\\u2029string'", `"stringstringstringstringstringstring"`},
{`"\x7H\u877H"`, `"\x7H\u877H"`},
{`"\u01ac\u01de\u0187\u{0001a0}"`, `"ƬǞƇƠ"`},
Expand Down Expand Up @@ -813,7 +814,7 @@ func TestJS(t *testing.T) {
{`function transform(){{var aaaa=[];for(var b=0;;){}for(var b in aaaa){}var aaaa=[];for(var b=0;;){}}}`, `function transform(){{for(var aaaa=[],b=0;;);for(b in aaaa);for(var aaaa=[],b=0;;);}}`}, // #619
{`for(var a=0;;){var b=5;for(var c=0;;);}`, `for(var b,c,a=0;;)for(b=5,c=0;;);`}, // #634
{"if(a)for(;;)\n;else b", `if(a)for(;;);else b`}, // #636
{`'\u000A\u000D'`, "`\n\r`"}, // #653
{`'\u000A\u000D'`, "`\n\\r`"}, // #653
{`for(!a;b;c);`, "for(!a;b;c);"}, // #656
{"var a = /*! comment */ b;", "/*! comment */var a=b"}, // #664
{`var c="";for(let i=0;;);var d="";for(let i=0;;);`, `var d,c="";for(let i=0;;);d="";for(let i=0;;);`}, // #687
Expand Down
11 changes: 4 additions & 7 deletions js/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ func replaceEscapes(b []byte, quote byte, prefix, suffix int) []byte {
for i := prefix; i < len(b)-suffix; i++ {
if c := b[i]; c == '\\' {
c = b[i+1]
if c == quote || c == '\\' || quote != '`' && (c == 'n' || c == 'r') || c == '0' && (len(b)-suffix <= i+2 || b[i+2] < '0' || '7' < b[i+2]) {
if c == quote || c == '\\' || c == 'r' || quote != '`' && c == 'n' || c == '0' && (len(b)-suffix <= i+2 || b[i+2] < '0' || '7' < b[i+2]) {
// keep escape sequence
i++
continue
Expand All @@ -1047,7 +1047,7 @@ func replaceEscapes(b []byte, quote byte, prefix, suffix int) []byte {
// hexadecimal escapes
_, _ = hex.Decode(b[i:i+1:i+1], b[i+2:i+4])
n = 4
if b[i] == '\\' || b[i] == quote || quote != '`' && (b[i] == '\n' || b[i] == '\r') || b[i] == 0 {
if b[i] == '\\' || b[i] == quote || b[i] == '\r' || quote != '`' && b[i] == '\n' || b[i] == 0 {
if b[i] == '\n' {
b[i+1] = 'n'
} else if b[i] == '\r' {
Expand Down Expand Up @@ -1104,7 +1104,7 @@ func replaceEscapes(b []byte, quote byte, prefix, suffix int) []byte {
i += 4
n -= 4
}
} else if quote == '`' || num != 10 && num != 13 {
} else if num != 13 && (quote == '`' || num != 10) {
// decode unicode character to UTF-8 and put at the end of the escape sequence
// then skip the first part of the escape sequence until the decoded character
m := utf8.RuneLen(rune(num))
Expand Down Expand Up @@ -1141,7 +1141,7 @@ func replaceEscapes(b []byte, quote byte, prefix, suffix int) []byte {
}
}
b[i] = num
if num == 0 || num == '\\' || num == quote || quote != '`' && (num == '\n' || num == '\r') {
if num == 0 || num == '\\' || num == quote || num == '\r' || quote != '`' && num == '\n' {
if num == 0 {
b[i+1] = '0'
} else if num == '\n' {
Expand All @@ -1160,9 +1160,6 @@ func replaceEscapes(b []byte, quote byte, prefix, suffix int) []byte {
} else if quote == '`' && c == 'n' {
b[i] = '\n'
i++
} else if quote == '`' && c == 'r' {
b[i] = '\r'
i++
} else if c == 't' {
b[i] = '\t'
i++
Expand Down
10 changes: 5 additions & 5 deletions js/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func TestString(t *testing.T) {
{`"abc"`, `"abc"`},
{`'abc'`, `"abc"`},
{`"\8\9\t"`, "\"89\t\""},
{`"\n\r"`, "`\n\r`"},
{`"\12\15"`, "`\n\r`"},
{`"\x0A\x0D"`, "`\n\r`"},
{`"\u000A\u000D"`, "`\n\r`"},
{`"\u{A}\u{D}"`, "`\n\r`"},
{`"\n\r"`, "`\n\\r`"},
{`"\12\15"`, "`\n\\r`"},
{`"\x0A\x0D"`, "`\n\\r`"},
{`"\u000A\u000D"`, "`\n\\r`"},
{`"\u{A}\u{D}"`, "`\n\\r`"},
{`"\n$"`, "`\n$`"},
{`"\n${"`, `"\n${"`},
{`"\n\r${${"`, `"\n\r${${"`},
Expand Down

0 comments on commit 7930f01

Please sign in to comment.