Skip to content

Commit 071026d

Browse files
committed
Allow ignoring types for autogen
1 parent 710b427 commit 071026d

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

codersdk/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func New(serverURL *url.URL) *Client {
2626
}
2727

2828
// Client is an HTTP caller for methods to the Coder API.
29+
// @typescript-ignore Client
2930
type Client struct {
3031
HTTPClient *http.Client
3132
SessionToken string

scripts/apitypings/main.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"reflect"
10+
"regexp"
1011
"strings"
1112

1213
"cdr.dev/slog/sloggers/sloghuman"
@@ -43,13 +44,26 @@ type TypescriptTypes struct {
4344
// String just combines all the codeblocks. I store them in a map for unit testing purposes
4445
func (t TypescriptTypes) String() string {
4546
var s strings.Builder
46-
for _, v := range t.Types {
47+
sortedTypes := make([]string, 0, len(t.Types))
48+
sortedEnums := make([]string, 0, len(t.Types))
49+
50+
for k := range t.Types {
51+
sortedTypes = append(sortedTypes, k)
52+
}
53+
for k := range t.Enums {
54+
sortedEnums = append(sortedEnums, k)
55+
}
56+
57+
for _, k := range sortedTypes {
58+
v := t.Types[k]
4759
s.WriteString(v)
4860
s.WriteRune('\n')
4961
}
5062

51-
for _, v := range t.Enums {
63+
for _, k := range sortedEnums {
64+
v := t.Enums[k]
5265
s.WriteString(v)
66+
s.WriteRune('\n')
5367
}
5468
return s.String()
5569
}
@@ -104,22 +118,38 @@ func (g *Generator) parsePackage(ctx context.Context, patterns ...string) error
104118
return nil
105119
}
106120

107-
type Generated struct {
108-
}
109-
110121
// generateAll will generate for all types found in the pkg
111122
func (g *Generator) generateAll() (*TypescriptTypes, error) {
112123
structs := make(map[string]string)
113124
enums := make(map[string]types.Object)
114125
constants := make(map[string][]*types.Const)
115126

127+
ignoredTypes := make(map[string]struct{})
128+
ignoreRegex := regexp.MustCompile("@typescript-ignore:(?P<ignored_types>)")
129+
for _, file := range g.pkg.Syntax {
130+
for _, comment := range file.Comments {
131+
matches := ignoreRegex.FindStringSubmatch(comment.Text())
132+
ignored := ignoreRegex.SubexpIndex("ignored_types")
133+
if len(matches) >= ignored && matches[ignored] != "" {
134+
arr := strings.Split(matches[ignored], ",")
135+
for _, s := range arr {
136+
ignoredTypes[strings.TrimSpace(s)] = struct{}{}
137+
}
138+
}
139+
}
140+
}
141+
116142
for _, n := range g.pkg.Types.Scope().Names() {
117143
obj := g.pkg.Types.Scope().Lookup(n)
118144
if obj == nil || obj.Type() == nil {
119145
// This would be weird, but it is if the package does not have the type def.
120146
continue
121147
}
122148

149+
if _, ok := ignoredTypes[obj.Name()]; ok {
150+
continue
151+
}
152+
123153
switch obj.(type) {
124154
// All named types are type declarations
125155
case *types.TypeName:

0 commit comments

Comments
 (0)