natdm / typewriter

Synchronize your front-end models with your Go models
82 stars 5 forks source link

Parse and fix invalid names #12

Closed natdm closed 7 years ago

natdm commented 7 years ago

Quoting @scottmmjackson -

Transform valid Go names for struct fields and types to valid TS/Flow/Elm names.

Below, Scott suggests a way to fix names that are invalid ECMA. It makes sense, but I'd like to leave room to parse invalid Elm names as well. If we could turn this in to a method on the Language type that will parse it somehow (like func (l Language) ParseFromReserved(s string) string) that will just parse ALL input and transform to valid output (or just noop and return the word if not invalid), I feel that leaves more room for growth, as well is available anywhere that the Language type is passed in.


Sure, right around here: https://github.com/natdm/typewriter/blob/master/template/template.go#L230

You've got strings.Contains(t.name, '-'). That test could be generalized to check for anything that makes the identifier invalid. Here's a basic attempt:

var ecmaReservedWords = map[string]struct{}{
 "break": struct{}{},
"case": struct{}{},
 "catch": struct{}{},
"class": struct{}{},
"const": struct{}{},
"continue": struct{}{},
"debugger": struct{}{},
"default": struct{}{},
"delete": struct{}{},
"do": struct{}{},
"else": struct{}{},
"export": struct{}{},
"extends": struct{}{},
"finally": struct{}{},
"for": struct{}{},
"function": struct{}{},
"if": struct{}{},
"import": struct{}{},
"in": struct{}{},
"instanceof": struct{}{},
"new": struct{}{},
"return": struct{}{},
"super": struct{}{},
"switch": struct{}{},
"this": struct{}{},
"throw": struct{}{},
"try": struct{}{},
"typeof": struct{}{},
"var": struct{}{},
"void": struct{}{},
"while": struct{}{},
"with": struct{}{},
"yield": struct{}{},
"enum": struct{}{},
"implements": struct{}{},
"interface": struct{}{},
"let": struct{}{},
"package": struct{}{},
"private": struct{}{},
"protected": struct{}{},
"public": struct{}{},
"static": struct{}{},
"abstract": struct{}{},
"boolean": struct{}{},
"byte": struct{}{},
"char": struct{}{},
"double": struct{}{},
"final": struct{}{},
"float": struct{}{},
"goto": struct{}{},
"int": struct{}{},
"long": struct{}{},
"native": struct{}{},
"short": struct{}{},
"synchronized": struct{}{},
"throws": struct{}{},
"transient": struct{}{},
"volatile": struct{}{},
"await": struct{}{},
}

// Greedy regex that permits common valid identifiers like `$apply` or `_`
// This quotes more than is strictly necessary, because unicode "letters" in identifier names
// are valid. See: https://stackoverflow.com/questions/2008279/validate-a-javascript-function-name
var validIdentifier = regex.MustCompile(`^[$\w]+$`)

func propertyShouldBeQuoted(name string) bool {
  _, isReservedWord := ecmaReservedWords[name]
  return isReservedWord || !validIdentifier.MatchString(name) 
}

See playground here: https://play.golang.org/p/SxVqckRKZ3

Note that this attempt is a little overkill, and will quote valid identifiers, but they'd be uncommon valid identifiers and it would ensure that the tool always works.


natdm commented 7 years ago

This is the Language type: https://github.com/natdm/typewriter/blob/f91e3c8/template/funcmaps.go#L10

natdm commented 7 years ago

To be clear, I'm not saying we SHOULD do Elm right now -- you can just have it noop for Elm and that can be a PR for another day.

@scottmmjackson -- would you want to take this on?

scottmmjackson commented 7 years ago

Yeah, I'd be able to make some movement on it this afternoon possibly.

scottmmjackson commented 7 years ago

13 is the related PR here

scottmmjackson commented 7 years ago

So it sounds like the status here is:

If I'm wrong about the latter, perhaps this issue can be closed.

natdm commented 7 years ago

We can separate elm out in to a new one at a later time. It's very unused.. I may be the only one that uses it :-P