Skip to content

Commit 0e3dfdb

Browse files
authored
feat: avoid generating repeated type constraints (#42)
* feat: avoid generating repeated type constraints * emphasize this only works with literals
1 parent 37918e0 commit 0e3dfdb

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

bindings/declarations.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package bindings
22

3-
import (
4-
"fmt"
5-
"reflect"
6-
)
7-
83
// DeclarationType is any type that can exist at the top level of a AST.
94
// Meaning it can be serialized into valid Typescript.
105
type DeclarationType interface {
@@ -69,23 +64,41 @@ func (p *TypeParameter) isNode() {}
6964

7065
// Simplify removes duplicate type parameters
7166
func Simplify(p []*TypeParameter) ([]*TypeParameter, error) {
72-
params := make([]*TypeParameter, 0, len(p))
73-
exists := make(map[string]*TypeParameter)
67+
params := []*TypeParameter{}
68+
set := make(map[string]bool)
7469
for _, tp := range p {
75-
if found, ok := exists[tp.Name.Ref()]; ok {
76-
// Compare types, make sure they are the same
77-
equal := reflect.DeepEqual(found, tp)
78-
if !equal {
79-
return nil, fmt.Errorf("type parameter %q already exists with different type", tp.Name)
70+
ref := tp.Name.Ref()
71+
if _, ok := set[ref]; !ok {
72+
params = append(params, tp)
73+
set[ref] = true
74+
75+
if union, ok := tp.Type.(*UnionType); ok {
76+
simplifyUnionLiterals(union)
8077
}
81-
continue
8278
}
83-
params = append(params, tp)
84-
exists[tp.Name.Ref()] = tp
8579
}
8680
return params, nil
8781
}
8882

83+
func simplifyUnionLiterals(union *UnionType) *UnionType {
84+
types := []ExpressionType{}
85+
literalSet := map[string]bool{}
86+
for _, arg := range union.Types {
87+
switch v := arg.(type) {
88+
case *LiteralKeyword:
89+
key := v.String()
90+
if _, ok := literalSet[key]; !ok {
91+
literalSet[key] = true
92+
types = append(types, arg)
93+
}
94+
default:
95+
types = append(types, arg)
96+
}
97+
}
98+
union.Types = types
99+
return union
100+
}
101+
89102
// VariableStatement is a top level declaration of a variable
90103
// var foo: string = "bar"
91104
// const foo: string = "bar"

testdata/union/union.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ type UnionConstraint[T string | int64] struct {
55
}
66

77
// Repeated constraints are redundant
8-
// TODO: Write a mutation to remove redundant constraints
98
type Repeated[T string | string | int64 | uint64] struct {
109
Value T
1110
}

testdata/union/union.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Code generated by 'guts'. DO NOT EDIT.
22

33
// From union/union.go
4-
export interface Repeated<T extends string | string | number | number> {
4+
export interface Repeated<T extends string | number> {
55
readonly Value: T;
66
}
77

0 commit comments

Comments
 (0)