Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle uppercase js names #141

Merged
merged 1 commit into from
Nov 24, 2023
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
31 changes: 21 additions & 10 deletions src/reflect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1234,18 +1234,29 @@ pub fn do(comptime types: anytype) Error![]Struct {
// Utils funcs
// -----------

fn jsName(comptime name: []const u8) []u8 {
comptime {
var js_name: [name.len]u8 = undefined;
js_name[0] = std.ascii.toLower(name[0]);
for (name, 0..) |char, i| {
if (i == 0) {
continue;
}
js_name[i] = char;
fn jsName(comptime name: []const u8) []const u8 {
std.debug.assert(@inComptime());

// uppercase names should not change
var is_upper = true;
for (name) |char| {
if (!std.ascii.isUpper(char)) {
is_upper = false;
break;
}
}
if (is_upper) return name;

// otherwhise lower first character
var js_name: [name.len]u8 = undefined;
js_name[0] = std.ascii.toLower(name[0]);
for (name, 0..) |char, i| {
if (i == 0) {
continue;
}
return &js_name;
js_name[i] = char;
}
return &js_name;
}

fn shortName(comptime T: type) []const u8 {
Expand Down
10 changes: 10 additions & 0 deletions src/tests/proto_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ const Person = struct {
return true;
}

pub fn get_UPPER(_: Person) bool {
return true;
}

pub fn _UPPERMETHOD(_: Person) bool {
return true;
}

pub fn set_allocator(_: *Person, alloc: std.mem.Allocator, _: bool) void {
Person.allocTest(alloc) catch unreachable;
}
Expand Down Expand Up @@ -239,6 +247,8 @@ pub fn exec(
var cases2 = [_]tests.Case{
.{ .src = "p.age === 40", .ex = "true" },
.{ .src = "p.allocator", .ex = "true" },
.{ .src = "p.UPPER", .ex = "true" },
.{ .src = "p.UPPERMETHOD()", .ex = "true" },
};
try tests.checkCases(js_env, &cases2);

Expand Down