@oddo/lang 0.0.8 → 0.0.10

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
@@ -3586,7 +3586,7 @@ var traverse = _traverse3.default.default || _traverse3.default;
3586
3586
  function isValidJSIdentifier(name) {
3587
3587
  return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
3588
3588
  }
3589
- function wrapDependenciesWithCalls(arrowFunc, deps) {
3589
+ function wrapDependenciesWithCalls(arrowFunc, deps, prefix = "") {
3590
3590
  const depSet = new Set(deps);
3591
3591
  const locals = /* @__PURE__ */ new Set();
3592
3592
  const tempFile = t.file(t.program([t.expressionStatement(arrowFunc)]));
@@ -3602,6 +3602,14 @@ function wrapDependenciesWithCalls(arrowFunc, deps) {
3602
3602
  const shorthandToExpand = [];
3603
3603
  traverse(tempFile, {
3604
3604
  noScope: true,
3605
+ // Skip already-processed reactive expression calls - don't modify their deps arrays
3606
+ // These have the pattern: _x(fn, deps) or _computed(fn, deps) or similar
3607
+ CallExpression(path) {
3608
+ const callee = path.node.callee;
3609
+ if (t.isIdentifier(callee) && callee.name.startsWith("_") && path.node.arguments.length === 2 && t.isArrowFunctionExpression(path.node.arguments[0]) && t.isArrayExpression(path.node.arguments[1])) {
3610
+ path.skip();
3611
+ }
3612
+ },
3605
3613
  Identifier(path) {
3606
3614
  var _a;
3607
3615
  if (t.isArrowFunctionExpression(path.parent) && path.parent.params.includes(path.node)) {
@@ -3631,9 +3639,9 @@ function wrapDependenciesWithCalls(arrowFunc, deps) {
3631
3639
  });
3632
3640
  shorthandToExpand.forEach(({ prop, name }) => {
3633
3641
  prop.shorthand = false;
3634
- prop.value = t.callExpression(t.identifier(name), []);
3642
+ prop.value = t.callExpression(t.identifier(prefix + name), []);
3635
3643
  });
3636
- toReplace.forEach((p) => p.replaceWith(t.callExpression(t.identifier(p.node.name), [])));
3644
+ toReplace.forEach((p) => p.replaceWith(t.callExpression(t.identifier(prefix + p.node.name), [])));
3637
3645
  }
3638
3646
  function getReactiveDeps(identifiers) {
3639
3647
  return identifiers.filter((id) => {
@@ -3670,7 +3678,7 @@ function createLiftedExpr(valueExpr, identifiers) {
3670
3678
  );
3671
3679
  }
3672
3680
  function createReactiveExpr(oddoExpr, valueExpr, attrExpression = false) {
3673
- const allIdentifiers = collectOddoIdentifiersOnly(oddoExpr);
3681
+ const allIdentifiers = collectOddoIdentifiersOnly(oddoExpr, /* @__PURE__ */ new Set(), true);
3674
3682
  const identifiers = allIdentifiers.filter((id) => isReactive(id));
3675
3683
  const pragma = attrExpression ? "computed" : "x";
3676
3684
  if (identifiers.length === 0) {
@@ -3685,10 +3693,10 @@ function createReactiveExpr(oddoExpr, valueExpr, attrExpression = false) {
3685
3693
  );
3686
3694
  }
3687
3695
  usedModifiers.add(pragma);
3688
- const params = identifiers.map((id) => t.identifier(id));
3696
+ const params = identifiers.map((id) => t.identifier("_" + id));
3689
3697
  const deps = identifiers.map((id) => t.identifier(id));
3690
3698
  const arrowFunc = t.arrowFunctionExpression(params, valueExpr);
3691
- wrapDependenciesWithCalls(arrowFunc, identifiers);
3699
+ wrapDependenciesWithCalls(arrowFunc, identifiers, "_");
3692
3700
  return t.callExpression(
3693
3701
  t.identifier(modifierAliases[pragma]),
3694
3702
  [arrowFunc, t.arrayExpression(deps)]
@@ -3728,7 +3736,11 @@ var MODIFIER_TRANSFORMATIONS = {
3728
3736
  transform: (oddoExpr, leftExpr) => {
3729
3737
  const allIds = collectOddoIdentifiersOnly(oddoExpr);
3730
3738
  const identifiers = allIds.filter((id) => isReactive(id));
3739
+ const savedScope = currentScope;
3740
+ currentScope = Object.create(currentScope);
3741
+ currentScope[reactiveScope] = false;
3731
3742
  const valueExpr = convertExpression2(oddoExpr);
3743
+ currentScope = savedScope;
3732
3744
  const params = identifiers.map((id) => t.identifier(id));
3733
3745
  const deps = identifiers.map((id) => t.identifier(id));
3734
3746
  const arrowFunc = t.arrowFunctionExpression(params, valueExpr);
@@ -3811,6 +3823,9 @@ var MODIFIER_TRANSFORMATIONS = {
3811
3823
  ...stateContainerNames,
3812
3824
  ...outerDepsArray.filter((id) => !mutableVariables.has(id))
3813
3825
  ]);
3826
+ const savedScopeMutate = currentScope;
3827
+ currentScope = Object.create(currentScope);
3828
+ currentScope[reactiveScope] = false;
3814
3829
  for (const assignment of assignments) {
3815
3830
  const rightBabel = convertExpression2(assignment.rightOddo);
3816
3831
  const tempFile = t.file(t.program([t.expressionStatement(rightBabel)]));
@@ -3863,6 +3878,7 @@ var MODIFIER_TRANSFORMATIONS = {
3863
3878
  );
3864
3879
  }
3865
3880
  }
3881
+ currentScope = savedScopeMutate;
3866
3882
  if (stateContainerNames.length > 0) {
3867
3883
  mutateBodyStmts.push(
3868
3884
  t.expressionStatement(
@@ -3916,6 +3932,7 @@ var MODIFIER_TRANSFORMATIONS = {
3916
3932
  const deps = identifiers.map((id) => t.identifier(id));
3917
3933
  const savedScope = currentScope;
3918
3934
  currentScope = Object.create(currentScope);
3935
+ currentScope[reactiveScope] = false;
3919
3936
  for (const id of identifiers) {
3920
3937
  currentScope[id] = { type: "param", reactive: false };
3921
3938
  }
@@ -3948,7 +3965,16 @@ var MODIFIER_TRANSFORMATIONS = {
3948
3965
  if (leftExpr && t.isIdentifier(leftExpr)) {
3949
3966
  mutableVariables.add(leftExpr.name);
3950
3967
  const identifiers = collectOddoIdentifiersOnly(oddoExpr);
3968
+ const reactiveDeps = getReactiveDeps(identifiers);
3969
+ const savedScope = currentScope;
3970
+ if (reactiveDeps.length > 0) {
3971
+ currentScope = Object.create(currentScope);
3972
+ currentScope[reactiveScope] = false;
3973
+ }
3951
3974
  const valueExpr = convertExpression2(oddoExpr);
3975
+ if (reactiveDeps.length > 0) {
3976
+ currentScope = savedScope;
3977
+ }
3952
3978
  const liftedExpr = createLiftedExpr(valueExpr, identifiers);
3953
3979
  return t.variableDeclaration("let", [
3954
3980
  t.variableDeclarator(leftExpr, liftedExpr || valueExpr)
@@ -3999,8 +4025,9 @@ var stateSetterMap = /* @__PURE__ */ new Map();
3999
4025
  var mutableVariables = /* @__PURE__ */ new Set();
4000
4026
  var moduleScope = null;
4001
4027
  var currentScope = null;
4028
+ var reactiveScope = /* @__PURE__ */ Symbol("reactive-scope");
4002
4029
  function declareVariable(name, type) {
4003
- const reactive = type === "state" || type === "computed" || type === "param" || type === "import-oddo";
4030
+ const reactive = type === "state" || type === "computed" || type === "reactive-param" || type === "import-oddo";
4004
4031
  currentScope[name] = { type, reactive };
4005
4032
  }
4006
4033
  function isDeclared(name) {
@@ -4059,6 +4086,7 @@ function collectOddoIdentifiers(node, names = /* @__PURE__ */ new Set()) {
4059
4086
  if (!node || typeof node !== "object") return names;
4060
4087
  if (node.type === "program") {
4061
4088
  moduleScope = /* @__PURE__ */ Object.create(null);
4089
+ moduleScope[reactiveScope] = true;
4062
4090
  currentScope = moduleScope;
4063
4091
  }
4064
4092
  if (node.type === "identifier") {
@@ -4301,14 +4329,23 @@ function convertExpressionStatement2(stmt) {
4301
4329
  if (stmt.expression && stmt.expression.type === "variableDeclaration") {
4302
4330
  const left = convertExpression2(stmt.expression.left);
4303
4331
  const oddoRight = stmt.expression.right;
4304
- const right = convertExpression2(oddoRight);
4305
- let finalExpr = right;
4332
+ let finalExpr;
4306
4333
  if (oddoRight.type !== "arrowFunction") {
4307
4334
  const identifiers = collectOddoIdentifiersOnly(oddoRight);
4308
- const liftedExpr = createLiftedExpr(right, identifiers);
4309
- if (liftedExpr) {
4310
- finalExpr = liftedExpr;
4335
+ const reactiveDeps = getReactiveDeps(identifiers);
4336
+ const savedScope = currentScope;
4337
+ if (reactiveDeps.length > 0) {
4338
+ currentScope = Object.create(currentScope);
4339
+ currentScope[reactiveScope] = false;
4340
+ }
4341
+ const right = convertExpression2(oddoRight);
4342
+ if (reactiveDeps.length > 0) {
4343
+ currentScope = savedScope;
4311
4344
  }
4345
+ const liftedExpr = createLiftedExpr(right, identifiers);
4346
+ finalExpr = liftedExpr || right;
4347
+ } else {
4348
+ finalExpr = convertExpression2(oddoRight);
4312
4349
  }
4313
4350
  return t.variableDeclaration("const", [
4314
4351
  t.variableDeclarator(left, finalExpr)
@@ -4457,17 +4494,21 @@ function convertArrowFunction2(expr) {
4457
4494
  if (expr._scope) {
4458
4495
  currentScope = expr._scope;
4459
4496
  }
4497
+ const inReactiveScope = (savedScope == null ? void 0 : savedScope[reactiveScope]) !== false;
4460
4498
  const bodyIdentifiers = collectOddoIdentifiersOnly(expr.body);
4461
- const reactiveDepsForBody = bodyIdentifiers.filter((id) => {
4499
+ const reactiveDepsForBody = inReactiveScope ? bodyIdentifiers.filter((id) => {
4462
4500
  var _a;
4463
4501
  const isOwnParam = (_a = expr.parameters) == null ? void 0 : _a.some((p) => p.name === id);
4464
4502
  if (isOwnParam) return false;
4465
4503
  const varInfo = savedScope == null ? void 0 : savedScope[id];
4466
4504
  return (varInfo == null ? void 0 : varInfo.reactive) === true;
4467
- });
4505
+ }) : [];
4468
4506
  for (const dep of reactiveDepsForBody) {
4469
4507
  currentScope[dep] = { type: "param", reactive: false };
4470
4508
  }
4509
+ if (reactiveDepsForBody.length > 0) {
4510
+ currentScope[reactiveScope] = false;
4511
+ }
4471
4512
  const params = expr.parameters.map((param) => {
4472
4513
  if (param.type === "restElement") {
4473
4514
  return t.restElement(convertExpression2(param.argument));
@@ -4498,7 +4539,7 @@ function convertArrowFunction2(expr) {
4498
4539
  body = null;
4499
4540
  }
4500
4541
  currentScope = savedScope;
4501
- if (reactiveDepsForBody.length > 0 || params.length > 0) {
4542
+ if (reactiveDepsForBody.length > 0 || params.length > 0 && inReactiveScope) {
4502
4543
  usedModifiers.add("liftFn");
4503
4544
  const depParams = reactiveDepsForBody.map((id) => t.identifier(id));
4504
4545
  const allParams = [...depParams, ...params];
@@ -4563,17 +4604,20 @@ function convertReactiveContainer(expr) {
4563
4604
  currentScope = savedScope;
4564
4605
  return t.functionExpression(null, params, body);
4565
4606
  }
4566
- function collectOddoIdentifiersOnly(node, names = /* @__PURE__ */ new Set()) {
4607
+ function collectOddoIdentifiersOnly(node, names = /* @__PURE__ */ new Set(), stopAtJsxExpressions = false) {
4567
4608
  if (!node || typeof node !== "object") return Array.from(names);
4609
+ if (stopAtJsxExpressions && node.type === "jsxExpression") {
4610
+ return Array.from(names);
4611
+ }
4568
4612
  if (node.type === "identifier") {
4569
4613
  names.add(node.name);
4570
4614
  }
4571
4615
  if (node.type === "property") {
4572
4616
  if (node.computed && node.key) {
4573
- collectOddoIdentifiersOnly(node.key, names);
4617
+ collectOddoIdentifiersOnly(node.key, names, stopAtJsxExpressions);
4574
4618
  }
4575
4619
  if (node.value) {
4576
- collectOddoIdentifiersOnly(node.value, names);
4620
+ collectOddoIdentifiersOnly(node.value, names, stopAtJsxExpressions);
4577
4621
  }
4578
4622
  return Array.from(names);
4579
4623
  }
@@ -4581,9 +4625,9 @@ function collectOddoIdentifiersOnly(node, names = /* @__PURE__ */ new Set()) {
4581
4625
  if (key === "type") continue;
4582
4626
  const val = node[key];
4583
4627
  if (Array.isArray(val)) {
4584
- val.forEach((item) => collectOddoIdentifiersOnly(item, names));
4628
+ val.forEach((item) => collectOddoIdentifiersOnly(item, names, stopAtJsxExpressions));
4585
4629
  } else if (val && typeof val === "object") {
4586
- collectOddoIdentifiersOnly(val, names);
4630
+ collectOddoIdentifiersOnly(val, names, stopAtJsxExpressions);
4587
4631
  }
4588
4632
  }
4589
4633
  return Array.from(names);
@@ -4821,7 +4865,11 @@ function convertJSXChild2(child) {
4821
4865
  if (!text) return null;
4822
4866
  return t.stringLiteral(text);
4823
4867
  } else if (child.type === "jsxExpression") {
4868
+ const savedScope = currentScope;
4869
+ currentScope = Object.create(currentScope);
4870
+ currentScope[reactiveScope] = false;
4824
4871
  const innerExpr = convertExpression2(child.expression);
4872
+ currentScope = savedScope;
4825
4873
  return createReactiveExpr(child.expression, innerExpr);
4826
4874
  } else if (child.type === "jsxElement") {
4827
4875
  return convertJSXElement2(child);
@@ -4838,6 +4886,9 @@ function convertJSXElement2(expr) {
4838
4886
  const hasSpread = expr.attributes.some((attr) => attr.type === "jsxSpread");
4839
4887
  let propsArg;
4840
4888
  if (hasSpread) {
4889
+ const savedScopeSpread = currentScope;
4890
+ currentScope = Object.create(currentScope);
4891
+ currentScope[reactiveScope] = false;
4841
4892
  const properties = [];
4842
4893
  const oddoExprs = [];
4843
4894
  for (const attr of expr.attributes) {
@@ -4861,6 +4912,7 @@ function convertJSXElement2(expr) {
4861
4912
  properties.push(t.objectProperty(key, value));
4862
4913
  }
4863
4914
  }
4915
+ currentScope = savedScopeSpread;
4864
4916
  const propsObj = t.objectExpression(properties);
4865
4917
  const syntheticOddo = { type: "array", elements: oddoExprs };
4866
4918
  propsArg = createReactiveExpr(syntheticOddo, propsObj, true);
@@ -4876,10 +4928,18 @@ function convertJSXElement2(expr) {
4876
4928
  } else if (attr.value.type === "string") {
4877
4929
  value = t.stringLiteral(attr.value.value);
4878
4930
  } else if (attr.value.type === "expression") {
4931
+ const savedScopeAttr = currentScope;
4932
+ currentScope = Object.create(currentScope);
4933
+ currentScope[reactiveScope] = false;
4879
4934
  const innerExpr = convertExpression2(attr.value.value);
4935
+ currentScope = savedScopeAttr;
4880
4936
  value = createReactiveExpr(attr.value.value, innerExpr, true);
4881
4937
  } else {
4938
+ const savedScopeAttr = currentScope;
4939
+ currentScope = Object.create(currentScope);
4940
+ currentScope[reactiveScope] = false;
4882
4941
  const innerExpr = convertExpression2(attr.value);
4942
+ currentScope = savedScopeAttr;
4883
4943
  value = createReactiveExpr(attr.value, innerExpr, true);
4884
4944
  }
4885
4945
  properties.push(t.objectProperty(key, value));