Skip to content

Commit

Permalink
Delete unused functions, fix some lints and unnecessary comments
Browse files Browse the repository at this point in the history
  • Loading branch information
spinillos committed Apr 11, 2023
1 parent 9ee4277 commit 5139f7f
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 93 deletions.
54 changes: 6 additions & 48 deletions analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,6 @@ func getKindFor(v cue.Value) (TSType, error) {
return TSType(tt), nil
}

func getForceText(v cue.Value) string {
var found bool
var attr cue.Attribute
for _, a := range v.Attributes(cue.ValueAttr) {
if a.Name() == attrname {
found = true
attr = a
}
}
if !found {
return ""
}

ft, found, err := attr.Lookup(0, attrForceText)
if err != nil || !found {
return ""
}

return ft
}

func targetsAnyKind(v cue.Value) bool {
return targetsKind(v)
}

func targetsKind(v cue.Value, kinds ...TSType) bool {
vkind, err := getKindFor(v)
if err != nil {
Expand All @@ -88,23 +63,6 @@ func targetsKind(v cue.Value, kinds ...TSType) bool {
return false
}

// containsReference recursively flattens expressions within a Value to find all
// its constituent Values, and checks if any of those Values are references.
//
// It does NOT walk struct fields - only expression structures, as returned from Expr().
// Remember that Expr() _always_ drops values in default branches.
func containsReference(v cue.Value) bool {
if isReference(v) {
return true
}
for _, dv := range flatten(v) {
if isReference(dv) {
return true
}
}
return false
}

// containsCuetsyReference does the same as containsReference, but returns true
// iff at least one referenced node passes the targetsKind predicate check
func containsCuetsyReference(v cue.Value, kinds ...TSType) bool {
Expand Down Expand Up @@ -298,19 +256,19 @@ type listField struct {
props listProps
}

func (li *listField) eq(oli *listField) bool {
if li.props.isOpen == oli.props.isOpen && li.props.divergentTypes == oli.props.divergentTypes && li.lenElems == oli.lenElems {
if !li.props.isOpen {
if li.lenElems == 0 {
func (l *listField) eq(oli *listField) bool {
if l.props.isOpen == oli.props.isOpen && l.props.divergentTypes == oli.props.divergentTypes && l.lenElems == oli.lenElems {
if !l.props.isOpen {
if l.lenElems == 0 {
return true
}
p := cue.MakePath(cue.Index(0))
// Sloppy, but enough to cover all but really complicated cases that
// are likely unsupportable anyway
return li.v.LookupPath(p).Equals(oli.v.LookupPath(p))
return l.v.LookupPath(p).Equals(oli.v.LookupPath(p))
}

return li.anyType.Subsume(oli.anyType, cue.Raw(), cue.Schema()) == nil && oli.anyType.Subsume(li.anyType, cue.Raw(), cue.Schema()) == nil
return l.anyType.Subsume(oli.anyType, cue.Raw(), cue.Schema()) == nil && oli.anyType.Subsume(l.anyType, cue.Raw(), cue.Schema()) == nil
}

return false
Expand Down
6 changes: 3 additions & 3 deletions dobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func doit(conf NewConfig, inst *cue.Instance) (result []tsast.Decl, err error) {
panic("TODO")
}

func (c *buildContext) build(name string, v cue.Value) *tsoutput {
func (b *buildContext) build(name string, v cue.Value) *tsoutput {
// TODO should we let errors escape here? Maybe only unsupported-type ones?
return newTypeBuilder(c).enterGen(nil, name, v)
return newTypeBuilder(b).enterGen(nil, name, v)
}

func (c *buildContext) makeRef(inst *cue.Instance, ref []string) string {
func (b *buildContext) makeRef(inst *cue.Instance, ref []string) string {
ref = append([]string{}, ref...)

// NOTE this is where oapi does things with its NameFunc
Expand Down
22 changes: 0 additions & 22 deletions generator.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package cuetsy

import (
"bytes"
"fmt"
"math/bits"
"sort"
"strings"
"text/template"

"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
Expand All @@ -17,10 +15,8 @@ import (

const (
attrname = "cuetsy"
attrEnumDefault = "enumDefault"
attrEnumMembers = "memberNames"
attrKind = "kind"
attrForceText = "forceText"
)

// TSType strings indicate the kind of TypeScript declaration to which a CUE
Expand Down Expand Up @@ -177,15 +173,6 @@ func (g *generator) addErr(err error) {
}
}

func execGetString(t *template.Template, data interface{}) (string, error) {
var tpl bytes.Buffer
if err := t.Execute(&tpl, data); err != nil {
return "", err
}
result := tpl.String()
return result, nil
}

func (g *generator) decl(name string, v cue.Value) []ts.Decl {
tst, err := getKindFor(v)
if err != nil {
Expand Down Expand Up @@ -1024,11 +1011,6 @@ func (g generator) tsPrintDefault(v cue.Value) (bool, ts.Expr, error) {
// Render a string containing a Typescript semantic equivalent to the provided
// Value for placement in a single field, if possible.
func (g generator) tsprintField(v cue.Value, isType bool) (ts.Expr, error) {
// Let the forceText attribute supersede everything.
if ft := getForceText(v); ft != "" {
return ts.Raw(ft), nil
}

if hasEnumReference(v) {
ref, err := g.genEnumReference(v)
return ref.T, err
Expand Down Expand Up @@ -1325,12 +1307,10 @@ func refAsInterface(v cue.Value) (ts.Expr, error) {
return ts.Ident(dstr), nil
}
case *ast.SelectorExpr:
// panic("case 2")
if targetsKind(deref, TypeInterface) {
return ts.Ident(dstr), nil
}
case *ast.Ident:
// panic("case 3")
if targetsKind(deref, TypeInterface) {
str, ok := dvals[0].Source().(fmt.Stringer)
if !ok {
Expand Down Expand Up @@ -1410,12 +1390,10 @@ func referenceValueAs(v cue.Value, kinds ...TSType) (ts.Expr, error) {
return ts.Ident(dstr), nil
}
case *ast.SelectorExpr:
// panic("case 2")
if targetsKind(deref, kinds...) {
return ts.Ident(dstr), nil
}
case *ast.Ident:
// panic("case 3")
if targetsKind(deref, kinds...) {
str, ok := dvals[0].Source().(fmt.Stringer)
if !ok {
Expand Down
10 changes: 0 additions & 10 deletions ts/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,6 @@ var (
_ Expr = Num{}
)

type Raw struct {
Data string
}

func (r Raw) decl() {}
func (r Raw) expr() {}
func (r Raw) String() string {
return r.Data
}

type Ident struct {
Name string

Expand Down
10 changes: 0 additions & 10 deletions ts/ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ func Union(elems ...Expr) Expr {
return ast.ParenExpr{Expr: U}
}

func Raw(data string) ast.Raw {
// pc, file, no, ok := runtime.Caller(1)
// details := runtime.FuncForPC(pc)
// if ok && details != nil {
// fmt.Printf("fix: ts.Raw used by %s at %s#%d\n", details.Name(), file, no)
// }

return ast.Raw{Data: data}
}

func Object(fields map[string]Expr) Expr {
elems := make([]ast.KeyValueExpr, 0, len(fields))
for k, v := range fields {
Expand Down

0 comments on commit 5139f7f

Please sign in to comment.