Skip to content
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
42 changes: 41 additions & 1 deletion bindings/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bindings

import (
"fmt"
"strings"

"github.com/dop251/goja"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -733,8 +734,47 @@ func (b *Bindings) CommentGojaObject(comments []SyntheticComment, object *goja.O
return nil, err
}

node := object
// Group all comments that should be included into a JSDoc block.
jsDoc := make([]SyntheticComment, 0)
rest := make([]SyntheticComment, 0)

for _, c := range comments {
if !c.DoNotFormat && c.Leading && c.SingleLine {
jsDoc = append(jsDoc, c)
continue
}
rest = append(rest, c)
}

// JSDoc comments should be blocked together
node := object
if len(jsDoc) > 0 {
var jsDocComment strings.Builder
// JSDoc requires '/**' start and ' */' end. The default synthetic comment only places 1 '*'.
// So include the second '*', and start the comment line.
jsDocComment.WriteString("*\n *")
sep := ""
for _, cmt := range jsDoc {
jsDocComment.WriteString(sep)
jsDocComment.WriteString(cmt.Text)
sep = "\n *"
}
jsDocComment.WriteString("\n ")

res, err := commentF(goja.Undefined(),
node,
b.vm.ToValue(true),
b.vm.ToValue(false),
b.vm.ToValue(jsDocComment.String()),
b.vm.ToValue(true),
)
if err != nil {
return nil, xerrors.Errorf("call addSyntheticComment for JSDoc: %w", err)
}
node = res.ToObject(b.vm)
}

for _, c := range rest {
res, err := commentF(goja.Undefined(),
node,
b.vm.ToValue(c.Leading),
Expand Down
7 changes: 6 additions & 1 deletion bindings/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@ type SyntheticComment struct {
SingleLine bool
Text string
TrailingNewLine bool

// DoNotFormat indicates this comment should not be attempted to be reformatted.
// Guts will attempt to format comments into JSDoc style comments where possible.
DoNotFormat bool
}

type SupportComments struct {
comments []SyntheticComment
}

// LeadingComment is a helper function for the most common type of comment.
func (s *SupportComments) LeadingComment(text string) {
s.AppendComment(SyntheticComment{
Leading: true,
SingleLine: true,
// All go comments are `// ` prefixed, so add a space.
Text: " " + text,
TrailingNewLine: false,
DoNotFormat: true,
})
}

Expand Down Expand Up @@ -66,5 +70,6 @@ func (s Source) SourceComment() (SyntheticComment, bool) {
SingleLine: true,
Text: fmt.Sprintf(" From %s", s.File),
TrailingNewLine: false,
DoNotFormat: true,
}, s.File != ""
}
21 changes: 21 additions & 0 deletions config/mutations.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,24 @@ func isGoEnum(n bindings.Node) (*bindings.Alias, *bindings.UnionType, bool) {

return al, union, true
}

// NoJSDocTransform prevents `guts` from reformatting Golang comments to JSDoc.
// JSDoc comments use `/** */` style multi-line comments.
func NoJSDocTransform(ts *guts.Typescript) {
ts.ForEach(func(key string, node bindings.Node) {
walk.Walk(&noJSDocTransformWalker{}, node)
})
}

type noJSDocTransformWalker struct{}

func (v *noJSDocTransformWalker) Visit(node bindings.Node) walk.Visitor {
if commentedNode, ok := node.(bindings.Commentable); ok {
comments := commentedNode.Comments()
for i := range comments {
comments[i].DoNotFormat = true
}
}

return v
}
2 changes: 2 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func TestGeneration(t *testing.T) {
mutations = append(mutations, config.InterfaceToType)
case "BiomeLintIgnoreAnyTypeParameters":
mutations = append(mutations, config.BiomeLintIgnoreAnyTypeParameters)
case "NoJSDocTransform":
mutations = append(mutations, config.NoJSDocTransform)
default:
t.Fatal("unknown mutation, add it to the list:", m)
}
Expand Down
26 changes: 18 additions & 8 deletions testdata/comments/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@ export interface BlockComment {
}

// From comments/comments.go
// CommentedStructure is a struct with a comment.
//
// It actually has 2 comments?!
// TODO: Maybe add a third comment!
/**
* CommentedStructure is a struct with a comment.
*
* It actually has 2 comments?!
* TODO: Maybe add a third comment!
*/
export interface CommentedStructure {
readonly Inline: string; // Field comment
// Leading comment
/**
* Leading comment
*/
readonly Leading: string;
readonly Trailing: string;
// Leading comment
/**
* Leading comment
*/
readonly All: string; // Inline comment
// Another leading comment
/**
* Another leading comment
*/
readonly Block: string;
/* Multi
Line
Expand All @@ -29,7 +37,9 @@ export interface CommentedStructure {
}

// From comments/comments.go
// Constant is just a value
/**
* Constant is just a value
*/
export const Constant = "value"; // An inline note


Expand Down
4 changes: 3 additions & 1 deletion testdata/enums/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export enum EnumInt {
}

// From enums/enums.go
// EnumSliceType is a slice of string-based enums
/**
* EnumSliceType is a slice of string-based enums
*/
export type EnumSliceType = readonly EnumString[];

// From enums/enums.go
Expand Down
8 changes: 6 additions & 2 deletions testdata/generics/generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export interface Complex<C extends Comparable, S extends Single, T extends Custo
export type Custom = string | boolean | number | number | string[] | (number | null);

// From codersdk/generics.go
// Dynamic has some dynamic fields.
/**
* Dynamic has some dynamic fields.
*/
export interface Dynamic<A extends any, S extends Single> {
readonly dynamic: Fields<boolean, A, string, S>;
readonly comparable: boolean | null;
Expand All @@ -49,7 +51,9 @@ export interface FieldsDiffOrder<A extends any, C extends Comparable, S extends
export type Single = string;

// From codersdk/generics.go
// Static has all generic fields defined in the field
/**
* Static has all generic fields defined in the field
*/
export interface Static {
readonly static: Fields<string, number, number, string>;
}
Expand Down
6 changes: 4 additions & 2 deletions testdata/inheritance/inheritance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ export interface FooBarPtr extends Bar, GenBar<string> {
}

// From codersdk/inheritance.go
// FooBuzz has a json tag for the embedded
// See: https://go.dev/play/p/-p6QYmY8mtR
/**
* FooBuzz has a json tag for the embedded
* See: https://go.dev/play/p/-p6QYmY8mtR
*/
export interface FooBuzz {
readonly foo: Buzz; // Json tag changes the inheritance
readonly bazz: string;
Expand Down
1 change: 1 addition & 0 deletions testdata/nojsdoc/mutations
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NoJSDocTransform
7 changes: 7 additions & 0 deletions testdata/nojsdoc/nojsdoc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nojsdoc

// Commented is a struct that does nothing
type Commented struct {
// Nothing is not very helpful
Nothing string
}
8 changes: 8 additions & 0 deletions testdata/nojsdoc/nojsdoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Code generated by 'guts'. DO NOT EDIT.

// From nojsdoc/nojsdoc.go
// Commented is a struct that does nothing
interface Commented {
// Nothing is not very helpful
Nothing: string;
}
4 changes: 3 additions & 1 deletion testdata/nonids/nonids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

// From nonids/nonids.go
export interface Foo {
// Hyphen is an odd case, but this field is not ignored
/**
* Hyphen is an odd case, but this field is not ignored
*/
readonly "-": string;
readonly "hyphenated-string": string;
readonly "1numbered": number;
Expand Down
4 changes: 3 additions & 1 deletion testdata/union/union.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Code generated by 'guts'. DO NOT EDIT.

// From union/union.go
// Repeated constraints are redundant
/**
* Repeated constraints are redundant
*/
export interface Repeated<T extends string | number> {
readonly Value: T;
}
Expand Down