diff options
-rw-r--r-- | src/js_lexer.zig | 9 | ||||
-rw-r--r-- | src/js_printer.zig | 55 |
2 files changed, 61 insertions, 3 deletions
diff --git a/src/js_lexer.zig b/src/js_lexer.zig index fa161f2cd..ac575a062 100644 --- a/src/js_lexer.zig +++ b/src/js_lexer.zig @@ -279,7 +279,6 @@ pub const Lexer = struct { lexer.string_literal_slice = lexer.source.contents[lexer.start + 1 .. lexer.end - suffixLen]; lexer.string_literal_is_ascii = !needs_slow_path; lexer.string_literal_buffer.shrinkRetainingCapacity(0); - lexer.string_literal.len = lexer.string_literal_slice.len; if (needs_slow_path) { lexer.string_literal_buffer.ensureTotalCapacity(lexer.string_literal_slice.len) catch unreachable; var slice = lexer.string_literal_buffer.allocatedSlice(); @@ -1174,6 +1173,14 @@ pub const Lexer = struct { return lex; } + pub fn toEString(lexer: *LexerType) js_ast.E.String { + if (lexer.string_literal_is_ascii) { + return js_ast.E.String{ .utf8 = lexer.string_literal_slice }; + } else { + return js_ast.E.String{ .value = lexer.stringLiteralUTF16() }; + } + } + pub fn scanRegExp(lexer: *LexerType) !void { while (true) { switch (lexer.code_point) { diff --git a/src/js_printer.zig b/src/js_printer.zig index 243ce33da..0f5b41123 100644 --- a/src/js_printer.zig +++ b/src/js_printer.zig @@ -1717,7 +1717,41 @@ pub fn NewPrinter(comptime ascii_only: bool) type { }, .e_string => |key| { p.addSourceMapping(item.key.?.loc); - if (p.canPrintIdentifierUTF16(key.value)) { + if (key.isUTF8()) { + p.printSpaceBeforeIdentifier(); + p.printIdentifier(key.utf8); + + // Use a shorthand property if the names are the same + if (item.value) |val| { + switch (val.data) { + .e_identifier => |e| { + // TODO: is needing to check item.flags.was_shorthand a bug? + // esbuild doesn't have to do that... + // maybe it's a symptom of some other underlying issue + // or maybe, it's because i'm not lowering the same way that esbuild does. + if (strings.eql(key.utf8, p.renamer.nameForSymbol(e.ref))) { + if (item.initializer) |initial| { + p.printInitializer(initial); + } + return; + } + // if (strings) {} + }, + .e_import_identifier => |e| { + const ref = p.symbols.follow(e.ref); + if (p.symbols.get(ref)) |symbol| { + if (symbol.namespace_alias == null and strings.eql(key.utf8, p.renamer.nameForSymbol(e.ref))) { + if (item.initializer) |initial| { + p.printInitializer(initial); + } + return; + } + } + }, + else => {}, + } + } + } else if (p.canPrintIdentifierUTF16(key.value)) { p.printSpaceBeforeIdentifier(); p.printIdentifierUTF16(key.value) catch unreachable; @@ -1902,7 +1936,24 @@ pub fn NewPrinter(comptime ascii_only: bool) type { switch (property.key.data) { .e_string => |str| { - if (p.canPrintIdentifierUTF16(str.value)) { + if (str.isUTF8()) { + p.addSourceMapping(property.key.loc); + p.printSpaceBeforeIdentifier(); + p.printIdentifier(str.utf8); + + // Use a shorthand property if the names are the same + switch (property.value.data) { + .b_identifier => |id| { + if (str.eql(string, p.renamer.nameForSymbol(id.ref))) { + p.maybePrintDefaultBindingValue(property); + continue; + } + }, + else => { + p.printExpr(property.key, .lowest, ExprFlag.None()); + }, + } + } else if (p.canPrintIdentifierUTF16(str.value)) { p.addSourceMapping(property.key.loc); p.printSpaceBeforeIdentifier(); p.printIdentifierUTF16(str.value) catch unreachable; |