@barefootjs/go-template 0.5.1 → 0.5.2

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.
@@ -91,6 +91,21 @@ export declare class GoTemplateAdapter extends BaseAdapter implements ParsedExpr
91
91
  private localTypeNames;
92
92
  /** Local type aliases mapping type name to base type (e.g., Filter → 'string') */
93
93
  private localTypeAliases;
94
+ /**
95
+ * Per-struct field map (type name → source TS key → Go field name), populated
96
+ * during generateTypes. The object-literal baker consults this so a baked
97
+ * struct literal only names fields the generated struct actually declares.
98
+ */
99
+ private localStructFields;
100
+ /**
101
+ * Synthesised array types for untyped object-array signals (signal getter →
102
+ * `[]SynthStruct` TypeInfo), populated during generateTypes (#1680). An
103
+ * untyped `createSignal([{ id: "a" }])` has no element type to bake against;
104
+ * we infer a struct from the literal's shape so the field can be typed and
105
+ * the items baked. Consulted by both the signal field-type emitter and the
106
+ * initial-value baker.
107
+ */
108
+ private synthStructTypes;
94
109
  /** Set during type generation when any emit references
95
110
  * `template.HTML(...)`; toggles the `"html/template"` import. */
96
111
  private usesHtmlTemplate;
@@ -149,6 +164,50 @@ export declare class GoTemplateAdapter extends BaseAdapter implements ParsedExpr
149
164
  * Handles object types → Go structs, and union string literals → string alias.
150
165
  */
151
166
  private typeDefinitionToGo;
167
+ /**
168
+ * Derive a struct's Go fields from the analyzer-provided structured
169
+ * properties — no string parsing of the definition. This is the single
170
+ * source of truth for which fields a generated struct has and each field's Go
171
+ * name/type; both the struct emitter ({@link typeDefinitionToGo}) and the
172
+ * object-literal baker ({@link tsLiteralToGo}) consume it, so a baked literal
173
+ * can never name a field the struct doesn't declare.
174
+ *
175
+ * A property whose source key isn't a valid Go identifier (`"data-id"`, a
176
+ * numeric key, …) can't become a struct field, so it's dropped here — and is
177
+ * therefore absent from the baker's field map too, which bails to nil for any
178
+ * literal that uses such a key.
179
+ */
180
+ private structFieldsFor;
181
+ /**
182
+ * Synthesise a Go struct from an untyped object-array signal's inline initial
183
+ * value (#1680). Returns the struct name + fields, or `null` when synthesis
184
+ * isn't possible so the caller keeps the field `[]interface{}`/`nil`.
185
+ *
186
+ * Synthesis applies only when:
187
+ * - the signal's type is an array with no usable element type (untyped),
188
+ * - the initial value is a non-empty array literal of object literals,
189
+ * - every element shares the same set of Go-identifier keys, and
190
+ * - every value is a scalar literal whose Go type is consistent per key
191
+ * (numeric keys widen int→float64 when mixed).
192
+ *
193
+ * Any deviation (heterogeneous shape, a nested object/array value, a
194
+ * non-literal value, a non-identifier key, or a name collision with an
195
+ * existing type) returns `null`.
196
+ */
197
+ private synthesizeStructFromSignal;
198
+ /**
199
+ * The Go type for a scalar JS literal used as a synthesised struct field
200
+ * value, or `null` for anything non-scalar (objects, arrays, identifiers,
201
+ * calls, interpolated templates) so the caller bails out of synthesis.
202
+ */
203
+ private scalarLiteralGoType;
204
+ /** `int` for an integer literal, `float64` when the literal has a fraction
205
+ * or exponent. */
206
+ private numericLiteralGoType;
207
+ /** Reconcile two inferred Go types for the same key across elements: equal
208
+ * types stay; mixed numeric (int/float64) widens to float64; otherwise null
209
+ * (incompatible → bail). */
210
+ private mergeScalarGoType;
152
211
  /**
153
212
  * Convert a raw TypeScript type string to a Go type string.
154
213
  * Handles primitives (number, string, boolean) and basic arrays.
@@ -286,6 +345,34 @@ export declare class GoTemplateAdapter extends BaseAdapter implements ParsedExpr
286
345
  * References to props params are converted to in.FieldName format.
287
346
  */
