Compare commits

...

1 Commits

Author SHA1 Message Date
Meghan Denny
76222220b7 zig: make Location.{line,column} use bun.Ordinal 2025-10-10 23:15:41 -07:00
7 changed files with 38 additions and 43 deletions

View File

@@ -180,14 +180,14 @@ fn writeLogMsg(msg: *const bun.logger.Msg, w: Writer) !void {
fn writeLogData(data: bun.logger.Data, w: Writer) !void {
try writeString32(data.text, w);
if (data.location) |loc| {
if (loc.line < 0) {
if (loc.line.oneBased() < 0) {
try w.writeInt(u32, 0, .little);
return;
}
assert(loc.column >= 0); // zero based and not negative
assert(loc.column.zeroBased() >= 0); // zero based and not negative
try w.writeInt(i32, @intCast(loc.line), .little);
try w.writeInt(u32, @intCast(loc.column), .little);
try w.writeInt(i32, @intCast(loc.line.oneBased()), .little);
try w.writeInt(u32, @intCast(loc.column.zeroBased()), .little);
try w.writeInt(u32, @intCast(loc.length), .little);
// TODO: syntax highlighted line text + give more context lines

View File

@@ -147,7 +147,7 @@ pub const BuildMessage = struct {
// https://github.com/oven-sh/bun/issues/2375#issuecomment-2121530202
pub fn getColumn(this: *BuildMessage, _: *jsc.JSGlobalObject) jsc.JSValue {
if (this.msg.data.location) |location| {
return jsc.JSValue.jsNumber(@max(location.column - 1, 0));
return jsc.JSValue.jsNumber(@max(location.column.zeroBased() - 1, 0));
}
return jsc.JSValue.jsNumber(@as(i32, 0));
@@ -155,7 +155,7 @@ pub const BuildMessage = struct {
pub fn getLine(this: *BuildMessage, _: *jsc.JSGlobalObject) jsc.JSValue {
if (this.msg.data.location) |location| {
return jsc.JSValue.jsNumber(@max(location.line - 1, 0));
return jsc.JSValue.jsNumber(@max(location.line.oneBased() - 1, 0));
}
return jsc.JSValue.jsNumber(@as(i32, 0));

View File

@@ -56,7 +56,7 @@ pub const ResolveMessage = struct {
// https://github.com/oven-sh/bun/issues/2375#issuecomment-2121530202
pub fn getColumn(this: *ResolveMessage, _: *jsc.JSGlobalObject) jsc.JSValue {
if (this.msg.data.location) |location| {
return jsc.JSValue.jsNumber(@max(location.column - 1, 0));
return jsc.JSValue.jsNumber(@max(location.column.zeroBased() - 1, 0));
}
return jsc.JSValue.jsNumber(@as(i32, 0));
@@ -64,7 +64,7 @@ pub const ResolveMessage = struct {
pub fn getLine(this: *ResolveMessage, _: *jsc.JSGlobalObject) jsc.JSValue {
if (this.msg.data.location) |location| {
return jsc.JSValue.jsNumber(@max(location.line - 1, 0));
return jsc.JSValue.jsNumber(@max(location.line.oneBased() - 1, 0));
}
return jsc.JSValue.jsNumber(@as(i32, 0));

View File

@@ -232,8 +232,8 @@ pub const SourceLocation = struct {
pub fn toLoggerLocation(this: SourceLocation, file: []const u8) bun.logger.Location {
return bun.logger.Location{
.file = file,
.line = @intCast(this.line),
.column = @intCast(this.column),
.line = .fromOneBased(@intCast(this.line)),
.column = .fromZeroBased(@intCast(this.column)),
};
}

View File

@@ -178,8 +178,8 @@ pub const ErrorLocation = struct {
return logger.Location{
.file = source.path.text,
.namespace = source.path.namespace,
.line = @intCast(this.line + 1),
.column = @intCast(this.column),
.line = .fromZeroBased(@intCast(this.line)),
.column = .fromOneBased(@intCast(this.column)),
.line_text = if (bun.strings.getLinesInText(source.contents, this.line, 1)) |lines| try allocator.dupe(u8, lines.buffer[0]) else null,
};
}

View File

@@ -73,14 +73,9 @@ pub const Loc = struct {
pub const Location = struct {
file: string,
namespace: string = "file",
/// 1-based line number.
/// Line <= 0 means there is no line and column information.
// TODO: move to `bun.Ordinal`
line: i32,
// TODO: figure out how this is interpreted, convert to `bun.Ordinal`
// original docs: 0-based, in bytes.
// but there is a place where this is emitted in output, implying one based character offset
column: i32,
line: bun.Ordinal,
column: bun.Ordinal,
/// Number of bytes this location should highlight.
/// 0 to just point at a single character
length: usize = 0,
@@ -137,8 +132,8 @@ pub const Location = struct {
return api.Location{
.file = this.file,
.namespace = this.namespace,
.line = this.line,
.column = this.column,
.line = this.line.oneBased(),
.column = this.column.zeroBased(),
.line_text = this.line_text orelse "",
.suggestion = this.suggestion orelse "",
.offset = @as(u32, @truncate(this.offset)),
@@ -152,8 +147,8 @@ pub const Location = struct {
return Location{
.file = file,
.namespace = namespace,
.line = line,
.column = column,
.line = .fromOneBased(line),
.column = .fromZeroBased(column),
.length = length,
.line_text = line_text,
.suggestion = suggestion,
@@ -167,8 +162,8 @@ pub const Location = struct {
return Location{
.file = source.path.text,
.namespace = source.path.namespace,
.line = -1,
.column = -1,
.line = .fromOneBased(-1),
.column = .fromZeroBased(-1),
.length = 0,
.line_text = "",
.offset = 0,
@@ -183,8 +178,8 @@ pub const Location = struct {
return Location{
.file = source.path.text,
.namespace = source.path.namespace,
.line = usize2Loc(data.line_count).start,
.column = usize2Loc(data.column_count).start,
.line = .fromOneBased(usize2Loc(data.line_count).start),
.column = .fromZeroBased(usize2Loc(data.column_count).start),
.length = if (r.len > -1) @as(u32, @intCast(r.len)) else 1,
.line_text = std.mem.trimLeft(u8, full_line, "\n\r"),
.offset = @as(usize, @intCast(@max(r.loc.start, 0))),
@@ -279,10 +274,10 @@ pub const Data = struct {
if (location.line_text) |line_text_| {
const line_text_right_trimmed = std.mem.trimRight(u8, line_text_, " \r\n\t");
const line_text = std.mem.trimLeft(u8, line_text_right_trimmed, "\n\r");
if (location.column > 0 and line_text.len > 0) {
var line_offset_for_second_line: usize = @intCast(location.column - 1);
if (location.column.zeroBased() > 0 and line_text.len > 0) {
var line_offset_for_second_line: usize = @intCast(location.column.zeroBased() - 1);
if (location.line > -1) {
if (location.line.oneBased() > -1) {
switch (kind == .err or kind == .warn) {
inline else => |bold| try to.print(
// bold the line number for error but dim for the attached note
@@ -291,12 +286,12 @@ pub const Data = struct {
else
comptime Output.prettyFmt("<d>{d} | <r>", enable_ansi_colors),
.{
location.line,
location.line.oneBased(),
},
),
}
line_offset_for_second_line += std.fmt.count("{d} | ", .{location.line});
line_offset_for_second_line += std.fmt.count("{d} | ", .{location.line.oneBased()});
}
try to.print("{}\n", .{bun.fmt.fmtJavaScript(line_text, .{
@@ -344,14 +339,14 @@ pub const Data = struct {
location.file,
});
if (location.line > 0 and location.column > -1) {
if (location.line.oneBased() > 0 and location.column.zeroBased() > -1) {
try to.print(comptime Output.prettyFmt("<d>:<r><yellow>{d}<r><d>:<r><yellow>{d}<r>", enable_ansi_colors), .{
location.line,
location.column,
location.line.oneBased(),
location.column.zeroBased(),
});
} else if (location.line > -1) {
} else if (location.line.oneBased() > -1) {
try to.print(comptime Output.prettyFmt("<d>:<r><yellow>{d}<r>", enable_ansi_colors), .{
location.line,
location.line.oneBased(),
});
}
@@ -414,8 +409,8 @@ pub const Msg = struct {
.text = try zig_exception_holder.zigException().message.toOwnedSlice(allocator),
.location = Location{
.file = file,
.line = 0,
.column = 0,
.line = .fromOneBased(0),
.column = .fromZeroBased(0),
},
},
};
@@ -1064,8 +1059,8 @@ pub const Log = struct {
.text = try allocPrint(allocator, text, args),
.location = Location{
.file = filepath,
.line = @intCast(line),
.column = @intCast(col),
.line = .fromOneBased(@intCast(line)),
.column = .fromZeroBased(@intCast(col)),
},
},
log.clone_line_text,

View File

@@ -849,8 +849,8 @@ pub const ParseResult = union(enum) {
.file = path,
.offset = this.loc.toUsize(),
// TODO: populate correct line and column information
.line = -1,
.column = -1,
.line = .fromOneBased(-1),
.column = .fromZeroBased(-1),
},
.text = this.msg,
};