@zvenigora/ng-eval-core 0.1.105 → 0.1.106

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.
@@ -1027,7 +1027,7 @@ class EvalContext {
1027
1027
  }
1028
1028
  }
1029
1029
  for (const scope of this._priorScopes) {
1030
- const value = getContextValue(scope, key);
1030
+ const value = scope.get(key);
1031
1031
  if (value !== undefined) {
1032
1032
  return value;
1033
1033
  }
@@ -1088,7 +1088,7 @@ class EvalContext {
1088
1088
  }
1089
1089
  }
1090
1090
  for (const scope of this._priorScopes) {
1091
- const foundKey = getContextKey(scope, key, caseInsensitive);
1091
+ const foundKey = getContextKey(scope.context, key, caseInsensitive);
1092
1092
  if (foundKey !== undefined) {
1093
1093
  return foundKey;
1094
1094
  }
@@ -1127,6 +1127,46 @@ class EvalContext {
1127
1127
  pop() {
1128
1128
  this._scopes.pop();
1129
1129
  }
1130
+ /**
1131
+ * Converts the EvalContext instance to an object representation.
1132
+ * If the original value is an instance of Registry, it converts it to an object using the toObject method of the registry.
1133
+ * If the original value is an object, it returns the original object.
1134
+ * @returns The object representation of the EvalContext instance.
1135
+ */
1136
+ toObject() {
1137
+ if (this._original && this._original instanceof Registry) {
1138
+ const registry = this._original;
1139
+ const object = registry.toObject();
1140
+ return object;
1141
+ }
1142
+ else if (this._original && this._original instanceof Object) {
1143
+ const obj = this._original;
1144
+ return obj;
1145
+ }
1146
+ return undefined;
1147
+ }
1148
+ }
1149
+
1150
+ /**
1151
+ * Represents a trace of evaluation for a specific code execution.
1152
+ */
1153
+ class EvalTrace extends Array {
1154
+ /**
1155
+ * Adds a new trace item to the evaluation trace.
1156
+ * @param node - The AST node associated with the trace item.
1157
+ * @param value - The value of the evaluated expression.
1158
+ * @param expression - The expression being evaluated.
1159
+ */
1160
+ add(node, value, expression) {
1161
+ const item = {
1162
+ type: node.type,
1163
+ value
1164
+ };
1165
+ if (expression) {
1166
+ item.expression = expression.substring(node.start, node.end);
1167
+ }
1168
+ this.push(item);
1169
+ }
1130
1170
  }
1131
1171
 
1132
1172
  /**
@@ -1203,6 +1243,21 @@ class EvalResult {
1203
1243
  }
1204
1244
  return this._endDate - this._startDate;
1205
1245
  }
1246
+ /**
1247
+ * Gets the expression associated with the evaluation result.
1248
+ * @returns The expression as a string, or undefined if no expression is associated.
1249
+ */
1250
+ get expression() {
1251
+ return this._expression;
1252
+ }
1253
+ /**
1254
+ * Sets the expression for the evaluation result.
1255
+ *
1256
+ * @param value - The expression to be set.
1257
+ */
1258
+ set expression(value) {
1259
+ this._expression = value;
1260
+ }
1206
1261
  /**
1207
1262
  * Creates a new instance of EvalResult.
1208
1263
  * @param trace The trace of evaluated values.
@@ -1210,7 +1265,7 @@ class EvalResult {
1210
1265
  */
1211
1266
  constructor(context) {
1212
1267
  this._stack = new Stack();
1213
- this._trace = new Registry;
1268
+ this._trace = new EvalTrace();
1214
1269
  this._context = context;
1215
1270
  }
1216
1271
  /**
@@ -1317,90 +1372,10 @@ const defaultParserOptions = {
1317
1372
  cacheSize: 100
1318
1373
  };
1319
1374
 
1320
- /**
1321
- * Base class for evaluation.
1322
- */
1323
- class BaseEval {
1324
- constructor(parserService) {
1325
- this.parserService = parserService;
1326
- }
1327
- /**
1328
- * Gets the parser options.
1329
- */
1330
- get parserOptions() {
1331
- return this._parserOptions;
1332
- }
1333
- /**
1334
- * Sets the parser options.
1335
- * @param value The parser options to set.
1336
- */
1337
- set parserOptions(value) {
1338
- this._parserOptions = value;
1339
- }
1340
- /**
1341
- * Parses the given expression into an abstract syntax tree (AST).
1342
- * @param expression - The expression to parse.
1343
- * @returns The parsed AST.
1344
- */
1345
- parse(expression) {
1346
- if (!expression) {
1347
- return undefined;
1348
- }
1349
- else if (typeof expression === 'string') {
1350
- const ast = this.parserService.parse(expression, this.parserOptions);
1351
- return ast;
1352
- }
1353
- else {
1354
- return expression;
1355
- }
1356
- }
1357
- /**
1358
- * Creates an instance of EvalState.
1359
- * @param context - The evaluation context or context object.
1360
- * @param options - The evaluation options.
1361
- * @returns The created EvalState instance.
1362
- */
1363
- createState(context, options) {
1364
- const state = EvalState.fromContext(context, options);
1365
- return state;
1366
- }
1367
- }
1368
-
1369
- /**
1370
- * @description
1371
- * Parses expression and returns ES6/ES2020 AST.
1372
- * @param expr expression to parse
1373
- * @param options parser options
1374
- */
1375
- const parse = (expr, options) => {
1376
- options = { ecmaVersion: 2020, ...options };
1377
- const acornOptions = { ecmaVersion: 2020, ...options };
1378
- const program = parse$1(expr, acornOptions);
1379
- if (!options.extractExpressions) {
1380
- return program;
1381
- }
1382
- return extractExpression(program);
1383
- };
1384
- /**
1385
- * @description
1386
- * Walks through the AST and extracts first expression.
1387
- * @param expr expression to parse
1388
- */
1389
- const extractExpression = (program) => {
1390
- if (program.body.length === 1) {
1391
- const expression = program.body[0];
1392
- if (expression.type === 'ExpressionStatement') {
1393
- return expression.expression;
1394
- }
1395
- }
1396
- return undefined;
1397
- };
1398
-
1399
1375
  const pushVisitorResult = (node, st, value) => {
1400
- const key = getKey(node);
1401
1376
  const result = st.result;
1402
1377
  result.stack.push(value);
1403
- result.trace.set(key, value);
1378
+ result.trace.add(node, value, st.result.expression);
1404
1379
  return value;
1405
1380
  };
1406
1381
  const popVisitorResult = (node, st) => {
@@ -1409,10 +1384,9 @@ const popVisitorResult = (node, st) => {
1409
1384
  return value;
1410
1385
  };
1411
1386
  const pushVisitorResultAsync = (node, st, value) => {
1412
- const key = getKey(node);
1413
1387
  const result = st.result;
1414
1388
  result.stack.push(value);
1415
- result.trace.set(key, value);
1389
+ result.trace.add(node, value, st.result.expression);
1416
1390
  return value;
1417
1391
  };
1418
1392
  const popVisitorResultAsync = (node, st) => {
@@ -1420,10 +1394,6 @@ const popVisitorResultAsync = (node, st) => {
1420
1394
  const value = result.stack.pop();
1421
1395
  return value;
1422
1396
  };
1423
- const getKey = (node) => {
1424
- const key = `${node.type}(${node.start}:${node.end})`;
1425
- return key;
1426
- };
1427
1397
 
1428
1398
  const beforeVisitor = (node, st) => {
1429
1399
  const options = st.options;
@@ -1549,6 +1519,11 @@ const evaluateMember = (node, st, callback) => {
1549
1519
  const thisValue = st.context.getThis(key);
1550
1520
  return [thisValue ?? object, key, value];
1551
1521
  }
1522
+ else if (object instanceof EvalScope) {
1523
+ const value = object.get(key);
1524
+ const thisValue = object;
1525
+ return [thisValue ?? object, key, value];
1526
+ }
1552
1527
  else {
1553
1528
  const caseInsesitive = !!(st.options?.caseInsensitive);
1554
1529
  const pair = getKeyValue(object, key, caseInsesitive);
@@ -1894,6 +1869,135 @@ const updateExpressionVisitor = (node, st, callback) => {
1894
1869
  afterVisitor(node, st);
1895
1870
  };
1896
1871
 
1872
+ /**
1873
+ * @description
1874
+ * Parses expression and returns ES6/ES2020 AST.
1875
+ * @param expr expression to parse
1876
+ * @param options parser options
1877
+ */
1878
+ const parse = (expr, options) => {
1879
+ options = { ecmaVersion: 2020, ...options };
1880
+ const acornOptions = { ecmaVersion: 2020, ...options };
1881
+ const program = parse$1(expr, acornOptions);
1882
+ if (!options.extractExpressions) {
1883
+ return program;
1884
+ }
1885
+ return extractExpression(program);
1886
+ };
1887
+ /**
1888
+ * @description
1889
+ * Walks through the AST and extracts first expression.
1890
+ * @param expr expression to parse
1891
+ */
1892
+ const extractExpression = (program) => {
1893
+ if (program.body.length === 1) {
1894
+ const expression = program.body[0];
1895
+ if (expression.type === 'ExpressionStatement') {
1896
+ return expression.expression;
1897
+ }
1898
+ }
1899
+ return undefined;
1900
+ };
1901
+
1902
+ /**
1903
+ * Evaluates the given node and returns the result.
1904
+ *
1905
+ * @param node The node to evaluate.
1906
+ * @param state The evaluation state.
1907
+ * @returns The result of the evaluation.
1908
+ */
1909
+ const evaluate = (node, state) => {
1910
+ if (node) {
1911
+ state.result.start();
1912
+ try {
1913
+ const visitors = getDefaultVisitors();
1914
+ walk.recursive(node, state, visitors);
1915
+ const value = popVisitorResult(node, state);
1916
+ state.result.stop();
1917
+ state.result.setSuccess(value);
1918
+ return value;
1919
+ }
1920
+ catch (error) {
1921
+ state.result.stop();
1922
+ state.result.setFailure(error);
1923
+ throw error;
1924
+ }
1925
+ }
1926
+ return undefined;
1927
+ };
1928
+ /**
1929
+ * Asynchronously evaluates the given abstract syntax tree (AST) using the provided evaluation state.
1930
+ * @param ast The abstract syntax tree to evaluate.
1931
+ * @param state The evaluation state.
1932
+ * @returns A promise that resolves to the evaluated value or undefined.
1933
+ */
1934
+ const evaluateAsync = (ast, state) => {
1935
+ const promise = new Promise((resolve) => {
1936
+ const value = evaluate(ast, state);
1937
+ resolve(value);
1938
+ });
1939
+ return promise;
1940
+ };
1941
+
1942
+ /**
1943
+ * Compiles the given node and returns a function that can be used to evaluate it.
1944
+ *
1945
+ * @param node The node to compile.
1946
+ * @returns A function that takes an `EvalState` and returns the result of evaluating the node.
1947
+ */
1948
+ const compile = (node) => {
1949
+ return evaluate.bind(null, node);
1950
+ };
1951
+ /**
1952
+ * Compiles the given node asynchronously.
1953
+ *
1954
+ * @param node - The node to compile.
1955
+ * @returns A function that takes an `EvalState` object and returns a `Promise` that resolves to the compiled result.
1956
+ */
1957
+ const compileAsync = (node) => {
1958
+ return evaluateAsync.bind(null, node);
1959
+ };
1960
+ /**
1961
+ * Calls the provided function with the given state and returns the result.
1962
+ *
1963
+ * @param fn The function to be called.
1964
+ * @param state The state to be passed to the function.
1965
+ * @returns The result of calling the function.
1966
+ */
1967
+ const call = (fn, state) => {
1968
+ const value = fn(state);
1969
+ return value;
1970
+ };
1971
+ /**
1972
+ * Calls an asynchronous function with the given state and returns a promise that resolves to the result.
1973
+ * @param fn The asynchronous function to call.
1974
+ * @param state The state to pass to the function.
1975
+ * @returns A promise that resolves to the result of the function.
1976
+ */
1977
+ const callAsync = (fn, state) => {
1978
+ const promise = fn(state);
1979
+ return promise;
1980
+ };
1981
+
1982
+ /**
1983
+ * Finds all nodes of a specific type in the given AST.
1984
+ *
1985
+ * @param ast The AST to search in.
1986
+ * @param searchType The type of nodes to search for.
1987
+ * @returns An array of nodes of the specified type if found, otherwise undefined.
1988
+ */
1989
+ const extract = (node, searchType) => {
1990
+ if (node) {
1991
+ const state = [];
1992
+ const visitors = {
1993
+ [searchType]: (node) => { state.push(node); return; }
1994
+ };
1995
+ walk.simple(node, visitors);
1996
+ return state.length > 0 ? state : undefined;
1997
+ }
1998
+ return undefined;
1999
+ };
2000
+
1897
2001
  const arrowFunctionExpressionVisitor = (node, st, callback) => {
1898
2002
  beforeVisitor(node, st);
1899
2003
  const fn = (...arrowArgs) => {
@@ -2038,104 +2142,135 @@ const evaluateRestElement = (pattern, st, callback, args) => {
2038
2142
  // return object;
2039
2143
  // }
2040
2144
 
2145
+ const defaultEvalScopeOptions = {
2146
+ global: false,
2147
+ caseInsensitive: false,
2148
+ namespace: undefined,
2149
+ thisArg: undefined
2150
+ };
2041
2151
  /**
2042
- * Evaluates the given node and returns the result.
2043
- *
2044
- * @param node The node to evaluate.
2045
- * @param state The evaluation state.
2046
- * @returns The result of the evaluation.
2152
+ * Represents an evaluation scope.
2047
2153
  */
2048
- const evaluate = (node, state) => {
2049
- if (node) {
2050
- state.result.start();
2051
- try {
2052
- const visitors = getDefaultVisitors();
2053
- walk.recursive(node, state, visitors);
2054
- const value = popVisitorResult(node, state);
2055
- state.result.stop();
2056
- state.result.setSuccess(value);
2057
- return value;
2154
+ class EvalScope {
2155
+ /**
2156
+ * Gets the context of the evaluation scope.
2157
+ * @returns The context object.
2158
+ */
2159
+ get context() {
2160
+ return this._context;
2161
+ }
2162
+ /**
2163
+ * Gets the options for evaluating the scope.
2164
+ * @returns The evaluation options.
2165
+ */
2166
+ get options() {
2167
+ return this._options;
2168
+ }
2169
+ /**
2170
+ * Creates a new instance of EvalScope.
2171
+ * @param context - The context of the evaluation scope.
2172
+ * @param options - The options for evaluating the scope.
2173
+ */
2174
+ constructor(context, options) {
2175
+ this.type = 'EvalScope';
2176
+ this._context = context ?? {};
2177
+ this._options = { ...defaultEvalScopeOptions, ...options ?? {} };
2178
+ }
2179
+ /**
2180
+ * Creates an instance of EvalScope from the given context and options.
2181
+ *
2182
+ * @param context - The context object.
2183
+ * @param options - The options object.
2184
+ * @returns An instance of EvalScope.
2185
+ */
2186
+ static fromObject(context, options) {
2187
+ return new EvalScope(context, options);
2188
+ }
2189
+ /**
2190
+ * Gets the value of the specified key.
2191
+ * @param key - The key to retrieve the value for.
2192
+ * @returns The value of the specified key, or undefined if the key is not found.
2193
+ */
2194
+ get(key) {
2195
+ const { caseInsensitive, namespace } = this._options;
2196
+ const { context } = this;
2197
+ if (typeof key === 'string' && namespace) {
2198
+ const found = caseInsensitive
2199
+ ? equalIgnoreCase(key, namespace)
2200
+ : key === namespace;
2201
+ if (found) {
2202
+ return this.context;
2203
+ }
2058
2204
  }
2059
- catch (error) {
2060
- state.result.stop();
2061
- state.result.setFailure(error);
2062
- throw error;
2205
+ const value = getContextValue(context, key);
2206
+ return value;
2207
+ }
2208
+ /**
2209
+ * Sets the value of the specified key.
2210
+ * @param key - The key to set the value for.
2211
+ * @param value - The value to set.
2212
+ */
2213
+ set(key, value) {
2214
+ const { context } = this;
2215
+ if (context && context instanceof Registry) {
2216
+ const registry = context;
2217
+ registry.set(key, value);
2218
+ }
2219
+ else if (context && context instanceof Object) {
2220
+ const obj = context;
2221
+ obj[key] = value;
2063
2222
  }
2064
2223
  }
2065
- return undefined;
2066
- };
2067
- /**
2068
- * Asynchronously evaluates the given abstract syntax tree (AST) using the provided evaluation state.
2069
- * @param ast The abstract syntax tree to evaluate.
2070
- * @param state The evaluation state.
2071
- * @returns A promise that resolves to the evaluated value or undefined.
2072
- */
2073
- const evaluateAsync = (ast, state) => {
2074
- const promise = new Promise((resolve) => {
2075
- const value = evaluate(ast, state);
2076
- resolve(value);
2077
- });
2078
- return promise;
2079
- };
2080
-
2081
- /**
2082
- * Compiles the given node and returns a function that can be used to evaluate it.
2083
- *
2084
- * @param node The node to compile.
2085
- * @returns A function that takes an `EvalState` and returns the result of evaluating the node.
2086
- */
2087
- const compile = (node) => {
2088
- return evaluate.bind(null, node);
2089
- };
2090
- /**
2091
- * Compiles the given node asynchronously.
2092
- *
2093
- * @param node - The node to compile.
2094
- * @returns A function that takes an `EvalState` object and returns a `Promise` that resolves to the compiled result.
2095
- */
2096
- const compileAsync = (node) => {
2097
- return evaluateAsync.bind(null, node);
2098
- };
2099
- /**
2100
- * Calls the provided function with the given state and returns the result.
2101
- *
2102
- * @param fn The function to be called.
2103
- * @param state The state to be passed to the function.
2104
- * @returns The result of calling the function.
2105
- */
2106
- const call = (fn, state) => {
2107
- const value = fn(state);
2108
- return value;
2109
- };
2110
- /**
2111
- * Calls an asynchronous function with the given state and returns a promise that resolves to the result.
2112
- * @param fn The asynchronous function to call.
2113
- * @param state The state to pass to the function.
2114
- * @returns A promise that resolves to the result of the function.
2115
- */
2116
- const callAsync = (fn, state) => {
2117
- const promise = fn(state);
2118
- return promise;
2119
- };
2224
+ }
2120
2225
 
2121
2226
  /**
2122
- * Finds all nodes of a specific type in the given AST.
2123
- *
2124
- * @param ast The AST to search in.
2125
- * @param searchType The type of nodes to search for.
2126
- * @returns An array of nodes of the specified type if found, otherwise undefined.
2227
+ * Base class for evaluation.
2127
2228
  */
2128
- const extract = (node, searchType) => {
2129
- if (node) {
2130
- const state = [];
2131
- const visitors = {
2132
- [searchType]: (node) => { state.push(node); return; }
2133
- };
2134
- walk.simple(node, visitors);
2135
- return state.length > 0 ? state : undefined;
2229
+ class BaseEval {
2230
+ constructor(parserService) {
2231
+ this.parserService = parserService;
2136
2232
  }
2137
- return undefined;
2138
- };
2233
+ /**
2234
+ * Gets the parser options.
2235
+ */
2236
+ get parserOptions() {
2237
+ return this._parserOptions;
2238
+ }
2239
+ /**
2240
+ * Sets the parser options.
2241
+ * @param value The parser options to set.
2242
+ */
2243
+ set parserOptions(value) {
2244
+ this._parserOptions = value;
2245
+ }
2246
+ /**
2247
+ * Parses the given expression into an abstract syntax tree (AST).
2248
+ * @param expression - The expression to parse.
2249
+ * @returns The parsed AST.
2250
+ */
2251
+ parse(expression) {
2252
+ if (!expression) {
2253
+ return undefined;
2254
+ }
2255
+ else if (typeof expression === 'string') {
2256
+ const ast = this.parserService.parse(expression, this.parserOptions);
2257
+ return ast;
2258
+ }
2259
+ else {
2260
+ return expression;
2261
+ }
2262
+ }
2263
+ /**
2264
+ * Creates an instance of EvalState.
2265
+ * @param context - The evaluation context or context object.
2266
+ * @param options - The evaluation options.
2267
+ * @returns The created EvalState instance.
2268
+ */
2269
+ createState(context, options) {
2270
+ const state = EvalState.fromContext(context, options);
2271
+ return state;
2272
+ }
2273
+ }
2139
2274
 
2140
2275
  /**
2141
2276
  * @description
@@ -2256,6 +2391,9 @@ class EvalService extends BaseEval {
2256
2391
  try {
2257
2392
  const ast = this.parse(expression);
2258
2393
  const state = this.createState(context, options);
2394
+ if (state?.result && typeof expression === 'string') {
2395
+ state.result.expression = expression;
2396
+ }
2259
2397
  const value = evaluate(ast, state);
2260
2398
  return value;
2261
2399
  }
@@ -2279,6 +2417,9 @@ class EvalService extends BaseEval {
2279
2417
  eval(expression, state) {
2280
2418
  try {
2281
2419
  const ast = this.parse(expression);
2420
+ if (state?.result && typeof expression === 'string') {
2421
+ state.result.expression = expression;
2422
+ }
2282
2423
  const value = evaluate(ast, state);
2283
2424
  return value;
2284
2425
  }
@@ -2302,6 +2443,9 @@ class EvalService extends BaseEval {
2302
2443
  try {
2303
2444
  const ast = this.parse(expression);
2304
2445
  const state = this.createState(context, options);
2446
+ if (state?.result && typeof expression === 'string') {
2447
+ state.result.expression = expression;
2448
+ }
2305
2449
  const promise = evaluateAsync(ast, state);
2306
2450
  return promise;
2307
2451
  }
@@ -2325,6 +2469,9 @@ class EvalService extends BaseEval {
2325
2469
  evalAsync(expression, state) {
2326
2470
  try {
2327
2471
  const ast = this.parse(expression);
2472
+ if (state?.result && typeof expression === 'string') {
2473
+ state.result.expression = expression;
2474
+ }
2328
2475
  const promise = evaluateAsync(ast, state);
2329
2476
  return promise;
2330
2477
  }
@@ -2559,5 +2706,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImpor
2559
2706
  * Generated bundle index. Do not edit.
2560
2707
  */
2561
2708
 
2562
- export { BaseRegistry, Cache, CaseInsensitiveRegistry, CompilerService, DiscoveryService, EvalContext, EvalResult, EvalService, EvalState, ParserService, Queue, RecursiveVisitorResultType, Registry, Scope, ScopeRegistry, Stack, call, callAsync, compile, compileAsync, defaultParserOptions, evaluate, evaluateAsync, extract, extractExpression, fromContext, getContextKey, getContextValue, isRegistryContext, parse };
2709
+ export { BaseRegistry, Cache, CaseInsensitiveRegistry, CompilerService, DiscoveryService, EvalContext, EvalResult, EvalScope, EvalService, EvalState, EvalTrace, ParserService, Queue, RecursiveVisitorResultType, Registry, Scope, ScopeRegistry, Stack, call, callAsync, compile, compileAsync, defaultParserOptions, evaluate, evaluateAsync, extract, extractExpression, fromContext, getContextKey, getContextValue, isRegistryContext, parse };
2563
2710
  //# sourceMappingURL=zvenigora-ng-eval-core.mjs.map