@oddo/lang 0.0.2 → 0.0.4

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
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/lexer.mjs
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/lexer.mjs
2
2
  var _chevrotain = require('chevrotain');
3
3
  var Identifier = _chevrotain.createToken.call(void 0, {
4
4
  name: "Identifier",
@@ -2633,8 +2633,10 @@ function convertPostfix(cst) {
2633
2633
  return operand;
2634
2634
  }
2635
2635
  function convertFunctionCall(cst) {
2636
+ var _a;
2636
2637
  let callee = convertExpression(getFirstChild(cst, "memberAccess"));
2637
2638
  const leftParens = cst.children.LeftParen || [];
2639
+ const rightParens = cst.children.RightParen || [];
2638
2640
  const questionDots = cst.children.QuestionDot || [];
2639
2641
  const templateLiterals = cst.children.TemplateLiteral || [];
2640
2642
  if (templateLiterals.length > 0 && leftParens.length === 0) {
@@ -2652,11 +2654,16 @@ function convertFunctionCall(cst) {
2652
2654
  const argLists = getAllChildren(cst, "argumentList");
2653
2655
  const allExprs = getAllChildren(cst, "expression");
2654
2656
  const ops = [
2655
- ...leftParens.map((t2, i) => ({ k: "c", off: t2.startOffset, i, token: t2 })),
2657
+ ...leftParens.map((t2, i) => ({
2658
+ k: "c",
2659
+ off: t2.startOffset,
2660
+ token: t2,
2661
+ rightParen: rightParens[i]
2662
+ })),
2656
2663
  ...dots.map((t2, i) => ({ k: "d", off: t2.startOffset, i })),
2657
2664
  ...brackets.map((t2, i) => {
2658
- var _a;
2659
- return { k: "b", off: t2.startOffset, i, end: (_a = rightBrackets[i]) == null ? void 0 : _a.endOffset };
2665
+ var _a2;
2666
+ return { k: "b", off: t2.startOffset, i, end: (_a2 = rightBrackets[i]) == null ? void 0 : _a2.endOffset };
2660
2667
  }),
2661
2668
  ...questionDots.filter((q) => !leftParens.some((p) => Math.abs(p.startOffset - q.endOffset) <= 1)).map((t2, i) => ({ k: "q", off: t2.startOffset, i }))
2662
2669
  ].sort((a, b) => a.off - b.off);
@@ -2664,7 +2671,12 @@ function convertFunctionCall(cst) {
2664
2671
  for (const op of ops) {
2665
2672
  if (op.k === "c") {
2666
2673
  const isOptional = questionDots.some((q) => q.endOffset < op.token.startOffset && op.token.startOffset - q.endOffset <= 1);
2667
- const argsCST = argLists[op.i];
2674
+ const leftOff = op.token.startOffset;
2675
+ const rightOff = (_a = op.rightParen) == null ? void 0 : _a.endOffset;
2676
+ const argsCST = argLists.find((al) => {
2677
+ const alStart = getFirstTokenOffset(al);
2678
+ return alStart !== void 0 && alStart > leftOff && (rightOff === void 0 || alStart < rightOff);
2679
+ });
2668
2680
  const args = [];
2669
2681
  if (argsCST) {
2670
2682
  const exprs = getAllChildren(argsCST, "expression");
@@ -2684,9 +2696,9 @@ function convertFunctionCall(cst) {
2684
2696
  if (nextBracket) {
2685
2697
  const bIdx = brackets.indexOf(nextBracket);
2686
2698
  const expr = allExprs.find((e) => {
2687
- var _a;
2699
+ var _a2;
2688
2700
  const o = getFirstTokenOffset(e);
2689
- return o > nextBracket.startOffset && o < ((_a = rightBrackets[bIdx]) == null ? void 0 : _a.endOffset);
2701
+ return o > nextBracket.startOffset && o < ((_a2 = rightBrackets[bIdx]) == null ? void 0 : _a2.endOffset);
2690
2702
  });
2691
2703
  if (expr) callee = { type: "memberAccess", object: callee, property: convertExpression(expr), computed: true, optional: true };
2692
2704
  ops.splice(ops.findIndex((o) => o.k === "b" && o.i === bIdx), 1);
@@ -2709,6 +2721,7 @@ function convertMemberAccess(cst) {
2709
2721
  const dots = cst.children.Dot || [];
2710
2722
  const questionDots = cst.children.QuestionDot || [];
2711
2723
  const brackets = cst.children.LeftBracket || [];
2724
+ const rightBrackets = cst.children.RightBracket || [];
2712
2725
  const dotDotDots = cst.children.DotDotDot || [];
2713
2726
  if (dots.length === 0 && questionDots.length === 0 && brackets.length === 0) {
2714
2727
  return object;
@@ -2718,85 +2731,96 @@ function convertMemberAccess(cst) {
2718
2731
  allOps.push({ type: "dot", token: dot, optional: false });
2719
2732
  }
2720
2733
  for (const qDot of questionDots) {
2721
- allOps.push({ type: "dot", token: qDot, optional: true });
2722
- }
2723
- allOps.sort((a, b) => a.token.startOffset - b.token.startOffset);
2724
- const identifiers = cst.children.Identifier || [];
2725
- let identifierIndex = 0;
2726
- for (let i = 0; i < allOps.length; i++) {
2727
- const op = allOps[i];
2728
- const identifier2 = identifiers[identifierIndex];
2729
- if (identifier2) {
2730
- identifierIndex++;
2731
- object = {
2732
- type: "memberAccess",
2733
- object,
2734
- property: identifier2.image,
2735
- computed: false,
2736
- optional: op.optional
2737
- };
2734
+ const isFollowedByBracket = brackets.some((b) => b.startOffset > qDot.endOffset && b.startOffset - qDot.endOffset <= 1);
2735
+ if (!isFollowedByBracket) {
2736
+ allOps.push({ type: "dot", token: qDot, optional: true });
2738
2737
  }
2739
2738
  }
2740
- const expressions = getAllChildren(cst, "expression");
2741
- let expressionIndex = 0;
2742
2739
  for (let i = 0; i < brackets.length; i++) {
2743
2740
  const bracket = brackets[i];
2744
- const bracketStart = bracket.startOffset;
2745
- const isOptional = questionDots.some((qDot) => {
2746
- return qDot.endOffset < bracketStart && bracketStart - qDot.endOffset <= 1;
2747
- });
2748
- const rightBrackets = cst.children.RightBracket || [];
2749
2741
  const rightBracket = rightBrackets[i];
2750
2742
  if (!rightBracket) continue;
2743
+ const isOptional = questionDots.some(
2744
+ (qDot) => qDot.endOffset < bracket.startOffset && bracket.startOffset - qDot.endOffset <= 1
2745
+ );
2751
2746
  const dotDotDotsInRange = dotDotDots.filter(
2752
- (ddd) => ddd.startOffset > bracketStart && ddd.startOffset < rightBracket.startOffset
2747
+ (ddd) => ddd.startOffset > bracket.startOffset && ddd.startOffset < rightBracket.startOffset
2753
2748
  );
2754
- if (dotDotDotsInRange.length > 0) {
2755
- if (expressions.length === expressionIndex) {
2756
- object = {
2757
- type: "arraySlice",
2758
- object,
2759
- start: null,
2760
- end: null
2761
- };
2762
- continue;
2763
- }
2764
- if (expressions.length >= expressionIndex + 2) {
2765
- const startExpr = expressions[expressionIndex];
2766
- const endExpr = expressions[expressionIndex + 1];
2767
- expressionIndex += 2;
2749
+ allOps.push({
2750
+ type: "bracket",
2751
+ token: bracket,
2752
+ rightBracket,
2753
+ optional: isOptional,
2754
+ isSlice: dotDotDotsInRange.length > 0
2755
+ });
2756
+ }
2757
+ allOps.sort((a, b) => a.token.startOffset - b.token.startOffset);
2758
+ const identifiers = cst.children.Identifier || [];
2759
+ const expressions = getAllChildren(cst, "expression");
2760
+ let identifierIndex = 0;
2761
+ let expressionIndex = 0;
2762
+ for (const op of allOps) {
2763
+ if (op.type === "dot") {
2764
+ const identifier2 = identifiers[identifierIndex];
2765
+ if (identifier2) {
2766
+ identifierIndex++;
2768
2767
  object = {
2769
- type: "arraySlice",
2768
+ type: "memberAccess",
2770
2769
  object,
2771
- start: convertExpression(startExpr),
2772
- end: convertExpression(endExpr)
2770
+ property: identifier2.image,
2771
+ computed: false,
2772
+ optional: op.optional
2773
2773
  };
2774
- continue;
2775
2774
  }
2776
- if (expressions.length === expressionIndex + 1) {
2777
- const startExpr = expressions[expressionIndex];
2778
- expressionIndex++;
2779
- object = {
2780
- type: "arraySlice",
2781
- object,
2782
- start: convertExpression(startExpr),
2783
- end: null
2784
- };
2785
- continue;
2775
+ } else if (op.type === "bracket") {
2776
+ if (op.isSlice) {
2777
+ const exprsInBracket = expressions.filter((e, idx) => {
2778
+ if (idx < expressionIndex) return false;
2779
+ const exprStart = getFirstTokenOffset(e);
2780
+ return exprStart > op.token.startOffset && exprStart < op.rightBracket.startOffset;
2781
+ });
2782
+ if (exprsInBracket.length === 0) {
2783
+ object = {
2784
+ type: "arraySlice",
2785
+ object,
2786
+ start: null,
2787
+ end: null
2788
+ };
2789
+ } else if (exprsInBracket.length >= 2) {
2790
+ const startExpr = expressions[expressionIndex];
2791
+ const endExpr = expressions[expressionIndex + 1];
2792
+ expressionIndex += 2;
2793
+ object = {
2794
+ type: "arraySlice",
2795
+ object,
2796
+ start: convertExpression(startExpr),
2797
+ end: convertExpression(endExpr)
2798
+ };
2799
+ } else {
2800
+ const startExpr = expressions[expressionIndex];
2801
+ expressionIndex++;
2802
+ object = {
2803
+ type: "arraySlice",
2804
+ object,
2805
+ start: convertExpression(startExpr),
2806
+ end: null
2807
+ };
2808
+ }
2809
+ } else {
2810
+ if (expressions.length > expressionIndex) {
2811
+ const exprCst = expressions[expressionIndex];
2812
+ expressionIndex++;
2813
+ const property = convertExpression(exprCst);
2814
+ object = {
2815
+ type: "memberAccess",
2816
+ object,
2817
+ property,
2818
+ computed: true,
2819
+ optional: op.optional
2820
+ };
2821
+ }
2786
2822
  }
2787
2823
  }
2788
- if (expressions.length > expressionIndex) {
2789
- const exprCst = expressions[expressionIndex];
2790
- expressionIndex++;
2791
- const property = convertExpression(exprCst);
2792
- object = {
2793
- type: "memberAccess",
2794
- object,
2795
- property,
2796
- computed: true,
2797
- optional: isOptional
2798
- };
2799
- }
2800
2824
  }
2801
2825
  return object;
2802
2826
  }
@@ -3425,6 +3449,7 @@ function decodeHTMLEntities(text) {
3425
3449
  });
3426
3450
  }
3427
3451
  function convertJSXChildren(childrenCST) {
3452
+ var _a;
3428
3453
  if (!childrenCST || childrenCST.length === 0) return [];
3429
3454
  const childrenWithOffsets = childrenCST.map((child) => {
3430
3455
  const firstOffset = getFirstTokenOffset(child);
@@ -3435,18 +3460,9 @@ function convertJSXChildren(childrenCST) {
3435
3460
  const result = [];
3436
3461
  for (let i = 0; i < childrenWithOffsets.length; i++) {
3437
3462
  const current = childrenWithOffsets[i];
3438
- if (i > 0 && sourceText) {
3439
- const prev = childrenWithOffsets[i - 1];
3440
- const gapStart = prev.lastOffset + 1;
3441
- const gapEnd = current.firstOffset;
3442
- if (gapEnd > gapStart) {
3443
- const gap = sourceText.slice(gapStart, gapEnd);
3444
- if (gap && /\s/.test(gap)) {
3445
- result.push({ type: "jsxText", value: " " });
3446
- }
3447
- }
3448
- }
3449
- const converted = convertJSXChild(current.cst);
3463
+ const prevEnd = i > 0 ? childrenWithOffsets[i - 1].lastOffset + 1 : void 0;
3464
+ const nextStart = (_a = childrenWithOffsets[i + 1]) == null ? void 0 : _a.firstOffset;
3465
+ const converted = convertJSXChild(current.cst, prevEnd, nextStart);
3450
3466
  if (converted !== null) {
3451
3467
  result.push(converted);
3452
3468
  }
@@ -3479,7 +3495,7 @@ function getLastTokenOffset(node) {
3479
3495
  }
3480
3496
  return lastOffset;
3481
3497
  }
3482
- function convertJSXChild(cst) {
3498
+ function convertJSXChild(cst, startBoundary, endBoundary) {
3483
3499
  if (cst.children.jsxElement) {
3484
3500
  return convertJSXElement(getFirstChild(cst, "jsxElement"));
3485
3501
  }
@@ -3512,6 +3528,22 @@ function convertJSXChild(cst) {
3512
3528
  }
3513
3529
  if (allTokens2.length > 0) {
3514
3530
  allTokens2.sort((a, b) => a.offset - b.offset);
3531
+ const firstToken = allTokens2[0];
3532
+ const lastToken = allTokens2[allTokens2.length - 1];
3533
+ const lastTokenEnd = lastToken.token.endOffset !== void 0 ? lastToken.token.endOffset : lastToken.token.startOffset + lastToken.token.image.length - 1;
3534
+ if (sourceText && (startBoundary !== void 0 || endBoundary !== void 0)) {
3535
+ const textStart = _nullishCoalesce(startBoundary, () => ( firstToken.offset));
3536
+ const textEnd = _nullishCoalesce(endBoundary, () => ( lastTokenEnd + 1));
3537
+ const textValue2 = sourceText.slice(textStart, textEnd);
3538
+ if (textValue2) {
3539
+ const decodedValue2 = decodeHTMLEntities(textValue2);
3540
+ return {
3541
+ type: "jsxText",
3542
+ value: decodedValue2
3543
+ };
3544
+ }
3545
+ return null;
3546
+ }
3515
3547
  let textValue = "";
3516
3548
  for (let i = 0; i < allTokens2.length; i++) {
3517
3549
  const current = allTokens2[i];
@@ -3636,11 +3668,21 @@ function wrapDependenciesWithCalls(arrowFunc, deps) {
3636
3668
  traverse(tempFile, {
3637
3669
  noScope: true,
3638
3670
  Identifier(path) {
3671
+ var _a;
3639
3672
  if (t.isArrowFunctionExpression(path.parent) && path.parent.params.includes(path.node)) {
3640
3673
  return;
3641
3674
  }
3642
3675
  const parent = path.parent;
3643
- const isMemberProp = t.isMemberExpression(parent) && parent.property === path.node && !parent.computed;
3676
+ if (t.isObjectPattern(parent) || t.isArrayPattern(parent)) {
3677
+ return;
3678
+ }
3679
+ if (t.isVariableDeclarator(parent) && parent.id === path.node) {
3680
+ return;
3681
+ }
3682
+ if (t.isObjectProperty(parent) && t.isObjectPattern((_a = path.parentPath) == null ? void 0 : _a.parent)) {
3683
+ return;
3684
+ }
3685
+ const isMemberProp = (t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) && parent.property === path.node && !parent.computed;
3644
3686
  const isObjectKey = t.isObjectProperty(parent) && parent.key === path.node && !parent.shorthand;
3645
3687
  const isShorthand = t.isObjectProperty(parent) && parent.shorthand && parent.key === path.node;
3646
3688
  if (depSet.has(path.node.name) && !isMemberProp && !isObjectKey && !locals.has(path.node.name)) {
@@ -3693,7 +3735,8 @@ function createLiftedExpr(valueExpr, identifiers) {
3693
3735
  );
3694
3736
  }
3695
3737
  function createReactiveExpr(valueExpr, attrExpression = false) {
3696
- const identifiers = extractIdentifiers(valueExpr);
3738
+ const allIdentifiers = extractIdentifiers(valueExpr);
3739
+ const identifiers = getReactiveDeps(allIdentifiers);
3697
3740
  const pragma = attrExpression ? "computed" : "x";
3698
3741
  if (identifiers.length === 0) {
3699
3742
  if (t.isLiteral(valueExpr)) {
@@ -3747,7 +3790,8 @@ var MODIFIER_TRANSFORMATIONS = {
3747
3790
  needsImport: true,
3748
3791
  // @computed sum = x + y -> const sum = _computed((x, y) => x() + y(), [x, y])
3749
3792
  transform: (valueExpr, leftExpr) => {
3750
- const identifiers = extractIdentifiers(valueExpr);
3793
+ const allIds = extractIdentifiers(valueExpr);
3794
+ const identifiers = getReactiveDeps(allIds);
3751
3795
  const params = identifiers.map((id) => t.identifier(id));
3752
3796
  const deps = identifiers.map((id) => t.identifier(id));
3753
3797
  const arrowFunc = t.arrowFunctionExpression(params, valueExpr);
@@ -3768,7 +3812,8 @@ var MODIFIER_TRANSFORMATIONS = {
3768
3812
  needsImport: true,
3769
3813
  // @react sum = x + y -> const sum = _react((x, y) => x() + y(), [x, y])
3770
3814
  transform: (valueExpr, leftExpr) => {
3771
- const identifiers = extractIdentifiers(valueExpr);
3815
+ const allIds = extractIdentifiers(valueExpr);
3816
+ const identifiers = getReactiveDeps(allIds);
3772
3817
  const params = identifiers.map((id) => t.identifier(id));
3773
3818
  const deps = identifiers.map((id) => t.identifier(id));
3774
3819
  const arrowFunc = t.arrowFunctionExpression(params, valueExpr);
@@ -3860,7 +3905,7 @@ var MODIFIER_TRANSFORMATIONS = {
3860
3905
  noScope: true,
3861
3906
  Identifier(path) {
3862
3907
  const parent = path.parent;
3863
- const isMemberProp = t.isMemberExpression(parent) && parent.property === path.node && !parent.computed;
3908
+ const isMemberProp = (t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) && parent.property === path.node && !parent.computed;
3864
3909
  const isObjectKey = t.isObjectProperty(parent) && parent.key === path.node && !parent.shorthand;
3865
3910
  const isShorthand = t.isObjectProperty(parent) && parent.shorthand && parent.key === path.node;
3866
3911
  if (callableIds.has(path.node.name) && !isMemberProp && !isObjectKey) {
@@ -4004,7 +4049,7 @@ var mutableVariables = /* @__PURE__ */ new Set();
4004
4049
  var moduleScope = null;
4005
4050
  var currentScope = null;
4006
4051
  function declareVariable(name, type) {
4007
- const reactive = type === "state" || type === "computed" || type === "param";
4052
+ const reactive = type === "state" || type === "computed" || type === "param" || type === "import-oddo";
4008
4053
  currentScope[name] = { type, reactive };
4009
4054
  }
4010
4055
  function isDeclared(name) {
@@ -4082,6 +4127,20 @@ function collectOddoIdentifiers(node, names = /* @__PURE__ */ new Set()) {
4082
4127
  }
4083
4128
  }
4084
4129
  }
4130
+ if (node.type === "importStatement") {
4131
+ const source = node.source || "";
4132
+ const isOddoImport = source.endsWith(".oddo");
4133
+ const type = isOddoImport ? "import-oddo" : "import-js";
4134
+ if (node.defaultImport) {
4135
+ declareVariable(node.defaultImport, type);
4136
+ }
4137
+ for (const spec of node.specifiers || []) {
4138
+ const localName = spec.local || spec.imported;
4139
+ if (localName) {
4140
+ declareVariable(localName, type);
4141
+ }
4142
+ }
4143
+ }
4085
4144
  if (node.type === "arrowFunction") {
4086
4145
  const parentScope = currentScope;
4087
4146
  currentScope = Object.create(parentScope);