-
Notifications
You must be signed in to change notification settings - Fork 8
/
import_mapper.go
73 lines (63 loc) · 2 KB
/
import_mapper.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package cuetsy
import (
"fmt"
"strings"
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
tsast "github.com/grafana/cuetsy/ts/ast"
)
// An ImportMapper takes a string containing a CUE import path (e.g.
// "github.com/grafana/cuetsy") and returns a string indicating the import path
// that should be used in the corresponding generated typescript, or an error if
// no mapping can be made.
//
// An empty string return indicates no TS import statements
// should be generated for that CUE import path.
type ImportMapper func(string) (string, error)
func nilImportMapper(path string) (string, error) {
return "", fmt.Errorf("a corresponding typescript import is not available for %q", path)
}
// IgnoreImportMapper ignores all import paths cuetsy encounters, resulting in no
// import statements in generated TS output.
func IgnoreImportMapper(path string) (string, error) {
return "", nil
}
// mapImports converts CUE import statements, represented in their AST form,
// to the corresponding TS import, if the CUE import is allowed.
//
// Some CUE imports are allowed but have no corresponding TS import - the CUE
// types from that package are expected to be inlined.
func mapImports(raw cue.Value, fn ImportMapper) ([]tsast.ImportSpec, error) {
bi := findInstance(raw)
if bi == nil {
return nil, nil
}
var ims []*ast.ImportSpec
for _, src := range bi.Files {
ims = append(ims, src.Imports...)
}
var specs []tsast.ImportSpec
for _, im := range ims {
pkg, err := fn(strings.Trim(im.Path.Value, "\""))
if err != nil || pkg == "" {
// Empty string mapping means skip it
return nil, err
}
tsim := tsast.ImportSpec{
From: tsast.Str{Value: pkg},
}
if im.Name != nil && im.Name.String() != "" {
tsim.AsName = im.Name.String()
} else {
sl := strings.Split(strings.Trim(im.Path.Value, "\""), "/")
final := sl[len(sl)-1]
if idx := strings.Index(final, ":"); idx != -1 {
tsim.AsName = final[idx:]
} else {
tsim.AsName = final
}
}
specs = append(specs, tsim)
}
return specs, nil
}