Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix camelCase for typescript generation #198

Open
wants to merge 8 commits into
base: JSImpl
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Source/buildimplementationjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,14 @@ func buildJSInjectionClass(component ComponentDefinition, subComponent Component
}
if readOnly {
cppw.Writeln(" Cv8toolsUtils::Set_proto_accessor(localClassTemplate, \"%s\", Cv8%s::v8%s);", getter.PropertyGet, class.ClassName, getter.MethodName)
cppw.Writeln(" Cv8toolsUtils::Set_proto_accessor(localClassTemplate, \"%s\", Cv8%s::v8%s);", camelize(getter.PropertyGet), class.ClassName, getter.MethodName)
} else {
cppw.Writeln(" Cv8toolsUtils::Set_proto_accessor(localClassTemplate, \"%s\", Cv8%s::v8%s, Cv8%s::v8%s);", setter.PropertySet, class.ClassName, getter.MethodName, class.ClassName, setter.MethodName)
cppw.Writeln(" Cv8toolsUtils::Set_proto_accessor(localClassTemplate, \"%s\", Cv8%s::v8%s, Cv8%s::v8%s);", camelize(setter.PropertySet), class.ClassName, getter.MethodName, class.ClassName, setter.MethodName)
}
} else {
cppw.Writeln(" Cv8toolsUtils::Set_proto_method(localClassTemplate, \"%s\", Cv8%s::v8%s);", method.MethodName, class.ClassName, method.MethodName)
cppw.Writeln(" Cv8toolsUtils::Set_proto_method(localClassTemplate, \"%s\", Cv8%s::v8%s);", camelize(method.MethodName), class.ClassName, method.MethodName)
}
}
cppw.Writeln("")
Expand Down
56 changes: 36 additions & 20 deletions Source/buildimplementationts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ import (
"errors"
"log"
"path"
"regexp"
"strings"
"unicode"
"unicode/utf8"
)

type TypeScriptOptions struct {
Expand All @@ -57,7 +58,7 @@ func BuildImplementationTS(
log.Printf("Creating TypeScript Implementation")

options := TypeScriptOptions{
Camelize: false,
Camelize: true,
JsArrays: false,
LineLength: 80,
}
Expand Down Expand Up @@ -106,11 +107,11 @@ func writeTypescriptEnum(
writer LanguageWriter,
options TypeScriptOptions,
) error {
writer.Writeln("const enum %s {", getId(enum.Name, options))
writer.Writeln("const enum %s {", getTypeId(enum.Name, options))
writer.Indentation++
for _, option := range enum.Options {
writeCommentEnumOption(option, writer, options)
identifier := getId(option.Name, options)
identifier := getTypeId(option.Name, options)
value := option.Value
writer.Writeln("%s = %d,", identifier, value)
}
Expand Down Expand Up @@ -140,7 +141,7 @@ func writeTypescriptInterface(
options TypeScriptOptions,
) error {
writeCommentClass(class, writer, options)
identifier := getId(class.ClassName, options)
identifier := getTypeId(class.ClassName, options)
extends := ""
if class.ParentClass != "" {
extends = "extends " + class.ParentClass + " "
Expand Down Expand Up @@ -178,33 +179,33 @@ func writeTypescriptMethod(
writer.Writeln("")
writeCommentMethod(class, method, writer, options)
writer.BeginLine()
writer.Printf("%s: (", getId(method.MethodName, options))
writer.Printf("%s(", getName(method.MethodName, options))
for i, param := range inParams {
if param.ParamOptional == "true" {
writer.Printf(
"%s?: %s",
getId(param.ParamName, options),
getName(param.ParamName, options),
getType(param, options),
)
} else {
writer.Printf(
"%s: %s",
getId(param.ParamName, options),
getName(param.ParamName, options),
getType(param, options),
)
}
if i+1 < len(inParams) {
writer.Printf(", ")
}
}
writer.Printf(") => ")
writer.Printf("): ")

if len(outParams) > 0 {
writer.Printf("[")
for i, param := range outParams {
writer.Printf(
"%s: %s",
getId(param.ParamName, options),
getName(param.ParamName, options),
getType(param, options),
)
if i+1 < len(outParams) {
Expand Down Expand Up @@ -258,7 +259,7 @@ func writeTypescriptProperty(
writer.Writeln(
"%s%s: %s;",
readOnly,
getId(getter.PropertyGet, options),
getName(getter.PropertyGet, options),
getType(returnParams[0], options),
)
return nil
Expand Down Expand Up @@ -287,7 +288,11 @@ func filterOptional(params []ComponentDefinitionParam) []ComponentDefinitionPara
return result
}

func getId(identifier string, options TypeScriptOptions) string {
func getTypeId(identifier string, options TypeScriptOptions) string {
return identifier
}

func getName(identifier string, options TypeScriptOptions) string {
if options.Camelize {
return camelize(identifier)
}
Expand Down Expand Up @@ -327,13 +332,24 @@ func getTypeString(
return paramType
}

func camelize(identifier string) string {
if len(identifier) == 0 {
return identifier
func camelize(str string) string {
r, _ := regexp.Compile("^([A-Z]+)([A-Z])(.*)")
result := r.FindStringSubmatch(str)

if len(result) > 0 {
if len(result) > 1 {
if len(result) == 4 && result[3] == "" {
str = strings.ToLower(str)
} else {
str = strings.ToLower(result[1]) + strings.Join(result[2:], "")
}
}
} else {
r, _ := utf8.DecodeRuneInString(str)
str = strings.ToLower(string(r)) + str[len(string(r)):]
}
result := []rune(identifier)
result[0] = unicode.ToLower(result[0])
return string(result)

return str
}

func writeCommentEnumOption(
Expand Down Expand Up @@ -407,7 +423,7 @@ func writeCommentInParams(
) {
for _, param := range params {
prefix := " * @param {" + getType(param, options) + "} " +
getId(param.ParamName, options) + " "
getName(param.ParamName, options) + " "
lines := getCommentLines(prefix, param.ParamDescription, writer, options)
if len(lines) > 0 {
writer.Writeln(prefix + lines[0])
Expand All @@ -427,7 +443,7 @@ func writeCommentOutParams(
) {
for _, param := range params {
prefix := " * @returns {" + getType(param, options) + "} "
prefix2 := prefix + getId(param.ParamName, options) + " "
prefix2 := prefix + getName(param.ParamName, options) + " "
lines := getCommentLines(prefix2, param.ParamDescription, writer, options)
if len(lines) > 0 {
writer.Writeln(prefix2 + lines[0])
Expand Down