What other users are doing for documenting their goja bindings? #516
Replies: 1 comment
-
After experimenting with several different approaches (unfortunately calling gopls and godoc programmatically ended up very cumbersome and I abandoned it), I created a small helper package to semi-automate the process - tygoja (it relies on go/packages for loading and analyzing the packages). It's far from perfect, but for now this approach seems to work fine, at least for my use case. The final code intellisense result looks like this: In the above The general idea is that you map your exposed goja APIs against the generated package typings to inherit their documentation and fields, something like:
For custom constructors it is a slightly more complicated as it will depend on what arguments your constructor accept. vm.Set("Route", func(call goja.ConstructorCall) *goja.Object {
instance := &echo.Route{}
if data := call.Argument(0).Export(); data != nil {
if raw, err := json.Marshal(data); err == nil {
json.Unmarshal(raw, instance)
}
}
instanceValue := vm.ToValue(instance).(*goja.Object)
instanceValue.SetPrototype(call.This.Prototype())
return instanceValue
}) Is manually declared as: interface Route extends echo.Route{} // merge with the autogenerated type
declare class Route implements echo.Route {
constructor(data?: Partial<echo.Route>)
} For generating HTML docs, I plan to use typedoc by feeding it the generated My current docs integration code could be found in the I hope this helps. I'm still open for other suggestions if anyone has a better idea how to improve the above flow. |
Beta Was this translation helpful? Give feedback.
-
I'm currently experimenting with adding more extended goja support to one of my open source projects.
So far everything is great and works almost right out of the box without too much extra manual bindings.
My only problem is that I'm not sure how to document the Go->JS apis.
My general idea was to provide ambient TS declarations (aka.
.d.ts
) and allow users to import them (eg. manually with a/// <reference path="types.d.ts" />
comment, inside their tsconfig or eventually import it from the DefinitelyTyped repo).This in theory should work fine and would also allow easy generating html docs from the
.d.ts
file(s), but the problem is that it is a lot of manual work.Before investing too much time in an attempt to automate it, I wanted to check how other users are dealing with documenting their goja apis (especially when the binded go struct/func references/returns some 3rd-party package struct/func).
I checked k6 for an example and it has great html documentation and TS declarations, but at first glance they don't seem to be autogenerated.
Beta Was this translation helpful? Give feedback.
All reactions