soma-core 2.2.5 → 2.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (283) hide show
  1. package/README.md +5 -6
  2. package/bin/soma.cjs +2 -2
  3. package/core/build/compiler/flat-page.ts +16 -0
  4. package/core/build/compiler/full-manifest-builder.ts +0 -3
  5. package/core/build/compiler/page-assets.ts +0 -1
  6. package/core/build/compiler/page-closure.ts +245 -0
  7. package/core/build/compiler/page-declaration-compiler.ts +2 -5
  8. package/core/build/compiler/soma-precompile.js +1972 -3312
  9. package/core/build/compiler/soma-precompile.ts +4 -7
  10. package/core/build/component-tree/artifact-codec.ts +4 -8
  11. package/core/build/lsp/soma-declaration.cjs +1 -1
  12. package/core/build/lsp/std-completion.generated.json +3 -3
  13. package/core/client/a11y/a11y-config.ts +2 -1
  14. package/core/client/a11y/dom-id.ts +1 -1
  15. package/core/client/base/SomaHttpClient.ts +11 -3
  16. package/core/client/base/SomaPageRootBase.ts +6 -0
  17. package/core/client/base/defineSomaView.ts +178 -0
  18. package/core/client/base/index.ts +13 -0
  19. package/core/client/base/view-context.ts +328 -0
  20. package/core/client/behavior/adapter-collection.ts +2 -2
  21. package/core/client/compiler/manifest-compiler.ts +5 -36
  22. package/core/client/component/component-definition.types.ts +2 -1
  23. package/core/client/component/component-manager.ts +110 -20
  24. package/core/client/component/structure-adapter.ts +3 -7
  25. package/core/client/container/IContainerAdapter.ts +4 -0
  26. package/core/client/container/SomaContainerAdapter.ts +52 -3
  27. package/core/client/data/data-object-resolver.ts +523 -184
  28. package/core/client/data/data-write-actions.ts +79 -15
  29. package/core/client/data/format-config.ts +3 -1
  30. package/core/client/data/region-scope-port.ts +11 -0
  31. package/core/client/engine/asset-resolver.ts +2 -5
  32. package/core/client/engine/dom-renderer.ts +7 -40
  33. package/core/client/engine/event-engine.ts +29 -108
  34. package/core/client/engine/property-watcher.ts +1 -1
  35. package/core/client/engine/reactive-proxy.ts +78 -17
  36. package/core/client/engine/runtime-builder.ts +0 -14
  37. package/core/client/engine/scope-resolver.ts +18 -0
  38. package/core/client/engine/soma-bootstrap.ts +387 -99
  39. package/core/client/engine/style-binder.ts +49 -106
  40. package/core/client/engine/timer-engine.ts +11 -1
  41. package/core/client/host-bridge/load-host-domain-class.ts +2 -1
  42. package/core/client/infra/http/SomaHttpService.ts +15 -4
  43. package/core/client/infra/i18n/pack-locales.ts +2 -2
  44. package/core/client/infra/notify/DefaultNotifyService.ts +2 -2
  45. package/core/client/mount/domain-rule.ts +7 -3
  46. package/core/client/mount/mount-page.ts +38 -5
  47. package/core/client/mount/types.ts +2 -0
  48. package/core/client/parser/confirm-spec.ts +100 -7
  49. package/core/client/parser/data-config-parser.ts +28 -211
  50. package/core/client/parser/pipeline-parser.ts +16 -60
  51. package/core/client/parser/style-config-parser.ts +101 -95
  52. package/core/client/parser/timer-parser.ts +21 -0
  53. package/core/client/parser/ui-config-parser.ts +64 -12
  54. package/core/client/registry/declaration-renderer.ts +1 -1
  55. package/core/client/render-ports/hide-transition.ts +9 -0
  56. package/core/client/render-ports/index.ts +4 -0
  57. package/core/client/render-ports/notify-renderer.ts +2 -1
  58. package/core/client/runtime/SomaRuntimeServices.ts +2 -1
  59. package/core/client/runtime/SomaSnapshotStore.ts +5 -0
  60. package/core/client/security/url-guard.ts +71 -11
  61. package/core/client/validation/async-check-registry.ts +2 -1
  62. package/core/client/validation/contract-validator.ts +2 -6
  63. package/core/client/validation/field-rules.ts +3 -13
  64. package/core/client/validation/object-rules.ts +2 -10
  65. package/core/index.ts +17 -39
  66. package/core/inspect/check-product.ts +5 -5
  67. package/core/inspect/runtime.ts +2 -1
  68. package/core/server/api/dispatch-service-api.ts +35 -36
  69. package/core/server/api/load-host-service.ts +8 -4
  70. package/core/server/api/paged-list.ts +259 -0
  71. package/core/server/compile-port.ts +1 -1
  72. package/core/server/host/app-config-composer.ts +2 -1
  73. package/core/server/host/empty-host-bootstrap.mjs +3 -0
  74. package/core/server/host/host-assembly-entry.ts +1 -0
  75. package/core/server/host/host-assembly.ts +50 -3
  76. package/core/server/host/host-caller.ts +2 -1
  77. package/core/server/host/host-dir.ts +9 -0
  78. package/core/server/host/host-response-headers.ts +2 -1
  79. package/core/server/host/invoke-host-request-hook.ts +28 -4
  80. package/core/server/host/published-page-assets.ts +2 -1
  81. package/core/server/host/resolve-host-request-hook.ts +25 -7
  82. package/core/server/host/server-page-assets.ts +4 -4
  83. package/core/server/host/source-aliases.cjs +345 -0
  84. package/core/server/host/source-aliases.ts +32 -0
  85. package/core/server/host/source-packages.cjs +1 -0
  86. package/core/server/host/virtual-pages.ts +3 -2
  87. package/core/server/infra/cache/ServerFileCache.ts +83 -10
  88. package/core/server/node.ts +10 -0
  89. package/core/server/registry/api-registry-store.ts +3 -1
  90. package/core/server/registry/declaration-precompiler.ts +0 -2
  91. package/core/server/registry/page-registry-repository.ts +2 -1
  92. package/core/server/registry/page-registry-store.ts +2 -1
  93. package/core/server/registry/registry-error.ts +6 -1
  94. package/core/server/registry/registry-file-lock.ts +296 -23
  95. package/core/server/registry/request-body-limit.ts +3 -2
  96. package/core/server/registry/validation/declaration-safety-scan.ts +151 -27
  97. package/core/server/runtime-space/access-gate.ts +21 -7
  98. package/core/server/runtime-space/bindings.ts +3 -6
  99. package/core/server/runtime-space/dynamic-pages.ts +181 -49
  100. package/core/server/runtime-space/errors.ts +3 -0
  101. package/core/server/runtime-space/inject.ts +78 -16
  102. package/core/server/runtime-space/page-grants.ts +7 -1
  103. package/core/server/runtime-space/resolve.ts +66 -19
  104. package/core/server/runtime-space/store.ts +32 -3
  105. package/core/server/runtime-space/submit-steps.ts +2 -1
  106. package/core/server/runtime-space/types.ts +3 -1
  107. package/core/shared/component-tree-errors.ts +2 -1
  108. package/core/shared/contract-a11y.ts +1 -1
  109. package/core/shared/contract-sections.ts +2 -2
  110. package/core/shared/core-diagnostic-codes.ts +14 -0
  111. package/core/shared/data-declaration-checks.ts +29 -3
  112. package/core/shared/declaration-keys.ts +1 -1
  113. package/core/shared/engines/engine-error.ts +2 -1
  114. package/core/shared/generation-error.ts +2 -1
  115. package/core/shared/page-envelope.ts +1 -1
  116. package/core/shared/pure-binding.ts +88 -0
  117. package/core/shared/registry-generated/diagnostic-registry-index.ts +21 -19
  118. package/core/shared/registry-generated/diagnostic-registry.ts +170 -177
  119. package/core/shared/registry-generated/limits.ts +65 -13
  120. package/core/shared/registry-generated/std-grammar-completion.ts +0 -4
  121. package/core/shared/registry-generated/std-grammar-views.ts +40 -10
  122. package/core/shared/registry-generated/std-grammar.ts +43 -21
  123. package/core/shared/soma-error.ts +36 -0
  124. package/core/shared/types/compile-port.types.ts +1 -1
  125. package/core/shared/types/data-config.types.ts +20 -18
  126. package/core/shared/types/engine.types.ts +0 -3
  127. package/core/shared/types/event.types.ts +10 -11
  128. package/core/shared/types/generation-error.types.ts +5 -1
  129. package/core/shared/types/host-assembly.types.ts +7 -1
  130. package/core/shared/types/manifest.types.ts +0 -3
  131. package/core/shared/types/region-scope.types.ts +5 -1
  132. package/core/shared/types/scope-manifest.types.ts +0 -2
  133. package/core/shared/types/style-config.types.ts +21 -21
  134. package/core/shared/types/ui-config.types.ts +4 -5
  135. package/core/shared/types/watcher.types.ts +1 -1
  136. package/dist/browser/runtime-bundle.manifest.json +11 -13
  137. package/dist/browser/soma-runtime.js +2733 -2712
  138. package/dist/core/index.cjs +5665 -5534
  139. package/dist/host-native/aot-builder.cjs +330 -1
  140. package/dist/host-native/native-server.cjs +5434 -4726
  141. package/dist/lsp/component-tree.cjs +5868 -7040
  142. package/dist/types/build/compiler/full-manifest-builder.d.ts +0 -2
  143. package/dist/types/build/compiler/page-closure.d.ts +21 -0
  144. package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -4
  145. package/dist/types/build/compiler/soma-precompile.d.ts +1 -3
  146. package/dist/types/build/component-tree/artifact-codec.d.ts +0 -2
  147. package/dist/types/client/a11y/a11y-config.d.ts +2 -1
  148. package/dist/types/client/base/SomaHttpClient.d.ts +1 -1
  149. package/dist/types/client/base/SomaPageRootBase.d.ts +5 -0
  150. package/dist/types/client/base/defineSomaView.d.ts +46 -0
  151. package/dist/types/client/base/index.d.ts +2 -0
  152. package/dist/types/client/base/view-context.d.ts +136 -0
  153. package/dist/types/client/behavior/adapter-collection.d.ts +1 -1
  154. package/dist/types/client/compiler/manifest-compiler.d.ts +0 -3
  155. package/dist/types/client/component/component-definition.types.d.ts +2 -1
  156. package/dist/types/client/component/component-manager.d.ts +29 -0
  157. package/dist/types/client/container/IContainerAdapter.d.ts +3 -0
  158. package/dist/types/client/container/SomaContainerAdapter.d.ts +5 -0
  159. package/dist/types/client/data/data-object-resolver.d.ts +93 -31
  160. package/dist/types/client/data/format-config.d.ts +2 -1
  161. package/dist/types/client/engine/dom-renderer.d.ts +1 -2
  162. package/dist/types/client/engine/event-engine.d.ts +0 -7
  163. package/dist/types/client/engine/reactive-proxy.d.ts +11 -4
  164. package/dist/types/client/engine/runtime-builder.d.ts +0 -4
  165. package/dist/types/client/engine/soma-bootstrap.d.ts +44 -6
  166. package/dist/types/client/engine/style-binder.d.ts +8 -4
  167. package/dist/types/client/host-bridge/load-host-domain-class.d.ts +2 -1
  168. package/dist/types/client/mount/domain-rule.d.ts +2 -0
  169. package/dist/types/client/mount/mount-page.d.ts +2 -1
  170. package/dist/types/client/mount/types.d.ts +2 -0
  171. package/dist/types/client/parser/confirm-spec.d.ts +27 -3
  172. package/dist/types/client/parser/data-config-parser.d.ts +6 -5
  173. package/dist/types/client/parser/style-config-parser.d.ts +7 -3
  174. package/dist/types/client/parser/timer-parser.d.ts +5 -0
  175. package/dist/types/client/parser/ui-config-parser.d.ts +6 -2
  176. package/dist/types/client/render-ports/hide-transition.d.ts +9 -0
  177. package/dist/types/client/render-ports/index.d.ts +4 -0
  178. package/dist/types/client/render-ports/notify-renderer.d.ts +2 -1
  179. package/dist/types/client/runtime/SomaSnapshotStore.d.ts +2 -0
  180. package/dist/types/client/security/url-guard.d.ts +14 -0
  181. package/dist/types/client/validation/async-check-registry.d.ts +2 -1
  182. package/dist/types/client/validation/object-rules.d.ts +0 -5
  183. package/dist/types/index.d.ts +6 -11
  184. package/dist/types/inspect/runtime.d.ts +2 -1
  185. package/dist/types/server/api/dispatch-service-api.d.ts +5 -0
  186. package/dist/types/server/api/load-host-service.d.ts +1 -1
  187. package/dist/types/server/api/paged-list.d.ts +101 -0
  188. package/dist/types/server/compile-port.d.ts +1 -1
  189. package/dist/types/server/host/host-assembly-entry.d.ts +1 -1
  190. package/dist/types/server/host/host-assembly.d.ts +2 -0
  191. package/dist/types/server/host/host-dir.d.ts +2 -0
  192. package/dist/types/server/host/invoke-host-request-hook.d.ts +10 -3
  193. package/dist/types/server/host/published-page-assets.d.ts +2 -1
  194. package/dist/types/server/host/resolve-host-request-hook.d.ts +18 -5
  195. package/dist/types/server/host/server-page-assets.d.ts +2 -1
  196. package/dist/types/server/host/source-aliases.d.cts +53 -0
  197. package/dist/types/server/host/source-aliases.d.ts +30 -0
  198. package/dist/types/server/host/source-packages.d.cts +2 -0
  199. package/dist/types/server/infra/cache/ServerFileCache.d.ts +8 -0
  200. package/dist/types/server/node.d.ts +1 -0
  201. package/dist/types/server/registry/declaration-precompiler.d.ts +0 -1
  202. package/dist/types/server/registry/registry-error.d.ts +5 -1
  203. package/dist/types/server/registry/registry-file-lock.d.ts +30 -0
  204. package/dist/types/server/registry/request-body-limit.d.ts +3 -2
  205. package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +3 -0
  206. package/dist/types/server/runtime-space/access-gate.d.ts +7 -0
  207. package/dist/types/server/runtime-space/bindings.d.ts +3 -2
  208. package/dist/types/server/runtime-space/dynamic-pages.d.ts +6 -1
  209. package/dist/types/server/runtime-space/errors.d.ts +1 -1
  210. package/dist/types/server/runtime-space/resolve.d.ts +2 -0
  211. package/dist/types/server/runtime-space/store.d.ts +8 -1
  212. package/dist/types/server/runtime-space/submit-steps.d.ts +2 -1
  213. package/dist/types/server/runtime-space/types.d.ts +3 -1
  214. package/dist/types/shared/component-tree-errors.d.ts +2 -1
  215. package/dist/types/shared/core-diagnostic-codes.d.ts +1 -1
  216. package/dist/types/shared/declaration-keys.d.ts +1 -1
  217. package/dist/types/shared/engines/engine-error.d.ts +2 -1
  218. package/dist/types/shared/generation-error.d.ts +2 -1
  219. package/dist/types/shared/pure-binding.d.ts +48 -0
  220. package/dist/types/shared/registry-generated/diagnostic-registry.d.ts +149 -158
  221. package/dist/types/shared/registry-generated/limits.d.ts +54 -10
  222. package/dist/types/shared/registry-generated/std-grammar-completion.d.ts +0 -30
  223. package/dist/types/shared/registry-generated/std-grammar-views.d.ts +97 -23
  224. package/dist/types/shared/registry-generated/std-grammar.d.ts +110 -242
  225. package/dist/types/shared/soma-error.d.ts +15 -0
  226. package/dist/types/shared/types/data-config.types.d.ts +17 -17
  227. package/dist/types/shared/types/engine.types.d.ts +0 -3
  228. package/dist/types/shared/types/event.types.d.ts +9 -7
  229. package/dist/types/shared/types/generation-error.types.d.ts +1 -1
  230. package/dist/types/shared/types/host-assembly.types.d.ts +2 -0
  231. package/dist/types/shared/types/manifest.types.d.ts +0 -3
  232. package/dist/types/shared/types/region-scope.types.d.ts +4 -1
  233. package/dist/types/shared/types/scope-manifest.types.d.ts +0 -2
  234. package/dist/types/shared/types/style-config.types.d.ts +17 -19
  235. package/dist/types/shared/types/ui-config.types.d.ts +4 -5
  236. package/host-native/bin/host-native.cjs +22 -1
  237. package/host-native/client/browser-entry.ts +21 -172
  238. package/host-native/src/aot-builder.ts +75 -0
  239. package/host-native/src/host-prepare.ts +39 -5
  240. package/host-native/src/manifest-http.ts +2 -3
  241. package/host-native/src/native-server.ts +812 -174
  242. package/host-native/src/page-http.ts +3 -3
  243. package/host-native/src/public-packages.cjs +0 -1
  244. package/host-native/src/registry-handler.ts +12 -5
  245. package/host-native/src/static-file.ts +203 -53
  246. package/host-native/styles/globals.css +30 -28
  247. package/package.json +14 -2
  248. package/core/client/engine/step-runner.ts +0 -212
  249. package/core/client/engine/trigger-engine.ts +0 -55
  250. package/core/client/format/zoned.ts +0 -10
  251. package/core/client/parser/step-parser.ts +0 -359
  252. package/core/client/parser/trigger-parser.ts +0 -90
  253. package/core/client/parser/value-lang.ts +0 -340
  254. package/core/client/time/instant.ts +0 -2
  255. package/core/shared/calc/coerce.ts +0 -2
  256. package/core/shared/calc/errors.ts +0 -40
  257. package/core/shared/calc/evaluator.ts +0 -113
  258. package/core/shared/calc/index.ts +0 -11
  259. package/core/shared/calc/lexer.ts +0 -189
  260. package/core/shared/calc/parser.ts +0 -193
  261. package/core/shared/calc/typer.ts +0 -171
  262. package/core/shared/date-parts.ts +0 -2
  263. package/core/shared/show-when.ts +0 -77
  264. package/core/shared/types/step.types.ts +0 -91
  265. package/core/shared/types/trigger.types.ts +0 -14
  266. package/dist/types/client/engine/step-runner.d.ts +0 -42
  267. package/dist/types/client/engine/trigger-engine.d.ts +0 -16
  268. package/dist/types/client/format/zoned.d.ts +0 -1
  269. package/dist/types/client/parser/step-parser.d.ts +0 -23
  270. package/dist/types/client/parser/trigger-parser.d.ts +0 -13
  271. package/dist/types/client/parser/value-lang.d.ts +0 -63
  272. package/dist/types/client/time/instant.d.ts +0 -1
  273. package/dist/types/shared/calc/coerce.d.ts +0 -1
  274. package/dist/types/shared/calc/errors.d.ts +0 -14
  275. package/dist/types/shared/calc/evaluator.d.ts +0 -2
  276. package/dist/types/shared/calc/index.d.ts +0 -6
  277. package/dist/types/shared/calc/lexer.d.ts +0 -19
  278. package/dist/types/shared/calc/parser.d.ts +0 -43
  279. package/dist/types/shared/calc/typer.d.ts +0 -11
  280. package/dist/types/shared/date-parts.d.ts +0 -1
  281. package/dist/types/shared/show-when.d.ts +0 -21
  282. package/dist/types/shared/types/step.types.d.ts +0 -76
  283. package/dist/types/shared/types/trigger.types.d.ts +0 -12
