Skip to content

Commit

Permalink
Merge pull request #43 from moeshin/fix-constant-value
Browse files Browse the repository at this point in the history
Fix constant value
  • Loading branch information
gzuidhof authored Oct 11, 2023
2 parents 50c1ef0 + d4b6b4b commit 72ed956
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions tygo/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

var validJSNameRegexp = regexp.MustCompile(`(?m)^[\pL_][\pL\pN_]*$`)
var backquoteEscapeRegexp = regexp.MustCompile(`([$\\])`)

func validJSName(n string) bool {
return validJSNameRegexp.MatchString(n)
Expand Down Expand Up @@ -88,6 +89,9 @@ func (g *PackageGenerator) writeType(
g.writeType(s, t.Value, depth, false)
s.WriteByte('}')
case *ast.BasicLit:
if strings.HasPrefix(t.Value, "`") {
t.Value = backquoteEscapeRegexp.ReplaceAllString(t.Value, `\$1`)
}
s.WriteString(t.Value)
case *ast.ParenExpr:
s.WriteByte('(')
Expand All @@ -104,11 +108,18 @@ func (g *PackageGenerator) writeType(
case *ast.CallExpr, *ast.FuncType, *ast.ChanType:
s.WriteString(g.conf.FallbackType)
case *ast.UnaryExpr:
if t.Op == token.TILDE {
switch t.Op {
case token.TILDE:
// We just ignore the tilde token, in Typescript extended types are
// put into the generic typing itself, which we can't support yet.
g.writeType(s, t.X, depth, false)
} else {
case token.XOR:
s.WriteString("~")
g.writeType(s, t.X, depth, false)
case token.ADD, token.SUB, token.NOT:
s.WriteString(t.Op.String())
g.writeType(s, t.X, depth, false)
default:
err := fmt.Errorf("unhandled unary expr: %v\n %T", t, t)
fmt.Println(err)
panic(err)
Expand Down

0 comments on commit 72ed956

Please sign in to comment.