@barefootjs/go-template 0.18.7 → 0.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -699,7 +699,7 @@ function buildGoPropsInit(
699
699
  // not the naive `Id`) — see `goMapLiteralFromObject`'s identical fix.
700
700
  const goField = capitalizeFieldName(key)
701
701
  if (typeof value === 'string') {
702
- lines.push(`\t\t${goField}: "${value}",`)
702
+ lines.push(`\t\t${goField}: ${goStringLit(value)},`)
703
703
  } else if (typeof value === 'number') {
704
704
  lines.push(`\t\t${goField}: ${value},`)
705
705
  } else if (typeof value === 'boolean') {
@@ -828,6 +828,20 @@ function goTypedMapSliceLiteralFromArray(arr: unknown[], elemType: string): stri
828
828
  * names. Only the keys the caller supplied are set, so an omitted optional prop
829
829
  * (e.g. `defaultOn` on the third toggle item) takes the Go zero value. (#1297)
830
830
  */
831
+ /**
832
+ * Emit a JS string as a Go interpreted string literal. JSON string
833
+ * escaping is a subset of Go's (`\"`, `\\`, `\n`, `\uXXXX` are all valid
834
+ * Go escapes), so `JSON.stringify` is a correct emitter — unlike the
835
+ * previous quote-only `.replace(/"/g, '\\"')`, it also survives
836
+ * backslashes, newlines, and control characters (data-point conformance
837
+ * caught the quote case: a `"` in a string prop broke the generated
838
+ * `main.go` at compile time). Lone surrogates emit as `\uDXXX`, which Go
839
+ * rejects — acceptable until a fixture needs malformed-UTF-16 props.
840
+ */
841
+ function goStringLit(v: string): string {
842
+ return JSON.stringify(v)
843
+ }
844
+
831
845
  function goStructLiteral(obj: Record<string, unknown>, typeName: string): string {
832
846
  const fields: string[] = []
833
847
  for (const [k, v] of Object.entries(obj)) {
@@ -836,7 +850,7 @@ function goStructLiteral(obj: Record<string, unknown>, typeName: string): string
836
850
  // adapter's own struct-literal baking (`parsed-literal-to-go.ts`)
837
851
  // sanitizes those to `DataX`, not `Data-x` (Copilot review, #2202).
838
852
  const goField = goFieldNameForKey(k)
839
- if (typeof v === 'string') fields.push(`${goField}: "${v.replace(/"/g, '\\"')}"`)
853
+ if (typeof v === 'string') fields.push(`${goField}: ${goStringLit(v)}`)
840
854
  else if (typeof v === 'number' || typeof v === 'boolean') fields.push(`${goField}: ${v}`)
841
855
  else if (v === null) fields.push(`${goField}: nil`)
842
856
  else if (Array.isArray(v)) fields.push(`${goField}: ${goArrayLiteralFromArray(v)}`)
@@ -848,7 +862,7 @@ function goStructLiteral(obj: Record<string, unknown>, typeName: string): string
848
862
  function goArrayLiteralFromArray(arr: unknown[]): string {
849
863
  const entries: string[] = []
850
864
  for (const v of arr) {
851
- if (typeof v === 'string') entries.push(`"${v.replace(/"/g, '\\"')}"`)
865
+ if (typeof v === 'string') entries.push(goStringLit(v))
852
866
  else if (typeof v === 'number') entries.push(String(v))
853
867
  else if (typeof v === 'boolean') entries.push(String(v))
854
868
  else if (v === null) entries.push('nil')
@@ -886,7 +900,7 @@ function goMapLiteralFromObject(
886
900
  // uses for exactly this key-to-Go-field sanitization.
887
901
  const emittedKey = capitalizeKeys ? goFieldNameForKey(k) : k
888
902
  const key = JSON.stringify(emittedKey)
889
- if (typeof v === 'string') entries.push(`${key}: "${v.replace(/"/g, '\\"')}"`)
903
+ if (typeof v === 'string') entries.push(`${key}: ${goStringLit(v)}`)
890
904
  else if (typeof v === 'number') entries.push(`${key}: ${v}`)
891
905
  else if (typeof v === 'boolean') entries.push(`${key}: ${v}`)
892
906
  else if (v === null) entries.push(`${key}: nil`)