@@ -1,189 +0,0 @@
1
- // calc 词法分析(派生列)。
2
- //
3
- // 表外的任何字符立即报错,绝不跳过——被静默忽略的字符会让 `[a] & [b]` 变成 `[a] [b]`,
4
- // 然后在语法分析里报一句莫名其妙的「意外的 REF」。越早报错,错误信息离真正的原因越近。
5
- //
6
- // 三个刻意的词法决定:
7
- // 1. 日期字面量用井号包裹 `#2026-09-04#`。不包裹的话 `2026-09-04` 与「2026 减 9 减 4」在词法上无法区分。
8
- // 2. 字符串只认双引号。声明住在 YAML 里,单引号是 YAML 自己的引号。
9
- // 3. 函数名只认大写的那六个。其它标识符都是错,不给「调用自定义函数」留口子。
10
-
11
- import { SomaCalcSyntaxError, type CalcSpan } from './errors';
12
-
13
- export type CalcTokenKind =
14
- | 'number' | 'string' | 'date' | 'bool' | 'ref' | 'ident'
15
- | 'op' | 'lparen' | 'rparen' | 'comma' | 'eof';
16
-
17
- export interface CalcToken {
18
- kind: CalcTokenKind;
19
- /** number → 数值;string → 已去引号去转义的文本;date → UTC 毫秒;bool → 布尔;
20
- * ref → 字段名;ident → 函数名;op → 运算符原文 */
21
- value: string | number | boolean;
22
- pos: CalcSpan;
23
- }
24
-
25
- /** 六个函数名——封闭集 */
26
- export const CALC_FUNCTIONS = ['IF', 'MIN', 'MAX', 'ROUND', 'CONCAT', 'ABS'] as const;
27
- export type CalcFunctionName = typeof CALC_FUNCTIONS[number];
28
-
29
- /** 运算符——按长度降序排列,保证 `>=` 先于 `>` 匹配 */
30
- const OPERATORS = ['>=', '<=', '==', '!=', '+', '-', '*', '/', '>', '<'] as const;
31
-
32
- const IDENT_START = /[A-Za-z_]/;
33
- const IDENT_PART = /[A-Za-z0-9_]/;
34
- const DIGIT = /[0-9]/;
35
-
36
- export function tokenize(source: string): CalcToken[] {
37
- const tokens: CalcToken[] = [];
38
- let i = 0;
39
-
40
- const fail: (message: string, from: number, to: number) => never = (message, from, to) => {
41
- throw new SomaCalcSyntaxError(message, source, [from, to] as CalcSpan);
42
- };
43
-
44
- while (i < source.length) {
45
- const char = source[i];
46
-
47
- // 空白:只认空格与制表;换行不该出现在一行 YAML 标量里,出现了多半是块标量写错了
48
- if (char === ' ' || char === '\t') { i += 1; continue; }
49
- if (char === '\n' || char === '\r') {
50
- fail('表达式不能换行(它是一个 YAML 标量,不是块)', i, i + 1);
51
- }
52
-
53
- const start = i;
54
-
55
- // 字段引用 [name]
56
- if (char === '[') {
57
- i += 1;
58
- const nameStart = i;
59
- while (i < source.length && source[i] !== ']') i += 1;
60
- if (i >= source.length) fail('字段引用缺少闭合的 "]"', start, source.length);
61
- const name = source.slice(nameStart, i);
62
- i += 1; // 吃掉 ]
63
- if (name === '') fail('字段引用不能为空(写成 [字段名])', start, i);
64
- if (!IDENT_START.test(name[0]) || ![...name].every((c) => IDENT_PART.test(c))) {
65
- fail(`字段名 "${name}" 必须是标识符(字母或下划线开头);派生列只能引用同层字段,不支持 . 深取`, start, i);
66
- }
67
- tokens.push({ kind: 'ref', value: name, pos: [start, i] });
68
- continue;
69
- }
70
-
71
- // 日期字面量 #YYYY-MM-DD# / #YYYY-MM-DDTHH:mm#
72
- if (char === '#') {
73
- i += 1;
74
- const textStart = i;
75
- while (i < source.length && source[i] !== '#') i += 1;
76
- if (i >= source.length) fail('日期字面量缺少闭合的 "#"', start, source.length);
77
- const text = source.slice(textStart, i);
78
- i += 1;
79
- const ms = parseDateLiteral(text);
80
- if (ms === null) {
81
- fail(`日期字面量 "${text}" 不合法(只认 #YYYY-MM-DD# 与 #YYYY-MM-DDTHH:mm#)`, start, i);
82
- }
83
- tokens.push({ kind: 'date', value: ms, pos: [start, i] });
84
- continue;
85
- }
86
-
87
- // 字符串 "…"
88
- if (char === '"') {
89
- i += 1;
90
- let text = '';
91
- while (i < source.length && source[i] !== '"') {
92
- if (source[i] === '\n') fail('字符串不能跨行', start, i);
93
- if (source[i] === '\\') {
94
- const next = source[i + 1];
95
- if (next !== '"' && next !== '\\') {
96
- fail(`字符串里只允许 \\" 与 \\\\ 两种转义,收到 "\\${String(next ?? '')}"`, i, i + 2);
97
- }
98
- text += next;
99
- i += 2;
100
- continue;
101
- }
102
- text += source[i];
103
- i += 1;
104
- }
105
- if (i >= source.length) fail('字符串缺少闭合的双引号', start, source.length);
106
- i += 1;
107
- tokens.push({ kind: 'string', value: text, pos: [start, i] });
108
- continue;
109
- }
110
-
111
- // 数字(无指数、无前导正号——`-` 一律是一元算子)
112
- if (DIGIT.test(char)) {
113
- while (i < source.length && DIGIT.test(source[i])) i += 1;
114
- if (source[i] === '.') {
115
- i += 1;
116
- if (!DIGIT.test(source[i] ?? '')) fail('小数点后缺少数字', start, i + 1);
117
- while (i < source.length && DIGIT.test(source[i])) i += 1;
118
- }
119
- if (source[i] === 'e' || source[i] === 'E') {
120
- fail('不支持科学计数法——派生列的数值应当是人能一眼读懂的字面量', start, i + 1);
121
- }
122
- tokens.push({ kind: 'number', value: Number(source.slice(start, i)), pos: [start, i] });
123
- continue;
124
- }
125
-
126
- // 标识符:true / false / 六个函数名;其余一律错
127
- if (IDENT_START.test(char)) {
128
- while (i < source.length && IDENT_PART.test(source[i])) i += 1;
129
- const word = source.slice(start, i);
130
- if (word === 'true' || word === 'false') {
131
- tokens.push({ kind: 'bool', value: word === 'true', pos: [start, i] });
132
- continue;
133
- }
134
- if ((CALC_FUNCTIONS as readonly string[]).includes(word)) {
135
- tokens.push({ kind: 'ident', value: word, pos: [start, i] });
136
- continue;
137
- }
138
- fail(
139
- `未知的标识符 "${word}"。字段要写成 [${word}];函数只有 ${CALC_FUNCTIONS.join(' / ')}`,
140
- start, i,
141
- );
142
- }
143
-
144
- if (char === '(') { tokens.push({ kind: 'lparen', value: '(', pos: [start, i + 1] }); i += 1; continue; }
145
- if (char === ')') { tokens.push({ kind: 'rparen', value: ')', pos: [start, i + 1] }); i += 1; continue; }
146
- if (char === ',') { tokens.push({ kind: 'comma', value: ',', pos: [start, i + 1] }); i += 1; continue; }
147
-
148
- const op = OPERATORS.find((candidate) => source.startsWith(candidate, i));
149
- if (op) {
150
- i += op.length;
151
- tokens.push({ kind: 'op', value: op, pos: [start, i] });
152
- continue;
153
- }
154
-
155
- // 单独的 = & | ! 最容易被误写,专门给一句能直接改的提示
156
- const hint = char === '=' ? '判断相等要写 =='
157
- : char === '&' ? '并且要写成 IF 的嵌套;calc 不支持逻辑运算符'
158
- : char === '|' ? '或者要写成 IF 的嵌套;calc 不支持逻辑运算符'
159
- : char === '!' ? '不等要写 !='
160
- : char === "'" ? '字符串要用双引号(单引号是 YAML 的引号)'
161
- : '它不在 calc 的词法表里';
162
- fail(`无法识别的字符 "${char}"——${hint}`, start, i + 1);
163
- }
164
-
165
- tokens.push({ kind: 'eof', value: '', pos: [source.length, source.length] });
166
- return tokens;
167
- }
168
-
169
- /**
170
- * 日期字面量 → UTC 毫秒。
171
- * 用 `Date.UTC` 而不是 `Date.parse`:后者对无时区的串在各引擎上曾有本地/UTC 之争,
172
- * 而派生列的日期比较必须在任何机器上给同一个答案。
173
- */
174
- export function parseDateLiteral(text: string): number | null {
175
- const match = text.match(/^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2}))?$/);
176
- if (!match) return null;
177
- const [, y, mo, d, h, mi] = match;
178
- const year = Number(y);
179
- const month = Number(mo);
180
- const day = Number(d);
181
- const hour = h === undefined ? 0 : Number(h);
182
- const minute = mi === undefined ? 0 : Number(mi);
183
- if (month < 1 || month > 12 || day < 1 || day > 31 || hour > 23 || minute > 59) return null;
184
- const ms = Date.UTC(year, month - 1, day, hour, minute);
185
- // 回读校验:2026-02-31 会被 Date.UTC 滚成 3 月 3 日,这里把它挡回去
186
- const back = new Date(ms);
187
- if (back.getUTCMonth() !== month - 1 || back.getUTCDate() !== day) return null;
188
- return ms;
189
- }
@@ -1,193 +0,0 @@
1
- // calc 语法分析(派生列)——递归下降,不是 eval。
2
- //
3
- // 受限表达式串(`"[revenue] - [cost]"`、`IF([margin] >= 0.25, "Healthy", "Review")`)足以覆盖
4
- // 绝大多数派生列需求,且天然只读、可序列化。DSL 刻意不图灵完备(`verify:no-eval` 是硬闸门),
5
- // 递归下降解析器让能力够用,产物是可静态检查的 AST。
6
- //
7
- // 两条刻意的语法限制:
8
- // 1. 比较不可链:`a < b < c` 是语法错,不是 `(a<b)<c`。
9
- // 2. 比较只能出现在 IF 的第一参(由 typer 判定,不在文法里限制):求值为 bool 的派生列没有意义。
10
- //
11
- // 文法(左结合):
12
- // calc := cmp
13
- // cmp := sum (('>'|'>='|'<'|'<='|'=='|'!=') sum)?
14
- // sum := product (('+'|'-') product)*
15
- // product := unary (('*'|'/') unary)*
16
- // unary := '-' unary | primary
17
- // primary := NUMBER | STRING | DATE | BOOL | REF | call | '(' calc ')'
18
- // call := IF '(' calc ',' calc ',' calc ')'
19
- // | MIN '(' calc (',' calc)+ ')' | MAX '(' calc (',' calc)+ ')'
20
- // | ROUND '(' calc (',' NUMBER)? ')'
21
- // | CONCAT '(' calc (',' calc)+ ')'
22
- // | ABS '(' calc ')'
23
-
24
- import { SomaCalcSyntaxError, type CalcSpan } from './errors';
25
- import { tokenize, type CalcFunctionName, type CalcToken } from './lexer';
26
-
27
- export type CalcBinaryOp = '+' | '-' | '*' | '/' | '>' | '>=' | '<' | '<=' | '==' | '!=';
28
-
29
- export type CalcAst =
30
- | { k: 'num'; v: number; pos: CalcSpan }
31
- | { k: 'str'; v: string; pos: CalcSpan }
32
- | { k: 'date'; v: number; pos: CalcSpan }
33
- | { k: 'bool'; v: boolean; pos: CalcSpan }
34
- | { k: 'ref'; name: string; pos: CalcSpan }
35
- | { k: 'un'; op: '-'; arg: CalcAst; pos: CalcSpan }
36
- | { k: 'bin'; op: CalcBinaryOp; left: CalcAst; right: CalcAst; pos: CalcSpan }
37
- | { k: 'call'; fn: CalcFunctionName; args: CalcAst[]; pos: CalcSpan };
38
-
39
- const CMP_OPS: readonly string[] = ['>', '>=', '<', '<=', '==', '!='];
40
-
41
- /** 每个函数的参数个数约束:[最少, 最多],Infinity = 不限 */
42
- const ARITY: Readonly<Record<CalcFunctionName, readonly [number, number]>> = {
43
- IF: [3, 3],
44
- MIN: [2, Infinity],
45
- MAX: [2, Infinity],
46
- ROUND: [1, 2],
47
- CONCAT: [2, Infinity],
48
- ABS: [1, 1],
49
- };
50
-
51
- export function parseCalc(source: string): CalcAst {
52
- const tokens = tokenize(source);
53
- let at = 0;
54
-
55
- const peek = (): CalcToken => tokens[at];
56
- const next = (): CalcToken => tokens[at++];
57
- const fail = (message: string, token: CalcToken): never => {
58
- throw new SomaCalcSyntaxError(message, source, token.pos);
59
- };
60
- const expect = (kind: CalcToken['kind'], what: string): CalcToken => {
61
- const token = peek();
62
- if (token.kind !== kind) fail(`缺少${what}`, token);
63
- return next();
64
- };
65
-
66
- const parseCmp = (): CalcAst => {
67
- const left = parseSum();
68
- const token = peek();
69
- if (token.kind === 'op' && CMP_OPS.includes(String(token.value))) {
70
- next();
71
- const right = parseSum();
72
- const after = peek();
73
- if (after.kind === 'op' && CMP_OPS.includes(String(after.value))) {
74
- fail('比较不能连写(a < b < c 几乎总是写错了意思)——请拆成两个 IF 或改用 between 谓词', after);
75
- }
76
- return { k: 'bin', op: String(token.value) as CalcBinaryOp, left, right, pos: [left.pos[0], right.pos[1]] };
77
- }
78
- return left;
79
- };
80
-
81
- const parseSum = (): CalcAst => {
82
- let node = parseProduct();
83
- for (;;) {
84
- const token = peek();
85
- if (token.kind !== 'op' || (token.value !== '+' && token.value !== '-')) return node;
86
- next();
87
- const right = parseProduct();
88
- node = { k: 'bin', op: token.value, left: node, right, pos: [node.pos[0], right.pos[1]] };
89
- }
90
- };
91
-
92
- const parseProduct = (): CalcAst => {
93
- let node = parseUnary();
94
- for (;;) {
95
- const token = peek();
96
- if (token.kind !== 'op' || (token.value !== '*' && token.value !== '/')) return node;
97
- next();
98
- const right = parseUnary();
99
- node = { k: 'bin', op: token.value, left: node, right, pos: [node.pos[0], right.pos[1]] };
100
- }
101
- };
102
-
103
- const parseUnary = (): CalcAst => {
104
- const token = peek();
105
- if (token.kind === 'op' && token.value === '-') {
106
- next();
107
- const arg = parseUnary();
108
- return { k: 'un', op: '-', arg, pos: [token.pos[0], arg.pos[1]] };
109
- }
110
- return parsePrimary();
111
- };
112
-
113
- const parsePrimary = (): CalcAst => {
114
- const token = next();
115
- switch (token.kind) {
116
- case 'number': return { k: 'num', v: token.value as number, pos: token.pos };
117
- case 'string': return { k: 'str', v: token.value as string, pos: token.pos };
118
- case 'date': return { k: 'date', v: token.value as number, pos: token.pos };
119
- case 'bool': return { k: 'bool', v: token.value as boolean, pos: token.pos };
120
- case 'ref': return { k: 'ref', name: token.value as string, pos: token.pos };
121
- case 'lparen': {
122
- const inner = parseCmp();
123
- expect('rparen', '右括号 ")"');
124
- return inner;
125
- }
126
- case 'ident': return parseCall(token);
127
- case 'op':
128
- return fail(`运算符 "${String(token.value)}" 前面缺少操作数`, token);
129
- case 'eof':
130
- return fail('表达式不完整', token);
131
- default:
132
- return fail(`意外的 "${String(token.value)}"`, token);
133
- }
134
- };
135
-
136
- const parseCall = (name: CalcToken): CalcAst => {
137
- const fn = name.value as CalcFunctionName;
138
- expect('lparen', `${fn} 后的左括号 "("`);
139
- const args: CalcAst[] = [];
140
-
141
- // ROUND 的第二参必须是字面量整数(精度不能是算出来的——那会让类型推导无从谈起)
142
- if (peek().kind !== 'rparen') {
143
- args.push(parseCmp());
144
- while (peek().kind === 'comma') {
145
- next();
146
- if (fn === 'ROUND' && args.length === 1) {
147
- const digits = peek();
148
- if (digits.kind !== 'number' || !Number.isInteger(digits.value) || (digits.value as number) < 0 || (digits.value as number) > 6) {
149
- fail('ROUND 的第二参必须是 0~6 的整数字面量', digits);
150
- }
151
- next();
152
- args.push({ k: 'num', v: digits.value as number, pos: digits.pos });
153
- continue;
154
- }
155
- args.push(parseCmp());
156
- }
157
- }
158
- const close = expect('rparen', `${fn} 的右括号 ")"`);
159
-
160
- const [min, max] = ARITY[fn];
161
- if (args.length < min || args.length > max) {
162
- const want = max === Infinity ? `至少 ${min} 个` : (min === max ? `${min} 个` : `${min}~${max} 个`);
163
- fail(`${fn} 需要${want}参数,收到 ${args.length} 个`, name);
164
- }
165
- return { k: 'call', fn, args, pos: [name.pos[0], close.pos[1]] };
166
- };
167
-
168
- const ast = parseCmp();
169
- const rest = peek();
170
- if (rest.kind !== 'eof') {
171
- fail(`表达式在这里就结束了,后面的 "${String(rest.value)}" 是多余的`, rest);
172
- }
173
- return ast;
174
- }
175
-
176
- /** 收集表达式引用的字段名(去重、首次出现序):派生列互相引用时按它排求值次序 */
177
- export function collectCalcRefs(ast: CalcAst): string[] {
178
- const out: string[] = [];
179
- const seen = new Set<string>();
180
- const walk = (node: CalcAst): void => {
181
- switch (node.k) {
182
- case 'ref':
183
- if (!seen.has(node.name)) { seen.add(node.name); out.push(node.name); }
184
- return;
185
- case 'un': walk(node.arg); return;
186
- case 'bin': walk(node.left); walk(node.right); return;
187
- case 'call': node.args.forEach(walk); return;
188
- default: return;
189
- }
190
- };
191
- walk(ast);
192
- return out;
193
- }
@@ -1,171 +0,0 @@
1
- // calc 类型推导(派生列)。
2
- //
3
- // 字段类型是字典里的封闭枚举,所以「表达式算出来是什么类型」可以在解析期做完:
4
- // 每个节点推出类型,与字段声明的 `type` 对账,不配就当场点名到列号,
5
- // 而不是等页面上显示 `NaN` 时离写错的地方已经隔了很远。
6
- //
7
- // 一条刻意的收紧:`date - date → number`(毫秒差)有用,但 `date + date`、`date * 2` 无意义,
8
- // 所以日期的算术只开了「差」与「加减一个数」两扇门。
9
-
10
- import { SomaCalcTypeError } from './errors';
11
- import type { CalcAst } from './parser';
12
-
13
- export type CalcType = 'number' | 'text' | 'bool' | 'date';
14
-
15
- /**
16
- * 字段类型 → calc 类型。
17
- * 表里没有的字段类型不可被引用:`richtext` 是一段 HTML,`image / file` 是资源标识,
18
- * `tags / tree / cascade` 是集合或路径,`embed` 是页面路径。与其让它们悄悄按 text 参与拼接,不如直接拒绝。
19
- */
20
- const FIELD_TO_CALC: Readonly<Record<string, CalcType>> = {
21
- int: 'number', float: 'number', money: 'number', percent: 'number', progress: 'number',
22
- text: 'text', url: 'text', enum: 'text',
23
- bool: 'bool',
24
- date: 'date', datetime: 'date',
25
- };
26
-
27
- /** calc 类型 → 允许声明的字段类型(用于结果与 `type` 对账) */
28
- const CALC_TO_FIELD: Readonly<Record<CalcType, readonly string[]>> = {
29
- number: ['int', 'float', 'money', 'percent', 'progress'],
30
- text: ['text', 'url', 'enum'],
31
- bool: ['bool'],
32
- date: ['date', 'datetime'],
33
- };
34
-
35
- export interface CalcTypeContext {
36
- /** 字段名 → 字典里的 `type`(缺省视为 text,与字典本身的缺省一致) */
37
- fieldTypes: Readonly<Record<string, string | undefined>>;
38
- /** 原始表达式串,仅用于错误定位 */
39
- source: string;
40
- }
41
-
42
- export function inferCalcType(ast: CalcAst, ctx: CalcTypeContext): CalcType {
43
- return infer(ast, ctx);
44
- }
45
-
46
- function infer(node: CalcAst, ctx: CalcTypeContext): CalcType {
47
- const bad: (message: string, expected: string, actual: string) => never = (message, expected, actual) => {
48
- throw new SomaCalcTypeError(message, ctx.source, node.pos, expected, actual);
49
- };
50
-
51
- switch (node.k) {
52
- case 'num': return 'number';
53
- case 'str': return 'text';
54
- case 'bool': return 'bool';
55
- case 'date': return 'date';
56
-
57
- case 'ref': {
58
- const declared = ctx.fieldTypes[node.name];
59
- if (declared === undefined && !(node.name in ctx.fieldTypes)) {
60
- bad(`引用了不存在的字段 "${node.name}"`, '字典里的字段', '未声明');
61
- }
62
- const kind = FIELD_TO_CALC[declared ?? 'text'];
63
- if (!kind) {
64
- bad(
65
- `字段 "${node.name}" 是 ${declared} 类型,不能参与计算`
66
- + `(可引用的类型:${Object.keys(FIELD_TO_CALC).join(' / ')})`,
67
- '可计算的字段类型', String(declared),
68
- );
69
- }
70
- return kind;
71
- }
72
-
73
- case 'un': {
74
- const inner = infer(node.arg, ctx);
75
- if (inner !== 'number') bad('取负只能作用在数值上', 'number', inner);
76
- return 'number';
77
- }
78
-
79
- case 'bin': {
80
- const left = infer(node.left, ctx);
81
- const right = infer(node.right, ctx);
82
- switch (node.op) {
83
- case '+': case '-': {
84
- if (left === 'number' && right === 'number') return 'number';
85
- if (node.op === '-' && left === 'date' && right === 'date') return 'number'; // 毫秒差
86
- if (left === 'date' && right === 'number') return 'date'; // 日期 ± 天数(毫秒)
87
- if (node.op === '+' && left === 'number' && right === 'date') return 'date';
88
- // 只有 + 才可能是「想拼接」写错了;对减法说「请用 CONCAT」是句错话
89
- if (node.op === '+' && (left === 'text' || right === 'text')) {
90
- bad('文本不能用 + 拼接——请用 CONCAT(a, b)', 'number 或 date', `${left} 与 ${right}`);
91
- }
92
- return bad(`${node.op} 不能作用在 ${left} 与 ${right} 上`, 'number', `${left} 与 ${right}`);
93
- }
94
- case '*': case '/': {
95
- if (left === 'number' && right === 'number') return 'number';
96
- return bad(`${node.op} 只能作用在数值上`, 'number', `${left} 与 ${right}`);
97
- }
98
- case '>': case '>=': case '<': case '<=': {
99
- if (left === right && (left === 'number' || left === 'date')) return 'bool';
100
- return bad('比较大小只能在两个数值之间或两个日期之间', '同为 number 或同为 date', `${left} 与 ${right}`);
101
- }
102
- case '==': case '!=': {
103
- if (left === right) return 'bool';
104
- return bad('相等比较的两侧必须同型', left, right);
105
- }
106
- }
107
- }
108
-
109
- case 'call': {
110
- switch (node.fn) {
111
- case 'IF': {
112
- const cond = infer(node.args[0], ctx);
113
- if (cond !== 'bool') bad('IF 的第一个参数必须是判断(比较或布尔字段)', 'bool', cond);
114
- const a = infer(node.args[1], ctx);
115
- const b = infer(node.args[2], ctx);
116
- if (a !== b) bad('IF 的两个分支必须同型', a, b);
117
- return a;
118
- }
119
- case 'MIN': case 'MAX': {
120
- const kinds = node.args.map((arg) => infer(arg, ctx));
121
- const first = kinds[0];
122
- if (first !== 'number' && first !== 'date') {
123
- bad(`${node.fn} 只能作用在数值或日期上`, 'number 或 date', first);
124
- }
125
- const off = kinds.find((kind) => kind !== first);
126
- if (off) bad(`${node.fn} 的参数必须同型`, first, off);
127
- return first;
128
- }
129
- case 'ROUND': {
130
- const value = infer(node.args[0], ctx);
131
- if (value !== 'number') bad('ROUND 只能作用在数值上', 'number', value);
132
- return 'number';
133
- }
134
- case 'ABS': {
135
- const value = infer(node.args[0], ctx);
136
- if (value !== 'number') bad('ABS 只能作用在数值上', 'number', value);
137
- return 'number';
138
- }
139
- case 'CONCAT': {
140
- node.args.forEach((arg) => infer(arg, ctx)); // 任意型,逐个求值即可(错在里面照样点名)
141
- return 'text';
142
- }
143
- }
144
- }
145
- }
146
- }
147
-
148
- /** 与字段声明的 `type` 对账(单独一个函数,便于「只想知道类型」的场合不带目标字段) */
149
- export function assertCalcResultType(
150
- result: CalcType,
151
- declaredFieldType: string | undefined,
152
- ctx: CalcTypeContext,
153
- ast: CalcAst,
154
- ): void {
155
- const declared = declaredFieldType ?? 'text';
156
- const allowed = CALC_TO_FIELD[result];
157
- if (!allowed.includes(declared)) {
158
- throw new SomaCalcTypeError(
159
- `表达式算出来是 ${result},但字段声明的是 ${declared}`
160
- + `(${result} 可以声明为:${allowed.join(' / ')})`,
161
- ctx.source, ast.pos, allowed.join('|'), declared,
162
- );
163
- }
164
- if (result === 'bool') {
165
- throw new SomaCalcTypeError(
166
- '派生列不能是判断结果——字段类型里没有「显示 true」这种业务语义;'
167
- + '想按条件出不同文案请用 IF(条件, "A", "B")',
168
- ctx.source, ast.pos, 'number|text|date', 'bool',
169
- );
170
- }
171
- }
@@ -1,2 +0,0 @@
1
- // 日期部件闭集已下沉通用值区 `soma-foundation/values`;这里只再导出。
2
- export { SOMA_DATE_PARTS, isDatePart, type SomaDatePart } from 'soma-foundation/values';
@@ -1,77 +0,0 @@
1
- // Show 的 `when` 对象谓词:`{ field, op, value? }`(字符串形 `path` / `!path` 另行处理,保持原样)。
2
- //
3
- // 求值复用 `soma-core/predicates`(同一份算子实现);只有 `present` / `blank` 两个有无值算子在这里自己判,
4
- // 空值判据与动作 `available_when.requires` 对齐:`0` 与 `false` 算有值;null / undefined / 空串(去空白后)/ 空数组算空。
5
- // (谓词里的 `empty` / `not_empty` 把 `false` 当空,口径不同,所以不借它们判有无值。)
6
- // 对象形里 `field` 只许是纯路径,不许管道、算术与前导 `!`;纯函数、无 DOM。
7
-
8
- import { SOMA_STD_DOMAIN_PREDICATE_OPS, SOMA_STD_DOMAIN_SHOW_WHEN_PRESENCE_OPS } from './registry-generated/std-grammar-views';
9
- import { evaluatePredicate, validatePredicate, type SomaFieldPredicate } from './predicates';
10
-
11
- /** Show `when` 对象形的 `op` 闭集:谓词算子 + 有无值算子 */
12
- export const SOMA_SHOW_WHEN_OPS: readonly string[] = [
13
- ...SOMA_STD_DOMAIN_PREDICATE_OPS,
14
- ...SOMA_STD_DOMAIN_SHOW_WHEN_PRESENCE_OPS,
15
- ];
16
- /** 不带 `value` 的算子 */
17
- const VALUELESS_OPS: readonly string[] = [...SOMA_STD_DOMAIN_SHOW_WHEN_PRESENCE_OPS, 'empty', 'not_empty'];
18
- const SHOW_WHEN_KEYS: readonly string[] = ['field', 'op', 'value'];
19
- const FIELD_PATH = /^[A-Za-z_][A-Za-z0-9_-]*(\.[A-Za-z0-9_-]+)*$/;
20
-
21
- export interface SomaShowWhenObject {
22
- field: string;
23
- op: string;
24
- value?: unknown;
25
- }
26
-
27
- /** `when` 的两种写法:字符串(真值 / `!` 取反)或对象谓词 */
28
- export type SomaShowWhen = string | SomaShowWhenObject;
29
-
30
- /** 编译后挂在绑定上的谓词部分(路径另存在 dataPath) */
31
- export interface SomaShowWhenTest {
32
- op: string;
33
- value?: unknown;
34
- }
35
-
36
- export function isShowWhenObject(raw: unknown): raw is SomaShowWhenObject {
37
- return typeof raw === 'object' && raw !== null && !Array.isArray(raw);
38
- }
39
-
40
- /** 有值:非 null / undefined / 空白串 / 空数组;`0` 与 `false` 算有值 */
41
- export function isPresentValue(value: unknown): boolean {
42
- if (value === null || value === undefined) return false;
43
- if (typeof value === 'string') return value.trim() !== '';
44
- if (Array.isArray(value)) return value.length > 0;
45
- return true;
46
- }
47
-
48
- /** 校验对象形 `when`:封闭键、`field` 纯路径、`op` 在闭集内、`value` 与算子匹配;返回问题清单(空 = 通过) */
49
- export function inspectShowWhen(raw: unknown, path = 'when'): string[] {
50
- if (!isShowWhenObject(raw)) return [`${path} 必须是字符串或 { field, op, value? } 对象`];
51
- const issues: string[] = [];
52
- for (const key of Object.keys(raw)) {
53
- if (!SHOW_WHEN_KEYS.includes(key)) issues.push(`${path} 有未知的键 "${key}"(支持: ${SHOW_WHEN_KEYS.join(' / ')})`);
54
- }
55
- if (typeof raw.field !== 'string' || !FIELD_PATH.test(raw.field)) {
56
- issues.push(`${path}.field 必须是纯路径(a.b.c),不许管道、算术与前导 "!",当前值: ${JSON.stringify(raw.field)}`);
57
- }
58
- if (typeof raw.op !== 'string' || !SOMA_SHOW_WHEN_OPS.includes(raw.op)) {
59
- issues.push(`${path}.op 必须是 ${SOMA_SHOW_WHEN_OPS.join(' / ')} 之一,当前值: ${JSON.stringify(raw.op)}`);
60
- return issues;
61
- }
62
- if (VALUELESS_OPS.includes(raw.op)) {
63
- if (raw.value !== undefined) issues.push(`${path}.value 在 op=${raw.op} 时不能写`);
64
- return issues;
65
- }
66
- if (typeof raw.field === 'string') {
67
- issues.push(...validatePredicate({ field: 'v', op: raw.op, value: raw.value } as SomaFieldPredicate, ['v'], path));
68
- }
69
- return issues;
70
- }
71
-
72
- /** 对一个已读到的值求对象谓词 */
73
- export function evaluateShowWhen(test: SomaShowWhenTest, actual: unknown): boolean {
74
- if (test.op === 'present') return isPresentValue(actual);
75
- if (test.op === 'blank') return !isPresentValue(actual);
76
- return evaluatePredicate({ field: 'v', op: test.op, value: test.value } as SomaFieldPredicate, { v: actual });
77
- }