|
7 | 7 | "os"
|
8 | 8 | "path/filepath"
|
9 | 9 | "reflect"
|
| 10 | + "regexp" |
10 | 11 | "strings"
|
11 | 12 |
|
12 | 13 | "cdr.dev/slog/sloggers/sloghuman"
|
@@ -43,13 +44,26 @@ type TypescriptTypes struct {
|
43 | 44 | // String just combines all the codeblocks. I store them in a map for unit testing purposes
|
44 | 45 | func (t TypescriptTypes) String() string {
|
45 | 46 | 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] |
47 | 59 | s.WriteString(v)
|
48 | 60 | s.WriteRune('\n')
|
49 | 61 | }
|
50 | 62 |
|
51 |
| - for _, v := range t.Enums { |
| 63 | + for _, k := range sortedEnums { |
| 64 | + v := t.Enums[k] |
52 | 65 | s.WriteString(v)
|
| 66 | + s.WriteRune('\n') |
53 | 67 | }
|
54 | 68 | return s.String()
|
55 | 69 | }
|
@@ -104,22 +118,38 @@ func (g *Generator) parsePackage(ctx context.Context, patterns ...string) error
|
104 | 118 | return nil
|
105 | 119 | }
|
106 | 120 |
|
107 |
| -type Generated struct { |
108 |
| -} |
109 |
| - |
110 | 121 | // generateAll will generate for all types found in the pkg
|
111 | 122 | func (g *Generator) generateAll() (*TypescriptTypes, error) {
|
112 | 123 | structs := make(map[string]string)
|
113 | 124 | enums := make(map[string]types.Object)
|
114 | 125 | constants := make(map[string][]*types.Const)
|
115 | 126 |
|
| 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 | + |
116 | 142 | for _, n := range g.pkg.Types.Scope().Names() {
|
117 | 143 | obj := g.pkg.Types.Scope().Lookup(n)
|
118 | 144 | if obj == nil || obj.Type() == nil {
|
119 | 145 | // This would be weird, but it is if the package does not have the type def.
|
120 | 146 | continue
|
121 | 147 | }
|
122 | 148 |
|
| 149 | + if _, ok := ignoredTypes[obj.Name()]; ok { |
| 150 | + continue |
| 151 | + } |
| 152 | + |
123 | 153 | switch obj.(type) {
|
124 | 154 | // All named types are type declarations
|
125 | 155 | case *types.TypeName:
|
|
0 commit comments