288
347
  private convertInitialValue;
348
+ /**
349
+ * Convert a fully-literal JS expression string into an equivalent Go literal
350
+ * whose Go type matches `typeInfo` (#1672), used to bake a signal's inline
351
+ * initial value into the SSR data context:
352
+ *
353
+ * `["x", "y"]` (string[]) → `[]string{"x", "y"}`
354
+ * `["x", "y"]` (unknown[]) → `[]interface{}{"x", "y"}`
355
+ * `[{ id: "a" }]` (Item[]) → `[]Item{Item{ID: "a"}}`
356
+ *
357
+ * Returns `null` — so the caller keeps `nil` — when the expression (or any
358
+ * nested element) is not a pure literal (a call, identifier, template with
359
+ * interpolation, …) or cannot be expressed in the target Go type without a
360
+ * render/compile mismatch (e.g. an object element in a `[]interface{}` field,
361
+ * which the SSR template reaches via struct field access the map lacks).
362
+ */
363
+ private jsLiteralToGo;
364
+ /**
365
+ * Parse a JS expression string into its TS AST node (parentheses unwrapped),
366
+ * or `null` when it isn't a single expression. Shared by the literal baker
367
+ * and the struct-shape synthesiser.
368
+ */
369
+ private parseLiteralExpression;
370
+ /**
371
+ * Recursively convert a TS literal AST node to a Go literal typed as
372
+ * `typeInfo`, or null when the node is not a pure literal / cannot be
373
+ * represented in that Go type.
374
+ */
375
+ private tsLiteralToGo;
289
376
  /**
290
377
  * Convert TypeInfo to Go type string.
291
378
  * If type is unknown, tries to infer from defaultValue.
@@ -374,6 +461,13 @@ export declare class GoTemplateAdapter extends BaseAdapter implements ParsedExpr
374
461
  * e.g., "props.initial ?? 0" → "initial", "props.checked" → "checked"
375
462
  */
376
463
  private extractPropNameFromInitialValue;
464
+ /**
465
+ * A source key that can become a Go struct field — i.e. a valid Go
466
+ * identifier. TS object keys that aren't (e.g. `"data-id"`, numeric keys)
467
+ * can't be exported struct fields, so they're excluded from struct generation
468
+ * and from the baker's field map.
469
+ */
470
+ private static GO_IDENTIFIER;
377
471
  /** Go common initialisms that should be fully uppercased (https://go.dev/wiki/CodeReviewComments#initialisms) */
378
472
  private static GO_INITIALISMS;
379
473
  /**
@@ -445,6 +539,21 @@ export declare class GoTemplateAdapter extends BaseAdapter implements ParsedExpr
445
539
  */
446
540
  private renderParsedExpr;
447
541
  identifier(name: string): string;
542
+ /**
543
+ * True when `name` is a loop value variable from an enclosing (not the
544
+ * current) loop — i.e. it sits on `loopParamStack` below the top. Such a
545
+ * reference resolves to the Go range variable `$name`, not the inner dot or
546
+ * the root data.
547
+ */
548
+ private isOuterLoopParam;
549
+ /**
550
+ * A reference to a root-scope field — a signal, a prop, or a derived value
551
+ * that lives on the component's top-level data struct. Inside a `{{range}}`
552
+ * the dot is rebound to the iteration element, so root data must be reached
553
+ * through Go template's `$` (the top-level argument to Execute), which never
554
+ * rebinds. Outside any loop the root *is* the dot, so we emit `.Field` (#1677).
555
+ */
556
+ private rootFieldRef;
448
557
  literal(value: string | number | boolean | null, literalType: LiteralType): string;
449
558
  call(callee: ParsedExpr, args: ParsedExpr[], emit: (e: ParsedExpr) => string): string;
