Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(codegen): do not escape $ in strings unless using backtick as q…
…uote (#10103)

When printing `"${"`, it's unnecessary to escape the `$` unless the quote character is a backtick.
  • Loading branch information
overlookmotel committed Mar 29, 2025
commit 9b6e344da499b54462b4cfb9016e22c7521e09a0
2 changes: 1 addition & 1 deletion crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ impl<'a> Codegen<'a> {
self.print_ascii_byte(b'`');
}
'$' => {
if chars.peek() == Some(&'{') {
if quote == Quote::Backtick && chars.peek() == Some(&'{') {
self.print_ascii_byte(b'\\');
}
self.print_ascii_byte(b'$');
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,13 @@ fn getter_setter() {

#[test]
fn string() {
test("let x = \"${}\";", "let x = \"${}\";\n");
test_minify("let x = \"${}\";", "let x=\"${}\";");
test("let x = '\"\"${}';", "let x = \"\\\"\\\"${}\";\n");
test_minify("let x = '\"\"${}';", "let x='\"\"${}';");
test("let x = '\"\"\\'\\'${}';", "let x = \"\\\"\\\"''${}\";\n");
test_minify("let x = '\"\"\\'\\'${}';", "let x=`\"\"''\\${}`;");

test_minify(
r#";'eval("\'\\vstr\\ving\\v\'") === "\\vstr\\ving\\v"'"#,
r#";`eval("'\\vstr\\ving\\v'") === "\\vstr\\ving\\v"`;"#,
Expand Down
4 changes: 2 additions & 2 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Original | minified | minified | gzip | gzip | Fixture

3.20 MB | 1.01 MB | 1.01 MB | 324.14 kB | 331.56 kB | echarts.js

6.69 MB | 2.28 MB | 2.31 MB | 466.10 kB | 488.28 kB | antd.js
6.69 MB | 2.28 MB | 2.31 MB | 466.11 kB | 488.28 kB | antd.js

10.95 MB | 3.35 MB | 3.49 MB | 860.95 kB | 915.50 kB | typescript.js
10.95 MB | 3.35 MB | 3.49 MB | 860.94 kB | 915.50 kB | typescript.js