@zvenigora/ng-eval-core 0.1.104 → 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.
Files changed (29) hide show
  1. package/esm2022/lib/actual/services/base-eval.mjs +32 -1
  2. package/esm2022/lib/actual/services/compiler.service.mjs +66 -40
  3. package/esm2022/lib/actual/services/discovery.service.mjs +4 -23
  4. package/esm2022/lib/actual/services/eval.service.mjs +65 -30
  5. package/esm2022/lib/actual/services/parser.service.mjs +19 -9
  6. package/esm2022/lib/internal/classes/eval/eval-context.mjs +21 -3
  7. package/esm2022/lib/internal/classes/eval/eval-result.mjs +19 -3
  8. package/esm2022/lib/internal/classes/eval/eval-scope.mjs +83 -0
  9. package/esm2022/lib/internal/classes/eval/eval-state.mjs +1 -1
  10. package/esm2022/lib/internal/classes/eval/eval-trace.mjs +22 -0
  11. package/esm2022/lib/internal/classes/eval/parser-options.mjs +8 -0
  12. package/esm2022/lib/internal/classes/eval/public-api.mjs +4 -1
  13. package/esm2022/lib/internal/visitors/member-expression.mjs +7 -1
  14. package/esm2022/lib/internal/visitors/visitor-result.mjs +3 -9
  15. package/fesm2022/zvenigora-ng-eval-core.mjs +1026 -800
  16. package/fesm2022/zvenigora-ng-eval-core.mjs.map +1 -1
  17. package/lib/actual/services/base-eval.d.ts +21 -2
  18. package/lib/actual/services/compiler.service.d.ts +28 -16
  19. package/lib/actual/services/discovery.service.d.ts +1 -7
  20. package/lib/actual/services/eval.service.d.ts +22 -10
  21. package/lib/actual/services/parser.service.d.ts +11 -2
  22. package/lib/internal/classes/eval/eval-context.d.ts +9 -1
  23. package/lib/internal/classes/eval/eval-result.d.ts +16 -3
  24. package/lib/internal/classes/eval/eval-scope.d.ts +51 -0
  25. package/lib/internal/classes/eval/eval-state.d.ts +4 -4
  26. package/lib/internal/classes/eval/eval-trace.d.ts +20 -0
  27. package/lib/internal/classes/eval/parser-options.d.ts +2 -0
  28. package/lib/internal/classes/eval/public-api.d.ts +3 -0
  29. package/package.json +1 -1
@@ -1,16 +1,35 @@
1
+ import { AnyNode } from "acorn";
1
2
  import { ParserOptions } from "../../internal/interfaces";
3
+ import { ParserService } from "./parser.service";
4
+ import { EvalContext, EvalOptions, EvalState } from "../../internal/classes/eval";
5
+ import { Context } from "../../internal/public-api";
2
6
  /**
3
7
  * Base class for evaluation.
4
8
  */
5
9
  export declare abstract class BaseEval {
10
+ protected parserService: ParserService;
6
11
  private _parserOptions;
12
+ constructor(parserService: ParserService);
7
13
  /**
8
14
  * Gets the parser options.
9
15
  */
10
- get parserOptions(): ParserOptions;
16
+ protected get parserOptions(): ParserOptions;
11
17
  /**
12
18
  * Sets the parser options.
13
19
  * @param value The parser options to set.
14
20
  */
15
- set parserOptions(value: ParserOptions);
21
+ protected set parserOptions(value: ParserOptions);
22
+ /**
23
+ * Parses the given expression into an abstract syntax tree (AST).
24
+ * @param expression - The expression to parse.
25
+ * @returns The parsed AST.
26
+ */
27
+ protected parse(expression: string | AnyNode | undefined): AnyNode | undefined;
28
+ /**
29
+ * Creates an instance of EvalState.
30
+ * @param context - The evaluation context or context object.
31
+ * @param options - The evaluation options.
32
+ * @returns The created EvalState instance.
33
+ */
34
+ createState(context?: EvalContext | Context, options?: EvalOptions): EvalState;
16
35
  }
@@ -1,7 +1,7 @@
1
1
  import { BaseEval } from './base-eval';
2
2
  import { ParserService } from './parser.service';
3
3
  import { EvalService } from './eval.service';
