Skip to content

Commit 30b8729

Browse files
committed
chore: update all tests with preserved comments
1 parent ca0f56f commit 30b8729

File tree

8 files changed

+30
-47
lines changed

8 files changed

+30
-47
lines changed

bindings/comments.go

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ package bindings
22

33
import (
44
"fmt"
5-
"go/ast"
65
"go/token"
7-
"strings"
86
)
97

108
// Commentable indicates if the AST node supports adding comments.
119
// Any number of comments are supported and can be attached to a typescript AST node.
1210
type Commentable interface {
13-
Comment(comment SyntheticComment)
11+
AppendComment(comment SyntheticComment)
1412
Comments() []SyntheticComment
1513
}
1614

@@ -27,33 +25,9 @@ type SupportComments struct {
2725
comments []SyntheticComment
2826
}
2927

30-
func (s *SupportComments) ASTCommentGroup(grp *ast.CommentGroup) {
31-
if grp == nil {
32-
return
33-
}
34-
for _, cmt := range grp.List {
35-
s.ASTComment(cmt)
36-
}
37-
}
38-
39-
func (s *SupportComments) ASTComment(cmt *ast.Comment) {
40-
// TODO: Is there a better way to get just the text of the comment?
41-
text := cmt.Text
42-
text = strings.TrimPrefix(text, "//")
43-
text = strings.TrimPrefix(text, "/*")
44-
text = strings.TrimSuffix(text, "*/")
45-
46-
s.Comment(SyntheticComment{
47-
Leading: true,
48-
SingleLine: !strings.Contains(cmt.Text, "\n"),
49-
Text: text,
50-
TrailingNewLine: true,
51-
})
52-
}
53-
5428
// LeadingComment is a helper function for the most common type of comment.
5529
func (s *SupportComments) LeadingComment(text string) {
56-
s.Comment(SyntheticComment{
30+
s.AppendComment(SyntheticComment{
5731
Leading: true,
5832
SingleLine: true,
5933
// All go comments are `// ` prefixed, so add a space.
@@ -62,7 +36,11 @@ func (s *SupportComments) LeadingComment(text string) {
6236
})
6337
}
6438

65-
func (s *SupportComments) Comment(comment SyntheticComment) {
39+
func (s *SupportComments) AppendComments(comments []SyntheticComment) {
40+
s.comments = append(s.comments, comments...)
41+
}
42+
43+
func (s *SupportComments) AppendComment(comment SyntheticComment) {
6644
s.comments = append(s.comments, comment)
6745
}
6846

comments.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func CommentForObject(obj types.Object, pkg *packages.Package) []bindings.Synthe
3535
continue
3636
}
3737

38-
var found []*ast.CommentGroup
38+
var found []bindings.SyntheticComment
3939
ast.Inspect(f, func(n ast.Node) bool {
4040
// The decl nodes "cover" the types they comment on. So we can check quickly if
4141
// the node is relevant.
@@ -47,7 +47,7 @@ func CommentForObject(obj types.Object, pkg *packages.Package) []bindings.Synthe
4747
case *ast.FuncDecl:
4848
// Match function/method name token exactly.
4949
if nd.Name != nil && nd.Name.Pos() == pos {
50-
found = nd.Doc
50+
found = syntheticComments(true, nd.Doc)
5151
return false
5252
}
5353

@@ -63,9 +63,9 @@ func CommentForObject(obj types.Object, pkg *packages.Package) []bindings.Synthe
6363
for _, name := range spec.Names {
6464
if name.Pos() == pos {
6565
if spec.Doc != nil {
66-
found = spec.Doc
66+
found = syntheticComments(true, nd.Doc)
6767
} else {
68-
found = nd.Doc
68+
found = syntheticComments(true, spec.Doc)
6969
}
7070
return false
7171
}
@@ -76,9 +76,9 @@ func CommentForObject(obj types.Object, pkg *packages.Package) []bindings.Synthe
7676
if spec.Name != nil && spec.Name.Pos() == pos {
7777
// comment on the type itself
7878
if spec.Doc != nil {
79-
found = spec.Doc
79+
found = syntheticComments(true, spec.Doc)
8080
} else {
81-
found = nd.Doc
81+
found = syntheticComments(true, nd.Doc)
8282
}
8383
return false
8484
}
@@ -107,10 +107,7 @@ func CommentForObject(obj types.Object, pkg *packages.Package) []bindings.Synthe
107107
return true
108108
})
109109

110-
if len(found) > 0 {
111-
return found[0]
112-
}
113-
return nil
110+
return found
114111
}
115112

116113
return nil
@@ -130,15 +127,15 @@ func commentForFieldList(fl *ast.FieldList, pos token.Pos) []bindings.SyntheticC
130127
for _, nm := range fld.Names {
131128
if nm.Pos() == pos {
132129
cmts = append(cmts, syntheticComments(true, fld.Doc)...)
133-
cmts = append(cmts, syntheticComments(true, fld.Doc)...)
130+
cmts = append(cmts, syntheticComments(false, fld.Comment)...)
134131
return cmts
135132
}
136133
}
137134
} else {
138135
// Embedded field (anonymous): no Names; match on the Type span.
139136
if covers(fld.Type, pos) {
140137
cmts = append(cmts, syntheticComments(true, fld.Doc)...)
141-
cmts = append(cmts, syntheticComments(true, fld.Doc)...)
138+
cmts = append(cmts, syntheticComments(false, fld.Comment)...)
142139
return cmts
143140
}
144141
}
@@ -152,6 +149,9 @@ func covers(n ast.Node, p token.Pos) bool {
152149

153150
func syntheticComments(leading bool, grp *ast.CommentGroup) []bindings.SyntheticComment {
154151
cmts := []bindings.SyntheticComment{}
152+
if grp == nil {
153+
return cmts
154+
}
155155
for _, c := range grp.List {
156156
cmts = append(cmts, bindings.SyntheticComment{
157157
Leading: leading,

convert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ func (ts *Typescript) parse(obj types.Object) error {
449449

450450
if ts.preserveComments {
451451
cmts := ts.parsed.CommentForObject(obj)
452-
node.ASTCommentGroup(cmts)
452+
node.AppendComments(cmts)
453453
}
454454
return ts.setNode(objectIdentifier.Ref(), typescriptNode{
455455
Node: node,
@@ -807,7 +807,7 @@ func (ts *Typescript) buildStruct(obj types.Object, st *types.Struct) (*bindings
807807

808808
if ts.preserveComments {
809809
cmts := ts.parsed.CommentForObject(field)
810-
tsField.ASTCommentGroup(cmts)
810+
tsField.AppendComments(cmts)
811811
}
812812
tsi.Fields = append(tsi.Fields, tsField)
813813
}

testdata/comments/comments.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ export interface BlockComment {
1313
// It actually has 2 comments?!
1414
// TODO: Maybe add a third comment!
1515
export interface CommentedStructure {
16-
// Field comment
17-
readonly Inline: string;
16+
readonly Inline: string; // Field comment
1817
// Leading comment
1918
readonly Leading: string;
2019
readonly Trailing: string;
2120
// Leading comment
22-
readonly All: string;
21+
readonly All: string; // Inline comment
2322
// Another leading comment
2423
readonly Block: string;
2524
}

testdata/generics/generics.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface Complex<C extends Comparable, S extends Single, T extends Custo
2525
export type Custom = string | boolean | number | number | string[] | (number | null);
2626

2727
// From codersdk/generics.go
28+
// Dynamic has some dynamic fields.
2829
export interface Dynamic<A extends any, S extends Single> {
2930
readonly dynamic: Fields<boolean, A, string, S>;
3031
readonly comparable: boolean | null;
@@ -48,6 +49,7 @@ export interface FieldsDiffOrder<A extends any, C extends Comparable, S extends
4849
export type Single = string;
4950

5051
// From codersdk/generics.go
52+
// Static has all generic fields defined in the field
5153
export interface Static {
5254
readonly static: Fields<string, number, number, string>;
5355
}

testdata/inheritance/inheritance.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ export interface FooBarPtr extends Bar, GenBar<string> {
2121
}
2222

2323
// From codersdk/inheritance.go
24+
// FooBuzz has a json tag for the embedded
25+
// See: https://go.dev/play/p/-p6QYmY8mtR
2426
export interface FooBuzz {
25-
readonly foo: Buzz;
27+
readonly foo: Buzz; // Json tag changes the inheritance
2628
readonly bazz: string;
2729
}
2830

testdata/nonids/nonids.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// From nonids/nonids.go
44
export interface Foo {
5+
// Hyphen is an odd case, but this field is not ignored
56
readonly "-": string;
67
readonly "hyphenated-string": string;
78
readonly "1numbered": number;

testdata/union/union.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Code generated by 'guts'. DO NOT EDIT.
22

33
// From union/union.go
4+
// Repeated constraints are redundant
45
export interface Repeated<T extends string | number> {
56
readonly Value: T;
67
}

0 commit comments

Comments
 (0)