Skip to content

Commit

Permalink
Handle quotes in printer and not in bundler
Browse files Browse the repository at this point in the history
  • Loading branch information
DontBreakAlex committed Nov 26, 2023
1 parent 1d8b1cf commit 78377d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
8 changes: 1 addition & 7 deletions src/bundler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1435,18 +1435,12 @@ pub const Bundler = struct {
}, prop.key.?.loc),
.value = prop.value.?,
};
var maybe_quoted = name;
if (strings.containsChar(name, '-')) {
var quote_buf = MutableString.init(allocator, name.len + 2) catch return null;
quote_buf = js_printer.quoteForJSON(name, quote_buf, false) catch return null;
maybe_quoted = quote_buf.list.items;
}
export_clauses[i] = js_ast.ClauseItem{
.name = .{
.ref = ref,
.loc = prop.key.?.loc,
},
.alias = maybe_quoted,
.alias = name,
.alias_loc = prop.key.?.loc,
};
prop.value = js_ast.Expr.initIdentifier(ref, prop.value.?.loc);
Expand Down
2 changes: 1 addition & 1 deletion src/js_printer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ fn NewPrinter(
pub fn printClauseAlias(p: *Printer, alias: string) void {
std.debug.assert(alias.len > 0);

if (!strings.containsNonBmpCodePoint(alias)) {
if (!strings.containsNonBmpCodePointOrIsInvalidIdentifier(alias)) {
p.printSpaceBeforeIdentifier();
p.printIdentifier(alias);
} else {
Expand Down
20 changes: 19 additions & 1 deletion src/string_immutable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const CodePoint = bun.CodePoint;
const bun = @import("root").bun;
pub const joiner = @import("./string_joiner.zig");
const log = bun.Output.scoped(.STR, true);
const js_lexer = @import("./js_lexer.zig");

pub const Encoding = enum {
ascii,
Expand Down Expand Up @@ -216,7 +217,6 @@ pub fn fmtIdentifier(name: string) FormatValidIdentifier {
/// This will always allocate
pub const FormatValidIdentifier = struct {
name: string,
const js_lexer = @import("./js_lexer.zig");
pub fn format(self: FormatValidIdentifier, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
var iterator = strings.CodepointIterator.init(self.name);
var cursor = strings.CodepointIterator.Cursor{};
Expand Down Expand Up @@ -4367,6 +4367,24 @@ pub fn containsNonBmpCodePoint(text: string) bool {
return false;
}

pub fn containsNonBmpCodePointOrIsInvalidIdentifier(text: string) bool {
var iter = CodepointIterator.init(text);
var curs = CodepointIterator.Cursor{};

if (!iter.next(&curs)) return true;

if (curs.c > 0xFFFF or !js_lexer.isIdentifierStart(curs.c))
return true;

while (iter.next(&curs)) {
if (curs.c > 0xFFFF or !js_lexer.isIdentifierContinue(curs.c)) {
return true;
}
}

return false;
}

// this is std.mem.trim except it doesn't forcibly change the slice to be const
pub fn trim(slice: anytype, comptime values_to_strip: []const u8) @TypeOf(slice) {
var begin: usize = 0;
Expand Down

0 comments on commit 78377d5

Please sign in to comment.