4
- import { EvalContext, EvalOptions } from '../../internal/classes/eval';
4
+ import { EvalContext, EvalOptions, EvalState } from '../../internal/classes/eval';
5
5
  import { Context } from '../../internal/classes/common';
6
6
  import { AnyNode } from 'acorn';
7
7
  import { stateCallback, stateCallbackAsync } from '../../internal/functions';
@@ -11,15 +11,9 @@ import * as i0 from "@angular/core";
11
11
  * Provides methods for compiling and evaluating expressions.
12
12
  */
13
13
  export declare class CompilerService extends BaseEval {
14
- parserService: ParserService;
15
- evalService: EvalService;
14
+ protected parserService: ParserService;
15
+ protected evalService: EvalService;
16
16
  constructor(parserService: ParserService, evalService: EvalService);
17
- /**
18
- * Parses the given expression into an abstract syntax tree (AST).
19
- * @param expression - The expression to parse.
20
- * @returns The parsed AST.
21
- */
22
- private parse;
23
17
  /**
24
18
  * Compiles the given expression into a state callback function.
25
19
  * @param expression - The expression to compile.
@@ -35,7 +29,23 @@ export declare class CompilerService extends BaseEval {
35
29
  * @returns The result of the evaluation.
36
30
  * @throws Error if there is an error during the evaluation.
37
31
  */
38
- call(fn: stateCallback | undefined, context?: EvalContext | Context, options?: EvalOptions): unknown;
32
+ simpleCall(fn: stateCallback | undefined, context?: EvalContext | Context, options?: EvalOptions): unknown;
33
+ /**
34
+ * Calls the provided function with the given state.
35
+ *
36
+ * @param fn - The function to be called.
37
+ * @param state - The state to be passed to the function.
38
+ * @returns The value returned by the function.
39
+ * @throws If an error occurs during the function call.
40
+ */
41
+ call(fn: stateCallback | undefined, state: EvalState): unknown;
42
+ /**
43
+ * Compiles the given expression into an asynchronous state callback function.
44
+ * @param expression - The expression to compile.
45
+ * @returns The compiled asynchronous state callback function.
46
+ * @throws Error if there is an error during compilation.
47
+ */
48
+ compileAsync(expression: string | AnyNode | undefined): stateCallbackAsync | undefined;
39
49
  /**
40
50
  * Calls the given asynchronous state callback function with the specified context and options.
41
51
  * @param fn - The asynchronous state callback function to call.
@@ -44,14 +54,16 @@ export declare class CompilerService extends BaseEval {
44
54
  * @returns A promise that resolves to the result of the evaluation.
45
55
  * @throws Error if there is an error during the evaluation.
46
56
  */
47
- callAsync(fn: stateCallbackAsync | undefined, context?: EvalContext | Context, options?: EvalOptions): Promise<unknown>;
57
+ simpleCallAsync(fn: stateCallbackAsync | undefined, context?: EvalContext | Context, options?: EvalOptions): Promise<unknown>;
48
58
  /**
49
- * Compiles the given expression into an asynchronous state callback function.
50
- * @param expression - The expression to compile.
51
- * @returns The compiled asynchronous state callback function.
52
- * @throws Error if there is an error during compilation.
59
+ * Calls the provided asynchronous function with the given state.
60
+ *
61
+ * @param fn - The asynchronous function to call.
62
+ * @param state - The state to pass to the function.
63
+ * @returns A promise that resolves to the value returned by the function.
64
+ * @throws If an error occurs during the function call.
53
65
  */
54
- compileAsync(expression: string | AnyNode | undefined): stateCallbackAsync | undefined;
66
+ callAsync(fn: stateCallbackAsync | undefined, state: EvalState): Promise<unknown>;
55
67
  static ɵfac: i0.ɵɵFactoryDeclaration<CompilerService, never>;
56
68
  static ɵprov: i0.ɵɵInjectableDeclaration<CompilerService>;
57
69
  }
@@ -7,7 +7,7 @@ import * as i0 from "@angular/core";
7
7
  * Service responsible for discovering nodes in an abstract syntax tree (AST).
8
8
  */
