Skip to content

Commit

Permalink
fix(es/codegen): Fix codegen of \\0 (#8433)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8431
  • Loading branch information
kdy1 authored Jan 19, 2024
1 parent fefe963 commit 9f1ce3a
Show file tree
Hide file tree
Showing 41 changed files with 81 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log("\x000");
console.log("\00");
23 changes: 23 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8431/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2022",
"loose": false,
"minify": {
"mangle": true,
"compress": {
"defaults": true,
"arguments": true,
"passes": 2
}
}
},
"module": {
"type": "es6"
},
"minify": true,
"isModule": true
}
2 changes: 2 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8431/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const exampleCode_0 = '\0';
export const exampleCode_x00 = '\x00';
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issues-8xxx/8431/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const exampleCode_0="\0";export const exampleCode_x00="\0";
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var USTAR = "ustar\x0000";
var USTAR = "ustar\000";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//// [scannerStringLiteralWithContainingNullCharacter1.ts]
" \x00 ";
" \0 ";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//// [templateStringControlCharacterEscapes01.ts]
var x = "\x00\x00\x00 0 00 0000";
var x = "\0\0\0 0 00 0000";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//// [unicodeExtendedEscapesInStrings01_ES5.ts]
var x = "\x00";
var x = "\0";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//// [unicodeExtendedEscapesInStrings02_ES5.ts]
var x = "\x00";
var x = "\0";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//// [unicodeExtendedEscapesInStrings03_ES5.ts]
var x = "\x00";
var x = "\0";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//// [unicodeExtendedEscapesInStrings04_ES5.ts]
var x = "\x00";
var x = "\0";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//// [unicodeExtendedEscapesInTemplates01_ES5.ts]
var x = "\x00";
var x = "\0";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//// [unicodeExtendedEscapesInTemplates02_ES5.ts]
var x = "\x00";
var x = "\0";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//// [unicodeExtendedEscapesInTemplates03_ES5.ts]
var x = "\x00";
var x = "\0";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//// [unicodeExtendedEscapesInTemplates04_ES5.ts]
var x = "\x00";
var x = "\0";
6 changes: 5 additions & 1 deletion crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3951,7 +3951,11 @@ fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> String {
while let Some(c) = iter.next() {
match c {
'\x00' => {
buf.push_str("\\x00");
if target < EsVersion::Es5 {
buf.push_str("\\x00");
} else {
buf.push_str("\\0");
}
}
'\u{0008}' => buf.push_str("\\b"),
'\u{000c}' => buf.push_str("\\f"),
Expand Down
10 changes: 6 additions & 4 deletions crates/swc_ecma_codegen/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub(crate) fn assert_min(from: &str, to: &str) {
assert_eq!(DebugUsingDisplay(out.trim()), DebugUsingDisplay(to),);
}

#[track_caller]
pub(crate) fn assert_min_target(from: &str, to: &str, target: EsVersion) {
let out = parse_then_emit(
from,
Expand All @@ -117,6 +118,7 @@ pub(crate) fn assert_min_target(from: &str, to: &str, target: EsVersion) {
}

/// Clone of the regular `assert_min` function but with TypeScript syntax.
#[track_caller]
pub(crate) fn assert_min_typescript(from: &str, to: &str) {
let out = parse_then_emit(
from,
Expand Down Expand Up @@ -611,15 +613,15 @@ fn test_get_quoted_utf16() {
es2020("abcde", "\"abcde\"");
es2020(
"\x00\r\n\u{85}\u{2028}\u{2029};",
"\"\\x00\\r\\n\\x85\\u2028\\u2029;\"",
"\"\\0\\r\\n\\x85\\u2028\\u2029;\"",
);

es2020("\n", "\"\\n\"");
es2020("\t", "\"\t\"");

es2020("'string'", "\"'string'\"");

es2020("\u{0}", "\"\\x00\"");
es2020("\u{0}", "\"\\0\"");
es2020("\u{1}", "\"\\x01\"");

es2020("\u{1000}", "\"\\u1000\"");
Expand Down Expand Up @@ -655,7 +657,7 @@ fn issue_1452_1() {
fn issue_1619_1() {
assert_min_target(
"\"\\x00\" + \"\\x31\"",
"\"\\x00\"+\"1\"",
"\"\\0\"+\"1\"",
EsVersion::latest(),
);
}
Expand All @@ -664,7 +666,7 @@ fn issue_1619_1() {
fn issue_1619_2() {
assert_min_target(
"\"\\x00\" + \"\\x31\"",
"\"\\x00\"+\"1\"",
"\"\\0\"+\"1\"",
EsVersion::latest(),
);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_codegen/tests/fixture/string/output.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
"use strict";("\x00");
"use strict";("\0");
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"use strict";("\x00x");
"use strict";("\0x");
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"\n\r \v\b\f\\'\"\x00";
"\n\r \v\b\f\\'\"\0";
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"Hello\x00World";
"Hello\0World";
Original file line number Diff line number Diff line change
@@ -1 +1 @@
function a(){"use strict";"\x00"}
function a(){"use strict";"\0"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
("\x00");
("\0");
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(function(){"use strict";"\x00"}());
(function(){"use strict";"\0"}());
Original file line number Diff line number Diff line change
@@ -1 +1 @@
("\x00");
("\0");
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/tests/test262/0b1fc7208759253b.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/tests/test262/3ae4f46daa688c58.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"use strict";
("\x00");
("\0");
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/tests/test262/3d2ab39608730a47.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"use strict";
("\x00x");
("\0x");
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/tests/test262/4672c2ef688237c9.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"\n\r \v\b\f\\'\"\x00";
"\n\r \v\b\f\\'\"\0";
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/tests/test262/4fee4ac53bdfd7f7.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"Hello\x00World";
"Hello\0World";
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/tests/test262/7c03e5eb6a9f6f1a.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function a() {
"use strict";
"\x00";
"\0";
}
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/tests/test262/84f901eb37273117.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
("\x00");
("\0");
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/tests/test262/c7e5fba8bf3854cd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function() {
"use strict";
"\x00";
"\0";
}());
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/tests/test262/d81d71f4121e3193.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
("\x00");
("\0");
Original file line number Diff line number Diff line change
Expand Up @@ -2097,7 +2097,7 @@
}
})
}) : t.push({
string: s.replace(i, "\x00"),
string: s.replace(i, "\0"),
attributes: n.attributes
}), o = a + u.length;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var x = {
var r = {
"\0": "foo",
get "\0" () {
return "bar";
Expand All @@ -10,12 +10,12 @@ var x = {
return "foobar";
}
};
class r {
class e {
get "\0"() {
return "bar";
}
set "\0"(x) {
save(x);
set "\0"(r) {
save(r);
}
*"\0"() {
return "foobar";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var x = {
var r = {
"\0\x01": "foo",
get "\0\x01" () {
return "bar";
Expand All @@ -10,12 +10,12 @@ var x = {
return "foobar";
}
};
class r {
class e {
get "\0\x01"() {
return "bar";
}
set "\0\x01"(x) {
save(x);
set "\0\x01"(r) {
save(r);
}
*"\0\x01"() {
return "foobar";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
var x = "\0\08?\0?Àÿ";
var a = " 080\x000À0";
var a = "\0\08?\0?Àÿ";
var r = " 080\x000À0";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"use strict";
console.log("\x001");
console.log("\01");

0 comments on commit 9f1ce3a

Please sign in to comment.