soma-core 1.0.0 → 1.0.2
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/app/SomaPageClient.tsx +336 -0
- package/app/api/[...path]/route.ts +77 -0
- package/app/api/page/route.ts +18 -16
- package/app/api/soma-manifest/route.ts +39 -0
- package/app/components/SomaHydrationShell.tsx +23 -0
- package/app/layout.tsx +2 -2
- package/app/loading.tsx +5 -0
- package/app/page.tsx +19 -128
- package/bin/soma-lsp.cjs +195 -0
- package/bin/soma-sync.cjs +3 -0
- package/bin/soma.cjs +13 -1
- package/core/api/IService.ts +29 -0
- package/core/api/load-host-service.ts +72 -0
- package/core/api/service-path.ts +39 -0
- package/core/base/SomaEntity.ts +3 -10
- package/core/base/SomaLocaleOnlyPageRoot.ts +3 -4
- package/core/base/SomaPageRootBase.ts +20 -3
- package/core/base/decorators/Event.ts +23 -0
- package/core/base/decorators/Locale.ts +35 -0
- package/core/base/decorators/Page.ts +16 -0
- package/core/base/decorators/Show.ts +20 -0
- package/core/base/decorators/ShowList.ts +34 -0
- package/core/base/decorators/index.ts +5 -0
- package/core/base/index.ts +2 -0
- package/core/base/runAsync.ts +32 -0
- package/core/compiler/full-manifest-builder.ts +33 -7
- package/core/compiler/host-config-sync.cjs +38 -0
- package/core/compiler/manifest-compiler.ts +3 -2
- package/core/compiler/page-base-resolver.ts +23 -0
- package/core/compiler/precompiled-manifest-loader.ts +34 -0
- package/core/compiler/rollup-plugin-soma.ts +45 -0
- package/core/compiler/rspack-plugin-soma.js +33 -0
- package/core/compiler/soma-manifest-webpack-plugin.js +72 -0
- package/core/compiler/soma-precompile.js +3513 -0
- package/core/compiler/soma-precompile.ts +52 -6
- package/core/compiler/soma-rspack-yaml-loader.js +22 -0
- package/core/compiler/soma-webpack-loader.js +7 -1
- package/core/compiler/vite-plugin-soma.ts +11 -1
- package/core/container/IContainerAdapter.ts +36 -0
- package/core/container/MiniProgramContainerAdapter.ts +190 -0
- package/core/container/ReactNativeContainerAdapter.ts +189 -0
- package/core/container/SomaContainerAdapter.ts +17 -6
- package/core/diagnostics/SomaDiagnostics.ts +1 -1
- package/core/diagnostics/SomaTelemetry.ts +100 -0
- package/core/diagnostics/telemetry-context.ts +15 -0
- package/core/engine/asset-resolver.ts +39 -4
- package/core/engine/dom-renderer.ts +9 -11
- package/core/engine/event-engine.ts +110 -41
- package/core/engine/on-change-engine.ts +71 -0
- package/core/engine/property-watcher.ts +117 -0
- package/core/engine/reactive-proxy.ts +11 -5
- package/core/engine/render-engine.ts +97 -11
- package/core/engine/runtime-builder.ts +88 -11
- package/core/engine/scope-resolver.ts +65 -9
- package/core/engine/soma-bootstrap.ts +448 -244
- package/core/engine/style-binder.ts +423 -0
- package/core/engine/theme-variables.ts +37 -0
- package/core/engine/timer-engine.ts +92 -0
- package/core/engine/trigger-engine.ts +55 -0
- package/core/host/resolve-host-bootstrap.ts +28 -0
- package/core/host/soma-bootstrap-host.stub.ts +4 -0
- package/core/index.ts +40 -6
- package/core/infra/auth/TokenAuthService.ts +17 -3
- package/core/infra/http/IHttpClient.ts +11 -1
- package/core/infra/http/SomaHttpService.ts +89 -31
- package/core/infra/i18n/DefaultI18n.ts +6 -18
- package/core/infra/i18n/II18n.ts +2 -3
- package/core/infra/notify/DomNotifyService.ts +98 -0
- package/core/infra/notify/INotifyTheme.ts +22 -0
- package/core/infra/realtime/IRealtimeService.ts +23 -0
- package/core/infra/realtime/IRealtimeTransport.ts +9 -0
- package/core/infra/realtime/SomaRealtimeService.ts +95 -0
- package/core/infra/realtime/SseRealtimeTransport.ts +36 -0
- package/core/infra/realtime/WebSocketRealtimeTransport.ts +102 -0
- package/core/infra/router/router-path.ts +28 -0
- package/core/infra/storage/SessionStorageService.ts +59 -0
- package/core/lsp/cross-file-diagnostics.cjs +160 -0
- package/core/lsp/event-yaml-completion.cjs +106 -0
- package/core/lsp/extra-completion.cjs +64 -0
- package/core/lsp/lsp-navigation.cjs +191 -0
- package/core/lsp/ui-yaml-completion.cjs +170 -0
- package/core/manifest/manifest-contract.ts +12 -0
- package/core/middleware/createPermissionMiddleware.ts +37 -0
- package/core/parser/annotation-extractor.ts +113 -0
- package/core/parser/pipeline-parser.ts +344 -276
- package/core/parser/style-config-parser.ts +340 -0
- package/core/parser/timer-parser.ts +70 -0
- package/core/parser/trigger-parser.ts +90 -0
- package/core/parser/ui-config-parser.ts +73 -1
- package/core/quality/SomaQualityRegistry.ts +68 -0
- package/core/runtime/SomaRuntimeServices.ts +100 -0
- package/core/runtime/SomaSnapshotStore.ts +5 -3
- package/core/types/domain.types.ts +5 -2
- package/core/types/event.types.ts +82 -57
- package/core/types/manifest.types.ts +2 -0
- package/core/types/style-config.types.ts +76 -0
- package/core/types/timer.types.ts +23 -0
- package/core/types/trigger.types.ts +14 -0
- package/core/types/ui-config.types.ts +9 -0
- package/core/types/watcher.types.ts +29 -0
- package/core/validation/contract-validator.ts +465 -465
- package/dist/cli/soma.js +23 -23
- package/dist/core/index.cjs +8968 -0
- package/dist/diagnostics/telemetry.cjs +105 -0
- package/dist/preview-export.cjs +3401 -0
- package/dist/tsconfig.types.tsbuildinfo +1 -0
- package/dist/types/api/IService.d.ts +28 -0
- package/dist/types/api/load-host-service.d.ts +5 -0
- package/dist/types/api/service-path.d.ts +13 -0
- package/dist/types/base/SomaApi.d.ts +5 -0
- package/dist/types/base/SomaDict.d.ts +89 -0
- package/dist/types/base/SomaEntity.d.ts +15 -0
- package/dist/types/base/SomaHttpClient.d.ts +66 -0
- package/dist/types/base/SomaLocaleOnlyPageRoot.d.ts +3 -0
- package/dist/types/base/SomaPageRootBase.d.ts +49 -0
- package/dist/types/base/SomaValidator.d.ts +55 -0
- package/dist/types/base/decorators/Event.d.ts +10 -0
- package/dist/types/base/decorators/Locale.d.ts +9 -0
- package/dist/types/base/decorators/Page.d.ts +4 -0
- package/dist/types/base/decorators/Show.d.ts +7 -0
- package/dist/types/base/decorators/ShowList.d.ts +12 -0
- package/dist/types/base/decorators/index.d.ts +5 -0
- package/dist/types/base/index.d.ts +9 -0
- package/dist/types/base/runAsync.d.ts +12 -0
- package/dist/types/compiler/full-manifest-builder.d.ts +35 -0
- package/dist/types/compiler/manifest-compiler.d.ts +29 -0
- package/dist/types/compiler/page-base-resolver.d.ts +6 -0
- package/dist/types/compiler/precompiled-manifest-loader.d.ts +5 -0
- package/dist/types/compiler/rollup-plugin-soma.d.ts +14 -0
- package/dist/types/compiler/route-builder.d.ts +12 -0
- package/dist/types/compiler/soma-precompile.d.ts +40 -0
- package/dist/types/compiler/vite-plugin-soma.d.ts +34 -0
- package/dist/types/container/IContainerAdapter.d.ts +30 -0
- package/dist/types/container/MiniProgramContainerAdapter.d.ts +52 -0
- package/dist/types/container/ReactNativeContainerAdapter.d.ts +47 -0
- package/dist/types/container/SomaContainerAdapter.d.ts +30 -0
- package/dist/types/diagnostics/SomaDiagnostics.d.ts +64 -0
- package/dist/types/diagnostics/SomaTelemetry.d.ts +32 -0
- package/dist/types/diagnostics/telemetry-context.d.ts +4 -0
- package/dist/types/engine/asset-resolver.d.ts +17 -0
- package/dist/types/engine/dom-renderer.d.ts +35 -0
- package/dist/types/engine/event-engine.d.ts +64 -0
- package/dist/types/engine/lis.d.ts +10 -0
- package/dist/types/engine/on-change-engine.d.ts +16 -0
- package/dist/types/engine/property-watcher.d.ts +18 -0
- package/dist/types/engine/reactive-proxy.d.ts +58 -0
- package/dist/types/engine/render-engine.d.ts +73 -0
- package/dist/types/engine/runtime-builder.d.ts +58 -0
- package/dist/types/engine/scope-resolver.d.ts +90 -0
- package/dist/types/engine/soma-bootstrap.d.ts +112 -0
- package/dist/types/engine/style-binder.d.ts +81 -0
- package/dist/types/engine/theme-variables.d.ts +3 -0
- package/dist/types/engine/timer-engine.d.ts +19 -0
- package/dist/types/engine/trigger-engine.d.ts +16 -0
- package/dist/types/host/resolve-host-bootstrap.d.ts +11 -0
- package/dist/types/host/soma-bootstrap-host.stub.d.ts +3 -0
- package/dist/types/index.d.ts +35 -0
- package/dist/types/infra/auth/IAuthService.d.ts +12 -0
- package/dist/types/infra/auth/TokenAuthService.d.ts +26 -0
- package/dist/types/infra/cache/ICache.d.ts +12 -0
- package/dist/types/infra/cache/MemoryCache.d.ts +14 -0
- package/dist/types/infra/cache/ServerFileCache.d.ts +37 -0
- package/dist/types/infra/config/IConfig.d.ts +9 -0
- package/dist/types/infra/config/SomaConfig.d.ts +21 -0
- package/dist/types/infra/event-bus/IEventBus.d.ts +8 -0
- package/dist/types/infra/event-bus/SomaEventBus.d.ts +9 -0
- package/dist/types/infra/http/IHttpClient.d.ts +28 -0
- package/dist/types/infra/http/SomaHttpService.d.ts +27 -0
- package/dist/types/infra/i18n/DefaultI18n.d.ts +7 -0
- package/dist/types/infra/i18n/II18n.d.ts +6 -0
- package/dist/types/infra/logger/ConsoleLogger.d.ts +14 -0
- package/dist/types/infra/logger/ILogger.d.ts +17 -0
- package/dist/types/infra/notify/DefaultNotifyService.d.ts +16 -0
- package/dist/types/infra/notify/DomNotifyService.d.ts +16 -0
- package/dist/types/infra/notify/INotifyService.d.ts +12 -0
- package/dist/types/infra/notify/INotifyTheme.d.ts +12 -0
- package/dist/types/infra/permission/IPermission.d.ts +10 -0
- package/dist/types/infra/permission/SomaPermission.d.ts +19 -0
- package/dist/types/infra/realtime/IRealtimeService.d.ts +13 -0
- package/dist/types/infra/realtime/IRealtimeTransport.d.ts +8 -0
- package/dist/types/infra/realtime/SomaRealtimeService.d.ts +20 -0
- package/dist/types/infra/realtime/SseRealtimeTransport.d.ts +11 -0
- package/dist/types/infra/realtime/WebSocketRealtimeTransport.d.ts +30 -0
- package/dist/types/infra/router/IRouter.d.ts +16 -0
- package/dist/types/infra/router/SomaRouter.d.ts +18 -0
- package/dist/types/infra/router/router-path.d.ts +6 -0
- package/dist/types/infra/storage/IStorageService.d.ts +7 -0
- package/dist/types/infra/storage/MemoryStorageService.d.ts +15 -0
- package/dist/types/infra/storage/SessionStorageService.d.ts +14 -0
- package/dist/types/infra/timer/ITimer.d.ts +13 -0
- package/dist/types/infra/timer/SomaTimer.d.ts +21 -0
- package/dist/types/manifest/manifest-contract.d.ts +4 -0
- package/dist/types/middleware/SomaMiddlewareChain.d.ts +6 -0
- package/dist/types/middleware/createPermissionMiddleware.d.ts +14 -0
- package/dist/types/parser/annotation-extractor.d.ts +12 -0
- package/dist/types/parser/pipeline-parser.d.ts +62 -0
- package/dist/types/parser/style-config-parser.d.ts +20 -0
- package/dist/types/parser/timer-parser.d.ts +6 -0
- package/dist/types/parser/trigger-parser.d.ts +13 -0
- package/dist/types/parser/ui-config-parser.d.ts +34 -0
- package/dist/types/parser/yaml-loader.d.ts +21 -0
- package/dist/types/quality/SomaQualityRegistry.d.ts +31 -0
- package/dist/types/runtime/SomaRuntimeServices.d.ts +35 -0
- package/dist/types/runtime/SomaSnapshotStore.d.ts +13 -0
- package/dist/types/scheduler/SomaScheduler.d.ts +25 -0
- package/dist/types/types/domain.types.d.ts +82 -0
- package/dist/types/types/engine.types.d.ts +22 -0
- package/dist/types/types/event.types.d.ts +61 -0
- package/dist/types/types/keyboard.types.d.ts +4 -0
- package/dist/types/types/manifest.types.d.ts +29 -0
- package/dist/types/types/scheduler.types.d.ts +8 -0
- package/dist/types/types/style-config.types.d.ts +62 -0
- package/dist/types/types/timer.types.d.ts +19 -0
- package/dist/types/types/trigger.types.d.ts +12 -0
- package/dist/types/types/ui-config.types.d.ts +85 -0
- package/dist/types/types/watcher.types.d.ts +24 -0
- package/dist/types/validation/contract-validator.d.ts +40 -0
- package/editor/vscode/extension.js +39 -0
- package/editor/vscode/language-configuration.json +17 -0
- package/editor/vscode/package.json +98 -0
- package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +30 -0
- package/editor/vscode/syntaxes/soma-ui-yaml.tmLanguage.json +42 -0
- package/global.d.ts +19 -0
- package/next.config.js +9 -4
- package/package.json +53 -16
- package/tailwind.config.ts +26 -7
- package/tsconfig.json +6 -2
- package/core/router/RouteHandler.ts +0 -93
|
@@ -1,465 +1,465 @@
|
|
|
1
|
-
import type { SomaScopeResolver } from '../engine/scope-resolver';
|
|
2
|
-
import type { ParsedEvent } from '../types/event.types';
|
|
3
|
-
import type {
|
|
4
|
-
ExtendedShowValue,
|
|
5
|
-
NestedShowMapping,
|
|
6
|
-
ShowMapping,
|
|
7
|
-
UiConfig,
|
|
8
|
-
} from '../types/ui-config.types';
|
|
9
|
-
import type { SomaDiagnostics } from '../diagnostics/SomaDiagnostics';
|
|
10
|
-
|
|
11
|
-
export interface SomaContractValidationInput {
|
|
12
|
-
container: HTMLElement;
|
|
13
|
-
uiConfig: UiConfig;
|
|
14
|
-
eventConfig: ParsedEvent[];
|
|
15
|
-
resolver: SomaScopeResolver;
|
|
16
|
-
rootInstance: object;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface SomaContractReport {
|
|
20
|
-
errors: string[];
|
|
21
|
-
warnings: string[];
|
|
22
|
-
anchorIds: string[];
|
|
23
|
-
bindingCount: number;
|
|
24
|
-
eventCount: number;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export class SomaContractError extends Error {
|
|
28
|
-
readonly report: SomaContractReport;
|
|
29
|
-
|
|
30
|
-
constructor(report: SomaContractReport) {
|
|
31
|
-
const summary = report.errors[0] ?? 'SOMA contract validation failed';
|
|
32
|
-
super(`[SOMA Contract] ${summary}`);
|
|
33
|
-
this.name = 'SomaContractError';
|
|
34
|
-
this.report = report;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export class SomaContractValidator {
|
|
39
|
-
private diagnostics: SomaDiagnostics | null;
|
|
40
|
-
|
|
41
|
-
constructor(diagnostics?: SomaDiagnostics) {
|
|
42
|
-
this.diagnostics = diagnostics ?? null;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
validate(input: SomaContractValidationInput): SomaContractReport {
|
|
46
|
-
const hostCount = input.container.querySelectorAll('[data-soma-host]').length;
|
|
47
|
-
const { anchors, duplicates } = this.collectAnchorStats(input.container);
|
|
48
|
-
const report: SomaContractReport = {
|
|
49
|
-
errors: [],
|
|
50
|
-
warnings: [],
|
|
51
|
-
anchorIds: [...anchors].sort(),
|
|
52
|
-
bindingCount: 0,
|
|
53
|
-
eventCount: input.eventConfig.length,
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
if (hostCount === 0) {
|
|
57
|
-
this.addError(report, 'container: 缺少 data-soma-host 边界');
|
|
58
|
-
} else if (hostCount > 1) {
|
|
59
|
-
this.addWarning(report, `container: 检测到 ${hostCount} 个 data-soma-host
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
for (const [somaId, count] of duplicates.entries()) {
|
|
63
|
-
this.addError(report, `anchor.${somaId}: 检测到 ${count} 个重复的静态 data-soma-id,绑定会产生歧义`);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
this.validateShowMapping(
|
|
67
|
-
input.uiConfig.show,
|
|
68
|
-
anchors,
|
|
69
|
-
input.resolver,
|
|
70
|
-
input.rootInstance,
|
|
71
|
-
report,
|
|
72
|
-
'show',
|
|
73
|
-
);
|
|
74
|
-
this.validateEvents(
|
|
75
|
-
input.eventConfig,
|
|
76
|
-
anchors,
|
|
77
|
-
input.resolver,
|
|
78
|
-
input.rootInstance,
|
|
79
|
-
report,
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
if (report.errors.length > 0) {
|
|
83
|
-
throw new SomaContractError(report);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return report;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
private collectAnchorStats(container: HTMLElement): {
|
|
90
|
-
anchors: Set<string>;
|
|
91
|
-
duplicates: Map<string, number>;
|
|
92
|
-
} {
|
|
93
|
-
const anchors = new Set<string>();
|
|
94
|
-
const counts = new Map<string, number>();
|
|
95
|
-
|
|
96
|
-
const scanNode = (root: HTMLElement | DocumentFragment) => {
|
|
97
|
-
root.querySelectorAll<HTMLElement>('[data-soma-id]').forEach((element) => {
|
|
98
|
-
const somaId = element.getAttribute('data-soma-id');
|
|
99
|
-
if (somaId) {
|
|
100
|
-
anchors.add(somaId);
|
|
101
|
-
counts.set(somaId, (counts.get(somaId) ?? 0) + 1);
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
// Also scan inside <template> elements whose content lives in a DocumentFragment
|
|
105
|
-
root.querySelectorAll<HTMLTemplateElement>('template[data-soma-role]').forEach((tmpl) => {
|
|
106
|
-
if (tmpl.content) {
|
|
107
|
-
scanNode(tmpl.content);
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
scanNode(container);
|
|
113
|
-
|
|
114
|
-
const duplicates = new Map<string, number>();
|
|
115
|
-
for (const [somaId, count] of counts.entries()) {
|
|
116
|
-
if (count > 1) {
|
|
117
|
-
duplicates.set(somaId, count);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return { anchors, duplicates };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
private validateShowMapping(
|
|
125
|
-
show: ShowMapping,
|
|
126
|
-
anchors: Set<string>,
|
|
127
|
-
resolver: SomaScopeResolver,
|
|
128
|
-
rootInstance: object,
|
|
129
|
-
report: SomaContractReport,
|
|
130
|
-
basePath: string,
|
|
131
|
-
): void {
|
|
132
|
-
for (const [rawKey, value] of Object.entries(show)) {
|
|
133
|
-
report.bindingCount += 1;
|
|
134
|
-
const currentPath = `${basePath}.${rawKey}`;
|
|
135
|
-
const somaId = this.extractSomaId(rawKey);
|
|
136
|
-
|
|
137
|
-
if (!anchors.has(somaId)) {
|
|
138
|
-
this.addError(report, `${currentPath}: 缺少 data-soma-id="${somaId}"`);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (typeof value === 'string') {
|
|
142
|
-
this.validateReadablePath(value, currentPath, resolver, rootInstance, report);
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (this.isNestedShowMapping(value)) {
|
|
147
|
-
this.validateReadablePath(
|
|
148
|
-
value.bind.obj,
|
|
149
|
-
`${currentPath}.bind.obj`,
|
|
150
|
-
resolver,
|
|
151
|
-
rootInstance,
|
|
152
|
-
report,
|
|
153
|
-
);
|
|
154
|
-
this.validateShowMapping(
|
|
155
|
-
value.show,
|
|
156
|
-
anchors,
|
|
157
|
-
resolver,
|
|
158
|
-
rootInstance,
|
|
159
|
-
report,
|
|
160
|
-
`${currentPath}.show`,
|
|
161
|
-
);
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (this.isExtendedShowValue(value)) {
|
|
166
|
-
if (value.text) {
|
|
167
|
-
this.validateReadablePath(
|
|
168
|
-
value.text,
|
|
169
|
-
`${currentPath}.text`,
|
|
170
|
-
resolver,
|
|
171
|
-
rootInstance,
|
|
172
|
-
report,
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
if (value.model) {
|
|
176
|
-
this.validateReadablePath(
|
|
177
|
-
value.model,
|
|
178
|
-
`${currentPath}.model`,
|
|
179
|
-
resolver,
|
|
180
|
-
rootInstance,
|
|
181
|
-
report,
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
if (value.when) {
|
|
185
|
-
this.validateReadablePath(
|
|
186
|
-
value.when,
|
|
187
|
-
`${currentPath}.when`,
|
|
188
|
-
resolver,
|
|
189
|
-
rootInstance,
|
|
190
|
-
report,
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
if (value.class) {
|
|
194
|
-
for (const [className, expr] of Object.entries(value.class)) {
|
|
195
|
-
this.validateReadablePath(
|
|
196
|
-
expr,
|
|
197
|
-
`${currentPath}.class.${className}`,
|
|
198
|
-
resolver,
|
|
199
|
-
rootInstance,
|
|
200
|
-
report,
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
if (value.show) {
|
|
205
|
-
this.validateShowMapping(
|
|
206
|
-
value.show,
|
|
207
|
-
anchors,
|
|
208
|
-
resolver,
|
|
209
|
-
rootInstance,
|
|
210
|
-
report,
|
|
211
|
-
`${currentPath}.show`,
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
private validateEvents(
|
|
219
|
-
events: ParsedEvent[],
|
|
220
|
-
anchors: Set<string>,
|
|
221
|
-
resolver: SomaScopeResolver,
|
|
222
|
-
rootInstance: object,
|
|
223
|
-
report: SomaContractReport,
|
|
224
|
-
): void {
|
|
225
|
-
const eventKeys = new Set<string>();
|
|
226
|
-
|
|
227
|
-
for (const event of events) {
|
|
228
|
-
const eventKey = `${event.domEvent}:${event.somaId}`;
|
|
229
|
-
if (eventKeys.has(eventKey)) {
|
|
230
|
-
this.addError(report, `event.${eventKey}: 重复声明的事件绑定`);
|
|
231
|
-
} else {
|
|
232
|
-
eventKeys.add(eventKey);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
if (!anchors.has(event.somaId)) {
|
|
236
|
-
this.addError(report, `event.${eventKey}: 缺少事件源 data-soma-id="${event.somaId}"`);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (event.targetId && event.targetId !== 'self' && !anchors.has(event.targetId)) {
|
|
240
|
-
this.addError(
|
|
241
|
-
report,
|
|
242
|
-
`event.${eventKey}: 缺少交互目标 data-soma-id="${event.targetId}"`,
|
|
243
|
-
);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (event.funcPath) {
|
|
247
|
-
this.validateFunctionPath(
|
|
248
|
-
event.funcPath,
|
|
249
|
-
`event.${eventKey}.funcPath`,
|
|
250
|
-
resolver,
|
|
251
|
-
rootInstance,
|
|
252
|
-
report,
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
private validateReadablePath(
|
|
259
|
-
rawPath: string,
|
|
260
|
-
location: string,
|
|
261
|
-
resolver: SomaScopeResolver,
|
|
262
|
-
rootInstance: object,
|
|
263
|
-
report: SomaContractReport,
|
|
264
|
-
): void {
|
|
265
|
-
let path = rawPath.trim();
|
|
266
|
-
if (!path) {
|
|
267
|
-
this.addError(report, `${location}: 绑定路径不能为空`);
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (path.startsWith('!')) {
|
|
272
|
-
path = path.slice(1);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const segments = path.split('.').filter(Boolean);
|
|
276
|
-
if (segments.length === 0) {
|
|
277
|
-
this.addError(report, `${location}: 绑定路径不能为空`);
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const rootToken = segments[0];
|
|
282
|
-
|
|
283
|
-
if (resolver.hasVar(rootToken)) {
|
|
284
|
-
const varPath = resolver.getVarPath(rootToken);
|
|
285
|
-
if (varPath === '') {
|
|
286
|
-
this.validateSegments(rootInstance, segments.slice(1), location, report);
|
|
287
|
-
}
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
this.validateSegments(rootInstance, segments, location, report);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
private validateFunctionPath(
|
|
295
|
-
funcPath: string,
|
|
296
|
-
location: string,
|
|
297
|
-
resolver: SomaScopeResolver,
|
|
298
|
-
rootInstance: object,
|
|
299
|
-
report: SomaContractReport,
|
|
300
|
-
): void {
|
|
301
|
-
const segments = funcPath.split('.').filter(Boolean);
|
|
302
|
-
if (segments.length < 2) {
|
|
303
|
-
this.addError(report, `${location}: 函数路径至少需要 "对象.方法" 两段`);
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
const rootToken = segments[0];
|
|
308
|
-
if (resolver.hasVar(rootToken)) {
|
|
309
|
-
const varPath = resolver.getVarPath(rootToken);
|
|
310
|
-
if (varPath === '') {
|
|
311
|
-
try {
|
|
312
|
-
resolver.resolveFunction(funcPath);
|
|
313
|
-
} catch (error) {
|
|
314
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
315
|
-
this.addError(report, `${location}: ${message}`);
|
|
316
|
-
}
|
|
317
|
-
return;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
const sample = this.resolveAliasSample(rootToken, resolver, rootInstance);
|
|
321
|
-
if (sample === undefined) {
|
|
322
|
-
this.addWarning(
|
|
323
|
-
report,
|
|
324
|
-
`${location}: 别名 "${rootToken}" 当前缺少可验证样本,延迟到运行时校验`,
|
|
325
|
-
);
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
this.validateFunctionOnTarget(sample, segments.slice(1), location, report, funcPath);
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
try {
|
|
334
|
-
resolver.resolveFunction(funcPath);
|
|
335
|
-
} catch (error) {
|
|
336
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
337
|
-
this.addError(report, `${location}: ${message}`);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
private validateSegments(
|
|
342
|
-
rootInstance: object,
|
|
343
|
-
segments: string[],
|
|
344
|
-
location: string,
|
|
345
|
-
report: SomaContractReport,
|
|
346
|
-
): void {
|
|
347
|
-
if (segments.length === 0) {
|
|
348
|
-
return;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
let current: any = rootInstance;
|
|
352
|
-
for (let index = 0; index < segments.length; index++) {
|
|
353
|
-
const segment = segments[index];
|
|
354
|
-
|
|
355
|
-
if (current == null) {
|
|
356
|
-
this.addWarning(
|
|
357
|
-
report,
|
|
358
|
-
`${location}: 在 "${segments.slice(0, index).join('.')}" 处遇到空值,延迟到运行时校验`,
|
|
359
|
-
);
|
|
360
|
-
return;
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
if (Array.isArray(current)) {
|
|
364
|
-
this.addWarning(
|
|
365
|
-
report,
|
|
366
|
-
`${location}: "${segments.slice(0, index).join('.')}" 是集合,元素字段延迟到运行时校验`,
|
|
367
|
-
);
|
|
368
|
-
return;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
if (!(segment in Object(current))) {
|
|
372
|
-
const message = `${location}: 路径 "${segments.slice(0, index + 1).join('.')}" 不存在`;
|
|
373
|
-
if (index === 0) {
|
|
374
|
-
this.addError(report, message);
|
|
375
|
-
} else {
|
|
376
|
-
this.addWarning(report, message);
|
|
377
|
-
}
|
|
378
|
-
return;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
current = current[segment];
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
private validateFunctionOnTarget(
|
|
386
|
-
target: any,
|
|
387
|
-
segments: string[],
|
|
388
|
-
location: string,
|
|
389
|
-
report: SomaContractReport,
|
|
390
|
-
funcPath: string,
|
|
391
|
-
): void {
|
|
392
|
-
let current = target;
|
|
393
|
-
|
|
394
|
-
for (let index = 0; index < segments.length; index++) {
|
|
395
|
-
if (current == null) {
|
|
396
|
-
this.addWarning(report, `${location}: "${funcPath}" 在运行时可能为空对象`);
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
if (Array.isArray(current)) {
|
|
401
|
-
if (current.length === 0) {
|
|
402
|
-
this.addWarning(report, `${location}: "${funcPath}" 对应集合为空,延迟到运行时校验`);
|
|
403
|
-
return;
|
|
404
|
-
}
|
|
405
|
-
current = current[0];
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
const segment = segments[index];
|
|
409
|
-
if (!(segment in Object(current))) {
|
|
410
|
-
this.addError(
|
|
411
|
-
report,
|
|
412
|
-
`${location}: 函数路径 "${funcPath}" 缺少属性 "${segment}"`,
|
|
413
|
-
);
|
|
414
|
-
return;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
const next = current[segment];
|
|
418
|
-
if (index === segments.length - 1 && typeof next !== 'function') {
|
|
419
|
-
this.addError(report, `${location}: "${funcPath}" 不是可调用函数`);
|
|
420
|
-
return;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
current = next;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
private resolveAliasSample(
|
|
428
|
-
alias: string,
|
|
429
|
-
resolver: SomaScopeResolver,
|
|
430
|
-
rootInstance: object,
|
|
431
|
-
): any {
|
|
432
|
-
const varPath = resolver.getVarPath(alias);
|
|
433
|
-
if (varPath === undefined) return undefined;
|
|
434
|
-
if (!varPath) return rootInstance;
|
|
435
|
-
|
|
436
|
-
const resolved = resolver.readFromRoot(varPath);
|
|
437
|
-
if (Array.isArray(resolved)) {
|
|
438
|
-
return resolved[0];
|
|
439
|
-
}
|
|
440
|
-
return resolved;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
private extractSomaId(rawKey: string): string {
|
|
444
|
-
const [somaId] = rawKey.split('@');
|
|
445
|
-
return somaId;
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
private isNestedShowMapping(value: unknown): value is NestedShowMapping {
|
|
449
|
-
return typeof value === 'object' && value !== null && 'bind' in value && 'show' in value;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
private isExtendedShowValue(value: unknown): value is ExtendedShowValue {
|
|
453
|
-
return typeof value === 'object' && value !== null;
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
private addError(report: SomaContractReport, message: string): void {
|
|
457
|
-
report.errors.push(message);
|
|
458
|
-
this.diagnostics?.recordValidation('error', message);
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
private addWarning(report: SomaContractReport, message: string): void {
|
|
462
|
-
report.warnings.push(message);
|
|
463
|
-
this.diagnostics?.recordValidation('warning', message);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
1
|
+
import type { SomaScopeResolver } from '../engine/scope-resolver';
|
|
2
|
+
import type { ParsedEvent } from '../types/event.types';
|
|
3
|
+
import type {
|
|
4
|
+
ExtendedShowValue,
|
|
5
|
+
NestedShowMapping,
|
|
6
|
+
ShowMapping,
|
|
7
|
+
UiConfig,
|
|
8
|
+
} from '../types/ui-config.types';
|
|
9
|
+
import type { SomaDiagnostics } from '../diagnostics/SomaDiagnostics';
|
|
10
|
+
|
|
11
|
+
export interface SomaContractValidationInput {
|
|
12
|
+
container: HTMLElement;
|
|
13
|
+
uiConfig: UiConfig;
|
|
14
|
+
eventConfig: ParsedEvent[];
|
|
15
|
+
resolver: SomaScopeResolver;
|
|
16
|
+
rootInstance: object;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SomaContractReport {
|
|
20
|
+
errors: string[];
|
|
21
|
+
warnings: string[];
|
|
22
|
+
anchorIds: string[];
|
|
23
|
+
bindingCount: number;
|
|
24
|
+
eventCount: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class SomaContractError extends Error {
|
|
28
|
+
readonly report: SomaContractReport;
|
|
29
|
+
|
|
30
|
+
constructor(report: SomaContractReport) {
|
|
31
|
+
const summary = report.errors[0] ?? 'SOMA contract validation failed';
|
|
32
|
+
super(`[SOMA Contract] ${summary}`);
|
|
33
|
+
this.name = 'SomaContractError';
|
|
34
|
+
this.report = report;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class SomaContractValidator {
|
|
39
|
+
private diagnostics: SomaDiagnostics | null;
|
|
40
|
+
|
|
41
|
+
constructor(diagnostics?: SomaDiagnostics) {
|
|
42
|
+
this.diagnostics = diagnostics ?? null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
validate(input: SomaContractValidationInput): SomaContractReport {
|
|
46
|
+
const hostCount = input.container.querySelectorAll('[data-soma-host]').length;
|
|
47
|
+
const { anchors, duplicates } = this.collectAnchorStats(input.container);
|
|
48
|
+
const report: SomaContractReport = {
|
|
49
|
+
errors: [],
|
|
50
|
+
warnings: [],
|
|
51
|
+
anchorIds: [...anchors].sort(),
|
|
52
|
+
bindingCount: 0,
|
|
53
|
+
eventCount: input.eventConfig.length,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
if (hostCount === 0) {
|
|
57
|
+
this.addError(report, 'container: 缺少 data-soma-host 边界');
|
|
58
|
+
} else if (hostCount > 1) {
|
|
59
|
+
this.addWarning(report, `container: 检测到 ${hostCount} 个 data-soma-host,建议每页仅保留一个业务应用边界`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (const [somaId, count] of duplicates.entries()) {
|
|
63
|
+
this.addError(report, `anchor.${somaId}: 检测到 ${count} 个重复的静态 data-soma-id,绑定会产生歧义`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
this.validateShowMapping(
|
|
67
|
+
input.uiConfig.show,
|
|
68
|
+
anchors,
|
|
69
|
+
input.resolver,
|
|
70
|
+
input.rootInstance,
|
|
71
|
+
report,
|
|
72
|
+
'show',
|
|
73
|
+
);
|
|
74
|
+
this.validateEvents(
|
|
75
|
+
input.eventConfig,
|
|
76
|
+
anchors,
|
|
77
|
+
input.resolver,
|
|
78
|
+
input.rootInstance,
|
|
79
|
+
report,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
if (report.errors.length > 0) {
|
|
83
|
+
throw new SomaContractError(report);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return report;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private collectAnchorStats(container: HTMLElement): {
|
|
90
|
+
anchors: Set<string>;
|
|
91
|
+
duplicates: Map<string, number>;
|
|
92
|
+
} {
|
|
93
|
+
const anchors = new Set<string>();
|
|
94
|
+
const counts = new Map<string, number>();
|
|
95
|
+
|
|
96
|
+
const scanNode = (root: HTMLElement | DocumentFragment) => {
|
|
97
|
+
root.querySelectorAll<HTMLElement>('[data-soma-id]').forEach((element) => {
|
|
98
|
+
const somaId = element.getAttribute('data-soma-id');
|
|
99
|
+
if (somaId) {
|
|
100
|
+
anchors.add(somaId);
|
|
101
|
+
counts.set(somaId, (counts.get(somaId) ?? 0) + 1);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
// Also scan inside <template> elements whose content lives in a DocumentFragment
|
|
105
|
+
root.querySelectorAll<HTMLTemplateElement>('template[data-soma-role]').forEach((tmpl) => {
|
|
106
|
+
if (tmpl.content) {
|
|
107
|
+
scanNode(tmpl.content);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
scanNode(container);
|
|
113
|
+
|
|
114
|
+
const duplicates = new Map<string, number>();
|
|
115
|
+
for (const [somaId, count] of counts.entries()) {
|
|
116
|
+
if (count > 1) {
|
|
117
|
+
duplicates.set(somaId, count);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return { anchors, duplicates };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private validateShowMapping(
|
|
125
|
+
show: ShowMapping,
|
|
126
|
+
anchors: Set<string>,
|
|
127
|
+
resolver: SomaScopeResolver,
|
|
128
|
+
rootInstance: object,
|
|
129
|
+
report: SomaContractReport,
|
|
130
|
+
basePath: string,
|
|
131
|
+
): void {
|
|
132
|
+
for (const [rawKey, value] of Object.entries(show)) {
|
|
133
|
+
report.bindingCount += 1;
|
|
134
|
+
const currentPath = `${basePath}.${rawKey}`;
|
|
135
|
+
const somaId = this.extractSomaId(rawKey);
|
|
136
|
+
|
|
137
|
+
if (!anchors.has(somaId)) {
|
|
138
|
+
this.addError(report, `${currentPath}: 缺少 data-soma-id="${somaId}"`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (typeof value === 'string') {
|
|
142
|
+
this.validateReadablePath(value, currentPath, resolver, rootInstance, report);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (this.isNestedShowMapping(value)) {
|
|
147
|
+
this.validateReadablePath(
|
|
148
|
+
value.bind.obj,
|
|
149
|
+
`${currentPath}.bind.obj`,
|
|
150
|
+
resolver,
|
|
151
|
+
rootInstance,
|
|
152
|
+
report,
|
|
153
|
+
);
|
|
154
|
+
this.validateShowMapping(
|
|
155
|
+
value.show,
|
|
156
|
+
anchors,
|
|
157
|
+
resolver,
|
|
158
|
+
rootInstance,
|
|
159
|
+
report,
|
|
160
|
+
`${currentPath}.show`,
|
|
161
|
+
);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (this.isExtendedShowValue(value)) {
|
|
166
|
+
if (value.text) {
|
|
167
|
+
this.validateReadablePath(
|
|
168
|
+
value.text,
|
|
169
|
+
`${currentPath}.text`,
|
|
170
|
+
resolver,
|
|
171
|
+
rootInstance,
|
|
172
|
+
report,
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
if (value.model) {
|
|
176
|
+
this.validateReadablePath(
|
|
177
|
+
value.model,
|
|
178
|
+
`${currentPath}.model`,
|
|
179
|
+
resolver,
|
|
180
|
+
rootInstance,
|
|
181
|
+
report,
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
if (value.when) {
|
|
185
|
+
this.validateReadablePath(
|
|
186
|
+
value.when,
|
|
187
|
+
`${currentPath}.when`,
|
|
188
|
+
resolver,
|
|
189
|
+
rootInstance,
|
|
190
|
+
report,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
if (value.class) {
|
|
194
|
+
for (const [className, expr] of Object.entries(value.class)) {
|
|
195
|
+
this.validateReadablePath(
|
|
196
|
+
expr,
|
|
197
|
+
`${currentPath}.class.${className}`,
|
|
198
|
+
resolver,
|
|
199
|
+
rootInstance,
|
|
200
|
+
report,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (value.show) {
|
|
205
|
+
this.validateShowMapping(
|
|
206
|
+
value.show,
|
|
207
|
+
anchors,
|
|
208
|
+
resolver,
|
|
209
|
+
rootInstance,
|
|
210
|
+
report,
|
|
211
|
+
`${currentPath}.show`,
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private validateEvents(
|
|
219
|
+
events: ParsedEvent[],
|
|
220
|
+
anchors: Set<string>,
|
|
221
|
+
resolver: SomaScopeResolver,
|
|
222
|
+
rootInstance: object,
|
|
223
|
+
report: SomaContractReport,
|
|
224
|
+
): void {
|
|
225
|
+
const eventKeys = new Set<string>();
|
|
226
|
+
|
|
227
|
+
for (const event of events) {
|
|
228
|
+
const eventKey = `${event.domEvent}:${event.somaId}`;
|
|
229
|
+
if (eventKeys.has(eventKey)) {
|
|
230
|
+
this.addError(report, `event.${eventKey}: 重复声明的事件绑定`);
|
|
231
|
+
} else {
|
|
232
|
+
eventKeys.add(eventKey);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (!anchors.has(event.somaId)) {
|
|
236
|
+
this.addError(report, `event.${eventKey}: 缺少事件源 data-soma-id="${event.somaId}"`);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (event.targetId && event.targetId !== 'self' && !anchors.has(event.targetId)) {
|
|
240
|
+
this.addError(
|
|
241
|
+
report,
|
|
242
|
+
`event.${eventKey}: 缺少交互目标 data-soma-id="${event.targetId}"`,
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (event.funcPath) {
|
|
247
|
+
this.validateFunctionPath(
|
|
248
|
+
event.funcPath,
|
|
249
|
+
`event.${eventKey}.funcPath`,
|
|
250
|
+
resolver,
|
|
251
|
+
rootInstance,
|
|
252
|
+
report,
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
private validateReadablePath(
|
|
259
|
+
rawPath: string,
|
|
260
|
+
location: string,
|
|
261
|
+
resolver: SomaScopeResolver,
|
|
262
|
+
rootInstance: object,
|
|
263
|
+
report: SomaContractReport,
|
|
264
|
+
): void {
|
|
265
|
+
let path = rawPath.trim();
|
|
266
|
+
if (!path) {
|
|
267
|
+
this.addError(report, `${location}: 绑定路径不能为空`);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (path.startsWith('!')) {
|
|
272
|
+
path = path.slice(1);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const segments = path.split('.').filter(Boolean);
|
|
276
|
+
if (segments.length === 0) {
|
|
277
|
+
this.addError(report, `${location}: 绑定路径不能为空`);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const rootToken = segments[0];
|
|
282
|
+
|
|
283
|
+
if (resolver.hasVar(rootToken)) {
|
|
284
|
+
const varPath = resolver.getVarPath(rootToken);
|
|
285
|
+
if (varPath === '') {
|
|
286
|
+
this.validateSegments(rootInstance, segments.slice(1), location, report);
|
|
287
|
+
}
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
this.validateSegments(rootInstance, segments, location, report);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
private validateFunctionPath(
|
|
295
|
+
funcPath: string,
|
|
296
|
+
location: string,
|
|
297
|
+
resolver: SomaScopeResolver,
|
|
298
|
+
rootInstance: object,
|
|
299
|
+
report: SomaContractReport,
|
|
300
|
+
): void {
|
|
301
|
+
const segments = funcPath.split('.').filter(Boolean);
|
|
302
|
+
if (segments.length < 2) {
|
|
303
|
+
this.addError(report, `${location}: 函数路径至少需要 "对象.方法" 两段`);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const rootToken = segments[0];
|
|
308
|
+
if (resolver.hasVar(rootToken)) {
|
|
309
|
+
const varPath = resolver.getVarPath(rootToken);
|
|
310
|
+
if (varPath === '') {
|
|
311
|
+
try {
|
|
312
|
+
resolver.resolveFunction(funcPath);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
315
|
+
this.addError(report, `${location}: ${message}`);
|
|
316
|
+
}
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const sample = this.resolveAliasSample(rootToken, resolver, rootInstance);
|
|
321
|
+
if (sample === undefined) {
|
|
322
|
+
this.addWarning(
|
|
323
|
+
report,
|
|
324
|
+
`${location}: 别名 "${rootToken}" 当前缺少可验证样本,延迟到运行时校验`,
|
|
325
|
+
);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
this.validateFunctionOnTarget(sample, segments.slice(1), location, report, funcPath);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
try {
|
|
334
|
+
resolver.resolveFunction(funcPath);
|
|
335
|
+
} catch (error) {
|
|
336
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
337
|
+
this.addError(report, `${location}: ${message}`);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
private validateSegments(
|
|
342
|
+
rootInstance: object,
|
|
343
|
+
segments: string[],
|
|
344
|
+
location: string,
|
|
345
|
+
report: SomaContractReport,
|
|
346
|
+
): void {
|
|
347
|
+
if (segments.length === 0) {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
let current: any = rootInstance;
|
|
352
|
+
for (let index = 0; index < segments.length; index++) {
|
|
353
|
+
const segment = segments[index];
|
|
354
|
+
|
|
355
|
+
if (current == null) {
|
|
356
|
+
this.addWarning(
|
|
357
|
+
report,
|
|
358
|
+
`${location}: 在 "${segments.slice(0, index).join('.')}" 处遇到空值,延迟到运行时校验`,
|
|
359
|
+
);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if (Array.isArray(current)) {
|
|
364
|
+
this.addWarning(
|
|
365
|
+
report,
|
|
366
|
+
`${location}: "${segments.slice(0, index).join('.')}" 是集合,元素字段延迟到运行时校验`,
|
|
367
|
+
);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (!(segment in Object(current))) {
|
|
372
|
+
const message = `${location}: 路径 "${segments.slice(0, index + 1).join('.')}" 不存在`;
|
|
373
|
+
if (index === 0) {
|
|
374
|
+
this.addError(report, message);
|
|
375
|
+
} else {
|
|
376
|
+
this.addWarning(report, message);
|
|
377
|
+
}
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
current = current[segment];
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
private validateFunctionOnTarget(
|
|
386
|
+
target: any,
|
|
387
|
+
segments: string[],
|
|
388
|
+
location: string,
|
|
389
|
+
report: SomaContractReport,
|
|
390
|
+
funcPath: string,
|
|
391
|
+
): void {
|
|
392
|
+
let current = target;
|
|
393
|
+
|
|
394
|
+
for (let index = 0; index < segments.length; index++) {
|
|
395
|
+
if (current == null) {
|
|
396
|
+
this.addWarning(report, `${location}: "${funcPath}" 在运行时可能为空对象`);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (Array.isArray(current)) {
|
|
401
|
+
if (current.length === 0) {
|
|
402
|
+
this.addWarning(report, `${location}: "${funcPath}" 对应集合为空,延迟到运行时校验`);
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
current = current[0];
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const segment = segments[index];
|
|
409
|
+
if (!(segment in Object(current))) {
|
|
410
|
+
this.addError(
|
|
411
|
+
report,
|
|
412
|
+
`${location}: 函数路径 "${funcPath}" 缺少属性 "${segment}"`,
|
|
413
|
+
);
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const next = current[segment];
|
|
418
|
+
if (index === segments.length - 1 && typeof next !== 'function') {
|
|
419
|
+
this.addError(report, `${location}: "${funcPath}" 不是可调用函数`);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
current = next;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
private resolveAliasSample(
|
|
428
|
+
alias: string,
|
|
429
|
+
resolver: SomaScopeResolver,
|
|
430
|
+
rootInstance: object,
|
|
431
|
+
): any {
|
|
432
|
+
const varPath = resolver.getVarPath(alias);
|
|
433
|
+
if (varPath === undefined) return undefined;
|
|
434
|
+
if (!varPath) return rootInstance;
|
|
435
|
+
|
|
436
|
+
const resolved = resolver.readFromRoot(varPath);
|
|
437
|
+
if (Array.isArray(resolved)) {
|
|
438
|
+
return resolved[0];
|
|
439
|
+
}
|
|
440
|
+
return resolved;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
private extractSomaId(rawKey: string): string {
|
|
444
|
+
const [somaId] = rawKey.split('@');
|
|
445
|
+
return somaId;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
private isNestedShowMapping(value: unknown): value is NestedShowMapping {
|
|
449
|
+
return typeof value === 'object' && value !== null && 'bind' in value && 'show' in value;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
private isExtendedShowValue(value: unknown): value is ExtendedShowValue {
|
|
453
|
+
return typeof value === 'object' && value !== null;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
private addError(report: SomaContractReport, message: string): void {
|
|
457
|
+
report.errors.push(message);
|
|
458
|
+
this.diagnostics?.recordValidation('error', message);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
private addWarning(report: SomaContractReport, message: string): void {
|
|
462
|
+
report.warnings.push(message);
|
|
463
|
+
this.diagnostics?.recordValidation('warning', message);
|
|
464
|
+
}
|
|
465
|
+
}
|