450
559
  member(object: ParsedExpr, property: string, _computed: boolean, emit: (e: ParsedExpr) => string): string;
@@ -1 +1 @@
1
- {"version":3,"file":"go-template-adapter.d.ts","sourceRoot":"","sources":["../../src/adapter/go-template-adapter.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EACV,WAAW,EACX,MAAM,EACN,SAAS,EACT,MAAM,EACN,YAAY,EACZ,aAAa,EACb,MAAM,EAEN,WAAW,EACX,UAAU,EACV,MAAM,EAMN,UAAU,EAEV,cAAc,EACd,YAAY,EACZ,aAAa,EACb,UAAU,EACV,OAAO,EACP,yBAAyB,EAC1B,MAAM,iBAAiB,CAAA;AACxB,OAAO,EACL,WAAW,EACX,KAAK,aAAa,EAClB,KAAK,sBAAsB,EAE3B,KAAK,iBAAiB,EACtB,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,UAAU,EAUhB,MAAM,iBAAiB,CAAA;AAGxB;;;;;GAKG;AACH,KAAK,WAAW,GAAG;IACjB,uBAAuB,CAAC,EAAE,OAAO,CAAA;CAClC,CAAA;AA2ED,MAAM,WAAW,wBAAwB;IACvC,kEAAkE;IAClE,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAA;IAEzB;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;CACxB;AA+FD,qBAAa,iBAAkB,SAAQ,WAAY,YAAW,iBAAiB,EAAE,aAAa,CAAC,WAAW,CAAC;IACzG,IAAI,SAAgB;IACpB,SAAS,SAAU;IAGnB,kBA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mI/B;IAED;;;;;OAKG;IACH,OAAO,CAAC,cAAc;IAWtB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAmB1B,eAAe,CAAC,IAAI,EAAE,WAAW,EAAE,GAAG,CAAC,EAAE;QAAE,uBAAuB,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,MAAM,CAyBtF;IAED;;;;;;OAMG;IACH,OAAO,CAAC,qBAAqB;IAqB7B,OAAO,CAAC,cAAc;IAStB,OAAO,CAAC,UAAU;IAMlB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAMjC;IAED,OAAO,CAAC,cAAc;IAItB;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAyElC;IAED,OAAO,CAAC,gBAAgB;IAsBxB;;;;;;;;;;;;;;;;OAgBG;IACH,OAAO,CAAC,0BAA0B;IA4BlC;;;;;;;;OAQG;IACH,OAAO,CAAC,cAAc;IAStB,OAAO,CAAC,0BAA0B;IAiClC,iBAAiB,CAAC,eAAe,EAAE,MAAM,GAAG,MAAM,CAKjD;IAED,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEvC;IAED,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEvC;IAED,OAAO,CAAC,kBAAkB;CAmB3B;AAED,eAAO,MAAM,iBAAiB,mBAA0B,CAAA"}
1
+ {"version":3,"file":"go-template-adapter.d.ts","sourceRoot":"","sources":["../../src/adapter/go-template-adapter.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EACV,WAAW,EACX,MAAM,EACN,SAAS,EACT,MAAM,EACN,YAAY,EACZ,aAAa,EACb,MAAM,EAEN,WAAW,EACX,UAAU,EACV,MAAM,EAON,UAAU,EAEV,cAAc,EACd,YAAY,EACZ,aAAa,EACb,UAAU,EACV,OAAO,EACP,yBAAyB,EAC1B,MAAM,iBAAiB,CAAA;AACxB,OAAO,EACL,WAAW,EACX,KAAK,aA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WtB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAmB1B,eAAe,CAAC,IAAI,EAAE,WAAW,EAAE,GAAG,CAAC,EAAE;QAAE,uBAAuB,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,MAAM,CAyBtF;IAED;;;;;;OAMG;IACH,OAAO,CAAC,qBAAqB;IAqB7B,OAAO,CAAC,cAAc;IAStB,OAAO,CAAC,UAAU;IAMlB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAMjC;IAED,OAAO,CAAC,cAAc;IAItB;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAyElC;IAED,OAAO,CAAC,gBAAgB;IAsBxB;;;;;;;;;;;;;;;;OAgBG;IACH,OAAO,CAAC,0BAA0B;IA4BlC;;;;;;;;OAQG;IACH,OAAO,CAAC,cAAc;IAStB,OAAO,CAAC,0BAA0B;IAiClC,iBAAiB,CAAC,eAAe,EAAE,MAAM,GAAG,MAAM,CAKjD;IAED,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEvC;IAED,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEvC;IAED,OAAO,CAAC,kBAAkB;CAmB3B;AAED,eAAO,MAAM,iBAAiB,mBAA0B,CAAA"}
@@ -90,6 +90,8 @@ class GoTemplateAdapter extends BaseAdapter {
90
90
  templateVarCounter = 0;
91
91
  localTypeNames = new Set;
92
92
  localTypeAliases = new Map;
93
+ localStructFields = new Map;
94
+ synthStructTypes = new Map;
93
95
  usesHtmlTemplate = false;
94
96
  constructor(options = {}) {
95
97
  super();
@@ -323,6 +325,7 @@ ${scriptRegistrations}${templateBody}
323
325
  const componentName = ir.metadata.componentName;
324
326
  this.localTypeNames = new Set;
325
327
  this.localTypeAliases = new Map;
328
+ this.localStructFields = new Map;
326
329
  for (const td of ir.metadata.typeDefinitions) {
327
330
  if (td.name === "Props" || td.name === `${componentName}Props`)
328
331
  continue;
@@ -331,6 +334,11 @@ ${scriptRegistrations}${templateBody}
331
334
  this.localTypeNames.add(td.name);
332
335
  if (td.definition.match(/^type \w+ = ('[^']*'(\s*\|\s*'[^']*')*)/)) {
333
336
  this.localTypeAliases.set(td.name, "string");
337
+ } else {
338
+ const fields = this.structFieldsFor(td);
339
+ if (fields.length > 0) {
340
+ this.localStructFields.set(td.name, new Map(fields.map((f) => [f.tsName, f.goName])));
341
+ }
334
342
  }
335
343
  }
336
344
  for (const td of ir.metadata.typeDefinitions) {
@@ -344,6 +352,26 @@ ${scriptRegistrations}${templateBody}
344
352
  lines.push("");
345
353
  }
346
354
  }
355
+ this.synthStructTypes = new Map;
356
+ for (const signal of ir.metadata.signals) {
357
+ const synth = this.synthesizeStructFromSignal(signal, componentName);
358
+ if (!synth)
359
+ continue;
360
+ this.localTypeNames.add(synth.name);
361
+ this.localStructFields.set(synth.name, new Map(synth.fields.map((f) => [f.tsName, f.goName])));
362
+ this.synthStructTypes.set(signal.getter, {
363
+ kind: "array",
364
+ raw: `${synth.name}[]`,
365
+ elementType: { kind: "interface", raw: synth.name }
366
+ });
367
+ const goFields = synth.fields.map((f) => ` ${f.goName} ${f.goType} \`json:"${this.toJsonTag(f.tsName)}"\``);
368
+ lines.push(`// ${synth.name} is a synthesised element type for the ${signal.getter} signal.`);
369
+ lines.push(`type ${synth.name} struct {
370
+ ${goFields.join(`
371
+ `)}
372
+ }`);
373
+ lines.push("");
374
+ }
347
375
  const nestedComponents = this.findNestedComponents(ir.root);
348
376
  const propTypeOverrides = this.buildPropTypeOverrides(ir);
349
377
  const spreadSlots = this.collectSpreadSlots(ir.root);
@@ -365,35 +393,114 @@ ${scriptRegistrations}${templateBody}
365
393
  `);
366
394
  }
367
395
  typeDefinitionToGo(td) {
368
- const def = td.definition;
369
- if (def.match(/^type \w+ = ('[^']*'(\s*\|\s*'[^']*')*)/)) {
396
+ if (td.definition.match(/^type \w+ = ('[^']*'(\s*\|\s*'[^']*')*)/)) {
370
397
  return `// ${td.name} is a string type.
371
398
  type ${td.name} = string`;
372
399
  }
373
- const bodyMatch = def.match(/(?:type \w+ = |interface \w+ )\{([\s\S]*)\}/);
374
- if (!bodyMatch)
375
- return null;
376
- const body = bodyMatch[1];
377
- const goFields = [];
378
- const fieldEntries = body.split(/[;\n]/).map((s) => s.trim()).filter(Boolean);
379
- for (const entry of fieldEntries) {
380
- const fieldMatch = entry.match(/^(\w+)\??\s*:\s*(.+)$/);
381
- if (!fieldMatch)
382
- continue;
383
- const [, fieldName, tsType] = fieldMatch;
384
- const goFieldName = this.capitalizeFieldName(fieldName);
385
- const goType = this.tsTypeStringToGo(tsType.trim());
386
- const jsonTag = this.toJsonTag(fieldName);
387
- goFields.push(` ${goFieldName} ${goType} \`json:"${jsonTag}"\``);
388
- }
389
- if (goFields.length === 0)
400
+ const fields = this.structFieldsFor(td);
401
+ if (fields.length === 0)
390
402
  return null;
