@oddo/lang 0.0.1 → 0.0.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 +127 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +127 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -2
package/dist/index.js
CHANGED
|
@@ -1849,6 +1849,7 @@ var OddoParser = class extends _chevrotain.CstParser {
|
|
|
1849
1849
|
var parser = new OddoParser();
|
|
1850
1850
|
|
|
1851
1851
|
// src/ast-converter.mjs
|
|
1852
|
+
var sourceText = "";
|
|
1852
1853
|
function getFirstChild(node, ruleName) {
|
|
1853
1854
|
var _a;
|
|
1854
1855
|
const children = (_a = node.children) == null ? void 0 : _a[ruleName];
|
|
@@ -2022,8 +2023,11 @@ function convertImportSpecifier(cst) {
|
|
|
2022
2023
|
local
|
|
2023
2024
|
};
|
|
2024
2025
|
}
|
|
2025
|
-
function convertExpression(cst) {
|
|
2026
|
+
function convertExpression(cst, source) {
|
|
2026
2027
|
if (!cst) return null;
|
|
2028
|
+
if (source !== void 0) {
|
|
2029
|
+
sourceText = source;
|
|
2030
|
+
}
|
|
2027
2031
|
const nodeName = cst.name;
|
|
2028
2032
|
if (nodeName === "assignment") return convertAssignment(cst);
|
|
2029
2033
|
if (nodeName === "conditional") return convertConditional(cst);
|
|
@@ -3282,7 +3286,7 @@ function convertJSXElement(cst) {
|
|
|
3282
3286
|
const isSelfClosing = cst.children.JSXSelfClosing && cst.children.JSXSelfClosing.length > 0;
|
|
3283
3287
|
let children = [];
|
|
3284
3288
|
if (!isSelfClosing) {
|
|
3285
|
-
children = getAllChildren(cst, "jsxChild")
|
|
3289
|
+
children = convertJSXChildren(getAllChildren(cst, "jsxChild"));
|
|
3286
3290
|
}
|
|
3287
3291
|
return {
|
|
3288
3292
|
type: "jsxElement",
|
|
@@ -3295,7 +3299,7 @@ function convertJSXElement(cst) {
|
|
|
3295
3299
|
}
|
|
3296
3300
|
function convertJSXFragment(cst) {
|
|
3297
3301
|
const childrenCST = getAllChildren(cst, "jsxChild");
|
|
3298
|
-
const children = childrenCST
|
|
3302
|
+
const children = convertJSXChildren(childrenCST);
|
|
3299
3303
|
return {
|
|
3300
3304
|
type: "jsxFragment",
|
|
3301
3305
|
children
|
|
@@ -3420,6 +3424,61 @@ function decodeHTMLEntities(text) {
|
|
|
3420
3424
|
}
|
|
3421
3425
|
});
|
|
3422
3426
|
}
|
|
3427
|
+
function convertJSXChildren(childrenCST) {
|
|
3428
|
+
if (!childrenCST || childrenCST.length === 0) return [];
|
|
3429
|
+
const childrenWithOffsets = childrenCST.map((child) => {
|
|
3430
|
+
const firstOffset = getFirstTokenOffset(child);
|
|
3431
|
+
const lastOffset = getLastTokenOffset(child);
|
|
3432
|
+
return { cst: child, firstOffset, lastOffset };
|
|
3433
|
+
}).filter((c) => c.firstOffset !== void 0);
|
|
3434
|
+
childrenWithOffsets.sort((a, b) => a.firstOffset - b.firstOffset);
|
|
3435
|
+
const result = [];
|
|
3436
|
+
for (let i = 0; i < childrenWithOffsets.length; i++) {
|
|
3437
|
+
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);
|
|
3450
|
+
if (converted !== null) {
|
|
3451
|
+
result.push(converted);
|
|
3452
|
+
}
|
|
3453
|
+
}
|
|
3454
|
+
return result;
|
|
3455
|
+
}
|
|
3456
|
+
function getLastTokenOffset(node) {
|
|
3457
|
+
if (!node) return void 0;
|
|
3458
|
+
if (node.endOffset !== void 0) {
|
|
3459
|
+
return node.endOffset;
|
|
3460
|
+
}
|
|
3461
|
+
if (node.image !== void 0 && node.startOffset !== void 0) {
|
|
3462
|
+
return node.startOffset + node.image.length - 1;
|
|
3463
|
+
}
|
|
3464
|
+
let lastOffset;
|
|
3465
|
+
if (node.children) {
|
|
3466
|
+
for (const key in node.children) {
|
|
3467
|
+
const children = node.children[key];
|
|
3468
|
+
if (Array.isArray(children)) {
|
|
3469
|
+
for (const child of children) {
|
|
3470
|
+
const childOffset = getLastTokenOffset(child);
|
|
3471
|
+
if (childOffset !== void 0) {
|
|
3472
|
+
if (lastOffset === void 0 || childOffset > lastOffset) {
|
|
3473
|
+
lastOffset = childOffset;
|
|
3474
|
+
}
|
|
3475
|
+
}
|
|
3476
|
+
}
|
|
3477
|
+
}
|
|
3478
|
+
}
|
|
3479
|
+
}
|
|
3480
|
+
return lastOffset;
|
|
3481
|
+
}
|
|
3423
3482
|
function convertJSXChild(cst) {
|
|
3424
3483
|
if (cst.children.jsxElement) {
|
|
3425
3484
|
return convertJSXElement(getFirstChild(cst, "jsxElement"));
|
|
@@ -3473,8 +3532,9 @@ function convertJSXChild(cst) {
|
|
|
3473
3532
|
}
|
|
3474
3533
|
return null;
|
|
3475
3534
|
}
|
|
3476
|
-
function convertCSTToAST(cst) {
|
|
3535
|
+
function convertCSTToAST(cst, source = "") {
|
|
3477
3536
|
if (!cst) return null;
|
|
3537
|
+
sourceText = source;
|
|
3478
3538
|
if (cst.name === "program") {
|
|
3479
3539
|
const statements = getAllChildren(cst, "statement").map(convertStatement);
|
|
3480
3540
|
return {
|
|
@@ -3491,6 +3551,9 @@ var _traverse2 = require('@babel/traverse'); var _traverse3 = _interopRequireDef
|
|
|
3491
3551
|
var _types = require('@babel/types'); var t = _interopRequireWildcard(_types);
|
|
3492
3552
|
var generate = _generator2.default.default || _generator2.default;
|
|
3493
3553
|
var traverse = _traverse3.default.default || _traverse3.default;
|
|
3554
|
+
function isValidJSIdentifier(name) {
|
|
3555
|
+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
3556
|
+
}
|
|
3494
3557
|
function extractIdentifiers(babelNode) {
|
|
3495
3558
|
const identifiers = /* @__PURE__ */ new Set();
|
|
3496
3559
|
const locals = /* @__PURE__ */ new Set();
|
|
@@ -3891,6 +3954,11 @@ var MODIFIER_TRANSFORMATIONS = {
|
|
|
3891
3954
|
const identifiers = bodyIdentifiers.filter((id) => !ownParams.has(id) && isReactive(id));
|
|
3892
3955
|
const params = identifiers.map((id) => t.identifier(id));
|
|
3893
3956
|
const deps = identifiers.map((id) => t.identifier(id));
|
|
3957
|
+
const savedScope = currentScope;
|
|
3958
|
+
currentScope = Object.create(currentScope);
|
|
3959
|
+
for (const id of identifiers) {
|
|
3960
|
+
currentScope[id] = { type: "param", reactive: false };
|
|
3961
|
+
}
|
|
3894
3962
|
let convertedBody;
|
|
3895
3963
|
if (oddoExpr.body && oddoExpr.body.type === "blockStatement") {
|
|
3896
3964
|
const statements = oddoExpr.body.body.map((stmt) => convertStatement2(stmt));
|
|
@@ -3901,6 +3969,7 @@ var MODIFIER_TRANSFORMATIONS = {
|
|
|
3901
3969
|
} else {
|
|
3902
3970
|
convertedBody = t.blockStatement([]);
|
|
3903
3971
|
}
|
|
3972
|
+
currentScope = savedScope;
|
|
3904
3973
|
const arrowFunc = t.arrowFunctionExpression(params, convertedBody);
|
|
3905
3974
|
wrapDependenciesWithCalls(arrowFunc, identifiers);
|
|
3906
3975
|
const effectCall = t.callExpression(
|
|
@@ -3949,6 +4018,37 @@ function isNonReactive(name) {
|
|
|
3949
4018
|
var _a;
|
|
3950
4019
|
return ((_a = currentScope[name]) == null ? void 0 : _a.reactive) === false;
|
|
3951
4020
|
}
|
|
4021
|
+
function extractBoundNames(pattern, names = []) {
|
|
4022
|
+
var _a, _b;
|
|
4023
|
+
if (!pattern) return names;
|
|
4024
|
+
if (pattern.type === "identifier") {
|
|
4025
|
+
names.push(pattern.name);
|
|
4026
|
+
} else if (pattern.type === "objectPattern") {
|
|
4027
|
+
for (const prop of pattern.properties || []) {
|
|
4028
|
+
if (prop.type === "property") {
|
|
4029
|
+
extractBoundNames(prop.value, names);
|
|
4030
|
+
} else if (prop.type === "restProperty") {
|
|
4031
|
+
if (((_a = prop.argument) == null ? void 0 : _a.type) === "identifier") {
|
|
4032
|
+
names.push(prop.argument.name);
|
|
4033
|
+
}
|
|
4034
|
+
}
|
|
4035
|
+
}
|
|
4036
|
+
} else if (pattern.type === "arrayPattern") {
|
|
4037
|
+
for (const element of pattern.elements || []) {
|
|
4038
|
+
if (!element) continue;
|
|
4039
|
+
if (element.type === "identifier") {
|
|
4040
|
+
names.push(element.name);
|
|
4041
|
+
} else if (element.type === "restElement") {
|
|
4042
|
+
if (((_b = element.argument) == null ? void 0 : _b.type) === "identifier") {
|
|
4043
|
+
names.push(element.argument.name);
|
|
4044
|
+
}
|
|
4045
|
+
} else {
|
|
4046
|
+
extractBoundNames(element, names);
|
|
4047
|
+
}
|
|
4048
|
+
}
|
|
4049
|
+
}
|
|
4050
|
+
return names;
|
|
4051
|
+
}
|
|
3952
4052
|
function generateUniqueId(baseName) {
|
|
3953
4053
|
let candidate = baseName;
|
|
3954
4054
|
let i = 2;
|
|
@@ -3959,7 +4059,7 @@ function generateUniqueId(baseName) {
|
|
|
3959
4059
|
return candidate;
|
|
3960
4060
|
}
|
|
3961
4061
|
function collectOddoIdentifiers(node, names = /* @__PURE__ */ new Set()) {
|
|
3962
|
-
var _a, _b, _c;
|
|
4062
|
+
var _a, _b, _c, _d;
|
|
3963
4063
|
if (!node || typeof node !== "object") return names;
|
|
3964
4064
|
if (node.type === "program") {
|
|
3965
4065
|
moduleScope = /* @__PURE__ */ Object.create(null);
|
|
@@ -3990,6 +4090,13 @@ function collectOddoIdentifiers(node, names = /* @__PURE__ */ new Set()) {
|
|
|
3990
4090
|
for (const param of node.parameters) {
|
|
3991
4091
|
if (param.name) {
|
|
3992
4092
|
declareVariable(param.name, "param");
|
|
4093
|
+
} else if (param.type === "destructuringPattern") {
|
|
4094
|
+
const boundNames = extractBoundNames(param.pattern);
|
|
4095
|
+
for (const name of boundNames) {
|
|
4096
|
+
declareVariable(name, "param");
|
|
4097
|
+
}
|
|
4098
|
+
} else if (param.type === "restElement" && ((_d = param.argument) == null ? void 0 : _d.name)) {
|
|
4099
|
+
declareVariable(param.argument.name, "param");
|
|
3993
4100
|
}
|
|
3994
4101
|
}
|
|
3995
4102
|
}
|
|
@@ -4149,9 +4256,17 @@ function convertExpressionStatement2(stmt) {
|
|
|
4149
4256
|
let leftExpr = null;
|
|
4150
4257
|
if (blockStmt.expression.type === "variableDeclaration" || blockStmt.expression.type === "assignment") {
|
|
4151
4258
|
leftExpr = convertExpression2(blockStmt.expression.left);
|
|
4152
|
-
|
|
4259
|
+
if (stmt.modifier === "mutate" || stmt.modifier === "effect") {
|
|
4260
|
+
valueExpr = blockStmt.expression.right;
|
|
4261
|
+
} else {
|
|
4262
|
+
valueExpr = convertExpression2(blockStmt.expression.right);
|
|
4263
|
+
}
|
|
4153
4264
|
} else {
|
|
4154
|
-
|
|
4265
|
+
if (stmt.modifier === "mutate" || stmt.modifier === "effect") {
|
|
4266
|
+
valueExpr = blockStmt.expression;
|
|
4267
|
+
} else {
|
|
4268
|
+
valueExpr = convertExpression2(blockStmt.expression);
|
|
4269
|
+
}
|
|
4155
4270
|
}
|
|
4156
4271
|
const { transform } = modifierTransform;
|
|
4157
4272
|
usedModifiers.add(stmt.modifier);
|
|
@@ -4616,7 +4731,7 @@ function convertObjectPattern(expr) {
|
|
|
4616
4731
|
function convertJSXChild2(child) {
|
|
4617
4732
|
if (child.type === "jsxText") {
|
|
4618
4733
|
const text = child.value;
|
|
4619
|
-
if (!text
|
|
4734
|
+
if (!text) return null;
|
|
4620
4735
|
return t.stringLiteral(text);
|
|
4621
4736
|
} else if (child.type === "jsxExpression") {
|
|
4622
4737
|
const innerExpr = convertExpression2(child.expression);
|
|
@@ -4641,7 +4756,7 @@ function convertJSXElement2(expr) {
|
|
|
4641
4756
|
if (attr.type === "jsxSpread") {
|
|
4642
4757
|
properties.push(t.spreadElement(convertExpression2(attr.expression)));
|
|
4643
4758
|
} else {
|
|
4644
|
-
const key = t.identifier(attr.name);
|
|
4759
|
+
const key = isValidJSIdentifier(attr.name) ? t.identifier(attr.name) : t.stringLiteral(attr.name);
|
|
4645
4760
|
let value;
|
|
4646
4761
|
if (attr.value === null) {
|
|
4647
4762
|
value = t.booleanLiteral(true);
|
|
@@ -4662,7 +4777,7 @@ function convertJSXElement2(expr) {
|
|
|
4662
4777
|
} else {
|
|
4663
4778
|
const properties = [];
|
|
4664
4779
|
for (const attr of expr.attributes) {
|
|
4665
|
-
const key = t.identifier(attr.name);
|
|
4780
|
+
const key = isValidJSIdentifier(attr.name) ? t.identifier(attr.name) : t.stringLiteral(attr.name);
|
|
4666
4781
|
let value;
|
|
4667
4782
|
if (attr.value === null) {
|
|
4668
4783
|
value = t.booleanLiteral(true);
|
|
@@ -4917,7 +5032,7 @@ function parseOddo(input) {
|
|
|
4917
5032
|
});
|
|
4918
5033
|
throw new Error(`Parser errors: ${JSON.stringify(errors)}`);
|
|
4919
5034
|
}
|
|
4920
|
-
const ast = convertCSTToAST(cst);
|
|
5035
|
+
const ast = convertCSTToAST(cst, input);
|
|
4921
5036
|
return ast;
|
|
4922
5037
|
}
|
|
4923
5038
|
function parseOddoExpression(input) {
|
|
@@ -4944,7 +5059,7 @@ function parseOddoExpression(input) {
|
|
|
4944
5059
|
});
|
|
4945
5060
|
throw new Error(`Parser errors: ${JSON.stringify(errors)}`);
|
|
4946
5061
|
}
|
|
4947
|
-
const ast = convertExpression(cst);
|
|
5062
|
+
const ast = convertExpression(cst, input);
|
|
4948
5063
|
return ast;
|
|
4949
5064
|
}
|
|
4950
5065
|
function compileOddoToJS(input, config = {}) {
|