Skip to content

Commit e56c063

Browse files
committed
feat(eval): add arrow-function closures, chained calls & template literal interpolation to zero-eval evaluator
1 parent f48c40b commit e56c063

5 files changed

Lines changed: 246 additions & 31 deletions

File tree

‎dist/api/erp-invoices.json‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎dist/htmx-bolt.js‎

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,32 @@
744744
}
745745
return -1;
746746
}
747+
function findMatchingOpenParen(str) {
748+
if (!str.endsWith(")"))
749+
return -1;
750+
let depth = 0;
751+
let inQuote = null;
752+
for (let i = str.length - 1;i >= 0; i--) {
753+
const ch = str[i];
754+
if (inQuote) {
755+
if (ch === inQuote && str[i - 1] !== "\\")
756+
inQuote = null;
757+
continue;
758+
}
759+
if (ch === '"' || ch === "'" || ch === "`") {
760+
inQuote = ch;
761+
continue;
762+
}
763+
if (ch === ")")
764+
depth++;
765+
else if (ch === "(") {
766+
depth--;
767+
if (depth === 0)
768+
return i;
769+
}
770+
}
771+
return -1;
772+
}
747773
function findTopLevelOperator(str, ops) {
748774
let depthParen = 0;
749775
let depthBrace = 0;
@@ -875,6 +901,33 @@
875901
return;
876902
const ctx = context && typeof context === "object" ? context : {};
877903
const scope = { ...ctx, ...extraScope };
904+
const arrowMatch = findTopLevelOperator(trimmed, ["=>"]);
905+
if (arrowMatch) {
906+
const rawParams = trimmed.slice(0, arrowMatch.index).trim();
907+
const rawBody = trimmed.slice(arrowMatch.index + 2).trim();
908+
let cleanParamsStr = rawParams;
909+
if (cleanParamsStr.startsWith("(") && cleanParamsStr.endsWith(")")) {
910+
cleanParamsStr = cleanParamsStr.slice(1, -1).trim();
911+
}
912+
const paramNames = cleanParamsStr ? cleanParamsStr.split(",").map((p) => p.trim()) : [];
913+
let bodyExpr = rawBody;
914+
if (bodyExpr.startsWith("{") && bodyExpr.endsWith("}")) {
915+
const innerBody = bodyExpr.slice(1, -1).trim();
916+
if (innerBody.startsWith("return ")) {
917+
bodyExpr = innerBody.slice(7).replace(/;$/, "").trim();
918+
} else {
919+
bodyExpr = innerBody.replace(/;$/, "").trim();
920+
}
921+
}
922+
return (...args) => {
923+
const callScope = { ...extraScope };
924+
paramNames.forEach((name, idx) => {
925+
if (name)
926+
callScope[name] = args[idx];
927+
});
928+
return safeEvaluate(bodyExpr, context, callScope);
929+
};
930+
}
878931
const qIdx = findTopLevelChar(trimmed, "?");
879932
if (qIdx !== -1) {
880933
const colonIdx = findTopLevelChar(trimmed.slice(qIdx + 1), ":");
@@ -994,7 +1047,14 @@
9941047
return [];
9951048
return splitArguments(inner).map((arg) => safeEvaluate(arg, context, extraScope));
9961049
}
997-
if (trimmed.startsWith("'") && trimmed.endsWith("'") || trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("`") && trimmed.endsWith("`")) {
1050+
if (trimmed.startsWith("`") && trimmed.endsWith("`")) {
1051+
const inner = trimmed.slice(1, -1);
1052+
return inner.replace(/\$\{([^}]+)\}/g, (_, expr2) => {
1053+
const evaluated = safeEvaluate(expr2, context, extraScope);
1054+
return evaluated !== undefined && evaluated !== null ? String(evaluated) : "";
1055+
});
1056+
}
1057+
if (trimmed.startsWith("'") && trimmed.endsWith("'") || trimmed.startsWith('"') && trimmed.endsWith('"')) {
9981058
return trimmed.slice(1, -1);
9991059
}
10001060
if (!isNaN(Number(trimmed))) {
@@ -1009,7 +1069,7 @@
10091069
if (trimmed === "undefined")
10101070
return;
10111071
if (trimmed.endsWith(")")) {
1012-
const openParenIdx = findTopLevelChar(trimmed, "(");
1072+
const openParenIdx = findMatchingOpenParen(trimmed);
10131073
if (openParenIdx !== -1) {
10141074
const calleeStr = trimmed.slice(0, openParenIdx).trim();
10151075
const argsStr = trimmed.slice(openParenIdx + 1, -1).trim();
@@ -1020,9 +1080,10 @@
10201080
const lastDot = calleeStr.lastIndexOf(".");
10211081
const parentPath = calleeStr.slice(0, lastDot);
10221082
const method = calleeStr.slice(lastDot + 1);
1023-
fnThis = parentPath in extraScope ? extraScope[parentPath] : getNestedProperty(scope, parentPath);
1024-
if (fnThis && typeof fnThis[method] === "function") {
1025-
fn = fnThis[method];
1083+
const parentVal = safeEvaluate(parentPath, context, extraScope);
1084+
if (parentVal && typeof parentVal[method] === "function") {
1085+
fn = parentVal[method];
1086+
fnThis = parentVal;
10261087
}
10271088
} else {
10281089
if (calleeStr in extraScope && typeof extraScope[calleeStr] === "function") {

‎public/htmx-bolt.js‎

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,32 @@
744744
}
745745
return -1;
746746
}
747+
function findMatchingOpenParen(str) {
748+
if (!str.endsWith(")"))
749+
return -1;
750+
let depth = 0;
751+
let inQuote = null;
752+
for (let i = str.length - 1;i >= 0; i--) {
753+
const ch = str[i];
754+
if (inQuote) {
755+
if (ch === inQuote && str[i - 1] !== "\\")
756+
inQuote = null;
757+
continue;
758+
}
759+
if (ch === '"' || ch === "'" || ch === "`") {
760+
inQuote = ch;
761+
continue;
762+
}
763+
if (ch === ")")
764+
depth++;
765+
else if (ch === "(") {
766+
depth--;
767+
if (depth === 0)
768+
return i;
769+
}
770+
}
771+
return -1;
772+
}
747773
function findTopLevelOperator(str, ops) {
748774
let depthParen = 0;
749775
let depthBrace = 0;
@@ -875,6 +901,33 @@
875901
return;
876902
const ctx = context && typeof context === "object" ? context : {};
877903
const scope = { ...ctx, ...extraScope };
904+
const arrowMatch = findTopLevelOperator(trimmed, ["=>"]);
905+
if (arrowMatch) {
906+
const rawParams = trimmed.slice(0, arrowMatch.index).trim();
907+
const rawBody = trimmed.slice(arrowMatch.index + 2).trim();
908+
let cleanParamsStr = rawParams;
909+
if (cleanParamsStr.startsWith("(") && cleanParamsStr.endsWith(")")) {
910+
cleanParamsStr = cleanParamsStr.slice(1, -1).trim();
911+
}
912+
const paramNames = cleanParamsStr ? cleanParamsStr.split(",").map((p) => p.trim()) : [];
913+
let bodyExpr = rawBody;
914+
if (bodyExpr.startsWith("{") && bodyExpr.endsWith("}")) {
915+
const innerBody = bodyExpr.slice(1, -1).trim();
916+
if (innerBody.startsWith("return ")) {
917+
bodyExpr = innerBody.slice(7).replace(/;$/, "").trim();
918+
} else {
919+
bodyExpr = innerBody.replace(/;$/, "").trim();
920+
}
921+
}
922+
return (...args) => {
923+
const callScope = { ...extraScope };
924+
paramNames.forEach((name, idx) => {
925+
if (name)
926+
callScope[name] = args[idx];
927+
});
928+
return safeEvaluate(bodyExpr, context, callScope);
929+
};
930+
}
878931
const qIdx = findTopLevelChar(trimmed, "?");
879932
if (qIdx !== -1) {
880933
const colonIdx = findTopLevelChar(trimmed.slice(qIdx + 1), ":");
@@ -994,7 +1047,14 @@
9941047
return [];
9951048
return splitArguments(inner).map((arg) => safeEvaluate(arg, context, extraScope));
9961049
}
997-
if (trimmed.startsWith("'") && trimmed.endsWith("'") || trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("`") && trimmed.endsWith("`")) {
1050+
if (trimmed.startsWith("`") && trimmed.endsWith("`")) {
1051+
const inner = trimmed.slice(1, -1);
1052+
return inner.replace(/\$\{([^}]+)\}/g, (_, expr2) => {
1053+
const evaluated = safeEvaluate(expr2, context, extraScope);
1054+
return evaluated !== undefined && evaluated !== null ? String(evaluated) : "";
1055+
});
1056+
}
1057+
if (trimmed.startsWith("'") && trimmed.endsWith("'") || trimmed.startsWith('"') && trimmed.endsWith('"')) {
9981058
return trimmed.slice(1, -1);
9991059
}
10001060
if (!isNaN(Number(trimmed))) {
@@ -1009,7 +1069,7 @@
10091069
if (trimmed === "undefined")
10101070
return;
10111071
if (trimmed.endsWith(")")) {
1012-
const openParenIdx = findTopLevelChar(trimmed, "(");
1072+
const openParenIdx = findMatchingOpenParen(trimmed);
10131073
if (openParenIdx !== -1) {
10141074
const calleeStr = trimmed.slice(0, openParenIdx).trim();
10151075
const argsStr = trimmed.slice(openParenIdx + 1, -1).trim();
@@ -1020,9 +1080,10 @@
10201080
const lastDot = calleeStr.lastIndexOf(".");
10211081
const parentPath = calleeStr.slice(0, lastDot);
10221082
const method = calleeStr.slice(lastDot + 1);
1023-
fnThis = parentPath in extraScope ? extraScope[parentPath] : getNestedProperty(scope, parentPath);
1024-
if (fnThis && typeof fnThis[method] === "function") {
1025-
fn = fnThis[method];
1083+
const parentVal = safeEvaluate(parentPath, context, extraScope);
1084+
if (parentVal && typeof parentVal[method] === "function") {
1085+
fn = parentVal[method];
1086+
fnThis = parentVal;
10261087
}
10271088
} else {
10281089
if (calleeStr in extraScope && typeof extraScope[calleeStr] === "function") {

0 commit comments

Comments
 (0)