generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
analyzer.go
37 lines (32 loc) · 1.19 KB
/
analyzer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package typealias
import (
"go/ast"
"go/types"
"github.com/alecthomas/types/optional"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/backend/schema/strcase"
"github.com/TBD54566975/ftl/go-runtime/schema/common"
"github.com/TBD54566975/golang-tools/go/analysis"
)
// Extractor extracts type aliases to the module schema.
var Extractor = common.NewDeclExtractor[*schema.TypeAlias, *ast.TypeSpec]("typealias", Extract)
func Extract(pass *analysis.Pass, node *ast.TypeSpec, obj types.Object) optional.Option[*schema.TypeAlias] {
schType, ok := common.ExtractTypeForNode(pass, obj, node, nil).Get()
if !ok {
return optional.None[*schema.TypeAlias]()
}
// type aliases must have an underlying type, and the type cannot be a reference to the alias itself.
if common.IsSelfReference(pass, obj, schType) {
return optional.None[*schema.TypeAlias]()
}
alias := &schema.TypeAlias{
Pos: common.GoPosToSchemaPos(pass.Fset, node.Pos()),
Name: strcase.ToUpperCamel(obj.Name()),
Type: schType,
}
if md, ok := common.GetFactForObject[*common.ExtractedMetadata](pass, obj).Get(); ok {
alias.Comments = md.Comments
alias.Export = md.IsExported
}
return optional.Some(alias)
}