@oddo/lang 0.0.10 → 0.0.11
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 +74 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +74 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3706,10 +3706,29 @@ var MODIFIER_TRANSFORMATIONS = {
|
|
|
3706
3706
|
state: {
|
|
3707
3707
|
needsImport: true,
|
|
3708
3708
|
// @state x = 3 -> const [x, setX] = _state(3);
|
|
3709
|
-
|
|
3709
|
+
// @state x = reactiveVar -> const [x, setX] = _state(_lift(_reactiveVar => _reactiveVar(), [reactiveVar]));
|
|
3710
|
+
// Receives Oddo AST, extracts deps, lifts if needed
|
|
3711
|
+
transform: (oddoExpr, leftExpr) => {
|
|
3712
|
+
const allIdentifiers = collectOddoIdentifiersOnly(oddoExpr);
|
|
3713
|
+
const reactiveDeps = allIdentifiers.filter((id) => isReactive(id));
|
|
3714
|
+
const convertedExpr = convertExpression2(oddoExpr);
|
|
3715
|
+
let stateArg;
|
|
3716
|
+
if (reactiveDeps.length > 0) {
|
|
3717
|
+
usedModifiers.add("lift");
|
|
3718
|
+
const prefixedParams = reactiveDeps.map((id) => t.identifier("_" + id));
|
|
3719
|
+
const deps = reactiveDeps.map((id) => t.identifier(id));
|
|
3720
|
+
const arrowFunc = t.arrowFunctionExpression(prefixedParams, convertedExpr);
|
|
3721
|
+
wrapDependenciesWithCalls(arrowFunc, reactiveDeps, "_");
|
|
3722
|
+
stateArg = t.callExpression(
|
|
3723
|
+
t.identifier(modifierAliases["lift"]),
|
|
3724
|
+
[arrowFunc, t.arrayExpression(deps)]
|
|
3725
|
+
);
|
|
3726
|
+
} else {
|
|
3727
|
+
stateArg = convertedExpr;
|
|
3728
|
+
}
|
|
3710
3729
|
const stateCall = t.callExpression(
|
|
3711
3730
|
t.identifier(modifierAliases["state"]),
|
|
3712
|
-
[
|
|
3731
|
+
[stateArg]
|
|
3713
3732
|
);
|
|
3714
3733
|
if (leftExpr && t.isIdentifier(leftExpr)) {
|
|
3715
3734
|
const getterName = leftExpr.name;
|
|
@@ -3725,6 +3744,19 @@ var MODIFIER_TRANSFORMATIONS = {
|
|
|
3725
3744
|
stateCall
|
|
3726
3745
|
)
|
|
3727
3746
|
]);
|
|
3747
|
+
} else if (leftExpr && t.isArrayPattern(leftExpr) && leftExpr.elements.length === 2) {
|
|
3748
|
+
const getterName = leftExpr.elements[0].name;
|
|
3749
|
+
const setterName = leftExpr.elements[1].name;
|
|
3750
|
+
stateSetterMap.set(getterName, setterName);
|
|
3751
|
+
return t.variableDeclaration("const", [
|
|
3752
|
+
t.variableDeclarator(
|
|
3753
|
+
t.arrayPattern([
|
|
3754
|
+
t.identifier(getterName),
|
|
3755
|
+
t.identifier(setterName)
|
|
3756
|
+
]),
|
|
3757
|
+
stateCall
|
|
3758
|
+
)
|
|
3759
|
+
]);
|
|
3728
3760
|
}
|
|
3729
3761
|
return t.expressionStatement(stateCall);
|
|
3730
3762
|
}
|
|
@@ -4082,7 +4114,7 @@ function generateUniqueId(baseName) {
|
|
|
4082
4114
|
return candidate;
|
|
4083
4115
|
}
|
|
4084
4116
|
function collectOddoIdentifiers(node, names = /* @__PURE__ */ new Set()) {
|
|
4085
|
-
var _a, _b, _c, _d;
|
|
4117
|
+
var _a, _b, _c, _d, _e, _f;
|
|
4086
4118
|
if (!node || typeof node !== "object") return names;
|
|
4087
4119
|
if (node.type === "program") {
|
|
4088
4120
|
moduleScope = /* @__PURE__ */ Object.create(null);
|
|
@@ -4093,15 +4125,21 @@ function collectOddoIdentifiers(node, names = /* @__PURE__ */ new Set()) {
|
|
|
4093
4125
|
names.add(node.name);
|
|
4094
4126
|
}
|
|
4095
4127
|
if (node.type === "expressionStatement") {
|
|
4096
|
-
const
|
|
4097
|
-
|
|
4128
|
+
const left = (_a = node.expression) == null ? void 0 : _a.left;
|
|
4129
|
+
const varName = left == null ? void 0 : left.name;
|
|
4130
|
+
if (node.modifier === "state" && (left == null ? void 0 : left.type) === "arrayPattern" && ((_b = left.elements) == null ? void 0 : _b.length) === 2) {
|
|
4131
|
+
const getterName = (_c = left.elements[0]) == null ? void 0 : _c.name;
|
|
4132
|
+
const setterName = (_d = left.elements[1]) == null ? void 0 : _d.name;
|
|
4133
|
+
if (getterName) declareVariable(getterName, "state");
|
|
4134
|
+
if (setterName) declareVariable(setterName, "immutable");
|
|
4135
|
+
} else if (varName) {
|
|
4098
4136
|
if (node.modifier === "state") {
|
|
4099
4137
|
declareVariable(varName, "state");
|
|
4100
4138
|
} else if (node.modifier === "computed") {
|
|
4101
4139
|
declareVariable(varName, "computed");
|
|
4102
4140
|
} else if (node.modifier === "mutable") {
|
|
4103
4141
|
declareVariable(varName, "mutable");
|
|
4104
|
-
} else if (!node.modifier && ((
|
|
4142
|
+
} else if (!node.modifier && ((_e = node.expression) == null ? void 0 : _e.type) === "variableDeclaration") {
|
|
4105
4143
|
declareVariable(varName, "immutable");
|
|
4106
4144
|
}
|
|
4107
4145
|
}
|
|
@@ -4133,7 +4171,7 @@ function collectOddoIdentifiers(node, names = /* @__PURE__ */ new Set()) {
|
|
|
4133
4171
|
for (const name of boundNames) {
|
|
4134
4172
|
declareVariable(name, "param");
|
|
4135
4173
|
}
|
|
4136
|
-
} else if (param.type === "restElement" && ((
|
|
4174
|
+
} else if (param.type === "restElement" && ((_f = param.argument) == null ? void 0 : _f.name)) {
|
|
4137
4175
|
declareVariable(param.argument.name, "param");
|
|
4138
4176
|
}
|
|
4139
4177
|
}
|
|
@@ -4276,13 +4314,13 @@ function convertExpressionStatement2(stmt) {
|
|
|
4276
4314
|
let leftExpr = null;
|
|
4277
4315
|
if (stmt.expression.type === "variableDeclaration" || stmt.expression.type === "assignment") {
|
|
4278
4316
|
leftExpr = convertExpression2(stmt.expression.left);
|
|
4279
|
-
if (stmt.modifier === "mutate" || stmt.modifier === "effect" || stmt.modifier === "computed" || stmt.modifier === "mutable" || stmt.modifier === "component" || stmt.modifier === "hook") {
|
|
4317
|
+
if (stmt.modifier === "mutate" || stmt.modifier === "effect" || stmt.modifier === "computed" || stmt.modifier === "mutable" || stmt.modifier === "component" || stmt.modifier === "hook" || stmt.modifier === "state") {
|
|
4280
4318
|
valueExpr = stmt.expression.right;
|
|
4281
4319
|
} else {
|
|
4282
4320
|
valueExpr = convertExpression2(stmt.expression.right);
|
|
4283
4321
|
}
|
|
4284
4322
|
} else {
|
|
4285
|
-
if (stmt.modifier === "mutate" || stmt.modifier === "effect" || stmt.modifier === "computed" || stmt.modifier === "mutable" || stmt.modifier === "component" || stmt.modifier === "hook") {
|
|
4323
|
+
if (stmt.modifier === "mutate" || stmt.modifier === "effect" || stmt.modifier === "computed" || stmt.modifier === "mutable" || stmt.modifier === "component" || stmt.modifier === "hook" || stmt.modifier === "state") {
|
|
4286
4324
|
valueExpr = stmt.expression;
|
|
4287
4325
|
} else {
|
|
4288
4326
|
valueExpr = convertExpression2(stmt.expression);
|
|
@@ -4303,13 +4341,13 @@ function convertExpressionStatement2(stmt) {
|
|
|
4303
4341
|
let leftExpr = null;
|
|
4304
4342
|
if (blockStmt.expression.type === "variableDeclaration" || blockStmt.expression.type === "assignment") {
|
|
4305
4343
|
leftExpr = convertExpression2(blockStmt.expression.left);
|
|
4306
|
-
if (stmt.modifier === "mutate" || stmt.modifier === "effect" || stmt.modifier === "computed" || stmt.modifier === "mutable" || stmt.modifier === "component" || stmt.modifier === "hook") {
|
|
4344
|
+
if (stmt.modifier === "mutate" || stmt.modifier === "effect" || stmt.modifier === "computed" || stmt.modifier === "mutable" || stmt.modifier === "component" || stmt.modifier === "hook" || stmt.modifier === "state") {
|
|
4307
4345
|
valueExpr = blockStmt.expression.right;
|
|
4308
4346
|
} else {
|
|
4309
4347
|
valueExpr = convertExpression2(blockStmt.expression.right);
|
|
4310
4348
|
}
|
|
4311
4349
|
} else {
|
|
4312
|
-
if (stmt.modifier === "mutate" || stmt.modifier === "effect" || stmt.modifier === "computed" || stmt.modifier === "mutable" || stmt.modifier === "component" || stmt.modifier === "hook") {
|
|
4350
|
+
if (stmt.modifier === "mutate" || stmt.modifier === "effect" || stmt.modifier === "computed" || stmt.modifier === "mutable" || stmt.modifier === "component" || stmt.modifier === "hook" || stmt.modifier === "state") {
|
|
4313
4351
|
valueExpr = blockStmt.expression;
|
|
4314
4352
|
} else {
|
|
4315
4353
|
valueExpr = convertExpression2(blockStmt.expression);
|
|
@@ -4353,7 +4391,31 @@ function convertExpressionStatement2(stmt) {
|
|
|
4353
4391
|
}
|
|
4354
4392
|
let expression = null;
|
|
4355
4393
|
if (stmt.expression) {
|
|
4356
|
-
|
|
4394
|
+
const isPlainExpr = stmt.expression.type !== "variableDeclaration" && stmt.expression.type !== "assignment" && stmt.expression.type !== "arraySliceAssignment";
|
|
4395
|
+
if (isPlainExpr) {
|
|
4396
|
+
const allIdentifiers = collectOddoIdentifiersOnly(stmt.expression);
|
|
4397
|
+
const reactiveDeps = allIdentifiers.filter((id) => isReactive(id));
|
|
4398
|
+
if (reactiveDeps.length > 0) {
|
|
4399
|
+
const savedScope = currentScope;
|
|
4400
|
+
currentScope = Object.create(currentScope);
|
|
4401
|
+
currentScope[reactiveScope] = false;
|
|
4402
|
+
const convertedExpr = convertExpression2(stmt.expression);
|
|
4403
|
+
currentScope = savedScope;
|
|
4404
|
+
usedModifiers.add("lift");
|
|
4405
|
+
const prefixedParams = reactiveDeps.map((id) => t.identifier("_" + id));
|
|
4406
|
+
const deps = reactiveDeps.map((id) => t.identifier(id));
|
|
4407
|
+
const arrowFunc = t.arrowFunctionExpression(prefixedParams, convertedExpr);
|
|
4408
|
+
wrapDependenciesWithCalls(arrowFunc, reactiveDeps, "_");
|
|
4409
|
+
expression = t.callExpression(
|
|
4410
|
+
t.identifier(modifierAliases["lift"]),
|
|
4411
|
+
[arrowFunc, t.arrayExpression(deps)]
|
|
4412
|
+
);
|
|
4413
|
+
} else {
|
|
4414
|
+
expression = convertExpression2(stmt.expression);
|
|
4415
|
+
}
|
|
4416
|
+
} else {
|
|
4417
|
+
expression = convertExpression2(stmt.expression);
|
|
4418
|
+
}
|
|
4357
4419
|
}
|
|
4358
4420
|
if (stmt.block) {
|
|
4359
4421
|
const block = convertBlockStatement(stmt.block);
|