diff options
author | 2023-07-21 01:35:06 -0700 | |
---|---|---|
committer | 2023-07-21 01:35:06 -0700 | |
commit | aa1ad7f009173ae01e41f005c563b902f53906c3 (patch) | |
tree | 90bde80bcbdf99055fdd705d4832eb9ad56c62b8 | |
parent | 21bb3b2bdd39297395c59e8fe49ae73c8dfc2bb7 (diff) | |
download | bun-aa1ad7f009173ae01e41f005c563b902f53906c3.tar.gz bun-aa1ad7f009173ae01e41f005c563b902f53906c3.tar.zst bun-aa1ad7f009173ae01e41f005c563b902f53906c3.zip |
string escape edgecase (#3717)bun-v0.7.0
* fix edgecase when joining rope strings with backtick
* bonus bugfix in ts decorator
* Update transpiler.test.js
* Fix test
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
-rw-r--r-- | src/js_parser.zig | 5 | ||||
-rw-r--r-- | src/js_printer.zig | 13 | ||||
-rw-r--r-- | test/bundler/bundler_string.test.ts | 8 | ||||
-rw-r--r-- | test/transpiler/transpiler.test.js | 4 |
4 files changed, 22 insertions, 8 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig index f5dda75f0..6390dfdba 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -213,6 +213,8 @@ fn foldStringAddition(lhs: Expr, rhs: Expr) ?Expr { rhs_clone.data.e_string, ); + orig.prefer_template = orig.prefer_template or rhs_clone.data.e_string.prefer_template; + return Expr.init(E.String, orig, lhs.loc); } }, @@ -19517,7 +19519,8 @@ fn NewParser_( .e_number => |k| p.newExpr(E.Number{ .value = k.value }, loc), .e_string => |k| p.newExpr(E.String{ .data = k.data }, loc), .e_index => |k| p.newExpr(E.Index{ .target = k.target, .index = k.index }, loc), - else => unreachable, + .e_private_identifier => |k| p.newExpr(E.PrivateIdentifier{ .ref = k.ref }, loc), + else => bun.unreachablePanic("Unexpected AST node type {any}", .{prop.key.?}), }; const descriptor_kind: f64 = if (!prop.flags.contains(.is_method)) 2 else 1; diff --git a/src/js_printer.zig b/src/js_printer.zig index 08a1cb5c1..1f920327c 100644 --- a/src/js_printer.zig +++ b/src/js_printer.zig @@ -2592,7 +2592,7 @@ fn NewPrinter( p.addSourceMapping(expr.loc); // If this was originally a template literal, print it as one as long as we're not minifying - if (e.prefer_template) { + if (e.prefer_template and !p.options.minify_syntax) { p.print("`"); p.printStringContent(e, '`'); p.print("`"); @@ -3065,6 +3065,17 @@ fn NewPrinter( while (i < len) { switch (utf8[i]) { '\\' => i += 2, + '$' => { + if (comptime c == '`') { + p.print(utf8[0..i]); + p.print("\\$"); + utf8 = utf8[i + 1 ..]; + len = utf8.len; + i = 0; + } else { + i += 1; + } + }, c => { p.print(utf8[0..i]); p.print("\\" ++ &[_]u8{c}); diff --git a/test/bundler/bundler_string.test.ts b/test/bundler/bundler_string.test.ts index 3c2d7802c..45faaee3e 100644 --- a/test/bundler/bundler_string.test.ts +++ b/test/bundler/bundler_string.test.ts @@ -14,8 +14,8 @@ const templateStringTests: Record<string, TemplateStringTest> = { // note for writing tests: .print is .trim()'ed due to how run.stdout works Empty: { expr: '""', captureRaw: '""' }, NullByte: { expr: '"hello\0"', captureRaw: '"hello\0"' }, - EmptyTemplate: { expr: "``", captureRaw: "``" }, - ConstantTemplate: { expr: "`asdf`", captureRaw: "`asdf`" }, + EmptyTemplate: { expr: "``", captureRaw: '""' }, + ConstantTemplate: { expr: "`asdf`", captureRaw: '"asdf"' }, AddConstant: { expr: "`${7 + 6}`", capture: true }, AddConstant2: { expr: "`${7 + 6 + 96}`", capture: true }, AddConstant3: { expr: "`${0.1 + 0.2}`", print: true }, @@ -80,8 +80,8 @@ const templateStringTests: Record<string, TemplateStringTest> = { FoldNested4: { expr: "`a${`b`}c${`${`${`${'d'}`}`}`}e`", capture: true }, FoldNested5: { expr: "`\\$${`d`}`", print: true }, // could be captured FoldNested6: { expr: "`a\0${5}c\\${{$${`d`}e`", print: true }, - EscapedDollar: { expr: "`\\${'a'}`", captureRaw: "`\\${'a'}`" }, - EscapedDollar2: { expr: "`\\${'a'}\\${'b'}`", captureRaw: "`\\${'a'}\\${'b'}`" }, + EscapedDollar: { expr: "`\\${'a'}`", captureRaw: "\"${'a'}\"" }, + EscapedDollar2: { expr: "`\\${'a'}\\${'b'}`", captureRaw: "\"${'a'}${'b'}\"" }, }; describe("bundler", () => { diff --git a/test/transpiler/transpiler.test.js b/test/transpiler/transpiler.test.js index f83c00296..8c77dffe6 100644 --- a/test/transpiler/transpiler.test.js +++ b/test/transpiler/transpiler.test.js @@ -2809,7 +2809,7 @@ console.log(foo, array); expectPrinted("'string' + `template`", `"stringtemplate"`); - expectPrinted("`template` + 'string'", "`templatestring`"); + expectPrinted("`template` + 'string'", '"templatestring"'); // TODO: string template simplification // expectPrinted("'string' + `a${foo}b`", "`stringa${foo}b`"); @@ -3088,7 +3088,7 @@ console.log(foo, array); expectPrinted_('const x = `str` + "``";', "const x = `str\\`\\``"); expectPrinted_('const x = `` + "`";', "const x = `\\``"); expectPrinted_('const x = `` + "``";', "const x = `\\`\\``"); - expectPrinted_('const x = "``" + ``;', 'const x = "``"'); + expectPrinted_('const x = "``" + ``;', "const x = `\\`\\``"); }); }); |