Skip to content

Commit

Permalink
feat(codegen): print real newline when \n is inside template litera…
Browse files Browse the repository at this point in the history
…ls (#8178)
  • Loading branch information
Boshen committed Dec 29, 2024
1 parent 75d5f17 commit ad146bb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
14 changes: 10 additions & 4 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,9 @@ impl<'a> Codegen<'a> {

fn print_quoted_utf16(&mut self, s: &str, allow_backtick: bool) {
let quote = if self.options.minify {
let mut single_cost: u32 = 0;
let mut double_cost: u32 = 0;
let mut backtick_cost: u32 = 0;
let mut single_cost: i32 = 0;
let mut double_cost: i32 = 0;
let mut backtick_cost: i32 = 0;
let mut bytes = s.as_bytes().iter().peekable();
while let Some(b) = bytes.next() {
match b {
Expand Down Expand Up @@ -642,7 +642,13 @@ impl<'a> Codegen<'a> {
'\u{8}' => self.print_str("\\b"), // \b
'\u{b}' => self.print_str("\\v"), // \v
'\u{c}' => self.print_str("\\f"), // \f
'\n' => self.print_str("\\n"),
'\n' => {
if quote == b'`' {
self.print_ascii_byte(b'\n');
} else {
self.print_str("\\n");
}
}
'\r' => self.print_str("\\r"),
'\x1B' => self.print_str("\\x1B"),
'\\' => self.print_str("\\\\"),
Expand Down
14 changes: 9 additions & 5 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ fn expr() {
test("delete 2e308", "delete (0, Infinity);\n");
test_minify("delete 2e308", "delete(1/0);");

test_minify(
r#";'eval("\'\\vstr\\ving\\v\'") === "\\vstr\\ving\\v"'"#,
r#";`eval("'\\vstr\\ving\\v'") === "\\vstr\\ving\\v"`;"#,
);

test_minify_same(r#"({"http://a\r\" \n<'b:b@c\r\nd/e?f":{}});"#);
}

Expand Down Expand Up @@ -438,3 +433,12 @@ fn getter_setter() {
test_minify("({ get [foo]() {} })", "({get[foo](){}});");
test_minify("({ set [foo]() {} })", "({set[foo](){}});");
}

#[test]
fn string() {
test_minify(
r#";'eval("\'\\vstr\\ving\\v\'") === "\\vstr\\ving\\v"'"#,
r#";`eval("'\\vstr\\ving\\v'") === "\\vstr\\ving\\v"`;"#,
);
test_minify(r#"foo("\n")"#, "foo(`\n`);");
}
24 changes: 12 additions & 12 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
| Oxc | ESBuild | Oxc | ESBuild |
Original | minified | minified | gzip | gzip | Fixture
-------------------------------------------------------------------------------------
72.14 kB | 23.74 kB | 23.70 kB | 8.61 kB | 8.54 kB | react.development.js
72.14 kB | 23.72 kB | 23.70 kB | 8.62 kB | 8.54 kB | react.development.js

173.90 kB | 60.16 kB | 59.82 kB | 19.48 kB | 19.33 kB | moment.js
173.90 kB | 60.15 kB | 59.82 kB | 19.50 kB | 19.33 kB | moment.js

287.63 kB | 90.57 kB | 90.07 kB | 32.18 kB | 31.95 kB | jquery.js
287.63 kB | 90.57 kB | 90.07 kB | 32.19 kB | 31.95 kB | jquery.js

342.15 kB | 118.59 kB | 118.14 kB | 44.53 kB | 44.37 kB | vue.js
342.15 kB | 118.54 kB | 118.14 kB | 44.56 kB | 44.37 kB | vue.js

544.10 kB | 72.02 kB | 72.48 kB | 26.18 kB | 26.20 kB | lodash.js
544.10 kB | 72.00 kB | 72.48 kB | 26.20 kB | 26.20 kB | lodash.js

555.77 kB | 273.89 kB | 270.13 kB | 91.18 kB | 90.80 kB | d3.js
555.77 kB | 273.88 kB | 270.13 kB | 91.19 kB | 90.80 kB | d3.js

1.01 MB | 461.04 kB | 458.89 kB | 126.89 kB | 126.71 kB | bundle.min.js
1.01 MB | 461.00 kB | 458.89 kB | 126.92 kB | 126.71 kB | bundle.min.js

1.25 MB | 656.62 kB | 646.76 kB | 164.13 kB | 163.73 kB | three.js
1.25 MB | 653.54 kB | 646.76 kB | 164.05 kB | 163.73 kB | three.js

2.14 MB | 727.99 kB | 724.14 kB | 180.35 kB | 181.07 kB | victory.js
2.14 MB | 727.91 kB | 724.14 kB | 180.39 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 332.24 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 332.27 kB | 331.56 kB | echarts.js

6.69 MB | 2.32 MB | 2.31 MB | 493.24 kB | 488.28 kB | antd.js
6.69 MB | 2.32 MB | 2.31 MB | 493.25 kB | 488.28 kB | antd.js

10.95 MB | 3.51 MB | 3.49 MB | 910.83 kB | 915.50 kB | typescript.js
10.95 MB | 3.51 MB | 3.49 MB | 910.90 kB | 915.50 kB | typescript.js

0 comments on commit ad146bb

Please sign in to comment.