@barefootjs/go-template 0.35.0 → 0.35.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.
package/dist/index.js CHANGED
@@ -876,21 +876,38 @@ function lowerUrlGuard(ctx, g) {
876
876
  if (isBoolShape) {
877
877
  return ctx.convertConditionToGo(stringifyParsedExpr(g), g).condition;
878
878
  }
879
- const valueGo = wrapIfMultiToken(ctx.convertExpressionToGo(stringifyParsedExpr(g), undefined, g));
879
+ const valueGo = lowerValueOperand(ctx, g);
880
880
  return `ne ${valueGo} ""`;
881
881
  }
882
882
  function lowerTernary(ctx, test, consequent, alternate) {
883
883
  const t = lowerTernaryTest(ctx, test);
884
- return `(bf_ternary ${t} ${lowerTernaryOperand(ctx, consequent)} ${lowerTernaryOperand(ctx, alternate)})`;
884
+ return `(bf_ternary ${t} ${lowerValueOperand(ctx, consequent)} ${lowerValueOperand(ctx, alternate)})`;
885
885
  }
886
- function lowerTernaryOperand(ctx, n) {
886
+ function lowerTemplateLiteralValue(ctx, parts) {
887
+ const terms = [];
888
+ for (const part of parts) {
889
+ if (part.type === "string") {
890
+ if (part.value !== "")
891
+ terms.push(`"${escapeGoString(part.value)}"`);
892
+ } else {
893
+ terms.push(lowerValueOperand(ctx, part.expr));
894
+ }
895
+ }
896
+ if (terms.length === 0)
897
+ return '""';
898
+ return terms.slice(1).reduce((acc, t) => `(bf_concat_str ${acc} ${t})`, terms[0]);
899
+ }
900
+ function lowerValueOperand(ctx, n) {
887
901
  if (n.kind === "conditional") {
888
902
  return lowerTernary(ctx, n.test, n.consequent, n.alternate);
889
903
  }
904
+ if (n.kind === "template-literal") {
905
+ return wrapIfMultiToken(lowerTemplateLiteralValue(ctx, n.parts));
906
+ }
890
907
  return wrapIfMultiToken(ctx.convertExpressionToGo(stringifyParsedExpr(n), undefined, n));
891
908
  }
892
909
  function lowerTernaryTest(ctx, test) {
893
- const go = wrapIfMultiToken(ctx.convertExpressionToGo(stringifyParsedExpr(test), undefined, test));
910
+ const go = lowerValueOperand(ctx, test);
894
911
  const isBoolShape = test.kind === "binary" && BOOL_COMPARISON_OPS.has(test.op) || test.kind === "unary" && test.op === "!" || test.kind === "literal" && test.literalType === "boolean";
895
912
  return isBoolShape ? go : `(bf_truthy ${go})`;
896
913
  }
@@ -951,18 +968,16 @@ function renderLoweringNode(ctx, node) {
951
968
  const helper = goHelperName(node.helper);
952
969
  if (!helper)
953
970
  return null;
954
- const lowerExpr = (n) => ctx.convertExpressionToGo(stringifyParsedExpr(n), undefined, n);
955
- const lowerArg = (n) => n.kind === "conditional" ? lowerTernary(ctx, n.test, n.consequent, n.alternate) : wrapIfMultiToken(lowerExpr(n));
956
971
  if (node.kind === "helper-call") {
957
- const args = node.args.map((a) => lowerArg(a));
972
+ const args = node.args.map((a) => lowerValueOperand(ctx, a));
958
973
  return [helper, ...args].join(" ");
959
974
  }
960
- const parts = [wrapIfMultiToken(lowerExpr(node.base))];
975
+ const parts = [lowerValueOperand(ctx, node.base)];
961
976
  for (const t of node.triples) {
962
977
  const includeGo = t.guard === null ? "true" : lowerUrlGuard(ctx, t.guard);
963
978
  parts.push(`(${includeGo})`);
964
979
  parts.push(JSON.stringify(t.key));
965
- parts.push(wrapIfMultiToken(lowerExpr(t.value)));
980
+ parts.push(lowerValueOperand(ctx, t.value));
966
981
  }
967
982
  return `${helper} ${parts.join(" ")}`;
968
983
  }
@@ -1210,12 +1225,9 @@ function convertInitialValue(ctx, value, _typeInfo, propsParams, preParsed) {
1210
1225
  const inlinedStr = ctx.resolveModuleStringConst(value);
1211
1226
  if (inlinedStr !== null)
1212
1227
  return inlinedStr;
1213
- const inlinedNum = ctx.resolveModuleNumericConst(value);
1214
- if (inlinedNum !== null)
1215
- return inlinedNum;
1216
- const inlinedBool = ctx.resolveModuleBooleanConst(value);
1217
- if (inlinedBool !== null)
1218
- return inlinedBool;
1228
+ const inlinedConst = ctx.resolveModuleConstAsGo(value, { kind: "go-source", bakeType: typeInfo });
1229
+ if (inlinedConst !== null)
1230
+ return inlinedConst;
1219
1231
  }
1220
1232
  const propName = ctx.extractPropNameFromInitialValue(value, preParsed);
1221
1233
  const param = propName ? propsParams?.find((p) => p.name === propName) : undefined;
@@ -2937,8 +2949,7 @@ class GoTemplateAdapter extends BaseAdapter {
2937
2949
  extractPropFallback: (initialValue, preParsed) => this.extractPropFallback(initialValue, preParsed),
2938
2950
  extractCollisionDerivation: (parsed) => this.extractCollisionDerivation(parsed),
2939
2951
  resolveModuleStringConst: (name) => this.resolveModuleStringConst(name),
2940
- resolveModuleNumericConst: (name) => this.resolveModuleNumericConst(name),
2941
- resolveModuleBooleanConst: (name) => this.resolveModuleBooleanConst(name)
2952
+ resolveModuleConstAsGo: (name, target) => this.resolveModuleConstAsGo(name, target)
2942
2953
  };
2943
2954
  get errors() {
2944
2955
  return this.state.errors;
@@ -5136,9 +5147,9 @@ ${goFields.join(`
5136
5147
  const inlined = this.resolveModuleStringConst(name);
5137
5148
  if (inlined !== null)
5138
5149
  return inlined;
5139
- const inlinedNum = this.resolveModuleNumericConst(name);
5140
- if (inlinedNum !== null)
5141
- return inlinedNum;
5150
+ const inlinedScalar = this.resolveModuleConstAsGo(name, { kind: "template-action" });
5151
+ if (inlinedScalar !== null)
5152
+ return inlinedScalar;
5142
5153
  if (this.isCurrentLoopItem(name))
5143
5154
  return ".";
5144
5155
  if (this.isOuterLoopParam(name))
@@ -5215,11 +5226,18 @@ ${goFields.join(`
5215
5226
  return hit !== null && hit.depth > 0 && hit.binding.source === "item";
5216
5227
  }
5217
5228
  rootFieldRef(name) {
5218
- const resolved = this.state.getterAliases.get(name) ?? this.state.propDestructureAliases.get(name) ?? name;
5229
+ const resolved = this.resolveRootFieldAlias(name);
5219
5230
  this.state.templateReadRootFields.add(resolved);
5220
5231
  const prefix = this.inLoop ? "$." : ".";
5221
5232
  return `${prefix}${capitalizeFieldName(resolved)}`;
5222
5233
  }
5234
+ resolveRootFieldAlias(name) {
5235
+ return this.state.getterAliases.get(name) ?? this.state.propDestructureAliases.get(name) ?? name;
5236
+ }
5237
+ filterRootFieldRef(name) {
5238
+ this.rootFieldRef(name);
5239
+ return `$.${capitalizeFieldName(this.resolveRootFieldAlias(name))}`;
5240
+ }
5223
5241
  searchParamsFieldRef(name) {
5224
5242
  return this.state.searchParamsLocals.has(name) ? this.rootFieldRef("searchParams") : null;
5225
5243
  }
@@ -5241,20 +5259,7 @@ ${goFields.join(`
5241
5259
  findModuleConst(name) {
5242
5260
  return this.state.localConstants.find((k) => k.name === name && k.isModule && !k.containsArrow);
5243
5261
  }
5244
- resolveModuleNumericConst(name) {
5245
- if (this.isCurrentLoopItem(name))
5246
- return null;
5247
- if (this.loopVarRefCount.has(name))
5248
- return null;
5249
- if (this.isOuterLoopParam(name))
5250
- return null;
5251
- const c = this.findModuleConst(name);
5252
- if (!c || c.value === undefined)
5253
- return null;
5254
- const v = c.value.trim().replace(/(?<=\d)_(?=\d)/g, "");
5255
- return /^-?\d+(\.\d+)?$/.test(v) ? v : null;
5256
- }
5257
- resolveModuleBooleanConst(name) {
5262
+ resolveModuleConstAsGo(name, target) {
5258
5263
  if (this.isCurrentLoopItem(name))
5259
5264
  return null;
5260
5265
  if (this.loopVarRefCount.has(name))
@@ -5262,10 +5267,13 @@ ${goFields.join(`
5262
5267
  if (this.isOuterLoopParam(name))
5263
5268
  return null;
5264
5269
  const c = this.findModuleConst(name);
5265
- if (!c || c.value === undefined)
5270
+ if (!c?.parsed)
5266
5271
  return null;
5267
- const v = c.value.trim();
5268
- return v === "true" || v === "false" ? v : null;
5272
+ if (target.kind === "template-action") {
5273
+ return c.parsed.kind === "literal" || c.parsed.kind === "unary" ? parsedLiteralToGo(this.emitCtx, c.parsed) : null;
5274
+ }
5275
+ const bakeType = target.bakeType.kind !== "unknown" ? target.bakeType : c.type ?? undefined;
5276
+ return parsedLiteralToGo(this.emitCtx, c.parsed, bakeType);
5269
5277
  }
5270
5278
  literal(value, literalType) {
5271
5279
  if (literalType === "string")
@@ -5354,11 +5362,17 @@ ${goFields.join(`
5354
5362
  return `${obj}.${goFieldNameForKey(property)}`;
5355
5363
  }
5356
5364
  indexAccess(object, index, emit) {
5357
- return `bf_get ${wrapIfMultiToken(emit(object))} ${wrapIfMultiToken(emit(index))}`;
5365
+ return `bf_get ${wrapIfMultiToken(this.emitOperand(object, emit))} ${wrapIfMultiToken(this.emitOperand(index, emit))}`;
5366
+ }
5367
+ emitOperand(n, emit) {
5368
+ if (n.kind === "template-literal") {
5369
+ return wrapIfMultiToken(lowerTemplateLiteralValue(this.emitCtx, n.parts));
5370
+ }
5371
+ return emit(n);
5358
5372
  }
5359
5373
  binary(op, left, right, emit) {
5360
- const l = emit(left);
5361
- const r = emit(right);
5374
+ const l = this.emitOperand(left, emit);
5375
+ const r = this.emitOperand(right, emit);
5362
5376
  const wl = wrapIfMultiToken(l);
5363
5377
  const wr = wrapIfMultiToken(r);
5364
5378
  switch (op) {
@@ -5404,7 +5418,7 @@ ${goFields.join(`
5404
5418
  return this.state.stringValueNames.has(name);
5405
5419
  }
5406
5420
  unary(op, argument, emit) {
5407
- const arg = emit(argument);
5421
+ const arg = this.emitOperand(argument, emit);
5408
5422
  if (op === "!")
5409
5423
  return `not ${wrapIfMultiToken(arg)}`;
5410
5424
  if (op === "-")
@@ -5412,8 +5426,8 @@ ${goFields.join(`
5412
5426
  return arg;
5413
5427
  }
5414
5428
  logical(op, left, right, emit) {
5415
- const wrapLeft = wrapIfMultiToken(emit(left));
5416
- const wrapRight = wrapIfMultiToken(emit(right));
5429
+ const wrapLeft = wrapIfMultiToken(this.emitOperand(left, emit));
5430
+ const wrapRight = wrapIfMultiToken(this.emitOperand(right, emit));
5417
5431
  if (op === "&&")
5418
5432
  return `and ${wrapLeft} ${wrapRight}`;
5419
5433
  if (op === "??" && this.nillablePropNameOf(left) !== null) {
@@ -5578,7 +5592,8 @@ ${goFields.join(`
5578
5592
  }
5579
5593
  return this.pushCallbackBF101(method, true);
5580
5594
  }
5581
- arrayMethod(method, object, args, emit) {
5595
+ arrayMethod(method, object, args, rawEmit) {
5596
+ const emit = (e) => this.emitOperand(e, rawEmit);
5582
5597
  switch (method) {
5583
5598
  case "join": {
5584
5599
  const obj = emit(object);
@@ -5907,11 +5922,9 @@ ${goFields.join(`
5907
5922
  }
5908
5923
  const signal = localVarMap.get(expr.name);
5909
5924
  if (signal) {
5910
- this.rootFieldRef(signal);
5911
- return `$.${capitalizeFieldName(signal)}`;
5925
+ return this.filterRootFieldRef(signal);
5912
5926
  }
5913
- this.rootFieldRef(expr.name);
5914
- return `.${capitalizeFieldName(expr.name)}`;
5927
+ return this.filterRootFieldRef(expr.name);
5915
5928
  }
5916
5929
  case "literal":
5917
5930
  if (expr.literalType === "string") {
@@ -5925,6 +5938,9 @@ ${goFields.join(`
5925
5938
  if (expr.object.kind === "identifier" && expr.object.name === param) {
5926
5939
  return `${paramPrefix}.${capitalizeFieldName(expr.property)}`;
5927
5940
  }
5941
+ if (expr.object.kind === "identifier" && this.state.propsObjectName && expr.object.name === this.state.propsObjectName) {
5942
+ return this.filterRootFieldRef(expr.property);
5943
+ }
5928
5944
  if (expr.property === "length") {
5929
5945
  const innerHO = this.higherOrderShapeOf(expr.object);
5930
5946
  if (innerHO && innerHO.method === "filter") {
@@ -5943,8 +5959,7 @@ ${goFields.join(`
5943
5959
  return `${paramPrefix}.${capitalizeFieldName(expr.callee.property)}`;
5944
5960
  }
5945
5961
  if (expr.callee.kind === "identifier" && expr.args.length === 0) {
5946
- this.rootFieldRef(expr.callee.name);
5947
- return `$.${capitalizeFieldName(expr.callee.name)}`;
5962
+ return this.filterRootFieldRef(expr.callee.name);
5948
5963
  }
5949
5964
  if (asCallbackMethodCall4(expr) !== null) {
5950
5965
  return this.refuseFilterExprNode(expr);
@@ -5974,29 +5989,33 @@ ${goFields.join(`
5974
5989
  const right = this.renderFilterExpr(expr.right, param, localVarMap, datumField);
5975
5990
  if (this.filterExprUnsupported)
5976
5991
  return "false";
5992
+ const wl = wrapIfMultiToken(left);
5993
+ const wr = wrapIfMultiToken(right);
5977
5994
  switch (expr.op) {
5978
5995
  case "===":
5979
5996
  case "==":
5980
- return `eq ${left} ${right}`;
5997
+ return `eq ${wl} ${wr}`;
5981
5998
  case "!==":
5982
5999
  case "!=":
5983
- return `ne ${left} ${right}`;
6000
+ return `ne ${wl} ${wr}`;
5984
6001
  case ">":
5985
- return `gt ${left} ${right}`;
6002
+ return `gt ${wl} ${wr}`;
5986
6003
  case "<":
5987
- return `lt ${left} ${right}`;
6004
+ return `lt ${wl} ${wr}`;
5988
6005
  case ">=":
5989
- return `ge ${left} ${right}`;
6006
+ return `ge ${wl} ${wr}`;
5990
6007
  case "<=":
5991
- return `le ${left} ${right}`;
6008
+ return `le ${wl} ${wr}`;
5992
6009
  case "+":
5993
- return this._emitPlus(expr.left, expr.right, left, right);
6010
+ return this._emitPlus(expr.left, expr.right, wl, wr);
5994
6011
  case "-":
5995
- return `bf_sub ${left} ${right}`;
6012
+ return `bf_sub ${wl} ${wr}`;
5996
6013
  case "*":
5997
- return `bf_mul ${left} ${right}`;
6014
+ return `bf_mul ${wl} ${wr}`;
5998
6015
  case "/":
5999
- return `bf_div ${left} ${right}`;
6016
+ return `bf_div ${wl} ${wr}`;
6017
+ case "%":
6018
+ return `bf_mod ${wl} ${wr}`;
6000
6019
  default:
6001
6020
  return `${left} ${expr.op} ${right}`;
6002
6021
  }
@@ -6168,11 +6187,9 @@ ${goFields.join(`
6168
6187
  }
6169
6188
  continue;
6170
6189
  }
6171
- const singlePartTemplateLiteral = exprOut.parsed?.kind === "template-literal" && exprOut.parsed.parts.length === 1 && exprOut.parsed.parts[0].type !== "string" && go.startsWith("{{") && go.endsWith("}}");
6172
- if (singlePartTemplateLiteral) {
6173
- go = go.slice(2, -2);
6174
- }
6175
- if (!singlePartTemplateLiteral && this.isTemplateFragment(go, exprOut.parsed?.kind)) {
6190
+ if (exprOut.parsed?.kind === "template-literal" || exprOut.parsed?.kind === "conditional") {
6191
+ go = lowerValueOperand(this.emitCtx, exprOut.parsed);
6192
+ } else if (this.isTemplateFragment(go, exprOut.parsed?.kind)) {
6176
6193
  this.state.errors.push({
6177
6194
  code: "BF101",
6178
6195
  severity: "error",
@@ -6449,6 +6466,9 @@ ${goFields.join(`
6449
6466
  case "/":
6450
6467
  result = `bf_div ${left} ${right}`;
6451
6468
  break;
6469
+ case "%":
6470
+ result = `bf_mod ${left} ${right}`;
6471
+ break;
6452
6472
  default:
6453
6473
  result = `${left} ${expr.op} ${right}`;
6454
6474
  }
@@ -6475,7 +6495,7 @@ ${goFields.join(`
6475
6495
  return plain(lowerTernary(this.emitCtx, expr.test, expr.consequent, expr.alternate));
6476
6496
  }
6477
6497
  case "template-literal":
6478
- return plain(this.renderParsedExpr(expr));
6498
+ return plain(lowerTemplateLiteralValue(this.emitCtx, expr.parts));
6479
6499
  case "arrow":
6480
6500
  return plain("[ARROW-FN]");
6481
6501
  case "regex":
@@ -7076,8 +7096,8 @@ var conformancePins = {
7076
7096
  "filter-nested-find-predicate": [{ code: "BF101", severity: "error", issue: "https://github.com/piconic-ai/barefootjs/issues/2320" }],
7077
7097
  "date-method-uncatalogued": [{ code: "BF021", severity: "error", issue: "https://github.com/piconic-ai/barefootjs/issues/2356" }],
7078
7098
  "rich-prop-client-read": [{ code: "BF049", severity: "error", issue: "https://github.com/piconic-ai/barefootjs/issues/2648" }],
7079
- "jsx-element-prop-ternary": [{ code: "BF021", severity: "error", issue: "https://github.com/piconic-ai/barefootjs/issues/2667" }],
7080
- "jsx-element-prop-array": [{ code: "BF021", severity: "error", issue: "https://github.com/piconic-ai/barefootjs/issues/2667" }],
7099
+ "jsx-element-prop-ternary": [{ code: "BF021", severity: "error" }],
7100
+ "jsx-element-prop-array": [{ code: "BF021", severity: "error" }],
7081
7101
  "jsx-element-prop-rest-bag-dynamic": [{
7082
7102
  code: "BF101",
7083
7103
  severity: "error",
@@ -7089,7 +7109,7 @@ var conformancePins = {
7089
7109
  severity: "error",
7090
7110
  issue: "https://github.com/piconic-ai/barefootjs/issues/2700"
7091
7111
  }],
7092
- "namespace-import-primitive": [{ code: "BF013", severity: "error", issue: "https://github.com/piconic-ai/barefootjs/issues/2771" }]
7112
+ "namespace-import-primitive": [{ code: "BF013", severity: "error" }]
7093
7113
  };
7094
7114
  // src/render-divergences.ts
7095
7115
  var renderDivergences = {};