@@ -39,9 +39,10 @@ type GoParser struct {
3939 // This needs to be a producer function, as the AST is mutated directly,
4040 // and we cannot have shared references.
4141 // Eg: "time.Time" -> "string"
42- typeOverrides map [string ]TypeOverride
43- config * packages.Config
44- fileSet * token.FileSet
42+ typeOverrides map [string ]TypeOverride
43+ config * packages.Config
44+ fileSet * token.FileSet
45+ preserveComments bool
4546}
4647
4748// NewGolangParser returns a new GoParser object.
@@ -80,6 +81,14 @@ func NewGolangParser() (*GoParser, error) {
8081 }, nil
8182}
8283
84+ // PreserveComments will attempt to preserve any comments associated with
85+ // the golang types. This feature is still a work in progress, and may not
86+ // preserve all comments or match all expectations.
87+ func (p * GoParser ) PreserveComments () * GoParser {
88+ p .preserveComments = true
89+ return p
90+ }
91+
8392// IncludeCustomDeclaration is an advanced form of IncludeCustom.
8493func (p * GoParser ) IncludeCustomDeclaration (mappings map [string ]TypeOverride ) {
8594 for k , v := range mappings {
@@ -115,6 +124,7 @@ func (p *GoParser) IncludeCustom(mappings map[GolangType]GolangType) error {
115124 return exp
116125 }
117126 }
127+
118128 return nil
119129}
120130
@@ -174,9 +184,10 @@ func (p *GoParser) include(directory string, prefix string, reference bool) erro
174184// The returned typescript object can be mutated before serializing.
175185func (p * GoParser ) ToTypescript () (* Typescript , error ) {
176186 typescript := & Typescript {
177- typescriptNodes : make (map [string ]* typescriptNode ),
178- parsed : p ,
179- skip : p .Skips ,
187+ typescriptNodes : make (map [string ]* typescriptNode ),
188+ parsed : p ,
189+ skip : p .Skips ,
190+ preserveComments : p .preserveComments ,
180191 }
181192
182193 // Parse all go types to the typescript AST
@@ -209,9 +220,10 @@ type Typescript struct {
209220 // parsed go code. All names should be unique. If non-unique names exist, that
210221 // means packages contain the same named types.
211222 // TODO: the key "string" should be replaced with "Identifier"
212- typescriptNodes map [string ]* typescriptNode
213- parsed * GoParser
214- skip map [string ]struct {}
223+ typescriptNodes map [string ]* typescriptNode
224+ parsed * GoParser
225+ skip map [string ]struct {}
226+ preserveComments bool
215227 // Do not allow calling serialize more than once.
216228 // The call affects the state.
217229 serialized bool
@@ -437,6 +449,11 @@ func (ts *Typescript) parse(obj types.Object) error {
437449 if err != nil {
438450 return xerrors .Errorf ("generate %q: %w" , objectIdentifier .Ref (), err )
439451 }
452+
453+ if ts .preserveComments {
454+ cmts := ts .parsed .CommentForObject (obj )
455+ node .AppendComments (cmts )
456+ }
440457 return ts .setNode (objectIdentifier .Ref (), typescriptNode {
441458 Node : node ,
442459 })
@@ -471,14 +488,21 @@ func (ts *Typescript) parse(obj types.Object) error {
471488 return xerrors .Errorf ("(map) generate %q: %w" , objectIdentifier .Ref (), err )
472489 }
473490
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+
474504 return ts .setNode (objectIdentifier .Ref (), typescriptNode {
475- Node : & bindings.Alias {
476- Name : objectIdentifier ,
477- Modifiers : []bindings.Modifier {},
478- Type : ty .Value ,
479- Parameters : ty .TypeParameters ,
480- Source : ts .location (obj ),
481- },
505+ Node : aliasNode ,
482506 })
483507 case * types.Interface :
484508 // Interfaces are used as generics. Non-generic interfaces are
@@ -559,6 +583,12 @@ func (ts *Typescript) parse(obj types.Object) error {
559583 if err != nil {
560584 return xerrors .Errorf ("basic const %q: %w" , objectIdentifier .Ref (), err )
561585 }
586+
587+ if ts .preserveComments {
588+ cmts := ts .parsed .CommentForObject (obj )
589+ cnst .AppendComments (cmts )
590+ }
591+
562592 return ts .setNode (objectIdentifier .Ref (), typescriptNode {
563593 Node : cnst ,
564594 })
@@ -791,6 +821,10 @@ func (ts *Typescript) buildStruct(obj types.Object, st *types.Struct) (*bindings
791821 }
792822 }
793823
824+ if ts .preserveComments {
825+ cmts := ts .parsed .CommentForObject (field )
826+ tsField .AppendComments (cmts )
827+ }
794828 tsi .Fields = append (tsi .Fields , tsField )
795829 }
796830
0 commit comments