403
+ const goFields = fields.map((f) => ` ${f.goName} ${f.goType} \`json:"${this.toJsonTag(f.tsName)}"\``);
391
404
  return `// ${td.name} represents a ${td.name.toLowerCase()}.
392
405
  type ${td.name} struct {
393
406
  ${goFields.join(`
394
407
  `)}
395
408
  }`;
396
409
  }
410
+ structFieldsFor(td) {
411
+ const fields = [];
412
+ for (const prop of td.properties ?? []) {
413
+ if (!GoTemplateAdapter.GO_IDENTIFIER.test(prop.name))
414
+ continue;
415
+ fields.push({
416
+ tsName: prop.name,
417
+ goName: this.capitalizeFieldName(prop.name),
418
+ goType: this.typeInfoToGo(prop.type)
419
+ });
420
+ }
421
+ return fields;
422
+ }
423
+ synthesizeStructFromSignal(signal, componentName) {
424
+ if (signal.type.kind !== "array")
425
+ return null;
426
+ const elem = signal.type.elementType;
427
+ if (elem && elem.kind !== "unknown")
428
+ return null;
429
+ const node = this.parseLiteralExpression(signal.initialValue);
430
+ if (!node || !ts.isArrayLiteralExpression(node) || node.elements.length === 0)
431
+ return null;
432
+ const order = [];
433
+ const goTypes = new Map;
434
+ for (let i = 0;i < node.elements.length; i++) {
435
+ const el = node.elements[i];
436
+ if (!ts.isObjectLiteralExpression(el))
437
+ return null;
438
+ const seen = new Set;
439
+ for (const prop of el.properties) {
440
+ if (!ts.isPropertyAssignment(prop))
441
+ return null;
442
+ if (!ts.isIdentifier(prop.name) && !ts.isStringLiteral(prop.name) && !ts.isNumericLiteral(prop.name)) {
443
+ return null;
444
+ }
445
+ const key = prop.name.text;
446
+ if (!GoTemplateAdapter.GO_IDENTIFIER.test(key))
447
+ return null;
448
+ const goType = this.scalarLiteralGoType(prop.initializer);
449
+ if (!goType)
450
+ return null;
451
+ seen.add(key);
452
+ const prev = goTypes.get(key);
453
+ if (prev === undefined) {
454
+ if (i !== 0)
455
+ return null;
456
+ order.push(key);
457
+ goTypes.set(key, goType);
458
+ } else {
459
+ const merged = this.mergeScalarGoType(prev, goType);
460
+ if (!merged)
461
+ return null;
462
+ goTypes.set(key, merged);
463
+ }
464
+ }
465
+ if (seen.size !== order.length)
466
+ return null;
467
+ }
468
+ const name = `${componentName}${this.capitalizeFieldName(signal.getter)}Item`;
469
+ if (this.localTypeNames.has(name))
470
+ return null;
471
+ return {
472
+ name,
473
+ fields: order.map((key) => ({
474
+ tsName: key,
475
+ goName: this.capitalizeFieldName(key),
476
+ goType: goTypes.get(key)
477
+ }))
478
+ };
479
+ }
480
+ scalarLiteralGoType(node) {
481
+ if (ts.isPrefixUnaryExpression(node) && node.operator === ts.SyntaxKind.MinusToken && ts.isNumericLiteral(node.operand)) {
482
+ return this.numericLiteralGoType(node.operand.text);
483
+ }
484
+ if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
485
+ return "string";
486
+ if (ts.isNumericLiteral(node))
487
+ return this.numericLiteralGoType(node.text);
488
+ if (node.kind === ts.SyntaxKind.TrueKeyword || node.kind === ts.SyntaxKind.FalseKeyword) {
489
+ return "bool";
490
+ }
491
+ return null;
492
+ }
493
+ numericLiteralGoType(text) {
494
+ return /[.eE]/.test(text) && !text.startsWith("0x") ? "float64" : "int";
495
+ }
496
+ mergeScalarGoType(a, b) {
497
+ if (a === b)
498
+ return a;
499
+ const numeric = new Set(["int", "float64"]);
500
+ if (numeric.has(a) && numeric.has(b))
501
+ return "float64";
502
+ return null;
503
+ }
397
504
  tsTypeStringToGo(tsType) {
398
505
  const t = tsType.trim();
399
506
  if (t === "number")
@@ -498,6 +605,11 @@ ${goFields.join(`
498
605
  if (propFieldNames.has(fieldName))
499
606
  continue;
500
607
  const jsonTag = this.toJsonTag(signal.getter);
608
+ const synthType = this.synthStructTypes.get(signal.getter);
609
+ if (synthType) {
610
+ lines.push(` ${fieldName} ${this.typeInfoToGo(synthType)} \`json:"${jsonTag}"\``);
611
+ continue;
612
+ }
501
613
  let goType;
502
614
  let referencedProp = propsParamMap.get(signal.initialValue);
503
615
  if (!referencedProp) {
@@ -626,7 +738,8 @@ ${goFields.join(`
626
738
  if (hoisted) {
627
739
  lines.push(` ${fieldName}: ${hoisted.varName},`);
628
740
  } else {
629
- const initialValue = this.convertInitialValue(signal.initialValue, signal.type, ir.metadata.propsParams);
741
+ const bakeType = this.synthStructTypes.get(signal.getter) ?? signal.type;
742
+ const initialValue = this.convertInitialValue(signal.initialValue, bakeType, ir.metadata.propsParams);
630
743
  lines.push(` ${fieldName}: ${initialValue},`);
631
744
  }
632
745
  }
@@ -1007,10 +1120,7 @@ ${goFields.join(`
1007
1120
  }
1008
1121
  }
1009
1122
  if (typeInfo.kind === "array") {
1010
- if (value === "[]" || value === "null" || value === "undefined") {
1011
- return "nil";
1012
- }
1013
- return "nil";
1123
+ return this.jsLiteralToGo(value, typeInfo) ?? "nil";
1014
1124
  }
1015
1125
  if (typeInfo.kind === "interface" && typeInfo.raw) {
1016
1126
  const aliasBase = this.localTypeAliases.get(typeInfo.raw);
@@ -1023,6 +1133,81 @@ ${goFields.join(`
1023
1133
  }
1024
1134
  return "nil";
1025
1135
  }
1136
+ jsLiteralToGo(value, typeInfo) {
1137
+ const expr = this.parseLiteralExpression(value);
1138
+ if (!expr)
1139
+ return null;
1140
+ return this.tsLiteralToGo(expr, typeInfo);
1141
+ }
1142
+ parseLiteralExpression(value) {
1143
+ const sf = ts.createSourceFile("__lit.ts", `(${value})`, ts.ScriptTarget.Latest, true);
1144
+ if (sf.statements.length !== 1)
1145
+ return null;
1146
+ const stmt = sf.statements[0];
1147
+ if (!ts.isExpressionStatement(stmt))
1148
+ return null;
1149
+ let expr = stmt.expression;
1150
+ while (ts.isParenthesizedExpression(expr))
1151
+ expr = expr.expression;
1152
+ return expr;
1153
+ }
1154
+ tsLiteralToGo(node, typeInfo) {
1155
+ if (ts.isPrefixUnaryExpression(node) && node.operator === ts.SyntaxKind.MinusToken && ts.isNumericLiteral(node.operand)) {
1156
+ return `-${node.operand.text}`;
1157
+ }
1158
+ if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
1159
+ return JSON.stringify(node.text);
1160
+ }
1161
+ if (ts.isNumericLiteral(node))
1162
+ return node.text;
1163
+ if (node.kind === ts.SyntaxKind.TrueKeyword)
1164
+ return "true";
1165
+ if (node.kind === ts.SyntaxKind.FalseKeyword)
1166
+ return "false";
1167
+ if (node.kind === ts.SyntaxKind.NullKeyword)
1168
+ return "nil";
1169
+ if (ts.isArrayLiteralExpression(node)) {
1170
+ if (node.elements.length === 0)
1171
+ return null;
1172
+ const elemType = typeInfo?.kind === "array" ? typeInfo.elementType : undefined;
1173
+ const sliceHeader = typeInfo?.kind === "array" ? this.typeInfoToGo(typeInfo) : "[]interface{}";
1174
+ const elems = [];
1175
+ for (const el of node.elements) {
1176
+ const go = this.tsLiteralToGo(el, elemType);
1177
+ if (go === null)
1178
+ return null;
1179
+ elems.push(go);
1180
+ }
1181
+ return `${sliceHeader}{${elems.join(", ")}}`;
1182
+ }
1183
+ if (ts.isObjectLiteralExpression(node)) {
1184
+ const goType = typeInfo ? this.typeInfoToGo(typeInfo) : "interface{}";
1185
+ const structFields = this.localStructFields.get(goType);
1186
+ if (!structFields)
1187
+ return null;
1188
+ const entries = [];
1189
+ for (const prop of node.properties) {
1190
+ if (!ts.isPropertyAssignment(prop))
1191
+ return null;
1192
+ if (!ts.isIdentifier(prop.name) && !ts.isStringLiteral(prop.name) && !ts.isNumericLiteral(prop.name)) {
1193
+ return null;
1194
+ }
1195
+ const goField = structFields.get(prop.name.text);
1196
+ if (!goField)
1197
+ return null;
1198
+ const init = prop.initializer;
1199
+ if (ts.isObjectLiteralExpression(init) || ts.isArrayLiteralExpression(init)) {
1200
+ return null;
1201
+ }
1202
+ const go = this.tsLiteralToGo(init);
1203
+ if (go === null)
1204
+ return null;
1205
+ entries.push(`${goField}: ${go}`);
1206
+ }
1207
+ return `${goType}{${entries.join(", ")}}`;
1208
+ }
1209
+ return null;
1210
+ }
1026
1211
  typeInfoToGo(typeInfo, defaultValue) {
1027
1212
  switch (typeInfo.kind) {
1028
1213
  case "primitive":
@@ -1335,6 +1520,7 @@ ${goFields.join(`
1335
1520
  }
1336
1521
  return null;
1337
1522
  }
1523
+ static GO_IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;
1338
1524
  static GO_INITIALISMS = new Set([
1339
1525
  "id",
1340
1526
  "url",
@@ -1541,9 +1727,23 @@ ${goFields.join(`
1541
1727
  const currentLoopParam = this.loopParamStack[this.loopParamStack.length - 1];
1542
1728
  if (currentLoopParam && name === currentLoopParam)
1543
1729
  return ".";
1730
+ if (this.isOuterLoopParam(name))
1731
+ return `$${name}`;
1544
1732
  if (this.loopVarRefCount.has(name))
1545
1733
  return `$${name}`;
1546
- return `.${this.capitalizeFieldName(name)}`;
1734
+ return this.rootFieldRef(name);
1735
+ }
1736
+ isOuterLoopParam(name) {
1737
+ const top = this.loopParamStack.length - 1;
1738
+ for (let i = 0;i < top; i++) {
1739
+ if (this.loopParamStack[i] === name)
1740
+ return true;
1741
+ }
1742
+ return false;
1743
+ }
1744
+ rootFieldRef(name) {
1745
+ const prefix = this.loopParamStack.length > 0 ? "$." : ".";
1746
+ return `${prefix}${this.capitalizeFieldName(name)}`;
1547
1747
  }
1548
1748
  literal(value, literalType) {
1549
1749
  if (literalType === "string")
@@ -1554,7 +1754,7 @@ ${goFields.join(`
1554
1754
  }
1555
1755
  call(callee, args, emit) {
1556
1756
  if (callee.kind === "identifier" && args.length === 0) {
1557
- return `.${this.capitalizeFieldName(callee.name)}`;
1757
+ return this.rootFieldRef(callee.name);
1558
1758
  }
1559
1759
  const path = identifierPath(callee);
1560
1760
  if (path && this.templatePrimitives[path]) {
@@ -1595,7 +1795,7 @@ ${goFields.join(`
1595
1795
  return templateBlock;
1596
1796
  }
1597
1797
  if (object.kind === "identifier" && this.propsObjectName && object.name === this.propsObjectName) {
1598
- return `.${this.capitalizeFieldName(property)}`;
1798
+ return this.rootFieldRef(property);
1599
1799
  }
1600
1800
  const currentLoopParam = this.loopParamStack[this.loopParamStack.length - 1];
1601
1801
  if (object.kind === "identifier" && currentLoopParam && object.name === currentLoopParam) {
@@ -2332,11 +2532,14 @@ Options:
2332
2532
  if (currentLoopParam && expr.name === currentLoopParam) {
2333
2533
  return plain(".");
2334
2534
  }
2535
+ if (this.isOuterLoopParam(expr.name)) {
2536
+ return plain(`$${expr.name}`);
2537
+ }
2335
2538
  if (this.loopVarRefCount.has(expr.name)) {
2336
2539
  return plain(`$${expr.name}`);
2337
2540
  }
2338
2541
  }
2339
- return plain(`.${this.capitalizeFieldName(expr.name)}`);
2542
+ return plain(this.rootFieldRef(expr.name));
2340
2543
  case "literal":
2341
2544
  if (expr.literalType === "string")
2342
2545
  return plain(`"${expr.value}"`);
@@ -2345,7 +2548,7 @@ Options:
2345
2548
  return plain(String(expr.value));
2346
2549
  case "call": {
2347
2550
  if (expr.callee.kind === "identifier" && expr.args.length === 0) {
2348
- return plain(`.${this.capitalizeFieldName(expr.callee.name)}`);
2551
+ return plain(this.rootFieldRef(expr.callee.name));
2349
2552
  }
2350
2553
  return plain(this.renderParsedExpr(expr));
2351
2554
  }
@@ -2356,7 +2559,7 @@ Options:
2356
2559
  return plain(result);
2357
2560
  }
2358
2561
  if (expr.object.kind === "identifier" && this.propsObjectName && expr.object.name === this.propsObjectName) {
2359
- return plain(`.${this.capitalizeFieldName(expr.property)}`);
2562
+ return plain(this.rootFieldRef(expr.property));
2360
2563
  }
2361
2564
  {
2362
2565
  const currentLoopParam = this.loopParamStack[this.loopParamStack.length - 1];