@pactor-app/expression 0.1.0

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.
@@ -0,0 +1,119 @@
1
+ import { Registry } from '@pactor-app/core';
2
+
3
+ /**
4
+ * 受控表达式 AST(设计文档第 16.1 节白名单节点)。
5
+ *
6
+ * 语法子集刻意保持最小:无赋值、无 new、无箭头函数、无数组字面量。
7
+ * 对象字面量(`{ name: state.x }`)仅作为白名单函数的参数载体开放
8
+ * (如 `t('key', { name: user.name })`),键仅允许标识符/字符串。
9
+ * 不支持的语法在 parse 阶段直接抛错,而不是解析后再黑名单过滤。
10
+ */
11
+ type LiteralValue = string | number | boolean | null | undefined;
12
+ interface Literal {
13
+ type: 'Literal';
14
+ value: LiteralValue;
15
+ }
16
+ interface Identifier {
17
+ type: 'Identifier';
18
+ name: string;
19
+ }
20
+ interface MemberExpression {
21
+ type: 'MemberExpression';
22
+ object: ExpressionNode;
23
+ property: ExpressionNode;
24
+ /** true: a[b];false: a.b */
25
+ computed: boolean;
26
+ }
27
+ type BinaryOperator = '==' | '!=' | '===' | '!==' | '<' | '<=' | '>' | '>=' | '+' | '-' | '*' | '/' | '%';
28
+ interface BinaryExpression {
29
+ type: 'BinaryExpression';
30
+ operator: BinaryOperator;
31
+ left: ExpressionNode;
32
+ right: ExpressionNode;
33
+ }
34
+ type LogicalOperator = '&&' | '||';
35
+ interface LogicalExpression {
36
+ type: 'LogicalExpression';
37
+ operator: LogicalOperator;
38
+ left: ExpressionNode;
39
+ right: ExpressionNode;
40
+ }
41
+ type UnaryOperator = '!' | '-' | '+';
42
+ interface UnaryExpression {
43
+ type: 'UnaryExpression';
44
+ operator: UnaryOperator;
45
+ argument: ExpressionNode;
46
+ }
47
+ interface ConditionalExpression {
48
+ type: 'ConditionalExpression';
49
+ test: ExpressionNode;
50
+ consequent: ExpressionNode;
51
+ alternate: ExpressionNode;
52
+ }
53
+ /**
54
+ * 白名单函数调用。callee 只允许直接 Identifier(如 `permission('x')`),
55
+ * 不允许成员方法调用(`obj.method()`),防止经原型链逃逸。
56
+ */
57
+ interface CallExpression {
58
+ type: 'CallExpression';
59
+ callee: Identifier;
60
+ arguments: ExpressionNode[];
61
+ }
62
+ /**
63
+ * 对象字面量(`{ key: value, ... }`):键为标识符或字符串,
64
+ * 值为任意表达式。主要作为白名单函数的参数表(如 t() 的插值参数)。
65
+ */
66
+ interface ObjectExpression {
67
+ type: 'ObjectExpression';
68
+ properties: Array<{
69
+ key: string;
70
+ value: ExpressionNode;
71
+ }>;
72
+ }
73
+ type ExpressionNode = Literal | Identifier | MemberExpression | BinaryExpression | LogicalExpression | UnaryExpression | ConditionalExpression | CallExpression | ObjectExpression;
74
+
75
+ /** 表达式语法 / 求值错误,携带源码位置信息 */
76
+ declare class ExpressionError extends Error {
77
+ readonly pos?: number | undefined;
78
+ constructor(message: string, pos?: number | undefined);
79
+ }
80
+
81
+ /**
82
+ * Recursive descent parser。
83
+ *
84
+ * 优先级(低 → 高):
85
+ * conditional → logicalOr → logicalAnd → equality → relational
86
+ * → additive → multiplicative → unary → member/call → primary
87
+ */
88
+ declare function parseExpression(source: string): ExpressionNode;
89
+
90
+ /** 白名单函数签名 */
91
+ type ExpressionFunction = (...args: unknown[]) => unknown;
92
+ /** 求值作用域:由调用方组装的普通对象(state/data/form/row/route/user 等) */
93
+ type ExpressionScope = Record<string, unknown>;
94
+ interface EvaluateOptions {
95
+ /** 额外的白名单函数表(Record 或 @pactor-app/core Registry) */
96
+ functions?: Record<string, ExpressionFunction> | Registry<ExpressionFunction>;
97
+ }
98
+ /**
99
+ * 解析并求值表达式。
100
+ *
101
+ * @param source 表达式源码(不含 `${}` 定界符)或已解析的 AST
102
+ * @param scope 求值作用域,由调用方组装
103
+ */
104
+ declare function evaluateExpression(source: string | ExpressionNode, scope?: ExpressionScope, options?: EvaluateOptions): unknown;
105
+
106
+ /** 字符串中是否包含 `${...}` 表达式 */
107
+ declare function hasExpression(value: string): boolean;
108
+ /**
109
+ * 求值 DSL 模板字符串。
110
+ *
111
+ * - 字符串整体恰好是一个 `${...}`:返回表达式的原始类型值
112
+ * (保留 boolean / number / object / array)。
113
+ * - 内嵌 `${...}` 与其他文本混合:各表达式结果字符串化后拼接,
114
+ * null / undefined 渲染为空串。
115
+ * - 不含 `${` 的普通字符串:原样返回。
116
+ */
117
+ declare function resolveTemplate(value: string, scope?: ExpressionScope, options?: EvaluateOptions): unknown;
118
+
119
+ export { type BinaryExpression, type BinaryOperator, type CallExpression, type ConditionalExpression, type EvaluateOptions, ExpressionError, type ExpressionFunction, type ExpressionNode, type ExpressionScope, type Identifier, type Literal, type LiteralValue, type LogicalExpression, type LogicalOperator, type MemberExpression, type UnaryExpression, type UnaryOperator, evaluateExpression, hasExpression, parseExpression, resolveTemplate };
@@ -0,0 +1,119 @@
1
+ import { Registry } from '@pactor-app/core';
2
+
3
+ /**
4
+ * 受控表达式 AST(设计文档第 16.1 节白名单节点)。
5
+ *
6
+ * 语法子集刻意保持最小:无赋值、无 new、无箭头函数、无数组字面量。
7
+ * 对象字面量(`{ name: state.x }`)仅作为白名单函数的参数载体开放
8
+ * (如 `t('key', { name: user.name })`),键仅允许标识符/字符串。
9
+ * 不支持的语法在 parse 阶段直接抛错,而不是解析后再黑名单过滤。
10
+ */
11
+ type LiteralValue = string | number | boolean | null | undefined;
12
+ interface Literal {
13
+ type: 'Literal';
14
+ value: LiteralValue;
15
+ }
16
+ interface Identifier {
17
+ type: 'Identifier';
18
+ name: string;
19
+ }
20
+ interface MemberExpression {
21
+ type: 'MemberExpression';
22
+ object: ExpressionNode;
23
+ property: ExpressionNode;
24
+ /** true: a[b];false: a.b */
25
+ computed: boolean;
26
+ }
27
+ type BinaryOperator = '==' | '!=' | '===' | '!==' | '<' | '<=' | '>' | '>=' | '+' | '-' | '*' | '/' | '%';
28
+ interface BinaryExpression {
29
+ type: 'BinaryExpression';
30
+ operator: BinaryOperator;
31
+ left: ExpressionNode;
32
+ right: ExpressionNode;
33
+ }
34
+ type LogicalOperator = '&&' | '||';
35
+ interface LogicalExpression {
36
+ type: 'LogicalExpression';
37
+ operator: LogicalOperator;
38
+ left: ExpressionNode;
39
+ right: ExpressionNode;
40
+ }
41
+ type UnaryOperator = '!' | '-' | '+';
42
+ interface UnaryExpression {
43
+ type: 'UnaryExpression';
44
+ operator: UnaryOperator;
45
+ argument: ExpressionNode;
46
+ }
47
+ interface ConditionalExpression {
48
+ type: 'ConditionalExpression';
49
+ test: ExpressionNode;
50
+ consequent: ExpressionNode;
51
+ alternate: ExpressionNode;
52
+ }
53
+ /**
54
+ * 白名单函数调用。callee 只允许直接 Identifier(如 `permission('x')`),
55
+ * 不允许成员方法调用(`obj.method()`),防止经原型链逃逸。
56
+ */
57
+ interface CallExpression {
58
+ type: 'CallExpression';
59
+ callee: Identifier;
60
+ arguments: ExpressionNode[];
61
+ }
62
+ /**
63
+ * 对象字面量(`{ key: value, ... }`):键为标识符或字符串,
64
+ * 值为任意表达式。主要作为白名单函数的参数表(如 t() 的插值参数)。
65
+ */
66
+ interface ObjectExpression {
67
+ type: 'ObjectExpression';
68
+ properties: Array<{
69
+ key: string;
70
+ value: ExpressionNode;
71
+ }>;
72
+ }
73
+ type ExpressionNode = Literal | Identifier | MemberExpression | BinaryExpression | LogicalExpression | UnaryExpression | ConditionalExpression | CallExpression | ObjectExpression;
74
+
75
+ /** 表达式语法 / 求值错误,携带源码位置信息 */
76
+ declare class ExpressionError extends Error {
77
+ readonly pos?: number | undefined;
78
+ constructor(message: string, pos?: number | undefined);
79
+ }
80
+
81
+ /**
82
+ * Recursive descent parser。
83
+ *
84
+ * 优先级(低 → 高):
85
+ * conditional → logicalOr → logicalAnd → equality → relational
86
+ * → additive → multiplicative → unary → member/call → primary
87
+ */
88
+ declare function parseExpression(source: string): ExpressionNode;
89
+
90
+ /** 白名单函数签名 */
91
+ type ExpressionFunction = (...args: unknown[]) => unknown;
92
+ /** 求值作用域:由调用方组装的普通对象(state/data/form/row/route/user 等) */
93
+ type ExpressionScope = Record<string, unknown>;
94
+ interface EvaluateOptions {
95
+ /** 额外的白名单函数表(Record 或 @pactor-app/core Registry) */
96
+ functions?: Record<string, ExpressionFunction> | Registry<ExpressionFunction>;
97
+ }
98
+ /**
99
+ * 解析并求值表达式。
100
+ *
101
+ * @param source 表达式源码(不含 `${}` 定界符)或已解析的 AST
102
+ * @param scope 求值作用域,由调用方组装
103
+ */
104
+ declare function evaluateExpression(source: string | ExpressionNode, scope?: ExpressionScope, options?: EvaluateOptions): unknown;
105
+
106
+ /** 字符串中是否包含 `${...}` 表达式 */
107
+ declare function hasExpression(value: string): boolean;
108
+ /**
109
+ * 求值 DSL 模板字符串。
110
+ *
111
+ * - 字符串整体恰好是一个 `${...}`:返回表达式的原始类型值
112
+ * (保留 boolean / number / object / array)。
113
+ * - 内嵌 `${...}` 与其他文本混合:各表达式结果字符串化后拼接,
114
+ * null / undefined 渲染为空串。
115
+ * - 不含 `${` 的普通字符串:原样返回。
116
+ */
117
+ declare function resolveTemplate(value: string, scope?: ExpressionScope, options?: EvaluateOptions): unknown;
118
+
119
+ export { type BinaryExpression, type BinaryOperator, type CallExpression, type ConditionalExpression, type EvaluateOptions, ExpressionError, type ExpressionFunction, type ExpressionNode, type ExpressionScope, type Identifier, type Literal, type LiteralValue, type LogicalExpression, type LogicalOperator, type MemberExpression, type UnaryExpression, type UnaryOperator, evaluateExpression, hasExpression, parseExpression, resolveTemplate };