|
744 | 744 | } |
745 | 745 | return -1; |
746 | 746 | } |
| 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 | + } |
747 | 773 | function findTopLevelOperator(str, ops) { |
748 | 774 | let depthParen = 0; |
749 | 775 | let depthBrace = 0; |
|
875 | 901 | return; |
876 | 902 | const ctx = context && typeof context === "object" ? context : {}; |
877 | 903 | 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 | + } |
878 | 931 | const qIdx = findTopLevelChar(trimmed, "?"); |
879 | 932 | if (qIdx !== -1) { |
880 | 933 | const colonIdx = findTopLevelChar(trimmed.slice(qIdx + 1), ":"); |
|
994 | 1047 | return []; |
995 | 1048 | return splitArguments(inner).map((arg) => safeEvaluate(arg, context, extraScope)); |
996 | 1049 | } |
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('"')) { |
998 | 1058 | return trimmed.slice(1, -1); |
999 | 1059 | } |
1000 | 1060 | if (!isNaN(Number(trimmed))) { |
|
1009 | 1069 | if (trimmed === "undefined") |
1010 | 1070 | return; |
1011 | 1071 | if (trimmed.endsWith(")")) { |
1012 | | - const openParenIdx = findTopLevelChar(trimmed, "("); |
| 1072 | + const openParenIdx = findMatchingOpenParen(trimmed); |
1013 | 1073 | if (openParenIdx !== -1) { |
1014 | 1074 | const calleeStr = trimmed.slice(0, openParenIdx).trim(); |
1015 | 1075 | const argsStr = trimmed.slice(openParenIdx + 1, -1).trim(); |
|
1020 | 1080 | const lastDot = calleeStr.lastIndexOf("."); |
1021 | 1081 | const parentPath = calleeStr.slice(0, lastDot); |
1022 | 1082 | 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; |
1026 | 1087 | } |
1027 | 1088 | } else { |
1028 | 1089 | if (calleeStr in extraScope && typeof extraScope[calleeStr] === "function") { |
|
0 commit comments