Skip to content

Commit 5ade898

Browse files
committed
Apply minor style and documentation changes.
Add examples to show what kind of output formatJSStructTagVal returns. Simplify and shorten test. Remove unhelpful blank lines, they're best used sparingly.
1 parent 6672256 commit 5ade898

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

compiler/utils.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -646,22 +646,28 @@ func encodeIdent(name string) string {
646646
return strings.Replace(url.QueryEscape(name), "%", "$", -1)
647647
}
648648

649-
// formatJSStructTagVal returns a string of JavaScript code appropriate for
650-
// accessing the property identified by jsTag. If the jsTag value can be safely
651-
// encoded in JavaScript using the dot notation this is used, else we fallback
652-
// to the bracket notation. For more details see:
649+
// formatJSStructTagVal returns JavaScript code for accessing an object's property
650+
// identified by jsTag. It prefers the dot notation over the bracket notation when
651+
// possible, since the dot notation produces slightly smaller output.
653652
//
654-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors
653+
// For example:
654+
//
655+
// "my_name" -> ".my_name"
656+
// "my name" -> `["my name"]`
657+
//
658+
// For more information about JavaScript property accessors and identifiers, see
659+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors and
660+
// https://developer.mozilla.org/en-US/docs/Glossary/Identifier.
655661
//
656-
// Uses definition of an identifier from
657-
// https://developer.mozilla.org/en-US/docs/Glossary/Identifier
658662
func formatJSStructTagVal(jsTag string) string {
659663
for i, r := range jsTag {
660664
ok := unicode.IsLetter(r) || (i != 0 && unicode.IsNumber(r)) || r == '$' || r == '_'
661665
if !ok {
662-
return "[\"" + template.JSEscapeString(jsTag) + "\"]"
666+
// Saw an invalid JavaScript identifier character,
667+
// so use bracket notation.
668+
return `["` + template.JSEscapeString(jsTag) + `"]`
663669
}
664670
}
665-
671+
// Safe to use dot notation without any escaping.
666672
return "." + jsTag
667673
}

js/js_test.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -598,33 +598,25 @@ func TestTypeSwitchJSObject(t *testing.T) {
598598
}
599599
}
600600

601-
type StructWithNonIdentifierJsTags struct {
602-
object *js.Object
603-
Name string `js:"@&\"'<>//my name"`
604-
}
605-
606-
func TestStructWithNonIdentifierJsTags(t *testing.T) {
607-
s := StructWithNonIdentifierJsTags{
608-
object: js.Global.Get("Object").New(),
601+
func TestStructWithNonIdentifierJSTag(t *testing.T) {
602+
type S struct {
603+
*js.Object
604+
Name string `js:"@&\"'<>//my name"`
609605
}
606+
s := S{Object: js.Global.Get("Object").New()}
610607

611-
const want = "Paul"
608+
// externalise a value via field
609+
s.Name = "Paul"
612610

613-
// externalise a value
614-
s.Name = want
615-
616-
// internalise again
611+
// internalise via field
617612
got := s.Name
618-
619-
if want != got {
620-
t.Errorf("Value via non-identifier js tag field gave %q, want %q", got, want)
613+
if want := "Paul"; got != want {
614+
t.Errorf("value via field with non-identifier js tag gave %q, want %q", got, want)
621615
}
622616

623617
// verify we can do a Get with the struct tag
624-
got = s.object.Get("@&\"'<>//my name").String()
625-
626-
if want != got {
627-
t.Errorf("Value via .Get gave %q, want %q", got, want)
618+
got = s.Get("@&\"'<>//my name").String()
619+
if want := "Paul"; got != want {
620+
t.Errorf("value via js.Object.Get gave %q, want %q", got, want)
628621
}
629-
630622
}

0 commit comments

Comments
 (0)