@@ -646,22 +646,28 @@ func encodeIdent(name string) string {
646
646
return strings .Replace (url .QueryEscape (name ), "%" , "$" , - 1 )
647
647
}
648
648
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.
653
652
//
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.
655
661
//
656
- // Uses definition of an identifier from
657
- // https://developer.mozilla.org/en-US/docs/Glossary/Identifier
658
662
func formatJSStructTagVal (jsTag string ) string {
659
663
for i , r := range jsTag {
660
664
ok := unicode .IsLetter (r ) || (i != 0 && unicode .IsNumber (r )) || r == '$' || r == '_'
661
665
if ! ok {
662
- return "[\" " + template .JSEscapeString (jsTag ) + "\" ]"
666
+ // Saw an invalid JavaScript identifier character,
667
+ // so use bracket notation.
668
+ return `["` + template .JSEscapeString (jsTag ) + `"]`
663
669
}
664
670
}
665
-
671
+ // Safe to use dot notation without any escaping.
666
672
return "." + jsTag
667
673
}
0 commit comments