9
9
  export declare class DiscoveryService extends BaseEval {
10
- parserService: ParserService;
10
+ protected parserService: ParserService;
11
11
  /**
12
12
  * Creates a new instance of the DiscoveryService class.
13
13
  * @param parserService The parser service to be used.
@@ -22,12 +22,6 @@ export declare class DiscoveryService extends BaseEval {
22
22
  * @throws If an error occurs during the search process.
23
23
  */
24
24
  extract(expression: string | AnyNode | undefined, searchType: AnyNodeTypes): AnyNode[] | undefined;
25
- /**
26
- * Parses the given expression into an abstract syntax tree (AST).
27
- * @param expression The expression to parse.
28
- * @returns The AST representing the expression.
29
- */
30
- private parse;
31
25
  static ɵfac: i0.ɵɵFactoryDeclaration<DiscoveryService, never>;
32
26
  static ɵprov: i0.ɵɵInjectableDeclaration<DiscoveryService>;
33
27
  }
@@ -2,24 +2,18 @@ import { BaseEval } from './base-eval';
2
2
  import { ParserService } from './parser.service';
3
3
  import { AnyNode } from 'acorn';
4
4
  import { Context } from '../../internal/classes/common';
5
- import { EvalContext, EvalOptions } from '../../internal/classes/eval';
5
+ import { EvalContext, EvalOptions, EvalState } from '../../internal/classes/eval';
6
6
  import * as i0 from "@angular/core";
7
7
  /**
8
8
  * Service for evaluating and parsing expressions.
9
9
  */
10
10
  export declare class EvalService extends BaseEval {
11
- parserService: ParserService;
11
+ protected parserService: ParserService;
12
12
  /**
13
13
  * Constructs a new instance of the EvalService class.
14
14
  * @param parserService The parser service used for parsing expressions.
15
15
  */
16
16
  constructor(parserService: ParserService);
17
- /**
18
- * Parses the given expression into an abstract syntax tree (AST).
19
- * @param expression The expression to parse.
20
- * @returns The AST representing the expression.
21
- */
22
- private parse;
23
17
  /**
24
18
  * Evaluates the given expression.
25
19
  * @param expression The expression to evaluate.
@@ -27,7 +21,16 @@ export declare class EvalService extends BaseEval {
27
21
  * @param options The evaluation options.
28
22
  * @returns The result of the evaluation.
29
23
  */
30
- eval(expression: string | AnyNode | undefined, context?: EvalContext | Context, options?: EvalOptions): unknown | undefined;
24
+ simpleEval(expression: string | AnyNode | undefined, context?: EvalContext | Context, options?: EvalOptions): unknown | undefined;
25
+ /**
26
+ * Evaluates an expression using the provided state.
27
+ *
28
+ * @param expression - The expression to evaluate.
29
+ * @param state - The state object containing variables and functions used in the evaluation.
30
+ * @returns The result of the evaluation.
31
+ * @throws If an error occurs during evaluation.
32
+ */
33
+ eval(expression: string | AnyNode | undefined, state: EvalState): unknown | undefined;
31
34
  /**
32
35
  * Asynchronously evaluates the given expression.
33
36
  * @param expression The expression to evaluate.
@@ -35,7 +38,16 @@ export declare class EvalService extends BaseEval {
35
38
  * @param options The evaluation options.
36
39
  * @returns A promise that resolves to the result of the evaluation.
37
40
  */
38
- evalAsync(expression: string | AnyNode | undefined, context?: EvalContext | Context, options?: EvalOptions): Promise<unknown | undefined>;
41
+ simpleEvalAsync(expression: string | AnyNode | undefined, context?: EvalContext | Context, options?: EvalOptions): Promise<unknown | undefined>;
42
+ /**
43
+ * Asynchronously evaluates an expression using the provided state.
44
+ *
45
+ * @param expression - The expression to evaluate. Can be a string or an AST node.
46
+ * @param state - The evaluation state.
47
+ * @returns A promise that resolves to the result of the evaluation.
48
+ * @throws If an error occurs during evaluation.
49
+ */
50
+ evalAsync(expression: string | AnyNode | undefined, state: EvalState): Promise<unknown | undefined>;
39
51
  static ɵfac: i0.ɵɵFactoryDeclaration<EvalService, never>;
40
52
  static ɵprov: i0.ɵɵInjectableDeclaration<EvalService>;
41
53
  }
