@formvk/schema 0.1.0 → 1.0.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.
- package/CHANGELOG.md +14 -0
- package/README.md +66 -0
- package/package.json +8 -4
- package/src/__tests__/compiler.spec.ts +0 -334
- package/src/__tests__/create-schema.spec.ts +0 -83
- package/src/__tests__/expression.spec.ts +0 -174
- package/src/__tests__/fragment-types.spec.ts +0 -576
- package/src/__tests__/fragment.spec.ts +0 -380
- package/src/__tests__/schema.spec.ts +0 -325
- package/src/__tests__/server-validate.spec.ts +0 -135
- package/src/__tests__/shared.spec.ts +0 -28
- package/src/__tests__/transformer.spec.ts +0 -531
- package/src/__tests__/traverse.spec.ts +0 -77
- package/src/compiler.ts +0 -123
- package/src/create-schema.ts +0 -42
- package/src/expression.ts +0 -355
- package/src/fragment-types.ts +0 -497
- package/src/fragment.ts +0 -229
- package/src/index.ts +0 -6
- package/src/patches.ts +0 -22
- package/src/schema.ts +0 -307
- package/src/shared.ts +0 -226
- package/src/transformer.ts +0 -251
- package/src/types.ts +0 -295
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -4
- package/vite.config.ts +0 -19
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { FormPath } from '@formvk/shared'
|
|
2
|
-
import { expect, test } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import { traverse, traverseSchema } from '../shared'
|
|
5
|
-
|
|
6
|
-
test('traverseSchema', () => {
|
|
7
|
-
const visited: any[] = []
|
|
8
|
-
const omitted: any[] = []
|
|
9
|
-
traverseSchema(
|
|
10
|
-
{
|
|
11
|
-
type: 'string',
|
|
12
|
-
title: '{{aa}}',
|
|
13
|
-
required: true,
|
|
14
|
-
validator: 'phone',
|
|
15
|
-
compileOmitted: ['title'],
|
|
16
|
-
default: {
|
|
17
|
-
input: 123,
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
(value, path, omitCompile) => {
|
|
21
|
-
if (omitCompile) {
|
|
22
|
-
omitted.push(value)
|
|
23
|
-
} else {
|
|
24
|
-
visited.push(path)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
)
|
|
28
|
-
expect(visited).toEqual([['validator'], ['type'], ['required'], ['default']])
|
|
29
|
-
expect(omitted).toEqual(['{{aa}}'])
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
test('traverseSchema visits custom extension functions', () => {
|
|
33
|
-
const visited: any[] = []
|
|
34
|
-
function fn() {}
|
|
35
|
-
traverseSchema({ type: 'string', xCustomFn: fn } as any, (value, path) => {
|
|
36
|
-
visited.push([path.join('.'), value])
|
|
37
|
-
})
|
|
38
|
-
expect(visited.some(([path]) => path === 'xCustomFn')).toBeTruthy()
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test('traverse circular reference', () => {
|
|
42
|
-
const a: any = {
|
|
43
|
-
dd: {
|
|
44
|
-
mm: null,
|
|
45
|
-
},
|
|
46
|
-
bb: {
|
|
47
|
-
cc: {
|
|
48
|
-
dd: 123,
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
kk: {
|
|
52
|
-
toJS() {},
|
|
53
|
-
},
|
|
54
|
-
}
|
|
55
|
-
a.dd.mm = a
|
|
56
|
-
traverse(a, () => {})
|
|
57
|
-
traverseSchema(a as any, () => {})
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test('traverse none circular reference', () => {
|
|
61
|
-
const dd = {
|
|
62
|
-
mm: null,
|
|
63
|
-
}
|
|
64
|
-
const a = {
|
|
65
|
-
dd,
|
|
66
|
-
bb: {
|
|
67
|
-
dd,
|
|
68
|
-
},
|
|
69
|
-
}
|
|
70
|
-
const paths: any[] = []
|
|
71
|
-
traverse(a, (value, path) => {
|
|
72
|
-
paths.push(path)
|
|
73
|
-
})
|
|
74
|
-
traverseSchema(a, () => {})
|
|
75
|
-
expect(paths.some(path => FormPath.parse(path).includes('dd.mm'))).toBeTruthy()
|
|
76
|
-
expect(paths.some(path => FormPath.parse(path).includes('bb.dd.mm'))).toBeTruthy()
|
|
77
|
-
})
|
package/src/compiler.ts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import type { IGeneralFieldState } from '@formvk/core'
|
|
2
|
-
import { untracked } from '@formvk/reactive'
|
|
3
|
-
import { FormPath, isArr, isFn, isPlainObj, isStr, reduce } from '@formvk/shared'
|
|
4
|
-
|
|
5
|
-
import { isNoNeedCompileObject, patchStateFormSchema, traverse, traverseSchema } from './shared'
|
|
6
|
-
import type { ISchema } from './types'
|
|
7
|
-
|
|
8
|
-
const ExpRE = /^\s*\{\{([\s\S]*)\}\}\s*$/
|
|
9
|
-
|
|
10
|
-
const Registry = {
|
|
11
|
-
silent: false,
|
|
12
|
-
compile(expression: string, scope: any = {}) {
|
|
13
|
-
if (Registry.silent) {
|
|
14
|
-
try {
|
|
15
|
-
return new Function('$root', `with($root) { return (${expression}); }`)(scope)
|
|
16
|
-
} catch {
|
|
17
|
-
return undefined
|
|
18
|
-
}
|
|
19
|
-
} else {
|
|
20
|
-
return new Function('$root', `with($root) { return (${expression}); }`)(scope)
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function silent(value = true) {
|
|
26
|
-
Registry.silent = !!value
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function registerCompiler(compiler: (expression: string, scope: any) => any) {
|
|
30
|
-
if (isFn(compiler)) {
|
|
31
|
-
Registry.compile = compiler
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function shallowCompile<Source = any, Scope = any>(source: Source, scope?: Scope) {
|
|
36
|
-
if (isStr(source)) {
|
|
37
|
-
const matched = source.match(ExpRE)
|
|
38
|
-
if (!matched) return source
|
|
39
|
-
return Registry.compile(matched[1], scope)
|
|
40
|
-
}
|
|
41
|
-
return source
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 静态判断值中是否包含 `{{expression}}` 表达式。
|
|
46
|
-
* 用于 demand 模式下判断 schema 补丁是否存在动态依赖,
|
|
47
|
-
* 等价于 formily 的 `hasCollected`,但不需要额外的响应式收集标记。
|
|
48
|
-
*/
|
|
49
|
-
function hasExpression(source: any): boolean {
|
|
50
|
-
if (isStr(source)) {
|
|
51
|
-
return ExpRE.test(source)
|
|
52
|
-
}
|
|
53
|
-
if (isArr(source)) {
|
|
54
|
-
return source.some(hasExpression)
|
|
55
|
-
}
|
|
56
|
-
if (isPlainObj(source)) {
|
|
57
|
-
if (isNoNeedCompileObject(source)) return false
|
|
58
|
-
return Object.keys(source).some(key => hasExpression((source as any)[key]))
|
|
59
|
-
}
|
|
60
|
-
return false
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function compile<Source = any, Scope = any>(source: Source, scope?: Scope): any {
|
|
64
|
-
const seenObjects: any[] = []
|
|
65
|
-
function compile(source: any): any {
|
|
66
|
-
if (isStr(source)) {
|
|
67
|
-
return shallowCompile(source, scope)
|
|
68
|
-
} else if (isArr(source)) {
|
|
69
|
-
return source.map((value: any) => compile(value))
|
|
70
|
-
} else if (isPlainObj(source)) {
|
|
71
|
-
if (isNoNeedCompileObject(source)) return source
|
|
72
|
-
const seenIndex = seenObjects.indexOf(source)
|
|
73
|
-
if (seenIndex > -1) {
|
|
74
|
-
return source
|
|
75
|
-
}
|
|
76
|
-
const addIndex = seenObjects.length
|
|
77
|
-
seenObjects.push(source)
|
|
78
|
-
const results = reduce(
|
|
79
|
-
source,
|
|
80
|
-
(buf, value, key) => {
|
|
81
|
-
;(buf as any)[key] = compile(value)
|
|
82
|
-
return buf
|
|
83
|
-
},
|
|
84
|
-
{} as Record<string, any>
|
|
85
|
-
)
|
|
86
|
-
seenObjects.splice(addIndex, 1)
|
|
87
|
-
return results
|
|
88
|
-
}
|
|
89
|
-
return source
|
|
90
|
-
}
|
|
91
|
-
return compile(source)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function patchCompile(targetState: IGeneralFieldState, sourceState: any, scope: any) {
|
|
95
|
-
traverse(sourceState, (value, pattern) => {
|
|
96
|
-
const compiled = compile(value, scope)
|
|
97
|
-
if (compiled === undefined) return
|
|
98
|
-
const path = FormPath.parse(pattern)
|
|
99
|
-
const key = path.segments[0]
|
|
100
|
-
// 字段状态的 title/description 等是原型链上的 accessor,因此用 in 而不是 hasOwnProperty
|
|
101
|
-
if (key in targetState) {
|
|
102
|
-
untracked(() => FormPath.setIn(targetState as any, path, compiled))
|
|
103
|
-
}
|
|
104
|
-
})
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function patchSchemaCompile(targetState: IGeneralFieldState, sourceSchema: ISchema, scope: any, demand = false) {
|
|
108
|
-
traverseSchema(sourceSchema, (value, path, omitCompile) => {
|
|
109
|
-
const dynamic = hasExpression(value)
|
|
110
|
-
let compiled = value
|
|
111
|
-
if (!omitCompile) {
|
|
112
|
-
compiled = compile(value, scope)
|
|
113
|
-
}
|
|
114
|
-
if (compiled === undefined) return
|
|
115
|
-
if (demand) {
|
|
116
|
-
if (dynamic || !(targetState as any).initialized) {
|
|
117
|
-
patchStateFormSchema(targetState, path, compiled)
|
|
118
|
-
}
|
|
119
|
-
} else {
|
|
120
|
-
patchStateFormSchema(targetState, path, compiled)
|
|
121
|
-
}
|
|
122
|
-
})
|
|
123
|
-
}
|
package/src/create-schema.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
SchemaCreator,
|
|
3
|
-
SchemaCreatorLike,
|
|
4
|
-
SchemaPatchInput,
|
|
5
|
-
SchemaPropertiesValues,
|
|
6
|
-
ValidateSchemaProperties,
|
|
7
|
-
} from './fragment-types'
|
|
8
|
-
import type { ISchema } from './types'
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 对象 schema 快捷组装器。
|
|
12
|
-
* 默认不内置任何 patch,creator 使用传入的 patches 展开。
|
|
13
|
-
*/
|
|
14
|
-
export function createSchema<
|
|
15
|
-
Properties extends Record<string, unknown>,
|
|
16
|
-
IValues extends SchemaPropertiesValues<Properties>,
|
|
17
|
-
>(
|
|
18
|
-
properties: Properties & ValidateSchemaProperties<Properties>,
|
|
19
|
-
patches?: SchemaPatchInput | SchemaPatchInput[]
|
|
20
|
-
): ISchema & {
|
|
21
|
-
_$types$_: [IValues]
|
|
22
|
-
} {
|
|
23
|
-
const result = {
|
|
24
|
-
type: 'object',
|
|
25
|
-
properties: {},
|
|
26
|
-
} as ISchema & {
|
|
27
|
-
_$types$_: [IValues]
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
Object.keys(properties).forEach(key => {
|
|
31
|
-
const generator = properties[key]
|
|
32
|
-
if (typeof generator === 'function') {
|
|
33
|
-
result.properties![key] = (generator as SchemaCreatorLike)(patches)
|
|
34
|
-
} else if (generator && typeof generator === 'object') {
|
|
35
|
-
result.properties![key] = generator as ISchema
|
|
36
|
-
}
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
return result
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export type { SchemaCreator }
|
package/src/expression.ts
DELETED
|
@@ -1,355 +0,0 @@
|
|
|
1
|
-
import type { SchemaExpression } from './types'
|
|
2
|
-
|
|
3
|
-
const variableName = /^[$A-Za-z_][0-9A-Za-z_$]*$/
|
|
4
|
-
|
|
5
|
-
export interface ExpressionFnOptions {
|
|
6
|
-
/**
|
|
7
|
-
* 是否展开参数
|
|
8
|
-
* @default true
|
|
9
|
-
* @example
|
|
10
|
-
* objExp.fn.b('$args', { expand: false }) // {{function __b($args) {} return obj.b($args)}}
|
|
11
|
-
* objExp.fn.b('$args', { expand: true }) // {{function __b(...$args) {} return obj.b(...$args)}}
|
|
12
|
-
*/
|
|
13
|
-
expand?: boolean
|
|
14
|
-
/**
|
|
15
|
-
* 函数体
|
|
16
|
-
* @example
|
|
17
|
-
* objExp.fn.b('$args', 'title', { body: 'var title = $self.title' })
|
|
18
|
-
* `{{function __b(...$args) {
|
|
19
|
-
* var title = $self.title;
|
|
20
|
-
* return obj.b(...$args, title)
|
|
21
|
-
* }}}`
|
|
22
|
-
*/
|
|
23
|
-
body?: string
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export type Args = '$args' | '$event' | ExpressionFnOptions | (string & {})
|
|
27
|
-
|
|
28
|
-
export type IExpressionStr<T, IsExp extends boolean> = T extends object
|
|
29
|
-
? {
|
|
30
|
-
[K in keyof T]-?: ExpressionStr<IsExp> & (NonNullable<T[K]> & {}) & IExpressionStr<T[K], IsExp>
|
|
31
|
-
}
|
|
32
|
-
: NonNullable<T>
|
|
33
|
-
|
|
34
|
-
export type IExpressionFn<T> = {
|
|
35
|
-
[K in keyof T]-?: T[K] extends (...args: any[]) => any
|
|
36
|
-
? (args?: Args, ..._args: Args[]) => SchemaExpression
|
|
37
|
-
: T[K] extends object
|
|
38
|
-
? IExpressionFn<T[K]>
|
|
39
|
-
: never
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export type Expression<T> = IExpressionStr<T, true> & {
|
|
43
|
-
/**
|
|
44
|
-
* 返回其中函数表达式集合
|
|
45
|
-
* $args 表示函数的参数集合
|
|
46
|
-
* $event 表示函数的第一个参数
|
|
47
|
-
* $args 和 $event 不能同时传入
|
|
48
|
-
* @example
|
|
49
|
-
* objExp.fn.b('$args') // {{function __b(...$args) {} return obj.b(...$args)}}
|
|
50
|
-
* objExp.fn.b('$event') // {{function __b($event) {} return obj.b($event)}}
|
|
51
|
-
* objExp.fn.b('$self') // {{function __b() {} return obj.b($self)}}
|
|
52
|
-
* // 不可以同时传入 $args 和 $event
|
|
53
|
-
* objExp.fn.b('$args', '$event') // Error
|
|
54
|
-
* objExp.fn.b('$args', '$self') // {{function __b(...$args) {} return obj.b(...$args, $self)}}
|
|
55
|
-
* objExp.fn.b('$event', '$self') // {{function __b($event) {} return obj.b($event, $self)}}
|
|
56
|
-
*/
|
|
57
|
-
fn: IExpressionFn<T>
|
|
58
|
-
/**
|
|
59
|
-
* 返回其中属性表达式集合
|
|
60
|
-
* @example
|
|
61
|
-
* objExp.exp.a // obj.a
|
|
62
|
-
* objExp.exp['a.b'] // obj['a.b']
|
|
63
|
-
* objExp.exp.a.b.c // obj.a.b.c
|
|
64
|
-
*/
|
|
65
|
-
exp: IExpressionStr<T, false>
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
class ExpressionStr<
|
|
69
|
-
IsExp extends boolean,
|
|
70
|
-
Result extends (IsExp extends true ? SchemaExpression : string & {}) = IsExp extends true ? SchemaExpression : string,
|
|
71
|
-
> {
|
|
72
|
-
constructor(
|
|
73
|
-
private expression: string,
|
|
74
|
-
private isExp: IsExp = false as IsExp
|
|
75
|
-
) {}
|
|
76
|
-
|
|
77
|
-
getExpression(str: string) {
|
|
78
|
-
return (this.isExp ? `{{${str}}}` : str) as Result
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* 等于
|
|
83
|
-
* @example
|
|
84
|
-
* objExp.exp.a.eq(1) // obj.a === 1
|
|
85
|
-
* objExp.exp.a.b.c.eq('1') // obj.a.b.c === "1"
|
|
86
|
-
* objExp.exp.a.b.c.eq('$values.d', true) // obj.a.b.c === $values.d
|
|
87
|
-
*/
|
|
88
|
-
eq(str: any, exp?: boolean) {
|
|
89
|
-
return `${this.expression} === ${exp ? str : JSON.stringify(str)}` as Result
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* 不等于
|
|
94
|
-
* @example
|
|
95
|
-
* objExp.exp.a.neq(1) // obj.a !== 1
|
|
96
|
-
* objExp.exp.a.neq('$values.b', true) // obj.a !== $values.b
|
|
97
|
-
*/
|
|
98
|
-
neq(str: any, exp?: boolean) {
|
|
99
|
-
return `${this.expression} !== ${exp ? str : JSON.stringify(str)}` as Result
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 大于
|
|
104
|
-
* @example
|
|
105
|
-
* objExp.exp.a.gt('1') // obj.a > 1
|
|
106
|
-
* objExp.exp.a.b.c.gt('1') // obj.a.b.c > 1
|
|
107
|
-
*/
|
|
108
|
-
gt(str: any, exp?: boolean) {
|
|
109
|
-
return `${this.expression} > ${exp ? str : JSON.stringify(str)}` as Result
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* @example
|
|
114
|
-
* objExp.exp.a.gte('1') // obj.a >= 1
|
|
115
|
-
* objExp.exp.a.b.c.gte('1') // obj.a.b.c >= 1
|
|
116
|
-
*/
|
|
117
|
-
gte(str: any, exp?: boolean) {
|
|
118
|
-
return `${this.expression} >= ${exp ? str : JSON.stringify(str)}` as Result
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* 小于
|
|
123
|
-
* @example
|
|
124
|
-
* objExp.exp.a.lt('1') // obj.a < 1
|
|
125
|
-
* objExp.exp.a.b.c.lt('1') // obj.a.b.c < 1
|
|
126
|
-
*/
|
|
127
|
-
lt(str: any, exp?: boolean) {
|
|
128
|
-
return `${this.expression} < ${exp ? str : JSON.stringify(str)}` as Result
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 小于等于
|
|
133
|
-
* @example
|
|
134
|
-
* objExp.exp.a.lte(1) // obj.a <= 1
|
|
135
|
-
* objExp.exp.a.b.c.lte('$values.d', true) // obj.a.b.c <= $values.d
|
|
136
|
-
*/
|
|
137
|
-
lte(str: any, exp?: boolean) {
|
|
138
|
-
return `${this.expression} <= ${exp ? str : JSON.stringify(str)}` as Result
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* 与
|
|
143
|
-
* @example
|
|
144
|
-
* objExp.exp.a.and('1') // obj.a && 1
|
|
145
|
-
*/
|
|
146
|
-
and(str: any, exp?: boolean) {
|
|
147
|
-
return `${this.expression} && ${exp ? str : JSON.stringify(str)}` as Result
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* 或
|
|
152
|
-
* @example
|
|
153
|
-
* objExp.exp.a.or(1) // obj.a || 1
|
|
154
|
-
* objExp.exp.a.b.c.or('1') // obj.a.b.c || '1'
|
|
155
|
-
* objExp.exp.a.or('1 || 2', true) // obj.a || 1 || 2
|
|
156
|
-
* objExp.exp.a.or({b: 1}) // obj.a || {"b": 1}
|
|
157
|
-
*/
|
|
158
|
-
or(str: any, exp?: boolean) {
|
|
159
|
-
return `${this.expression} || ${exp ? str : JSON.stringify(str)}` as Result
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* 取反
|
|
164
|
-
* @example
|
|
165
|
-
* objExp.exp.a.not() // !obj.a
|
|
166
|
-
* objExp.exp.a.b.c.not() // !obj.a.b.c
|
|
167
|
-
*/
|
|
168
|
-
not() {
|
|
169
|
-
return `!${this.expression}` as Result
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* 取bool值
|
|
173
|
-
* @example
|
|
174
|
-
* objExp.exp.a.bool() // !!obj.a
|
|
175
|
-
* objExp.exp.a.b.c.bool() // !!obj.a.b.c
|
|
176
|
-
*/
|
|
177
|
-
bool() {
|
|
178
|
-
return `!!${this.expression}` as Result
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* 三元表达式
|
|
183
|
-
* @example
|
|
184
|
-
* objExp.exp.a.ter(1, 2) // obj.a ? 1 : 2
|
|
185
|
-
* objExp.exp.a.b.c.ter('1', '2') // obj.a.b.c ? "1" : "2"
|
|
186
|
-
* objExp.exp.a.ter("obj.a", "abj.b", true) // obj.a ? obj.a : abj.b
|
|
187
|
-
*/
|
|
188
|
-
ter(a: any, b: any, exp?: boolean) {
|
|
189
|
-
return `${this.expression} ? ${exp ? a : JSON.stringify(a)} : ${exp ? b : JSON.stringify(b)}` as Result
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export function getExpression(expression: string, key: string): string {
|
|
194
|
-
const isVariableName = variableName.test(key)
|
|
195
|
-
|
|
196
|
-
return `${expression}${isVariableName ? `.${key}` : `[${JSON.stringify(key)}]`}`
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function getLastArg(args: unknown[]): ExpressionFnOptions {
|
|
200
|
-
const lastArg = args[args.length - 1]
|
|
201
|
-
if (lastArg !== null && typeof lastArg === 'object') {
|
|
202
|
-
return lastArg as ExpressionFnOptions
|
|
203
|
-
}
|
|
204
|
-
return {}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function getExpressionFn(expression: string, key: string): (...args: any[]) => any {
|
|
208
|
-
const isVariableName = variableName.test(key)
|
|
209
|
-
const funcName = isVariableName ? `__${key}` : '__IS_NOT_VARIABLE_NAME__'
|
|
210
|
-
const expStr = getExpression(expression, key)
|
|
211
|
-
const fn = function (...args: unknown[]) {
|
|
212
|
-
const has$event = args.includes('$event')
|
|
213
|
-
const has$args = args.includes('$args')
|
|
214
|
-
if (has$event && has$args) {
|
|
215
|
-
throw new Error('$event and $args cannot be passed in at the same time')
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
const lastArg = getLastArg(args)
|
|
219
|
-
const { expand = true, body = '' } = lastArg
|
|
220
|
-
if (body.includes('return')) {
|
|
221
|
-
throw new Error('body cannot contain return')
|
|
222
|
-
}
|
|
223
|
-
const funcArgs = args
|
|
224
|
-
.map(arg => {
|
|
225
|
-
if (arg === '$args') {
|
|
226
|
-
return expand ? '...$args' : '$args'
|
|
227
|
-
}
|
|
228
|
-
if (typeof arg === 'string') {
|
|
229
|
-
return arg
|
|
230
|
-
}
|
|
231
|
-
})
|
|
232
|
-
.filter(Boolean)
|
|
233
|
-
.join(', ')
|
|
234
|
-
|
|
235
|
-
const funcBody = `
|
|
236
|
-
${body}
|
|
237
|
-
return ${expStr}(${funcArgs})`
|
|
238
|
-
const $args = has$args ? (expand ? '...$args' : '$args') : has$event ? '$event' : ''
|
|
239
|
-
return `{{function ${funcName}(${$args}) {${funcBody}
|
|
240
|
-
}}}`
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
return new Proxy(fn, {
|
|
244
|
-
get(_, key) {
|
|
245
|
-
if (typeof key !== 'string') {
|
|
246
|
-
throw new Error('key must be string')
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
return getExpressionFn(expStr, key)
|
|
250
|
-
},
|
|
251
|
-
apply(_, __, args) {
|
|
252
|
-
return fn(...args)
|
|
253
|
-
},
|
|
254
|
-
})
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
export function createExpressionFn<T>(expression: string): Expression<T>['fn'] {
|
|
258
|
-
const fn = {} as Expression<T>['fn']
|
|
259
|
-
|
|
260
|
-
return new Proxy(fn, {
|
|
261
|
-
get(_, key) {
|
|
262
|
-
if (typeof key !== 'string') {
|
|
263
|
-
throw new Error('key must be string')
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
return getExpressionFn(expression, key)
|
|
267
|
-
},
|
|
268
|
-
})
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
function createExpressionStr(expression: string, isExp = false) {
|
|
272
|
-
const expressionStr = new ExpressionStr(expression, isExp)
|
|
273
|
-
const exp = expressionStr.getExpression(expression)
|
|
274
|
-
|
|
275
|
-
const str = new String(exp)
|
|
276
|
-
|
|
277
|
-
return new Proxy(str, {
|
|
278
|
-
get(_, key, receiver) {
|
|
279
|
-
if (typeof key === 'symbol') {
|
|
280
|
-
if (key === Symbol.toPrimitive) {
|
|
281
|
-
return () => exp
|
|
282
|
-
}
|
|
283
|
-
if (key === Symbol.toStringTag) {
|
|
284
|
-
return 'String'
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return Reflect.get(str, key, receiver).bind(exp)
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
// 过滤掉 __ 开头的属性
|
|
291
|
-
// 过滤掉 String.prototype 上的属性
|
|
292
|
-
// 过滤掉 Object.prototype 上的属性
|
|
293
|
-
if (key.startsWith('__') || key in String.prototype || key in Object.prototype) {
|
|
294
|
-
const result = Reflect.get(str, key, receiver)
|
|
295
|
-
if (typeof result === 'function') {
|
|
296
|
-
return result.bind(exp)
|
|
297
|
-
}
|
|
298
|
-
return result
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// 过滤掉 toJSON 方法
|
|
302
|
-
if (key === 'toJSON') {
|
|
303
|
-
return () => exp
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
if (typeof expressionStr[key as keyof ExpressionStr<false>] === 'function') {
|
|
307
|
-
return function (...args: unknown[]) {
|
|
308
|
-
const result = (expressionStr[key as keyof ExpressionStr<false>] as (...args: any[]) => any)(...args)
|
|
309
|
-
return expressionStr.getExpression(result)
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
const expStr = getExpression(expression, key)
|
|
313
|
-
|
|
314
|
-
return createExpressionStr(expStr, isExp)
|
|
315
|
-
},
|
|
316
|
-
})
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* 返回一个对象的表达式主要方便于类型推导
|
|
321
|
-
* @param expression 对象表达式 如 'obj'
|
|
322
|
-
* @example
|
|
323
|
-
* const objExp = createExpression<{ a: number; b: () => string,'1b': string }>('obj')
|
|
324
|
-
* objExp.a // {{obj.a}}
|
|
325
|
-
* objExp['1b'] // {{obj['1b']}}
|
|
326
|
-
* objExp.fn.b() // {{function __b() {} return obj.b()}}
|
|
327
|
-
* objExp.fn.b('$args') // {{function __b(...$args) {} return obj.b(...$args)}}
|
|
328
|
-
* objExp.fn.b('$event') // {{function __b($event) {} return obj.b($event)}}
|
|
329
|
-
* objExp.fn.b('$self', '$event') // {{function __b($event) {} return obj.b($self, $event)}}
|
|
330
|
-
* objExp.fn.b('$self', '$args') // {{function __b(...$args) {} return obj.b($self, ...$args)}}
|
|
331
|
-
*/
|
|
332
|
-
export function createExpression<T extends object>(expression: string): Expression<T> {
|
|
333
|
-
const obj: Record<string, unknown> = {}
|
|
334
|
-
|
|
335
|
-
const fn = createExpressionFn<T>(expression)
|
|
336
|
-
const exp = createExpressionStr(expression)
|
|
337
|
-
return new Proxy(obj as Expression<T>, {
|
|
338
|
-
get(_, key, receiver) {
|
|
339
|
-
if (typeof key === 'symbol') {
|
|
340
|
-
const value = Reflect.get(obj, key, receiver)
|
|
341
|
-
if (typeof value === 'function') {
|
|
342
|
-
return value.bind(obj)
|
|
343
|
-
}
|
|
344
|
-
return value
|
|
345
|
-
}
|
|
346
|
-
if (key === 'fn') {
|
|
347
|
-
return fn
|
|
348
|
-
}
|
|
349
|
-
if (key === 'exp') {
|
|
350
|
-
return exp
|
|
351
|
-
}
|
|
352
|
-
return createExpressionStr(getExpression(expression, key), true)
|
|
353
|
-
},
|
|
354
|
-
})
|
|
355
|
-
}
|