soma-core 1.3.1 → 1.3.3
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/README.md +42 -0
- package/app/SomaPageClient.tsx +39 -2
- package/app/[...path]/page.tsx +7 -6
- package/app/api/[...path]/route.ts +3 -3
- package/app/api/soma-registry/[...path]/route.ts +67 -0
- package/app/components/SomaHostPage.tsx +3 -8
- package/app/components/SomaPageClientBoundary.tsx +13 -0
- package/app/layout.tsx +6 -1
- package/app/page.tsx +3 -3
- package/app/soma/manifest/route.ts +1 -0
- package/app/soma/page-object/route.ts +1 -1
- package/core/build/compiler/full-manifest-builder.ts +6 -1
- package/core/build/compiler/rollup-plugin-soma.ts +6 -0
- package/core/build/compiler/soma-manifest-webpack-plugin.js +4 -0
- package/core/build/compiler/soma-precompile.js +3507 -2387
- package/core/build/compiler/soma-precompile.ts +31 -2
- package/core/build/compiler/soma-rspack-yaml-loader.js +3 -0
- package/core/build/compiler/soma-webpack-loader.js +7 -1
- package/core/build/compiler/vite-plugin-soma.ts +11 -1
- package/core/build/component/package-loader.ts +135 -0
- package/core/build/eject/apply-eject.ts +133 -0
- package/core/build/eject/plan-eject.ts +139 -0
- package/core/client/adaptive/flush-scheduler.ts +96 -0
- package/core/client/adaptive/hysteresis.ts +56 -0
- package/core/client/adaptive/index.ts +23 -0
- package/core/client/adaptive/observer-pool.ts +90 -0
- package/core/client/base/SomaPageRootBase.ts +50 -0
- package/core/client/compiler/adaptive-compile.ts +136 -0
- package/core/client/compiler/manifest-compiler.ts +155 -27
- package/core/client/component/ai-catalog.ts +262 -0
- package/core/client/component/component-definition.types.ts +65 -0
- package/core/client/component/component-expander.ts +119 -0
- package/core/client/component/component-manager.ts +933 -0
- package/core/client/component/component-pack.ts +271 -0
- package/core/client/component/component-registry.ts +71 -0
- package/core/client/component/pack-styles.ts +94 -0
- package/core/client/component/scoped-adapter.ts +85 -0
- package/core/client/component/verify-pack.ts +247 -0
- package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
- package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
- package/core/client/container/SomaContainerAdapter.ts +186 -180
- package/core/client/data/data-object-resolver.ts +417 -0
- package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
- package/core/client/diagnostics/boot-timings.ts +152 -0
- package/core/client/engine/asset-resolver.ts +12 -2
- package/core/client/engine/dom-renderer.ts +235 -16
- package/core/client/engine/emit-event.ts +21 -0
- package/core/client/engine/event-engine.ts +34 -4
- package/core/client/engine/on-change-engine.ts +1 -1
- package/core/client/engine/property-watcher.ts +1 -1
- package/core/client/engine/reactive-proxy.ts +3 -3
- package/core/client/engine/render-engine.ts +43 -4
- package/core/client/engine/runtime-builder.ts +23 -0
- package/core/client/engine/soma-bootstrap.ts +389 -5
- package/core/client/engine/style-binder.ts +47 -1
- package/core/client/host-bridge/empty-host/data/README.md +17 -0
- package/core/client/infra/http/SomaHttpService.ts +5 -0
- package/core/client/observability/error-reporter.ts +173 -0
- package/core/client/parser/adaptive-parser.ts +264 -0
- package/core/client/parser/data-config-parser.ts +396 -0
- package/core/client/parser/pipeline-parser.ts +15 -3
- package/core/client/parser/style-config-parser.ts +65 -2
- package/core/client/parser/ui-config-parser.ts +52 -3
- package/core/client/pipes/pipe-registry.ts +249 -0
- package/core/client/quality/SomaQualityRegistry.ts +3 -0
- package/core/client/registry/declaration-renderer.ts +159 -0
- package/core/client/registry/index.ts +28 -0
- package/core/client/registry/page-lifecycle.ts +171 -0
- package/core/client/registry/remote-page.ts +139 -0
- package/core/client/runtime/SomaRuntimeServices.ts +24 -0
- package/core/client/scheduler/SomaScheduler.ts +29 -1
- package/core/client/security/attribute-presence.ts +70 -0
- package/core/client/security/html-sanitizer.ts +113 -0
- package/core/client/security/url-guard.ts +0 -0
- package/core/client/theme/theme-engine.ts +936 -0
- package/core/client/typography/typography-base.ts +34 -0
- package/core/client/validation/contract-validator.ts +48 -5
- package/core/index.ts +190 -2
- package/core/server/api/IService.ts +18 -6
- package/core/server/api/load-host-service.ts +3 -3
- package/core/server/host/content-security-policy.ts +190 -0
- package/core/server/host/csp-nonce-metadata.ts +75 -0
- package/core/server/host/resolve-host-request-hook.ts +5 -0
- package/core/server/host/server-page-assets.ts +10 -0
- package/core/server/index.ts +30 -1
- package/core/server/infra/cache/ServerFileCache.ts +1 -1
- package/core/server/mock/node/cli.mjs +2 -2
- package/core/server/registry/api-registry-store.ts +518 -0
- package/core/server/registry/conditional-object-store.ts +29 -0
- package/core/server/registry/declaration-precompiler.ts +129 -0
- package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
- package/core/server/registry/index.ts +142 -0
- package/core/server/registry/page-registry-repository.ts +373 -0
- package/core/server/registry/page-registry-store.ts +903 -0
- package/core/server/registry/registry-error.ts +17 -0
- package/core/server/registry/registry-file-lock.ts +173 -0
- package/core/server/registry/registry-response.ts +71 -0
- package/core/server/registry/registry-subjects.ts +194 -0
- package/core/server/registry/registry-write-pipeline.ts +240 -0
- package/core/server/registry/request-body-limit.ts +99 -0
- package/core/server/registry/services/apis/Get.ts +25 -0
- package/core/server/registry/services/apis/List.ts +41 -0
- package/core/server/registry/services/apis/Register.ts +39 -0
- package/core/server/registry/services/apis/Unregister.ts +24 -0
- package/core/server/registry/services/index.ts +40 -0
- package/core/server/registry/services/pages/Get.ts +86 -0
- package/core/server/registry/services/pages/List.ts +53 -0
- package/core/server/registry/services/pages/Register.ts +49 -0
- package/core/server/registry/services/pages/Rollback.ts +36 -0
- package/core/server/registry/services/pages/Unregister.ts +23 -0
- package/core/server/registry/services/service-utils.ts +21 -0
- package/core/server/registry/validation/compile-gate.ts +71 -0
- package/core/server/registry/validation/component-existence.ts +55 -0
- package/core/server/registry/validation/credential-ban.ts +86 -0
- package/core/server/registry/validation/cross-check.ts +77 -0
- package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
- package/core/shared/types/adaptive.types.ts +109 -0
- package/core/shared/types/component-contract.types.ts +110 -0
- package/core/shared/types/data-config.types.ts +105 -0
- package/core/shared/types/domain.types.ts +6 -0
- package/core/shared/types/engine.types.ts +41 -35
- package/core/shared/types/manifest.types.ts +55 -32
- package/core/shared/types/scope-manifest.types.ts +96 -0
- package/core/shared/types/style-config.types.ts +9 -0
- package/core/shared/types/ui-config.types.ts +118 -98
- package/dist/cli/soma.js +20 -5
- package/dist/core/index.cjs +5388 -888
- package/dist/types/build/compiler/full-manifest-builder.d.ts +4 -0
- package/dist/types/build/compiler/rollup-plugin-soma.d.ts +1 -0
- package/dist/types/build/compiler/soma-precompile.d.ts +9 -0
- package/dist/types/build/compiler/vite-plugin-soma.d.ts +3 -0
- package/dist/types/build/component/package-loader.d.ts +9 -0
- package/dist/types/build/eject/apply-eject.d.ts +22 -0
- package/dist/types/build/eject/plan-eject.d.ts +32 -0
- package/dist/types/client/adaptive/flush-scheduler.d.ts +13 -0
- package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
- package/dist/types/client/adaptive/index.d.ts +5 -0
- package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
- package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
- package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
- package/dist/types/client/component/ai-catalog.d.ts +73 -0
- package/dist/types/client/component/component-definition.types.d.ts +54 -0
- package/dist/types/client/component/component-expander.d.ts +25 -0
- package/dist/types/client/component/component-manager.d.ts +160 -0
- package/dist/types/client/component/component-pack.d.ts +90 -0
- package/dist/types/client/component/component-registry.d.ts +12 -0
- package/dist/types/client/component/pack-styles.d.ts +22 -0
- package/dist/types/client/component/scoped-adapter.d.ts +18 -0
- package/dist/types/client/component/verify-pack.d.ts +14 -0
- package/dist/types/client/data/data-object-resolver.d.ts +71 -0
- package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
- package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
- package/dist/types/client/engine/asset-resolver.d.ts +7 -1
- package/dist/types/client/engine/dom-renderer.d.ts +49 -1
- package/dist/types/client/engine/emit-event.d.ts +6 -0
- package/dist/types/client/engine/event-engine.d.ts +6 -0
- package/dist/types/client/engine/render-engine.d.ts +26 -1
- package/dist/types/client/engine/runtime-builder.d.ts +7 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +122 -0
- package/dist/types/client/infra/http/SomaHttpService.d.ts +2 -0
- package/dist/types/client/observability/error-reporter.d.ts +51 -0
- package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
- package/dist/types/client/parser/data-config-parser.d.ts +34 -0
- package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
- package/dist/types/client/parser/style-config-parser.d.ts +5 -3
- package/dist/types/client/parser/ui-config-parser.d.ts +22 -1
- package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
- package/dist/types/client/registry/declaration-renderer.d.ts +51 -0
- package/dist/types/client/registry/index.d.ts +10 -0
- package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
- package/dist/types/client/registry/remote-page.d.ts +52 -0
- package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
- package/dist/types/client/scheduler/SomaScheduler.d.ts +8 -0
- package/dist/types/client/security/attribute-presence.d.ts +7 -0
- package/dist/types/client/security/html-sanitizer.d.ts +37 -0
- package/dist/types/client/security/url-guard.d.ts +11 -0
- package/dist/types/client/theme/theme-engine.d.ts +49 -0
- package/dist/types/client/typography/typography-base.d.ts +15 -0
- package/dist/types/client/validation/contract-validator.d.ts +2 -0
- package/dist/types/index.d.ts +36 -2
- package/dist/types/server/api/IService.d.ts +18 -6
- package/dist/types/server/host/content-security-policy.d.ts +53 -0
- package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
- package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
- package/dist/types/server/host/server-page-assets.d.ts +5 -0
- package/dist/types/server/index.d.ts +4 -1
- package/dist/types/server/registry/api-registry-store.d.ts +141 -0
- package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
- package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
- package/dist/types/server/registry/index.d.ts +29 -0
- package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
- package/dist/types/server/registry/page-registry-store.d.ts +259 -0
- package/dist/types/server/registry/registry-error.d.ts +12 -0
- package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
- package/dist/types/server/registry/registry-response.d.ts +18 -0
- package/dist/types/server/registry/registry-subjects.d.ts +59 -0
- package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
- package/dist/types/server/registry/request-body-limit.d.ts +31 -0
- package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
- package/dist/types/server/registry/services/apis/List.d.ts +10 -0
- package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
- package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
- package/dist/types/server/registry/services/index.d.ts +3 -0
- package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
- package/dist/types/server/registry/services/pages/List.d.ts +13 -0
- package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
- package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
- package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
- package/dist/types/server/registry/services/service-utils.d.ts +10 -0
- package/dist/types/server/registry/validation/compile-gate.d.ts +10 -0
- package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
- package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
- package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
- package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
- package/dist/types/shared/types/adaptive.types.d.ts +88 -0
- package/dist/types/shared/types/component-contract.types.d.ts +68 -0
- package/dist/types/shared/types/data-config.types.d.ts +81 -0
- package/dist/types/shared/types/domain.types.d.ts +6 -0
- package/dist/types/shared/types/engine.types.d.ts +6 -0
- package/dist/types/shared/types/manifest.types.d.ts +23 -0
- package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
- package/dist/types/shared/types/style-config.types.d.ts +8 -0
- package/dist/types/shared/types/ui-config.types.d.ts +20 -0
- package/global.d.ts +12 -0
- package/next.config.js +76 -1
- package/package.json +32 -11
- package/proxy.ts +62 -0
- package/core/client/engine/theme-variables.ts +0 -37
- package/dist/tsconfig.types.tsbuildinfo +0 -1
- package/dist/types/client/engine/theme-variables.d.ts +0 -3
- package/middleware.ts +0 -33
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
// core/client/parser/data-config-parser.ts — 数据对象层(*_data.yaml)声明解析器
|
|
2
|
+
// 负责将 *_data.yaml 解析后的原始对象转换为类型安全的 SomaDataConfig 结构。
|
|
3
|
+
// 校验范围:type/permission 枚举合法性、rules 形状、`$` 引用语法、transform 管道语法;
|
|
4
|
+
// 非法声明抛带定位路径的 SomaDataConfigError(风格与 contract-validator/style-config-parser 一致)。
|
|
5
|
+
|
|
6
|
+
import yaml from 'js-yaml';
|
|
7
|
+
import { hasPipes, parsePipedValue } from '../pipes/pipe-registry';
|
|
8
|
+
import type {
|
|
9
|
+
SomaDataConfig,
|
|
10
|
+
SomaDataFieldConfig,
|
|
11
|
+
SomaDataFieldOption,
|
|
12
|
+
SomaDataFieldPermission,
|
|
13
|
+
SomaDataFieldRule,
|
|
14
|
+
SomaDataFieldType,
|
|
15
|
+
SomaDataObjectConfig,
|
|
16
|
+
SomaDataSourceConfig,
|
|
17
|
+
} from '../../shared/types/data-config.types';
|
|
18
|
+
|
|
19
|
+
// ─── 错误类 ───────────────────────────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
export class SomaDataConfigError extends Error {
|
|
22
|
+
public readonly path: string;
|
|
23
|
+
|
|
24
|
+
constructor(message: string, path: string = '') {
|
|
25
|
+
const prefix = path ? ` (路径: ${path})` : '';
|
|
26
|
+
super(`[SOMA DataConfigParser] ${message}${prefix}`);
|
|
27
|
+
this.name = 'SomaDataConfigError';
|
|
28
|
+
this.path = path;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ─── 常量 ─────────────────────────────────────────────────────────────────────
|
|
33
|
+
|
|
34
|
+
const VALID_FIELD_TYPES = new Set<SomaDataFieldType>([
|
|
35
|
+
'text', 'int', 'float', 'money', 'bool', 'enum',
|
|
36
|
+
'date', 'datetime', 'image', 'url', 'richtext',
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
const VALID_PERMISSIONS = new Set<SomaDataFieldPermission>(['r', 'w']);
|
|
40
|
+
|
|
41
|
+
/** 规则键 → 期望的值类型 */
|
|
42
|
+
const RULE_VALUE_TYPES: Record<string, 'boolean' | 'number' | 'string'> = {
|
|
43
|
+
required: 'boolean',
|
|
44
|
+
max_len: 'number',
|
|
45
|
+
min_len: 'number',
|
|
46
|
+
pattern: 'string',
|
|
47
|
+
min: 'number',
|
|
48
|
+
max: 'number',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** 对象名 / into 子键的合法标识符 */
|
|
52
|
+
const IDENTIFIER_PATTERN = /^[a-zA-Z_]\w*$/;
|
|
53
|
+
|
|
54
|
+
// ─── `$` 引用 ─────────────────────────────────────────────────────────────────
|
|
55
|
+
|
|
56
|
+
/** 解析后的 `$` 引用:三源之一 + 字段路径段 */
|
|
57
|
+
export interface SomaDataRef {
|
|
58
|
+
/** query:URL 参数;page:Domain 字段;object:已解析数据对象 */
|
|
59
|
+
source: 'query' | 'page' | 'object';
|
|
60
|
+
/** source 为 object 时的对象名 */
|
|
61
|
+
object?: string;
|
|
62
|
+
/** 引用的字段路径段(`$order.trace.no` → ['trace', 'no']) */
|
|
63
|
+
path: string[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 解析 `$` 引用语法:`$query.<键>` / `$page.<路径>` / `$<数据对象>.<字段路径>`。
|
|
68
|
+
* 语法非法时返回 null(由调用方按上下文报带定位的声明错误)。
|
|
69
|
+
*/
|
|
70
|
+
export function parseDataRef(text: string): SomaDataRef | null {
|
|
71
|
+
if (!text.startsWith('$')) return null;
|
|
72
|
+
const segments = text.slice(1).split('.');
|
|
73
|
+
if (segments.length < 2 || segments.some((seg) => !seg)) return null;
|
|
74
|
+
const head = segments[0];
|
|
75
|
+
if (!IDENTIFIER_PATTERN.test(head)) return null;
|
|
76
|
+
const path = segments.slice(1);
|
|
77
|
+
if (head === 'query') return { source: 'query', path };
|
|
78
|
+
if (head === 'page') return { source: 'page', path };
|
|
79
|
+
return { source: 'object', object: head, path };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ─── transform 管道语法归一化 ─────────────────────────────────────────────────
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 归一化 transform 值中的管道段:规格允许 `date: 'YYYY-MM-DD'` 的冒号简写,
|
|
86
|
+
* 统一改写成 pipe-registry 的标准语法 `date('YYYY-MM-DD')` 后复用其解析器。
|
|
87
|
+
* 首段(字段路径)原样保留。
|
|
88
|
+
*/
|
|
89
|
+
export function normalizeDataPipeValue(rawValue: string): string {
|
|
90
|
+
const segments = rawValue.split(/\s+\|\s+/);
|
|
91
|
+
if (segments.length <= 1) return rawValue.trim();
|
|
92
|
+
const normalized = segments.slice(1).map((segment) => {
|
|
93
|
+
const match = segment.trim().match(/^([a-z][a-zA-Z0-9_]*)\s*:\s*(.+)$/);
|
|
94
|
+
return match ? `${match[1]}(${match[2].trim()})` : segment.trim();
|
|
95
|
+
});
|
|
96
|
+
return [segments[0].trim(), ...normalized].join(' | ');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ─── 内部校验函数 ─────────────────────────────────────────────────────────────
|
|
100
|
+
|
|
101
|
+
function assertPlainObject(value: unknown, what: string, path: string): asserts value is Record<string, unknown> {
|
|
102
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
103
|
+
throw new SomaDataConfigError(`${what}必须是对象,当前值: ${JSON.stringify(value)}`, path);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** 递归校验 params 中的 `$` 引用语法,并收集引用到的其他数据对象名 */
|
|
108
|
+
function validateParamRefs(
|
|
109
|
+
value: unknown,
|
|
110
|
+
path: string,
|
|
111
|
+
objectNames: Set<string>,
|
|
112
|
+
selfName: string,
|
|
113
|
+
deps: Set<string>,
|
|
114
|
+
): void {
|
|
115
|
+
if (typeof value === 'string') {
|
|
116
|
+
if (!value.startsWith('$')) return;
|
|
117
|
+
const ref = parseDataRef(value);
|
|
118
|
+
if (!ref) {
|
|
119
|
+
throw new SomaDataConfigError(
|
|
120
|
+
`\`$\` 引用语法非法: "${value}"(应为 $query.<键> / $page.<路径> / $<数据对象>.<字段>)`,
|
|
121
|
+
path,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
if (ref.source === 'object') {
|
|
125
|
+
const target = ref.object!;
|
|
126
|
+
if (!objectNames.has(target)) {
|
|
127
|
+
throw new SomaDataConfigError(
|
|
128
|
+
`引用了未声明的数据对象 "${target}"(已声明: ${[...objectNames].join(', ') || '无'})`,
|
|
129
|
+
path,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
if (target !== selfName) {
|
|
133
|
+
deps.add(target);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (Array.isArray(value)) {
|
|
139
|
+
value.forEach((item, index) => {
|
|
140
|
+
validateParamRefs(item, `${path}[${index}]`, objectNames, selfName, deps);
|
|
141
|
+
});
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (typeof value === 'object' && value !== null) {
|
|
145
|
+
for (const [key, child] of Object.entries(value)) {
|
|
146
|
+
validateParamRefs(child, `${path}.${key}`, objectNames, selfName, deps);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function parseSource(
|
|
152
|
+
raw: unknown,
|
|
153
|
+
path: string,
|
|
154
|
+
objectNames: Set<string>,
|
|
155
|
+
selfName: string,
|
|
156
|
+
deps: Set<string>,
|
|
157
|
+
): SomaDataSourceConfig {
|
|
158
|
+
assertPlainObject(raw, '取数来源', path);
|
|
159
|
+
const source: SomaDataSourceConfig = { api: '' };
|
|
160
|
+
|
|
161
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
162
|
+
if (key === 'api') {
|
|
163
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
164
|
+
throw new SomaDataConfigError(`api 必须是非空字符串`, `${path}.api`);
|
|
165
|
+
}
|
|
166
|
+
source.api = value.trim();
|
|
167
|
+
} else if (key === 'params') {
|
|
168
|
+
assertPlainObject(value, 'params', `${path}.params`);
|
|
169
|
+
validateParamRefs(value, `${path}.params`, objectNames, selfName, deps);
|
|
170
|
+
source.params = value;
|
|
171
|
+
} else if (key === 'into') {
|
|
172
|
+
if (typeof value !== 'string' || !IDENTIFIER_PATTERN.test(value)) {
|
|
173
|
+
throw new SomaDataConfigError(
|
|
174
|
+
`into 必须是合法子键名(字母/下划线开头),当前值: ${JSON.stringify(value)}`,
|
|
175
|
+
`${path}.into`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
source.into = value;
|
|
179
|
+
} else {
|
|
180
|
+
throw new SomaDataConfigError(`未知的来源字段 "${key}"(支持: api / params / into)`, `${path}.${key}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (!source.api) {
|
|
185
|
+
throw new SomaDataConfigError(`缺少 api 声明`, `${path}.api`);
|
|
186
|
+
}
|
|
187
|
+
return source;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function parseTransform(raw: unknown, path: string): Record<string, string> {
|
|
191
|
+
assertPlainObject(raw, 'transform', path);
|
|
192
|
+
const result: Record<string, string> = {};
|
|
193
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
194
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
195
|
+
throw new SomaDataConfigError(
|
|
196
|
+
`派生字段的值必须是 "字段路径 | 管道链" 字符串,当前值: ${JSON.stringify(value)}`,
|
|
197
|
+
`${path}.${key}`,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
const normalized = normalizeDataPipeValue(value);
|
|
201
|
+
if (hasPipes(normalized)) {
|
|
202
|
+
try {
|
|
203
|
+
// 语法级校验(管道名白名单由解析器结合注册表在运行时校验)
|
|
204
|
+
parsePipedValue(normalized, () => false);
|
|
205
|
+
} catch (error) {
|
|
206
|
+
throw new SomaDataConfigError(
|
|
207
|
+
`派生字段管道链解析失败: ${(error as Error).message}`,
|
|
208
|
+
`${path}.${key}`,
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
result[key] = normalized;
|
|
213
|
+
}
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function parseRule(raw: unknown, path: string): SomaDataFieldRule {
|
|
218
|
+
assertPlainObject(raw, '校验规则', path);
|
|
219
|
+
const keys = Object.keys(raw);
|
|
220
|
+
const ruleKeys = keys.filter((key) => key !== 'message');
|
|
221
|
+
|
|
222
|
+
if (typeof raw.message !== 'string' || !raw.message) {
|
|
223
|
+
throw new SomaDataConfigError(`每条规则必须携带 message 提示文案`, `${path}.message`);
|
|
224
|
+
}
|
|
225
|
+
if (ruleKeys.length !== 1) {
|
|
226
|
+
throw new SomaDataConfigError(
|
|
227
|
+
`每条规则必须且只能声明一种条件(required / max_len / min_len / pattern / min / max),当前: ${ruleKeys.join(', ') || '无'}`,
|
|
228
|
+
path,
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const ruleKey = ruleKeys[0];
|
|
233
|
+
const expected = RULE_VALUE_TYPES[ruleKey];
|
|
234
|
+
if (!expected) {
|
|
235
|
+
throw new SomaDataConfigError(
|
|
236
|
+
`未知的规则条件 "${ruleKey}"(支持: ${Object.keys(RULE_VALUE_TYPES).join(' / ')})`,
|
|
237
|
+
`${path}.${ruleKey}`,
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
const ruleValue = (raw as Record<string, unknown>)[ruleKey];
|
|
241
|
+
if (typeof ruleValue !== expected) {
|
|
242
|
+
throw new SomaDataConfigError(
|
|
243
|
+
`规则 ${ruleKey} 的值必须是 ${expected},当前值: ${JSON.stringify(ruleValue)}`,
|
|
244
|
+
`${path}.${ruleKey}`,
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return { [ruleKey]: ruleValue, message: raw.message } as SomaDataFieldRule;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function parseOptions(raw: unknown, path: string): SomaDataFieldOption[] {
|
|
252
|
+
if (!Array.isArray(raw)) {
|
|
253
|
+
throw new SomaDataConfigError(`options 必须是数组({ key, label } 列表)`, path);
|
|
254
|
+
}
|
|
255
|
+
return raw.map((item, index) => {
|
|
256
|
+
const itemPath = `${path}[${index}]`;
|
|
257
|
+
assertPlainObject(item, '枚举候选项', itemPath);
|
|
258
|
+
if (item.key === undefined || item.key === null || item.key === '') {
|
|
259
|
+
throw new SomaDataConfigError(`候选项缺少 key`, `${itemPath}.key`);
|
|
260
|
+
}
|
|
261
|
+
const option: SomaDataFieldOption = { key: String(item.key) };
|
|
262
|
+
if (item.label !== undefined) {
|
|
263
|
+
option.label = String(item.label);
|
|
264
|
+
}
|
|
265
|
+
return option;
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function parseField(raw: unknown, path: string): SomaDataFieldConfig {
|
|
270
|
+
assertPlainObject(raw, '字段字典条目', path);
|
|
271
|
+
const field: SomaDataFieldConfig = {};
|
|
272
|
+
|
|
273
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
274
|
+
if (key === 'name') {
|
|
275
|
+
if (typeof value !== 'string') {
|
|
276
|
+
throw new SomaDataConfigError(`name 必须是字符串`, `${path}.name`);
|
|
277
|
+
}
|
|
278
|
+
field.name = value;
|
|
279
|
+
} else if (key === 'type') {
|
|
280
|
+
if (typeof value !== 'string' || !VALID_FIELD_TYPES.has(value as SomaDataFieldType)) {
|
|
281
|
+
throw new SomaDataConfigError(
|
|
282
|
+
`非法的字段类型 "${String(value)}"(支持: ${[...VALID_FIELD_TYPES].join(' / ')})`,
|
|
283
|
+
`${path}.type`,
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
field.type = value as SomaDataFieldType;
|
|
287
|
+
} else if (key === 'permission') {
|
|
288
|
+
if (typeof value !== 'string' || !VALID_PERMISSIONS.has(value as SomaDataFieldPermission)) {
|
|
289
|
+
throw new SomaDataConfigError(
|
|
290
|
+
`非法的字段权限 "${String(value)}"(支持: r 只读 / w 可写)`,
|
|
291
|
+
`${path}.permission`,
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
field.permission = value as SomaDataFieldPermission;
|
|
295
|
+
} else if (key === 'rules') {
|
|
296
|
+
if (!Array.isArray(value)) {
|
|
297
|
+
throw new SomaDataConfigError(`rules 必须是数组`, `${path}.rules`);
|
|
298
|
+
}
|
|
299
|
+
field.rules = value.map((rule, index) => parseRule(rule, `${path}.rules[${index}]`));
|
|
300
|
+
} else if (key === 'options') {
|
|
301
|
+
field.options = parseOptions(value, `${path}.options`);
|
|
302
|
+
} else {
|
|
303
|
+
throw new SomaDataConfigError(
|
|
304
|
+
`未知的字典字段 "${key}"(支持: name / type / permission / rules / options)`,
|
|
305
|
+
`${path}.${key}`,
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return field;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function parseObject(
|
|
313
|
+
name: string,
|
|
314
|
+
raw: unknown,
|
|
315
|
+
path: string,
|
|
316
|
+
objectNames: Set<string>,
|
|
317
|
+
): SomaDataObjectConfig {
|
|
318
|
+
if (!IDENTIFIER_PATTERN.test(name)) {
|
|
319
|
+
throw new SomaDataConfigError(`对象名 "${name}" 不合法(字母/下划线开头的标识符)`, path);
|
|
320
|
+
}
|
|
321
|
+
assertPlainObject(raw, '数据对象声明', path);
|
|
322
|
+
|
|
323
|
+
const deps = new Set<string>();
|
|
324
|
+
const config: SomaDataObjectConfig = { name, from: [], deps: [] };
|
|
325
|
+
|
|
326
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
327
|
+
if (key === 'from') {
|
|
328
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
329
|
+
throw new SomaDataConfigError(`from 必须是非空数组(取数来源列表)`, `${path}.from`);
|
|
330
|
+
}
|
|
331
|
+
config.from = value.map((source, index) =>
|
|
332
|
+
parseSource(source, `${path}.from[${index}]`, objectNames, name, deps),
|
|
333
|
+
);
|
|
334
|
+
} else if (key === 'transform') {
|
|
335
|
+
config.transform = parseTransform(value, `${path}.transform`);
|
|
336
|
+
} else if (key === 'fields') {
|
|
337
|
+
assertPlainObject(value, 'fields', `${path}.fields`);
|
|
338
|
+
const fields: Record<string, SomaDataFieldConfig> = {};
|
|
339
|
+
for (const [fieldName, fieldRaw] of Object.entries(value)) {
|
|
340
|
+
fields[fieldName] = parseField(fieldRaw, `${path}.fields.${fieldName}`);
|
|
341
|
+
}
|
|
342
|
+
config.fields = fields;
|
|
343
|
+
} else if (key === 'mock') {
|
|
344
|
+
config.mock = value;
|
|
345
|
+
} else {
|
|
346
|
+
throw new SomaDataConfigError(
|
|
347
|
+
`未知的声明字段 "${key}"(支持: from / transform / fields / mock)`,
|
|
348
|
+
`${path}.${key}`,
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// 无接口预览对象:允许缺省 from,但必须提供 mock 样本兜底
|
|
354
|
+
if (config.from.length === 0 && config.mock === undefined) {
|
|
355
|
+
throw new SomaDataConfigError(`缺少 from 取数来源(仅声明 mock 样本的预览对象可省略)`, `${path}.from`);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
config.deps = [...deps];
|
|
359
|
+
return config;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// ─── 公开 API ────────────────────────────────────────────────────────────────
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* 解析 *_data.yaml 的原始对象(yaml.load 产物)→ SomaDataConfig。
|
|
366
|
+
* 空输入(null/undefined)返回空声明;结构非法抛 SomaDataConfigError。
|
|
367
|
+
*/
|
|
368
|
+
export function parseDataConfig(raw: unknown): SomaDataConfig {
|
|
369
|
+
if (raw === null || raw === undefined) {
|
|
370
|
+
return { objects: {} };
|
|
371
|
+
}
|
|
372
|
+
assertPlainObject(raw, '数据对象配置', '');
|
|
373
|
+
|
|
374
|
+
const dataRaw = (raw as Record<string, unknown>).data;
|
|
375
|
+
if (dataRaw === undefined || dataRaw === null) {
|
|
376
|
+
return { objects: {} };
|
|
377
|
+
}
|
|
378
|
+
assertPlainObject(dataRaw, 'data', 'data');
|
|
379
|
+
|
|
380
|
+
const objectNames = new Set(Object.keys(dataRaw));
|
|
381
|
+
const objects: Record<string, SomaDataObjectConfig> = {};
|
|
382
|
+
for (const [name, objectRaw] of Object.entries(dataRaw)) {
|
|
383
|
+
objects[name] = parseObject(name, objectRaw, `data.${name}`, objectNames);
|
|
384
|
+
}
|
|
385
|
+
return { objects };
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* 解析 *_data.yaml 字符串 → SomaDataConfig(运行时字符串模式入口)。
|
|
390
|
+
*/
|
|
391
|
+
export function parseDataYaml(yamlStr: string): SomaDataConfig {
|
|
392
|
+
if (!yamlStr.trim()) {
|
|
393
|
+
return { objects: {} };
|
|
394
|
+
}
|
|
395
|
+
return parseDataConfig(yaml.load(yamlStr));
|
|
396
|
+
}
|
|
@@ -44,6 +44,14 @@ const VALID_DOM_EVENTS = new Set<string>([
|
|
|
44
44
|
/** 支持的 UI 交互动作 */
|
|
45
45
|
const VALID_ACTIONS = new Set<InteractAction>(['open', 'close', 'toggle']);
|
|
46
46
|
|
|
47
|
+
/** 组件契约 emit 名(两段式触发器放行;运行时由组件管理器标记路由) */
|
|
48
|
+
const EMIT_NAME_PATTERN = /^[a-z][a-z0-9_]*$/;
|
|
49
|
+
|
|
50
|
+
/** 是否为已知 DOM 事件(组件管理器区分 DOM 订阅与疑似 emit 订阅用) */
|
|
51
|
+
export function isKnownDomEvent(name: string): boolean {
|
|
52
|
+
return VALID_DOM_EVENTS.has(name);
|
|
53
|
+
}
|
|
54
|
+
|
|
47
55
|
/** Pipeline optional confirm segment prefix */
|
|
48
56
|
const CONFIRM_PREFIX = 'confirm:';
|
|
49
57
|
|
|
@@ -85,16 +93,20 @@ export function parseSelector(
|
|
|
85
93
|
|
|
86
94
|
if (parts.length === 2) {
|
|
87
95
|
// 标准格式:somaId.event(如 btn_refresh.click)
|
|
96
|
+
// 组件契约 emit:somaId.emit名(如 qty.change、stepper.reach_limit)——
|
|
97
|
+
// emit 名与 DOM 事件同构,snake_case 命名放行;是否匹配真实契约由
|
|
98
|
+
// 组件管理器在运行时标记(emitRoute),未匹配的既不监听也不触发。
|
|
88
99
|
const [somaId, domEvent] = parts;
|
|
89
100
|
|
|
90
101
|
if (!somaId) {
|
|
91
102
|
throw new SomaEventParseError(`选择器的 somaId 部分为空`, selector);
|
|
92
103
|
}
|
|
93
104
|
|
|
94
|
-
if (!VALID_DOM_EVENTS.has(domEvent)) {
|
|
105
|
+
if (!VALID_DOM_EVENTS.has(domEvent) && !EMIT_NAME_PATTERN.test(domEvent)) {
|
|
95
106
|
throw new SomaEventParseError(
|
|
96
|
-
|
|
97
|
-
|
|
107
|
+
`不支持的事件 "${domEvent}",` +
|
|
108
|
+
`支持的 DOM 事件: ${[...VALID_DOM_EVENTS].join(', ')},` +
|
|
109
|
+
`或组件契约 emit 名(小写 snake_case)`,
|
|
98
110
|
selector,
|
|
99
111
|
);
|
|
100
112
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// 负责将 _style.yaml 解析后的原始对象转换为类型安全的 StyleConfig 结构
|
|
3
3
|
|
|
4
4
|
import type {
|
|
5
|
+
SomaStyleViolationKind,
|
|
5
6
|
StyleConfig,
|
|
6
7
|
StyleStateRule,
|
|
7
8
|
StyleTransition,
|
|
@@ -13,12 +14,15 @@ import type {
|
|
|
13
14
|
|
|
14
15
|
export class SomaStyleConfigError extends Error {
|
|
15
16
|
public readonly path: string;
|
|
17
|
+
/** 违规类别;仅结构化校验失败时缺省(那类失败没有"类别"可言) */
|
|
18
|
+
public readonly kind?: SomaStyleViolationKind;
|
|
16
19
|
|
|
17
|
-
constructor(message: string, path: string = '') {
|
|
20
|
+
constructor(message: string, path: string = '', kind?: SomaStyleViolationKind) {
|
|
18
21
|
const prefix = path ? ` (路径: ${path})` : '';
|
|
19
22
|
super(`[SOMA StyleConfigParser] ${message}${prefix}`);
|
|
20
23
|
this.name = 'SomaStyleConfigError';
|
|
21
24
|
this.path = path;
|
|
25
|
+
this.kind = kind;
|
|
22
26
|
}
|
|
23
27
|
}
|
|
24
28
|
|
|
@@ -28,6 +32,61 @@ const VALID_TRIGGERS = new Set<TransitionTrigger>([
|
|
|
28
32
|
'hover', 'focus', 'active', 'enter', 'leave', 'active_state',
|
|
29
33
|
]);
|
|
30
34
|
|
|
35
|
+
// ─── 布局禁令(F-06 / 契约 C3) ───────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 布局类模式。
|
|
39
|
+
*
|
|
40
|
+
* 为什么校验在解析层而不在 lint:lint 可被跳过、可被配置关闭、对生成链没有约束力;
|
|
41
|
+
* 解析层是页面进入运行时的**唯一必经通道**。代价是 breaking(存量违规页面报错),
|
|
42
|
+
* 这是本需求记名接受的唯一破坏点。
|
|
43
|
+
*
|
|
44
|
+
* `\b` 词边界锚定是防误伤的关键:没有它,`border-gray-100` 会被 `order-` 命中,
|
|
45
|
+
* `pointer-events-none` 会被 `p-` 逼近。锚定后二者都安全。
|
|
46
|
+
*/
|
|
47
|
+
const LAYOUT_CLASS_PATTERN =
|
|
48
|
+
/\b(?:flex|grid|absolute|fixed|sticky|w-|h-|min-w-|max-w-|p[xytblre]?-|m[xytblre]?-|gap-|inset-|left-|right-|top-|bottom-|translate-|order-|col-|row-)\S*/;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 显隐对白名单:`hidden ↔ flex | block | inline-flex | grid`。
|
|
52
|
+
*
|
|
53
|
+
* 只在 `states` / `transitions` 生效——显隐是**状态语义**,不是布局组装:
|
|
54
|
+
* "加载中显示 spinner"必须能被声明,而它天然要写一个 display 值。
|
|
55
|
+
* `base` / `themes` 不放行:那两段是静态外观,静态地"用 flex 排版"就是布局组装。
|
|
56
|
+
*/
|
|
57
|
+
const DISPLAY_TOGGLE_CLASSES = new Set(['hidden', 'flex', 'block', 'inline-flex', 'grid']);
|
|
58
|
+
|
|
59
|
+
/** 报错必须指路:只说"不许"而不说"该写哪儿",等于把人堵在原地 */
|
|
60
|
+
const LAYOUT_BAN_HINT =
|
|
61
|
+
'基础布局请写入 HTML 骨架或布局原语,样式配置只承载视觉与显隐;'
|
|
62
|
+
+ '若该类名确属视觉类而非布局类,请改名或将其移入 HTML';
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 校验一串类名是否触犯布局禁令。
|
|
66
|
+
*
|
|
67
|
+
* @param allowDisplayToggle states / transitions 段为 true:放行单独的显隐类
|
|
68
|
+
*/
|
|
69
|
+
function assertNoLayoutClasses(
|
|
70
|
+
classes: string,
|
|
71
|
+
path: string,
|
|
72
|
+
allowDisplayToggle: boolean,
|
|
73
|
+
): void {
|
|
74
|
+
for (const token of classes.split(/\s+/)) {
|
|
75
|
+
if (!token) continue;
|
|
76
|
+
if (allowDisplayToggle && DISPLAY_TOGGLE_CLASSES.has(token)) continue;
|
|
77
|
+
if (!LAYOUT_CLASS_PATTERN.test(token)) continue;
|
|
78
|
+
|
|
79
|
+
const allowed = allowDisplayToggle
|
|
80
|
+
? `(本段仅放行显隐类: ${[...DISPLAY_TOGGLE_CLASSES].join(' / ')})`
|
|
81
|
+
: '';
|
|
82
|
+
throw new SomaStyleConfigError(
|
|
83
|
+
`布局类 "${token}" 不允许出现在样式配置中${allowed}。${LAYOUT_BAN_HINT}`,
|
|
84
|
+
path,
|
|
85
|
+
'layout-class',
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
31
90
|
// ─── 内部校验函数 ─────────────────────────────────────────────────────────────
|
|
32
91
|
|
|
33
92
|
function isValidConditionPath(path: string): boolean {
|
|
@@ -65,6 +124,7 @@ function parseBase(raw: unknown, path: string): Record<string, string> {
|
|
|
65
124
|
`${path}.${somaId}`,
|
|
66
125
|
);
|
|
67
126
|
}
|
|
127
|
+
assertNoLayoutClasses(classes, `${path}.${somaId}`, false);
|
|
68
128
|
result[somaId] = classes;
|
|
69
129
|
}
|
|
70
130
|
return result;
|
|
@@ -111,6 +171,7 @@ function parseStateRule(raw: unknown, path: string): StyleStateRule {
|
|
|
111
171
|
path,
|
|
112
172
|
);
|
|
113
173
|
}
|
|
174
|
+
assertNoLayoutClasses(obj.apply, `${path}.apply`, true);
|
|
114
175
|
|
|
115
176
|
return {
|
|
116
177
|
condition: obj.condition,
|
|
@@ -171,6 +232,7 @@ function parseTransition(raw: unknown, somaId: string, path: string): StyleTrans
|
|
|
171
232
|
path,
|
|
172
233
|
);
|
|
173
234
|
}
|
|
235
|
+
assertNoLayoutClasses(obj.apply, `${path}.apply`, true);
|
|
174
236
|
|
|
175
237
|
const result: StyleTransition = {
|
|
176
238
|
trigger: obj.trigger as TransitionTrigger,
|
|
@@ -276,6 +338,7 @@ function parseTheme(raw: unknown, themeName: string, path: string): ThemeConfig
|
|
|
276
338
|
`${path}.${key}`,
|
|
277
339
|
);
|
|
278
340
|
}
|
|
341
|
+
assertNoLayoutClasses(value, `${path}.${key}`, false);
|
|
279
342
|
base[key] = value;
|
|
280
343
|
}
|
|
281
344
|
}
|
|
@@ -309,7 +372,7 @@ function parseThemes(raw: unknown, path: string): Record<string, ThemeConfig> {
|
|
|
309
372
|
* - base 的值必须是纯 Tailwind 类名字符串
|
|
310
373
|
* - states 的 condition 必须是纯属性路径,value 必须是基础类型
|
|
311
374
|
* - transitions 的 trigger 必须是有效枚举值
|
|
312
|
-
* - themes
|
|
375
|
+
* - base / themes 命中布局类模式即失败;states / transitions 放行显隐对(F-06)
|
|
313
376
|
*
|
|
314
377
|
* @param raw - YAML 解析后的原始对象
|
|
315
378
|
* @returns 类型安全的 StyleConfig
|
|
@@ -11,6 +11,8 @@ import type {
|
|
|
11
11
|
ParsedShowEntry,
|
|
12
12
|
} from '../../shared/types/ui-config.types';
|
|
13
13
|
import type { OnChangeRule } from '../../shared/types/watcher.types';
|
|
14
|
+
import type { SomaAdaptiveVocabulary } from '../../shared/types/adaptive.types';
|
|
15
|
+
import { parseAdaptiveConfig } from './adaptive-parser';
|
|
14
16
|
|
|
15
17
|
// ─── 错误类 ───────────────────────────────────────────────────────────────────
|
|
16
18
|
|
|
@@ -29,6 +31,26 @@ export class SomaUiConfigError extends Error {
|
|
|
29
31
|
}
|
|
30
32
|
}
|
|
31
33
|
|
|
34
|
+
// ─── 解析选项 ─────────────────────────────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
export interface SomaUiConfigParseOptions {
|
|
37
|
+
/**
|
|
38
|
+
* 阶梯词汇表(F-09)。core 不内置任何级语义,词汇由宿主 bootstrap 注入;
|
|
39
|
+
* 不传而页面声明了 `adaptive.ladder` 时,按"词汇未知"快速失败。
|
|
40
|
+
*/
|
|
41
|
+
adaptiveVocabulary?: SomaAdaptiveVocabulary;
|
|
42
|
+
/**
|
|
43
|
+
* 词汇是否必须在本次解析时到位。默认 true(页面装载路径)。
|
|
44
|
+
*
|
|
45
|
+
* 构建期预编译是唯一的 false 场景:那一刻宿主 bootstrap 尚未加载,
|
|
46
|
+
* 词汇无处可取。此时结构校验(级数 / 单键 / pin 冲突 / profile)照做,
|
|
47
|
+
* 只把**词汇归属**这一项推迟到编译期 `compileAdaptive` ——
|
|
48
|
+
* 那里词汇必然在手,未注册同样报错、同样指路,
|
|
49
|
+
* 所以推迟的是报错时机,不是报错本身。
|
|
50
|
+
*/
|
|
51
|
+
adaptiveVocabularyRequired?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
32
54
|
// ─── 内部辅助函数 ─────────────────────────────────────────────────────────────
|
|
33
55
|
|
|
34
56
|
/**
|
|
@@ -127,7 +149,7 @@ function parseInitProps(raw: unknown, path: string): Record<string, unknown> {
|
|
|
127
149
|
/**
|
|
128
150
|
* 判断一个对象值是否为嵌套绑定(含 bind + show)
|
|
129
151
|
*/
|
|
130
|
-
function isNestedShowMapping(value: any): value is { bind: any; show: any } {
|
|
152
|
+
function isNestedShowMapping(value: any): value is { bind: any; show: any; when?: unknown } {
|
|
131
153
|
return (
|
|
132
154
|
typeof value === 'object' &&
|
|
133
155
|
value !== null &&
|
|
@@ -141,7 +163,7 @@ function isNestedShowMapping(value: any): value is { bind: any; show: any } {
|
|
|
141
163
|
*/
|
|
142
164
|
function isExtendedShowValue(value: any): boolean {
|
|
143
165
|
if (typeof value !== 'object' || value === null) return false;
|
|
144
|
-
const extendedKeys = ['text', 'model', 'when', 'class', 'show'];
|
|
166
|
+
const extendedKeys = ['text', 'model', 'when', 'class', 'show', 'props'];
|
|
145
167
|
return extendedKeys.some((key) => key in value);
|
|
146
168
|
}
|
|
147
169
|
|
|
@@ -225,6 +247,10 @@ function parseShowMapping(rawShow: any, path: string): ShowMapping {
|
|
|
225
247
|
if (typeof value === 'string') {
|
|
226
248
|
// 简单文本绑定:"var.prop" 或 "somaId@attr: var.prop"
|
|
227
249
|
result[key] = value;
|
|
250
|
+
} else if (/^item:.+/.test(key) && typeof value === 'object' && value !== null
|
|
251
|
+
&& !isNestedShowMapping(value) && !isExtendedShowValue(value)) {
|
|
252
|
+
// 异构列表分组键:item:变体 → 仅该变体行生效的子映射(DSL 扩展规范 2.1)
|
|
253
|
+
result[key] = { show: parseShowMapping(value, currentPath) };
|
|
228
254
|
} else if (isNestedShowMapping(value)) {
|
|
229
255
|
// 嵌套绑定:含 bind + show
|
|
230
256
|
const nestedPath = `${currentPath}.bind`;
|
|
@@ -234,8 +260,11 @@ function parseShowMapping(rawShow: any, path: string): ShowMapping {
|
|
|
234
260
|
bind: {
|
|
235
261
|
obj: value.bind.obj,
|
|
236
262
|
var: value.bind.var,
|
|
263
|
+
...(typeof value.bind.role_by === 'string' ? { role_by: value.bind.role_by } : {}),
|
|
237
264
|
},
|
|
238
265
|
show: parseShowMapping(value.show, `${currentPath}.show`),
|
|
266
|
+
// when 与 bind 同处一层:整块显隐(列表在错误/加载态整体隐藏靠它)
|
|
267
|
+
...(typeof value.when === 'string' ? { when: value.when } : {}),
|
|
239
268
|
};
|
|
240
269
|
|
|
241
270
|
result[key] = nested;
|
|
@@ -298,6 +327,16 @@ function parseShowMapping(rawShow: any, path: string): ShowMapping {
|
|
|
298
327
|
extended.show = parseShowMapping(value.show, `${currentPath}.show`);
|
|
299
328
|
}
|
|
300
329
|
|
|
330
|
+
if ('props' in value && value.props !== undefined) {
|
|
331
|
+
if (typeof value.props !== 'object' || value.props === null || Array.isArray(value.props)) {
|
|
332
|
+
throw new SomaUiConfigError(
|
|
333
|
+
`props 必须是对象({ prop名: 绑定路径或字面量 }),当前值: ${JSON.stringify(value.props)}`,
|
|
334
|
+
currentPath,
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
extended.props = { ...(value.props as Record<string, unknown>) };
|
|
338
|
+
}
|
|
339
|
+
|
|
301
340
|
result[key] = extended;
|
|
302
341
|
} else if (typeof value === 'object' && value !== null) {
|
|
303
342
|
// 对象值但不符合已知格式 — 可能是纯嵌套 show(不带 bind 的简写形式)
|
|
@@ -342,12 +381,15 @@ function parseShowMapping(rawShow: any, path: string): ShowMapping {
|
|
|
342
381
|
* - show 键不能为空
|
|
343
382
|
* - 嵌套绑定的 bind 和 show 必须成对出现
|
|
344
383
|
* - 处理 somaId@attr 扩展语法
|
|
384
|
+
* - 可选的 adaptive 保留键按阶梯 schema 校验(缺席则完全旁路,产物不变)
|
|
345
385
|
*
|
|
346
386
|
* @param raw - YAML 解析后的原始对象(通常来自 yaml-loader)
|
|
387
|
+
* @param options - 解析上下文;adaptiveVocabulary 由宿主 bootstrap 注入
|
|
347
388
|
* @returns 类型安全的 UiConfig 对象
|
|
348
389
|
* @throws SomaUiConfigError 格式不合法时抛出
|
|
390
|
+
* @throws SomaAdaptiveConfigError adaptive 声明不合法时抛出
|
|
349
391
|
*/
|
|
350
|
-
export function parseUiConfig(raw: any): UiConfig {
|
|
392
|
+
export function parseUiConfig(raw: any, options?: SomaUiConfigParseOptions): UiConfig {
|
|
351
393
|
if (!raw || typeof raw !== 'object') {
|
|
352
394
|
throw new SomaUiConfigError(`UI 配置必须是对象`);
|
|
353
395
|
}
|
|
@@ -380,6 +422,13 @@ export function parseUiConfig(raw: any): UiConfig {
|
|
|
380
422
|
config.initProps = parseInitProps(raw.initProps, 'initProps');
|
|
381
423
|
}
|
|
382
424
|
|
|
425
|
+
// adaptive 是可选保留键:缺席时连字段都不写,UiConfig 与迁移前逐键相同
|
|
426
|
+
if ('adaptive' in raw && raw.adaptive !== undefined) {
|
|
427
|
+
config.adaptive = parseAdaptiveConfig(raw.adaptive, options?.adaptiveVocabulary, 'adaptive', {
|
|
428
|
+
vocabularyRequired: options?.adaptiveVocabularyRequired !== false,
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
|
|
383
432
|
return config;
|
|
384
433
|
}
|
|
385
434
|
|