@@ -1,4 +1,3 @@
1
- import { BaseEval } from './base-eval';
2
1
  import { ParserOptions } from '../../internal/interfaces';
3
2
  import { Expression, Program, AnyNode } from 'acorn';
4
3
  import * as i0 from "@angular/core";
@@ -6,8 +5,18 @@ import * as i0 from "@angular/core";
6
5
  * @description
7
6
  * Service for parsing expressions and returning ES6/ES2020 AST.
8
7
  */
9
- export declare class ParserService extends BaseEval {
8
+ export declare class ParserService {
9
+ private _parserOptions;
10
10
  private _cache?;
11
+ /**
12
+ * Gets the parser options.
13
+ */
14
+ get parserOptions(): ParserOptions;
15
+ /**
16
+ * Sets the parser options.
17
+ * @param value The parser options to set.
18
+ */
19
+ set parserOptions(value: ParserOptions);
11
20
  /**
12
21
  * Constructs a new instance of the ParserService class.
13
22
  */
@@ -1,6 +1,7 @@
1
1
  import { Context, Registry, Stack } from '../common';
2
2
  import { EvalLookup } from './eval-lookup';
3
3
  import { EvalOptions } from './eval-options';
4
+ import { EvalScope } from './eval-scope';
4
5
  /**
5
6
  * Represents the evaluation context for the code execution.
6
7
  */
@@ -18,7 +19,7 @@ export declare class EvalContext {
18
19
  /**
19
20
  * Gets the scopes registry.
20
21
  */
21
- get priorScopes(): Readonly<Context[]>;
22
+ get priorScopes(): EvalScope[];
22
23
  /**
23
24
  * Gets the scopes registry.
24
25
  */
@@ -82,4 +83,11 @@ export declare class EvalContext {
82
83
  * Pops a context from the scope stack.
83
84
  */
84
85
  pop(): void;
86
+ /**
87
+ * Converts the EvalContext instance to an object representation.
88
+ * If the original value is an instance of Registry, it converts it to an object using the toObject method of the registry.
89
+ * If the original value is an object, it returns the original object.
90
+ * @returns The object representation of the EvalContext instance.
91
+ */
92
+ toObject(): Record<string | number | symbol, unknown> | undefined;
85
93
  }
@@ -1,6 +1,7 @@
1
- import { Registry, Stack } from "../common";
1
+ import { Stack } from "../common";
2
2
  import { EvalContext } from "./eval-context";
3
3
  import { EvalOptions } from "./eval-options";
4
+ import { EvalTrace } from "./eval-trace";
4
5
  type UnknownValue = unknown | Promise<unknown>;
5
6
  /**
6
7
  * Represents the result of an evaluation.
@@ -17,10 +18,11 @@ export declare class EvalResult {
17
18
  private _context;
18
19
  private _startDate?;
19
20
  private _endDate?;
21
+ private _expression?;
20
22
  /**
21
23
  * Gets the stack of evaluated values.
22
24
  */
23
- get stack(): Readonly<Stack<UnknownValue>>;
25
+ get stack(): Stack<UnknownValue>;
24
26
  /**
25
27
  * Gets the evaluated value.
26
28
  */
@@ -48,7 +50,7 @@ export declare class EvalResult {
48
50
  /**
49
51
  * Gets the trace of evaluated values.
50
52
  */
51
- get trace(): Readonly<Registry<unknown, UnknownValue>>;
53
+ get trace(): EvalTrace;
52
54
  /**
53
55
  * Gets the evaluation context.
54
56
  */
@@ -62,6 +64,17 @@ export declare class EvalResult {
62
64
  * @returns The duration in milliseconds.
63
65
  */
64
66
  get duration(): number | undefined;
67
+ /**
68
+ * Gets the expression associated with the evaluation result.
69
+ * @returns The expression as a string, or undefined if no expression is associated.
70
+ */
71
+ get expression(): string | undefined;
72
+ /**
73
+ * Sets the expression for the evaluation result.
74
+ *
75
+ * @param value - The expression to be set.
76
+ */
77
+ set expression(value: string | undefined);
65
78
  /**
66
79
  * Creates a new instance of EvalResult.
67
80
  * @param trace The trace of evaluated values.
@@ -0,0 +1,51 @@
1
+ import { Context } from "../common";
2
+ export interface EvalScopeOptions {
3
+ global?: boolean;
4
+ caseInsensitive?: boolean;
5
+ namespace?: string;
6
+ thisArg?: unknown;
7
+ }
8
+ /**
9
+ * Represents an evaluation scope.
10
+ */
11
+ export declare class EvalScope {
12
+ type: string;
13
+ private _context;
14
+ private _options;
15
+ /**
16
+ * Gets the context of the evaluation scope.
17
+ * @returns The context object.
18
+ */
19
+ get context(): Context;
20
+ /**
21
+ * Gets the options for evaluating the scope.
22
+ * @returns The evaluation options.
23
+ */
24
+ get options(): EvalScopeOptions;
25
+ /**
26
+ * Creates a new instance of EvalScope.
27
+ * @param context - The context of the evaluation scope.
28
+ * @param options - The options for evaluating the scope.
29
+ */
30
+ constructor(context?: Context, options?: EvalScopeOptions);
31
+ /**
32
+ * Creates an instance of EvalScope from the given context and options.
33
+ *
34
+ * @param context - The context object.
35
+ * @param options - The options object.
36
+ * @returns An instance of EvalScope.
37
+ */
38
+ static fromObject(context?: Context, options?: EvalScopeOptions): EvalScope;
39
+ /**
40
+ * Gets the value of the specified key.
41
+ * @param key - The key to retrieve the value for.
42
+ * @returns The value of the specified key, or undefined if the key is not found.
43
+ */
44
+ get(key: unknown): unknown | undefined;
45
+ /**
46
+ * Sets the value of the specified key.
47
+ * @param key - The key to set the value for.
48
+ * @param value - The value to set.
49
+ */
50
+ set(key: string, value: unknown): void;
51
+ }
@@ -13,19 +13,19 @@ export declare class EvalState {
13
13
  /**
14
14
  * Gets the evaluation context.
15
15
  */
16
- get context(): Readonly<EvalContext | undefined>;
16
+ get context(): EvalContext | undefined;
17
17
  /**
18
18
  * Gets the evaluation result.
19
19
  */
20
- get result(): Readonly<EvalResult>;
20
+ get result(): EvalResult;
21
21
  /**
22
22
  * Gets the evaluation options.
23
23
  */
24
- get options(): Readonly<EvalOptions | undefined>;
24
+ get options(): EvalOptions | undefined;
25
25
  /**
26
26
  * Gets whether the evaluation is asynchronous.
27
27
  */
28
- get isAsync(): Readonly<boolean | undefined>;
28
+ get isAsync(): boolean | undefined;
29
29
  /**
30
30
  * Represents the state of an evaluation.
31
31
  * @param context The evaluation context.
@@ -0,0 +1,20 @@
1
+ import { AnyNode } from "acorn";
2
+ export interface EvalTraceItem {
3
+ start?: number;
4
+ end?: number;
5
+ type: string;
6
+ expression?: string;
7
+ value: unknown;
8
+ }
9
+ /**
10
+ * Represents a trace of evaluation for a specific code execution.
11
+ */
12
+ export declare class EvalTrace extends Array<EvalTraceItem> {
13
+ /**
14
+ * Adds a new trace item to the evaluation trace.
15
+ * @param node - The AST node associated with the trace item.
16
+ * @param value - The value of the evaluated expression.
17
+ * @param expression - The expression being evaluated.
18
+ */
19
+ add(node: AnyNode, value?: unknown, expression?: string): void;
20
+ }
@@ -0,0 +1,2 @@
1
+ import { ParserOptions } from "../../interfaces";
2
+ export declare const defaultParserOptions: ParserOptions;
@@ -3,3 +3,6 @@ export { EvalContext } from './eval-context';
3
3
  export { EvalResult } from './eval-result';
4
4
  export { EvalState } from './eval-state';
5
5
  export { EvalLookup } from './eval-lookup';
6
+ export { defaultParserOptions } from './parser-options';
7
+ export { EvalTraceItem, EvalTrace } from './eval-trace';
8
+ export { EvalScope, EvalScopeOptions } from './eval-scope';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zvenigora/ng-eval-core",
3
- "version": "0.1.104",
3
+ "version": "0.1.106",
4
4
  "description": "An expression evaluator for Angular",
5
5
  "repository": {
6
6
  "type": "git",