ToTypescript translates the Go types into the intermediate typescript AST The returned typescript object can be mutated before serializing.
()
| 183 | // ToTypescript translates the Go types into the intermediate typescript AST |
| 184 | // The returned typescript object can be mutated before serializing. |
| 185 | func (p *GoParser) ToTypescript() (*Typescript, error) { |
| 186 | typescript := &Typescript{ |
| 187 | typescriptNodes: make(map[string]*typescriptNode), |
| 188 | parsed: p, |
| 189 | skip: p.Skips, |
| 190 | preserveComments: p.preserveComments, |
| 191 | } |
| 192 | |
| 193 | // Parse all go types to the typescript AST |
| 194 | err := typescript.parseGolangIdentifiers() |
| 195 | if err != nil { |
| 196 | return nil, err |
| 197 | } |
| 198 | |
| 199 | // Apply any post-processing mutations to the nodes. |
| 200 | for key, node := range typescript.typescriptNodes { |
| 201 | newNode, err := node.applyMutations() |
| 202 | if err != nil { |
| 203 | return nil, fmt.Errorf("node %q: %w", key, err) |
| 204 | } |
| 205 | |
| 206 | // If the Node is nil, then it serves no purpose and can be |
| 207 | // removed from the typescriptNodes map. |
| 208 | if newNode.Node == nil { |
| 209 | delete(typescript.typescriptNodes, key) |
| 210 | } else { |
| 211 | typescript.typescriptNodes[key] = &newNode |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return typescript, nil |
| 216 | } |
| 217 | |
| 218 | type Typescript struct { |
| 219 | // typescriptNodes is a map of typescript nodes that are generated from the |