Skip to content

Commit 9085662

Browse files
committed
cover some more type declarations
1 parent ab2c415 commit 9085662

File tree

7 files changed

+40
-21
lines changed

7 files changed

+40
-21
lines changed

bindings/declarations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type VariableStatement struct {
105105
Modifiers []Modifier
106106
Declarations *VariableDeclarationList
107107
Source
108+
SupportComments
108109
}
109110

110111
func (*VariableStatement) isNode() {}

comments.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,19 @@ func CommentForObject(obj types.Object, pkg *packages.Package) []bindings.Synthe
5858
if !covers(sp, pos) {
5959
continue
6060
}
61+
62+
// nd.Doc are the comments for the entire type/const/var block.
63+
if nd.Doc != nil {
64+
found = append(found, syntheticComments(true, nd.Doc)...)
65+
}
66+
6167
switch spec := sp.(type) {
6268
case *ast.ValueSpec:
6369
// const/var
6470
for _, name := range spec.Names {
6571
if name.Pos() == pos {
66-
if spec.Doc != nil {
67-
found = syntheticComments(true, nd.Doc)
68-
} else {
69-
found = syntheticComments(true, spec.Doc)
70-
}
72+
found = append(found, syntheticComments(true, spec.Doc)...)
73+
found = append(found, syntheticComments(false, spec.Comment)...)
7174
return false
7275
}
7376
}
@@ -76,11 +79,8 @@ func CommentForObject(obj types.Object, pkg *packages.Package) []bindings.Synthe
7679
// type declarations (struct/interface/alias)
7780
if spec.Name != nil && spec.Name.Pos() == pos {
7881
// comment on the type itself
79-
if spec.Doc != nil {
80-
found = syntheticComments(true, spec.Doc)
81-
} else {
82-
found = syntheticComments(true, nd.Doc)
83-
}
82+
found = append(found, syntheticComments(true, spec.Doc)...)
83+
found = append(found, syntheticComments(false, spec.Comment)...)
8484
return false
8585
}
8686

convert.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -488,14 +488,21 @@ func (ts *Typescript) parse(obj types.Object) error {
488488
return xerrors.Errorf("(map) generate %q: %w", objectIdentifier.Ref(), err)
489489
}
490490

491+
aliasNode := &bindings.Alias{
492+
Name: objectIdentifier,
493+
Modifiers: []bindings.Modifier{},
494+
Type: ty.Value,
495+
Parameters: ty.TypeParameters,
496+
Source: ts.location(obj),
497+
}
498+
499+
if ts.preserveComments {
500+
cmts := ts.parsed.CommentForObject(obj)
501+
aliasNode.AppendComments(cmts)
502+
}
503+
491504
return ts.setNode(objectIdentifier.Ref(), typescriptNode{
492-
Node: &bindings.Alias{
493-
Name: objectIdentifier,
494-
Modifiers: []bindings.Modifier{},
495-
Type: ty.Value,
496-
Parameters: ty.TypeParameters,
497-
Source: ts.location(obj),
498-
},
505+
Node: aliasNode,
499506
})
500507
case *types.Interface:
501508
// Interfaces are used as generics. Non-generic interfaces are
@@ -576,6 +583,12 @@ func (ts *Typescript) parse(obj types.Object) error {
576583
if err != nil {
577584
return xerrors.Errorf("basic const %q: %w", objectIdentifier.Ref(), err)
578585
}
586+
587+
if ts.preserveComments {
588+
cmts := ts.parsed.CommentForObject(obj)
589+
cnst.AppendComments(cmts)
590+
}
591+
579592
return ts.setNode(objectIdentifier.Ref(), typescriptNode{
580593
Node: cnst,
581594
})

testdata/comments/comments.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,4 @@ type BlockComment struct {
4040
}
4141

4242
// Constant is just a value
43-
// Constants are not yet supported for comments
44-
const Constant = "value" // Constant comment
43+
const Constant = "value" // An inline note

testdata/comments/comments.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export interface CommentedStructure {
2929
}
3030

3131
// From comments/comments.go
32-
export const Constant = "value";
32+
// Constant is just a value
33+
export const Constant = "value"; // An inline note
34+
3335

3436
// From comments/comments.go
3537
export interface InheritedCommentedStructure extends CommentedStructure {

testdata/enums/enums.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package enums
33
import "time"
44

55
type (
6-
EnumString string
6+
// EnumString is a string-based enum
7+
EnumString string
8+
// EnumSliceType is a slice of string-based enums
79
EnumSliceType []EnumString
810

911
EnumInt int
@@ -12,6 +14,7 @@ type (
1214
)
1315

1416
const (
17+
// EnumFoo is the "foo" value
1518
EnumFoo EnumString = "foo"
1619
EnumBar EnumString = "bar"
1720
EnumBaz EnumString = "baz"

testdata/enums/enums.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export enum EnumInt {
1414
}
1515

1616
// From enums/enums.go
17+
// EnumSliceType is a slice of string-based enums
1718
export type EnumSliceType = readonly EnumString[];
1819

1920
// From enums/enums.go

0 commit comments

Comments
 (0)