soma-core 1.3.2 → 1.3.4

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 (281) hide show
  1. package/README.md +43 -0
  2. package/app/SomaPageClient.tsx +22 -68
  3. package/app/[...path]/page.tsx +7 -6
  4. package/app/api/[...path]/route.ts +3 -3
  5. package/app/api/soma-registry/[...path]/route.ts +67 -0
  6. package/app/components/SomaHostPage.tsx +3 -8
  7. package/app/components/SomaPageClientBoundary.tsx +13 -0
  8. package/app/layout.tsx +6 -1
  9. package/app/page.tsx +3 -3
  10. package/app/soma/manifest/route.ts +1 -0
  11. package/app/soma/page-object/route.ts +1 -1
  12. package/bin/soma-lsp.cjs +1 -4
  13. package/core/build/compiler/full-manifest-builder.ts +19 -23
  14. package/core/build/compiler/html-include-expander.ts +56 -0
  15. package/core/build/compiler/page-assets.ts +26 -0
  16. package/core/build/compiler/page-declaration-compiler.ts +85 -0
  17. package/core/build/compiler/rollup-plugin-soma.ts +4 -13
  18. package/core/build/compiler/route-builder.ts +0 -2
  19. package/core/build/compiler/rspack-plugin-soma.js +1 -10
  20. package/core/build/compiler/soma-manifest-webpack-plugin.js +54 -9
  21. package/core/build/compiler/soma-precompile.js +3581 -2387
  22. package/core/build/compiler/soma-precompile.ts +44 -2
  23. package/core/build/compiler/soma-rspack-yaml-loader.js +5 -8
  24. package/core/build/compiler/soma-webpack-loader.js +6 -15
  25. package/core/build/compiler/vite-plugin-soma.ts +9 -21
  26. package/core/build/component/pack-from-dir.ts +58 -0
  27. package/core/build/component/package-loader.ts +281 -0
  28. package/core/build/eject/apply-eject.ts +133 -0
  29. package/core/build/eject/plan-eject.ts +133 -0
  30. package/core/build/lsp/cross-file-diagnostics.cjs +82 -113
  31. package/core/build/lsp/extra-completion.cjs +14 -8
  32. package/core/build/lsp/lsp-navigation.cjs +50 -43
  33. package/core/build/lsp/soma-declaration.cjs +85 -0
  34. package/core/build/lsp/ui-yaml-completion.cjs +98 -104
  35. package/core/build/v2/block-compile.ts +200 -0
  36. package/core/build/v2/block-loader.ts +33 -0
  37. package/core/build/v2/html-index.ts +120 -0
  38. package/core/build/v2/migrate-v1.ts +370 -0
  39. package/core/build/v2/soma-file-loader.ts +216 -0
  40. package/core/client/adaptive/flush-scheduler.ts +96 -0
  41. package/core/client/adaptive/hysteresis.ts +56 -0
  42. package/core/client/adaptive/index.ts +23 -0
  43. package/core/client/adaptive/observer-pool.ts +90 -0
  44. package/core/client/base/SomaPageRootBase.ts +50 -0
  45. package/core/client/compiler/adaptive-compile.ts +136 -0
  46. package/core/client/compiler/manifest-compiler.ts +155 -27
  47. package/core/client/component/ai-catalog.ts +277 -0
  48. package/core/client/component/component-definition.types.ts +65 -0
  49. package/core/client/component/component-expander.ts +119 -0
  50. package/core/client/component/component-manager.ts +933 -0
  51. package/core/client/component/component-pack.ts +445 -0
  52. package/core/client/component/component-registry.ts +71 -0
  53. package/core/client/component/contract-compat.ts +132 -0
  54. package/core/client/component/example-v2.ts +132 -0
  55. package/core/client/component/pack-styles.ts +94 -0
  56. package/core/client/component/scoped-adapter.ts +85 -0
  57. package/core/client/component/verify-pack.ts +247 -0
  58. package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
  59. package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
  60. package/core/client/container/SomaContainerAdapter.ts +186 -180
  61. package/core/client/data/data-object-resolver.ts +417 -0
  62. package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
  63. package/core/client/diagnostics/boot-timings.ts +152 -0
  64. package/core/client/engine/asset-resolver.ts +12 -2
  65. package/core/client/engine/dom-renderer.ts +249 -24
  66. package/core/client/engine/emit-event.ts +21 -0
  67. package/core/client/engine/event-engine.ts +34 -4
  68. package/core/client/engine/on-change-engine.ts +1 -1
  69. package/core/client/engine/property-watcher.ts +1 -1
  70. package/core/client/engine/reactive-proxy.ts +11 -10
  71. package/core/client/engine/render-engine.ts +39 -4
  72. package/core/client/engine/runtime-builder.ts +23 -0
  73. package/core/client/engine/soma-bootstrap.ts +380 -7
  74. package/core/client/engine/style-binder.ts +47 -1
  75. package/core/client/observability/error-reporter.ts +173 -0
  76. package/core/client/parser/adaptive-parser.ts +264 -0
  77. package/core/client/parser/data-config-parser.ts +396 -0
  78. package/core/client/parser/pipeline-parser.ts +15 -3
  79. package/core/client/parser/style-config-parser.ts +65 -2
  80. package/core/client/parser/ui-config-parser.ts +52 -3
  81. package/core/client/parser/v2/namespace.ts +182 -0
  82. package/core/client/parser/v2/page-parser.ts +359 -0
  83. package/core/client/parser/v2/use-parser.ts +412 -0
  84. package/core/client/parser/value-lang.ts +340 -0
  85. package/core/client/pipes/pipe-registry.ts +249 -0
  86. package/core/client/quality/SomaQualityRegistry.ts +3 -0
  87. package/core/client/registry/declaration-renderer.ts +161 -0
  88. package/core/client/registry/index.ts +28 -0
  89. package/core/client/registry/page-lifecycle.ts +171 -0
  90. package/core/client/registry/remote-page.ts +139 -0
  91. package/core/client/runtime/SomaRuntimeServices.ts +24 -0
  92. package/core/client/security/attribute-presence.ts +70 -0
  93. package/core/client/security/html-sanitizer.ts +113 -0
  94. package/core/client/security/url-guard.ts +0 -0
  95. package/core/client/theme/harmony.ts +307 -0
  96. package/core/client/theme/scene-tokens.ts +134 -0
  97. package/core/client/theme/theme-axes.ts +228 -0
  98. package/core/client/theme/theme-engine.ts +1196 -0
  99. package/core/client/theme/tone-solver.ts +118 -0
  100. package/core/client/typography/typography-base.ts +34 -0
  101. package/core/client/validation/contract-validator.ts +48 -5
  102. package/core/index.ts +262 -2
  103. package/core/server/api/IService.ts +18 -6
  104. package/core/server/api/load-host-service.ts +3 -3
  105. package/core/server/host/content-security-policy.ts +190 -0
  106. package/core/server/host/csp-nonce-metadata.ts +75 -0
  107. package/core/server/host/resolve-host-request-hook.ts +5 -0
  108. package/core/server/host/server-page-assets.ts +34 -7
  109. package/core/server/host/soma-page-compile.ts +33 -0
  110. package/core/server/index.ts +45 -1
  111. package/core/server/infra/cache/ServerFileCache.ts +1 -1
  112. package/core/server/mock/node/cli.mjs +2 -2
  113. package/core/server/registry/api-registry-store.ts +518 -0
  114. package/core/server/registry/conditional-object-store.ts +29 -0
  115. package/core/server/registry/declaration-precompiler.ts +99 -0
  116. package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
  117. package/core/server/registry/index.ts +142 -0
  118. package/core/server/registry/page-registry-repository.ts +373 -0
  119. package/core/server/registry/page-registry-store.ts +932 -0
  120. package/core/server/registry/registry-error.ts +17 -0
  121. package/core/server/registry/registry-file-lock.ts +173 -0
  122. package/core/server/registry/registry-response.ts +71 -0
  123. package/core/server/registry/registry-subjects.ts +194 -0
  124. package/core/server/registry/registry-write-pipeline.ts +240 -0
  125. package/core/server/registry/request-body-limit.ts +99 -0
  126. package/core/server/registry/services/apis/Get.ts +25 -0
  127. package/core/server/registry/services/apis/List.ts +41 -0
  128. package/core/server/registry/services/apis/Register.ts +39 -0
  129. package/core/server/registry/services/apis/Unregister.ts +24 -0
  130. package/core/server/registry/services/index.ts +40 -0
  131. package/core/server/registry/services/pages/Get.ts +96 -0
  132. package/core/server/registry/services/pages/List.ts +53 -0
  133. package/core/server/registry/services/pages/Register.ts +49 -0
  134. package/core/server/registry/services/pages/Rollback.ts +36 -0
  135. package/core/server/registry/services/pages/Unregister.ts +23 -0
  136. package/core/server/registry/services/service-utils.ts +21 -0
  137. package/core/server/registry/validation/compile-gate.ts +54 -0
  138. package/core/server/registry/validation/component-existence.ts +55 -0
  139. package/core/server/registry/validation/credential-ban.ts +86 -0
  140. package/core/server/registry/validation/cross-check.ts +67 -0
  141. package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
  142. package/core/shared/types/adaptive.types.ts +109 -0
  143. package/core/shared/types/component-contract.types.ts +120 -0
  144. package/core/shared/types/data-config.types.ts +105 -0
  145. package/core/shared/types/domain.types.ts +6 -0
  146. package/core/shared/types/engine.types.ts +41 -35
  147. package/core/shared/types/manifest.types.ts +55 -32
  148. package/core/shared/types/scope-manifest.types.ts +96 -0
  149. package/core/shared/types/style-config.types.ts +9 -0
  150. package/core/shared/types/ui-config.types.ts +118 -98
  151. package/dist/cli/soma.js +20 -5
  152. package/dist/core/index.cjs +7811 -1380
  153. package/dist/types/build/compiler/full-manifest-builder.d.ts +6 -2
  154. package/dist/types/build/compiler/html-include-expander.d.ts +19 -0
  155. package/dist/types/build/compiler/page-assets.d.ts +3 -0
  156. package/dist/types/build/compiler/page-declaration-compiler.d.ts +33 -0
  157. package/dist/types/build/compiler/rollup-plugin-soma.d.ts +0 -2
  158. package/dist/types/build/compiler/route-builder.d.ts +0 -1
  159. package/dist/types/build/compiler/soma-precompile.d.ts +17 -0
  160. package/dist/types/build/compiler/vite-plugin-soma.d.ts +2 -7
  161. package/dist/types/build/component/pack-from-dir.d.ts +17 -0
  162. package/dist/types/build/component/package-loader.d.ts +40 -0
  163. package/dist/types/build/eject/apply-eject.d.ts +22 -0
  164. package/dist/types/build/eject/plan-eject.d.ts +32 -0
  165. package/dist/types/build/v2/block-compile.d.ts +28 -0
  166. package/dist/types/build/v2/block-loader.d.ts +9 -0
  167. package/dist/types/build/v2/html-index.d.ts +37 -0
  168. package/dist/types/build/v2/migrate-v1.d.ts +32 -0
  169. package/dist/types/build/v2/soma-file-loader.d.ts +45 -0
  170. package/dist/types/client/adaptive/flush-scheduler.d.ts +13 -0
  171. package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
  172. package/dist/types/client/adaptive/index.d.ts +5 -0
  173. package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
  174. package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
  175. package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
  176. package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
  177. package/dist/types/client/component/ai-catalog.d.ts +73 -0
  178. package/dist/types/client/component/component-definition.types.d.ts +54 -0
  179. package/dist/types/client/component/component-expander.d.ts +25 -0
  180. package/dist/types/client/component/component-manager.d.ts +160 -0
  181. package/dist/types/client/component/component-pack.d.ts +110 -0
  182. package/dist/types/client/component/component-registry.d.ts +12 -0
  183. package/dist/types/client/component/contract-compat.d.ts +16 -0
  184. package/dist/types/client/component/example-v2.d.ts +9 -0
  185. package/dist/types/client/component/pack-styles.d.ts +22 -0
  186. package/dist/types/client/component/scoped-adapter.d.ts +18 -0
  187. package/dist/types/client/component/verify-pack.d.ts +14 -0
  188. package/dist/types/client/data/data-object-resolver.d.ts +71 -0
  189. package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
  190. package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
  191. package/dist/types/client/engine/asset-resolver.d.ts +7 -1
  192. package/dist/types/client/engine/dom-renderer.d.ts +51 -1
  193. package/dist/types/client/engine/emit-event.d.ts +6 -0
  194. package/dist/types/client/engine/event-engine.d.ts +6 -0
  195. package/dist/types/client/engine/render-engine.d.ts +25 -1
  196. package/dist/types/client/engine/runtime-builder.d.ts +7 -0
  197. package/dist/types/client/engine/soma-bootstrap.d.ts +118 -0
  198. package/dist/types/client/observability/error-reporter.d.ts +51 -0
  199. package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
  200. package/dist/types/client/parser/data-config-parser.d.ts +34 -0
  201. package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
  202. package/dist/types/client/parser/style-config-parser.d.ts +5 -3
  203. package/dist/types/client/parser/ui-config-parser.d.ts +22 -1
  204. package/dist/types/client/parser/v2/namespace.d.ts +52 -0
  205. package/dist/types/client/parser/v2/page-parser.d.ts +21 -0
  206. package/dist/types/client/parser/v2/use-parser.d.ts +72 -0
  207. package/dist/types/client/parser/value-lang.d.ts +63 -0
  208. package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
  209. package/dist/types/client/registry/declaration-renderer.d.ts +50 -0
  210. package/dist/types/client/registry/index.d.ts +10 -0
  211. package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
  212. package/dist/types/client/registry/remote-page.d.ts +52 -0
  213. package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
  214. package/dist/types/client/security/attribute-presence.d.ts +7 -0
  215. package/dist/types/client/security/html-sanitizer.d.ts +37 -0
  216. package/dist/types/client/security/url-guard.d.ts +11 -0
  217. package/dist/types/client/theme/harmony.d.ts +77 -0
  218. package/dist/types/client/theme/scene-tokens.d.ts +25 -0
  219. package/dist/types/client/theme/theme-axes.d.ts +81 -0
  220. package/dist/types/client/theme/theme-engine.d.ts +77 -0
  221. package/dist/types/client/theme/tone-solver.d.ts +47 -0
  222. package/dist/types/client/typography/typography-base.d.ts +15 -0
  223. package/dist/types/client/validation/contract-validator.d.ts +2 -0
  224. package/dist/types/index.d.ts +47 -2
  225. package/dist/types/server/api/IService.d.ts +18 -6
  226. package/dist/types/server/host/content-security-policy.d.ts +53 -0
  227. package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
  228. package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
  229. package/dist/types/server/host/server-page-assets.d.ts +2 -0
  230. package/dist/types/server/host/soma-page-compile.d.ts +7 -0
  231. package/dist/types/server/index.d.ts +6 -1
  232. package/dist/types/server/registry/api-registry-store.d.ts +141 -0
  233. package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
  234. package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
  235. package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
  236. package/dist/types/server/registry/index.d.ts +29 -0
  237. package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
  238. package/dist/types/server/registry/page-registry-store.d.ts +259 -0
  239. package/dist/types/server/registry/registry-error.d.ts +12 -0
  240. package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
  241. package/dist/types/server/registry/registry-response.d.ts +18 -0
  242. package/dist/types/server/registry/registry-subjects.d.ts +59 -0
  243. package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
  244. package/dist/types/server/registry/request-body-limit.d.ts +31 -0
  245. package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
  246. package/dist/types/server/registry/services/apis/List.d.ts +10 -0
  247. package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
  248. package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
  249. package/dist/types/server/registry/services/index.d.ts +3 -0
  250. package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
  251. package/dist/types/server/registry/services/pages/List.d.ts +13 -0
  252. package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
  253. package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
  254. package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
  255. package/dist/types/server/registry/services/service-utils.d.ts +10 -0
  256. package/dist/types/server/registry/validation/compile-gate.d.ts +12 -0
  257. package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
  258. package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
  259. package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
  260. package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
  261. package/dist/types/shared/types/adaptive.types.d.ts +88 -0
  262. package/dist/types/shared/types/component-contract.types.d.ts +78 -0
  263. package/dist/types/shared/types/data-config.types.d.ts +81 -0
  264. package/dist/types/shared/types/domain.types.d.ts +6 -0
  265. package/dist/types/shared/types/engine.types.d.ts +6 -0
  266. package/dist/types/shared/types/manifest.types.d.ts +23 -0
  267. package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
  268. package/dist/types/shared/types/style-config.types.d.ts +8 -0
  269. package/dist/types/shared/types/ui-config.types.d.ts +20 -0
  270. package/editor/vscode/package.json +1 -24
  271. package/global.d.ts +5 -11
  272. package/next.config.js +85 -28
  273. package/package.json +33 -11
  274. package/proxy.ts +62 -0
  275. package/tailwind.config.ts +0 -1
  276. package/core/build/lsp/event-yaml-completion.cjs +0 -106
  277. package/core/client/engine/theme-variables.ts +0 -37
  278. package/dist/tsconfig.types.tsbuildinfo +0 -1
  279. package/dist/types/client/engine/theme-variables.d.ts +0 -3
  280. package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +0 -30
  281. package/middleware.ts +0 -33
@@ -0,0 +1,133 @@
1
+ // 固化(eject):把运行态的页面声明变回开发态的源码文件。
2
+ //
3
+ // 两态模型(REQ-002 §1)里,运行态页面按名寻址、不占 URL;要独立 URL 的**唯一**路径
4
+ // 就是固化成 html + ts 源码,进 git、走文件路由——从此它归开发态管辖,
5
+ // 受全仓闸门看守、随版本管理,与人手写的页面再无区别。
6
+ //
7
+ // 这个模块只做**规划**,不碰磁盘:输入声明与页面名,输出"该写哪些文件、内容是什么"。
8
+ // 写盘、跑校验、回滚、提交都是 CLI 的事。分开的理由很实际——固化最危险的部分
9
+ // 是"写了一半",而一个纯函数的规划器可以先把全部产物算出来、全部检查完,
10
+ // 再由调用方一次性落盘。
11
+ //
12
+ // 命名约定(与 Next Host 的文件寻址一致,见 server-page-assets):
13
+ // 页面名 order-board → html/order-board.html · ui/order-board_ui.yaml
14
+ // · domain/ui/order-boardClass.ts。Domain 类名 OrderBoardPage 必须与 HTML 和声明一致。
15
+
16
+ /** 页面名:小写字母/数字/连字符——它同时是文件名与 URL 片段 */
17
+ export const EJECT_NAME_PATTERN = /^[a-z0-9-]{1,64}$/;
18
+
19
+ export interface EjectDeclaration {
20
+ html: string;
21
+ uiYaml: string;
22
+ eventYaml?: string;
23
+ styleYaml?: string;
24
+ dataYaml?: string;
25
+ }
26
+
27
+ export interface EjectFile {
28
+ /** 相对宿主根目录的路径 */
29
+ path: string;
30
+ contents: string;
31
+ }
32
+
33
+ export interface EjectPlan {
34
+ /** 页面名(URL 片段与文件名) */
35
+ name: string;
36
+ /** Domain 类名,已写进 html 的 data-soma-host 与 ui 的 bind.obj */
37
+ domainClass: string;
38
+ /** 固化后可访问的 URL */
39
+ url: string;
40
+ files: EjectFile[];
41
+ }
42
+
43
+ export class EjectPlanError extends Error {}
44
+
45
+ /** order-board → OrderBoardPage */
46
+ export function domainClassNameOf(name: string): string {
47
+ const pascal = name
48
+ .split('-')
49
+ .filter(Boolean)
50
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
51
+ .join('');
52
+ return pascal.endsWith('Page') ? pascal : `${pascal}Page`;
53
+ }
54
+
55
+ /**
56
+ * 把声明里的宿主类名换成固化后的类名。
57
+ *
58
+ * 为什么必须换:运行态声明的 host 是通道给的占位(PreviewPageRoot / GeneratedPage),
59
+ * 固化后页面有了自己的 Domain 类,html 的 data-soma-host 与声明的 page.domain
60
+ * 必须与类名三处一致——差一处,页面加载时就是"Domain class missing"。
61
+ */
62
+ function rewriteHost(html: string, domainClass: string): { html: string; previous: string } {
63
+ const match = html.match(/data-soma-host="([^"]+)"/);
64
+ if (!match) {
65
+ throw new EjectPlanError('声明的 html 缺少 data-soma-host——它不是一份完整的页面声明');
66
+ }
67
+ return {
68
+ html: html.replace(/data-soma-host="[^"]+"/, `data-soma-host="${domainClass}"`),
69
+ previous: match[1],
70
+ };
71
+ }
72
+
73
+ /** 把统一声明的 page.domain 换成固化后的类名。 */
74
+ function rewriteDomain(uiYaml: string, previousHost: string, domainClass: string): string {
75
+ if (previousHost === domainClass) return uiYaml;
76
+ if (!/^\s*\{\{/m.test(uiYaml) || !/^\s*\}\}\s*$/m.test(uiYaml)) {
77
+ throw new EjectPlanError('uiYaml 不是 SOMA DSL 统一声明(必须以 {{ 开头、以 }} 收口)');
78
+ }
79
+ const escaped = previousHost.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
80
+ const domain = new RegExp(`(^\\s*domain:\\s*)${escaped}\\s*$`, 'm');
81
+ if (domain.test(uiYaml)) return uiYaml.replace(domain, `$1${domainClass}`);
82
+ throw new EjectPlanError(
83
+ `SOMA DSL 的 page.domain 不是 "${previousHost}"——HTML 与声明的宿主对不上,拒绝固化`,
84
+ );
85
+ }
86
+
87
+ /** 最小 Domain 类:固化后的页面从这里开始长业务代码 */
88
+ function domainSource(name: string, domainClass: string): string {
89
+ return [
90
+ `// ${name} —— 由 \`soma eject\` 从运行态声明固化而来。`,
91
+ '//',
92
+ '// 固化的意思是:这一页从此归开发态管辖——进 git、走文件路由、受全仓闸门看守,',
93
+ '// 与人手写的页面再无区别。声明里的数据先前来自 initProps 或数据对象层;',
94
+ '// 要接真业务逻辑,就在这个类里写(事件处理、取数编排、跳转)。',
95
+ '',
96
+ "import { SomaPageRootBase } from 'soma-core/base';",
97
+ '',
98
+ `export default class ${domainClass} extends SomaPageRootBase {`,
99
+ ' // SOMA DSL 的 event 段可绑定到这里的方法;先留空,按需要添加。',
100
+ '}',
101
+ '',
102
+ ].join('\n');
103
+ }
104
+
105
+ /**
106
+ * 规划一次固化。不碰磁盘——产出"该写哪些文件、内容是什么",
107
+ * 由调用方检查冲突、一次性落盘、失败时整体回滚。
108
+ */
109
+ export function planEject(name: string, declaration: EjectDeclaration): EjectPlan {
110
+ if (!EJECT_NAME_PATTERN.test(name)) {
111
+ throw new EjectPlanError('页面名必须匹配 [a-z0-9-]{1,64}(它同时是文件名与 URL 片段)');
112
+ }
113
+ if (!declaration?.html?.trim() || !declaration?.uiYaml?.trim()) {
114
+ throw new EjectPlanError('声明缺少 html 或 uiYaml,不是一份可固化的页面');
115
+ }
116
+ const domainClass = domainClassNameOf(name);
117
+ const { html, previous } = rewriteHost(declaration.html, domainClass);
118
+ if (declaration.eventYaml?.trim() || declaration.styleYaml?.trim() || declaration.dataYaml?.trim()) {
119
+ throw new EjectPlanError('页面侧车已退役;固化输入只能包含 HTML 与一份 SOMA DSL 声明');
120
+ }
121
+ const uiYaml = rewriteDomain(declaration.uiYaml, previous, domainClass);
122
+
123
+ const files: EjectFile[] = [
124
+ { path: `html/${name}.html`, contents: ensureTrailingNewline(html) },
125
+ { path: `ui/${name}_ui.yaml`, contents: ensureTrailingNewline(uiYaml) },
126
+ { path: `domain/ui/${name}Class.ts`, contents: domainSource(name, domainClass) },
127
+ ];
128
+ return { name, domainClass, url: `/${name}`, files };
129
+ }
130
+
131
+ function ensureTrailingNewline(text: string): string {
132
+ return text.endsWith('\n') ? text : `${text}\n`;
133
+ }
@@ -1,160 +1,129 @@
1
- /**
2
- * LSP 跨文件锚点一致性诊断
3
- * html data-soma-id ↔ _ui.yaml show ↔ _event.yaml 事件锚点
4
- */
1
+ /** LSP 跨文件诊断:HTML ↔ 唯一 SOMA DSL 声明。 */
5
2
  const fs = require('node:fs');
6
3
  const path = require('node:path');
7
- const yaml = require('js-yaml');
4
+ const { declarationFacts } = require('./soma-declaration.cjs');
8
5
 
9
6
  function extractSomaIdsFromHtml(html) {
10
7
  const ids = new Set();
11
- for (const m of html.matchAll(/data-soma-id=["']([^"']+)["']/g)) {
12
- ids.add(m[1]);
13
- }
14
- return ids;
15
- }
16
-
17
- function extractShowKeys(uiYaml) {
18
- const keys = new Set();
19
- try {
20
- const doc = yaml.load(uiYaml);
21
- if (doc?.show && typeof doc.show === 'object') {
22
- for (const k of Object.keys(doc.show)) keys.add(k);
23
- }
24
- } catch {
25
- /* invalid yaml */
26
- }
27
- return keys;
28
- }
29
-
30
- function extractEventIds(eventYaml) {
31
- const ids = new Set();
32
- for (const line of eventYaml.split('\n')) {
33
- const trimmed = line.trim();
34
- if (!trimmed || trimmed.startsWith('#')) continue;
35
- const dot = trimmed.indexOf('.');
36
- if (dot > 0) ids.add(trimmed.slice(0, dot));
37
- }
8
+ for (const match of html.matchAll(/data-soma-id=["']([^"']+)["']/g)) ids.add(match[1]);
38
9
  return ids;
39
10
  }
40
11
 
41
12
  function collectHtmlPages(htmlDir) {
42
13
  const pages = [];
43
14
  if (!fs.existsSync(htmlDir)) return pages;
44
- function walk(dir, prefix = '') {
15
+ const walk = (dir, prefix = '') => {
45
16
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
46
17
  const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
47
18
  const full = path.join(dir, entry.name);
48
19
  if (entry.isDirectory()) walk(full, rel);
49
20
  else if (entry.name.endsWith('.html')) pages.push(rel.replace(/\.html$/, ''));
50
21
  }
51
- }
22
+ };
52
23
  walk(htmlDir);
53
24
  return pages;
54
25
  }
55
26
 
56
- function findAnchorInHtml(html, somaId) {
57
- const re = new RegExp(`data-soma-id=["']${somaId.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')}["']`, 'm');
58
- const m = re.exec(html);
59
- if (!m) return null;
60
- const before = html.slice(0, m.index);
61
- const line = before.split('\n').length - 1;
62
- const character = m.index - before.lastIndexOf('\n') - 1;
63
- return { line, character, length: m[0].length };
27
+ function escapeRegex(value) {
28
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
64
29
  }
65
30
 
66
- function findKeyInYaml(yamlText, key) {
67
- const re = new RegExp(`^\\s{0,4}${key.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')}\\s*:`, 'm');
68
- const m = re.exec(yamlText);
69
- if (!m) return null;
70
- const before = yamlText.slice(0, m.index);
71
- const line = before.split('\n').length - 1;
72
- const character = m.index - before.lastIndexOf('\n') - 1;
73
- return { line, character, length: key.length };
31
+ function locate(text, expression, length = 1) {
32
+ const match = expression.exec(text);
33
+ if (!match) return null;
34
+ const before = text.slice(0, match.index);
35
+ return {
36
+ line: before.split('\n').length - 1,
37
+ character: match.index - before.lastIndexOf('\n') - 1,
38
+ length,
39
+ };
74
40
  }
75
41
 
76
- function rangeFromLoc(loc) {
77
- if (!loc) return { start: { line: 0, character: 0 }, end: { line: 0, character: 1 } };
42
+ function range(loc) {
43
+ const item = loc ?? { line: 0, character: 0, length: 1 };
78
44
  return {
79
- start: { line: loc.line, character: loc.character },
80
- end: { line: loc.line, character: loc.character + loc.length },
45
+ start: { line: item.line, character: item.character },
46
+ end: { line: item.line, character: item.character + item.length },
81
47
  };
82
48
  }
83
49
 
50
+ function diagnostic(uri, code, message, loc, severity = 1) {
51
+ return { uri, range: range(loc), severity, code, source: 'soma-cross-file', message };
52
+ }
53
+
54
+ function retiredSidecarPaths(hostDir, base) {
55
+ return [
56
+ path.join(hostDir, 'event', `${base}_event.yaml`),
57
+ path.join(hostDir, 'ui', `${base}_style.yaml`),
58
+ path.join(hostDir, 'data', `${base}_data.yaml`),
59
+ path.join(hostDir, 'ui', `${base}_data.yaml`),
60
+ ];
61
+ }
62
+
84
63
  function lintAnchorConsistency(hostDir) {
85
64
  const htmlDir = path.join(hostDir, 'html');
86
65
  const diagnostics = [];
87
-
88
66
  for (const base of collectHtmlPages(htmlDir)) {
89
67
  const htmlPath = path.join(htmlDir, `${base}.html`);
90
- const uiPath = path.join(hostDir, 'ui', `${base}_ui.yaml`);
91
- const eventPath = path.join(hostDir, 'event', `${base}_event.yaml`);
92
- const uri = `file://${htmlPath}`;
93
-
94
- if (!fs.existsSync(htmlPath)) continue;
68
+ const declarationPath = path.join(hostDir, 'ui', `${base}_ui.yaml`);
95
69
  const html = fs.readFileSync(htmlPath, 'utf8');
96
70
  const htmlIds = extractSomaIdsFromHtml(html);
97
71
 
98
- const uiYaml = fs.existsSync(uiPath) ? fs.readFileSync(uiPath, 'utf8') : '';
99
- const eventYaml = fs.existsSync(eventPath) ? fs.readFileSync(eventPath, 'utf8') : '';
100
- const showKeys = extractShowKeys(uiYaml);
101
- const eventIds = extractEventIds(eventYaml);
72
+ for (const sidecar of retiredSidecarPaths(hostDir, base).filter(fs.existsSync)) {
73
+ diagnostics.push(diagnostic(
74
+ `file://${sidecar}`,
75
+ 'retired-page-sidecar',
76
+ `[${base}] 页面侧车已退役;事件、样式状态与数据对象必须写进 ${base}_ui.yaml`,
77
+ null,
78
+ ));
79
+ }
102
80
 
103
- for (const id of htmlIds) {
104
- if (uiYaml && showKeys.size > 0 && !showKeys.has(id)) {
105
- diagnostics.push({
106
- uri,
107
- range: rangeFromLoc(findAnchorInHtml(html, id)),
108
- severity: 2,
109
- code: 'orphan-html-anchor',
110
- source: 'soma-cross-file',
111
- message: `[${base}] HTML 锚点 "${id}" 未在 ${base}_ui.yaml show 中声明`,
112
- });
113
- }
81
+ if (!fs.existsSync(declarationPath)) {
82
+ diagnostics.push(diagnostic(
83
+ `file://${htmlPath}`,
84
+ 'missing-soma-declaration',
85
+ `[${base}] 缺少唯一 SOMA DSL 声明 ${base}_ui.yaml`,
86
+ null,
87
+ ));
88
+ continue;
114
89
  }
115
90
 
116
- for (const id of showKeys) {
117
- if (!htmlIds.has(id)) {
118
- diagnostics.push({
119
- uri: `file://${uiPath}`,
120
- range: rangeFromLoc(findKeyInYaml(uiYaml, id)),
121
- severity: 2,
122
- code: 'missing-html-anchor',
123
- source: 'soma-cross-file',
124
- message: `[${base}] ui show "${id}" 在 HTML 中无 data-soma-id`,
125
- });
126
- }
91
+ const source = fs.readFileSync(declarationPath, 'utf8');
92
+ let facts;
93
+ try {
94
+ facts = declarationFacts(source);
95
+ } catch (error) {
96
+ diagnostics.push(diagnostic(
97
+ `file://${declarationPath}`,
98
+ 'invalid-soma-declaration',
99
+ `[${base}] ${(error instanceof Error ? error.message : String(error))}`,
100
+ null,
101
+ ));
102
+ continue;
127
103
  }
128
104
 
129
- for (const id of eventIds) {
130
- if (!htmlIds.has(id)) {
131
- const eventYamlText = fs.existsSync(eventPath) ? fs.readFileSync(eventPath, 'utf8') : eventYaml;
132
- const loc = eventYamlText
133
- ? (() => {
134
- const re = new RegExp(`^\\s*${id.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')}\\.`, 'm');
135
- const m = re.exec(eventYamlText);
136
- if (!m) return null;
137
- const before = eventYamlText.slice(0, m.index);
138
- return {
139
- line: before.split('\n').length - 1,
140
- character: 0,
141
- length: id.length,
142
- };
143
- })()
144
- : null;
145
- diagnostics.push({
146
- uri: `file://${eventPath}`,
147
- range: rangeFromLoc(loc),
148
- severity: 1,
149
- code: 'orphan-event-anchor',
150
- source: 'soma-cross-file',
151
- message: `[${base}] event "${id}" 在 HTML 中无对应 data-soma-id`,
152
- });
153
- }
105
+ const declared = new Set([...facts.uiAnchors, ...facts.componentIds]);
106
+ for (const id of declared) {
107
+ if (htmlIds.has(id)) continue;
108
+ diagnostics.push(diagnostic(
109
+ `file://${declarationPath}`,
110
+ 'missing-html-anchor',
111
+ `[${base}] 声明锚点 "${id}" 在 HTML 中无 data-soma-id`,
112
+ locate(source, new RegExp(`^\\s*${escapeRegex(id)}(?:[.@]|\\s*:)`, 'm'), id.length),
113
+ 2,
114
+ ));
115
+ }
116
+ for (const id of facts.eventAnchors) {
117
+ if (htmlIds.has(id)) continue;
118
+ diagnostics.push(diagnostic(
119
+ `file://${declarationPath}`,
120
+ 'orphan-event-anchor',
121
+ `[${base}] 事件锚点 "${id}" 在 HTML 中无对应 data-soma-id`,
122
+ locate(source, new RegExp(`^\\s*${escapeRegex(id)}\\.`, 'm'), id.length),
123
+ ));
154
124
  }
155
125
  }
156
-
157
126
  return diagnostics;
158
127
  }
159
128
 
160
- module.exports = { lintAnchorConsistency, extractSomaIdsFromHtml, extractShowKeys, extractEventIds };
129
+ module.exports = { lintAnchorConsistency, extractSomaIdsFromHtml, declarationFacts };
@@ -3,7 +3,7 @@
3
3
  */
4
4
  const fs = require('node:fs');
5
5
  const path = require('node:path');
6
- const yaml = require('js-yaml');
6
+ const { declarationFacts } = require('./soma-declaration.cjs');
7
7
 
8
8
  const ANCHOR_PREFIXES = ['btn_', 'input_', 'txt_', 'list_', 'section_', 'nav_', 'link_', 'hero_'];
9
9
 
@@ -29,7 +29,10 @@ function getLocaleYamlCompletions(hostDir, _uri, text, position) {
29
29
 
30
30
  function getHtmlAnchorCompletions(hostDir, uri, text, position) {
31
31
  const filePath = decodeURIComponent(uri.replace(/^file:\/\//, ''));
32
- const pageBase = path.basename(filePath, '.html');
32
+ const rel = path.relative(path.join(hostDir, 'html'), filePath);
33
+ const pageBase = !rel.startsWith('..') && rel.endsWith('.html')
34
+ ? rel.replace(/\\/g, '/').replace(/\.html$/, '')
35
+ : path.basename(filePath, '.html');
33
36
  const uiPath = path.join(hostDir, 'ui', `${pageBase}_ui.yaml`);
34
37
  const existing = new Set();
35
38
  for (const m of text.matchAll(/data-soma-id=["']([^"']+)["']/g)) existing.add(m[1]);
@@ -37,12 +40,15 @@ function getHtmlAnchorCompletions(hostDir, uri, text, position) {
37
40
  const suggestions = [];
38
41
  if (fs.existsSync(uiPath)) {
39
42
  try {
40
- const doc = yaml.load(fs.readFileSync(uiPath, 'utf8'));
41
- if (doc?.show) {
42
- for (const k of Object.keys(doc.show)) {
43
- if (!existing.has(k)) {
44
- suggestions.push({ label: `data-soma-id="${k}"`, kind: 5, detail: 'ui.yaml show 键' });
45
- }
43
+ const facts = declarationFacts(fs.readFileSync(uiPath, 'utf8'));
44
+ const declared = new Set([...facts.uiAnchors, ...facts.componentIds, ...facts.eventAnchors]);
45
+ for (const key of [...declared].sort()) {
46
+ if (!existing.has(key)) {
47
+ suggestions.push({
48
+ label: `data-soma-id="${key}"`,
49
+ kind: 5,
50
+ detail: 'SOMA DSL 声明锚点',
51
+ });
46
52
  }
47
53
  }
48
54
  } catch {
@@ -1,9 +1,10 @@
1
1
  /**
2
- * LSP Hover / Go-to-Definition — html ↔ ui ↔ event ↔ domain
2
+ * LSP Hover / Go-to-Definition — HTML ↔ 唯一 SOMA DSL 声明 ↔ Domain
3
3
  */
4
4
  const fs = require('node:fs');
5
5
  const path = require('node:path');
6
6
  const { scanDomainProperties } = require('./ui-yaml-completion.cjs');
7
+ const { anchorOf, declarationFacts } = require('./soma-declaration.cjs');
7
8
 
8
9
  function filePathFromUri(uri) {
9
10
  return decodeURIComponent(uri.replace(/^file:\/\//, ''));
@@ -17,8 +18,6 @@ function pageBaseFromPath(filePath, hostDir) {
17
18
  const rel = path.relative(hostRoot, resolved);
18
19
  let mm = rel.match(/^ui[/\\](.+)_ui\.yaml$/);
19
20
  if (mm) return mm[1].replace(/\\/g, '/');
20
- mm = rel.match(/^event[/\\](.+)_event\.yaml$/);
21
- if (mm) return mm[1].replace(/\\/g, '/');
22
21
  mm = rel.match(/^html[/\\](.+)\.html$/);
23
22
  if (mm) return mm[1].replace(/\\/g, '/');
24
23
  }
@@ -26,8 +25,6 @@ function pageBaseFromPath(filePath, hostDir) {
26
25
  const name = path.basename(filePath);
27
26
  let m = name.match(/^(.+)_ui\.yaml$/);
28
27
  if (m) return m[1];
29
- m = name.match(/^(.+)_event\.yaml$/);
30
- if (m) return m[1];
31
28
  m = name.match(/^(.+)\.html$/);
32
29
  if (m) return m[1].replace(/\\/g, '/');
33
30
  return null;
@@ -52,13 +49,31 @@ function findAnchorInHtml(html, somaId) {
52
49
  }
53
50
 
54
51
  function findKeyInYaml(yaml, key) {
55
- const re = new RegExp(`^\\s{0,4}${key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*:`, 'm');
56
- const m = re.exec(yaml);
52
+ const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
53
+ const candidates = [
54
+ new RegExp(`^${escaped}\\s*:`, 'm'),
55
+ new RegExp(`^[ \\t]+["']?${escaped}(?:[.@][^"':\\s]+)?["']?[ \\t]*:`, 'm'),
56
+ ];
57
+ const matches = candidates.map((re) => re.exec(yaml)).filter(Boolean).sort((a, b) => a.index - b.index);
58
+ const m = matches[0];
57
59
  if (!m) return null;
58
60
  const line = lineAtIndex(yaml, m.index);
59
61
  return { line, character: m.index - yaml.lastIndexOf('\n', m.index - 1) - 1, length: key.length };
60
62
  }
61
63
 
64
+ function leadingYamlKey(line) {
65
+ const match = /^\s*["']?([\w@.-]+)["']?\s*:/.exec(line);
66
+ return match?.[1] ?? null;
67
+ }
68
+
69
+ function factsOrNull(source) {
70
+ try {
71
+ return declarationFacts(source);
72
+ } catch {
73
+ return null;
74
+ }
75
+ }
76
+
62
77
  function findEventAnchorInYaml(yaml, anchorId) {
63
78
  const re = new RegExp(`^\\s*${anchorId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\.`, 'm');
64
79
  const m = re.exec(yaml);
@@ -69,10 +84,15 @@ function findEventAnchorInYaml(yaml, anchorId) {
69
84
  function getHover(hostDir, uri, text, position) {
70
85
  const filePath = filePathFromUri(uri);
71
86
  const fileName = path.basename(filePath);
72
- const line = (text.split('\n')[position.line] ?? '').trim();
87
+ const rawLine = text.split('\n')[position.line] ?? '';
88
+ const line = rawLine.trim();
89
+ const colon = rawLine.indexOf(':');
90
+ const keyHoverRequested = fileName.endsWith('_ui.yaml')
91
+ && colon >= 0
92
+ && position.character <= colon;
73
93
 
74
94
  const pathMatch = line.match(/page\.([\w.]+)/);
75
- if (pathMatch && fileName.endsWith('_ui.yaml')) {
95
+ if (pathMatch && fileName.endsWith('_ui.yaml') && !keyHoverRequested) {
76
96
  return {
77
97
  contents: {
78
98
  kind: 'markdown',
@@ -81,26 +101,24 @@ function getHover(hostDir, uri, text, position) {
81
101
  };
82
102
  }
83
103
 
84
- const showKey = line.match(/^([\w@.-]+)\s*:/);
85
- if (showKey && fileName.endsWith('_ui.yaml')) {
104
+ const yamlKey = leadingYamlKey(line);
105
+ if (yamlKey && fileName.endsWith('_ui.yaml')) {
86
106
  const pageBase = pageBaseFromPath(filePath, hostDir);
87
107
  const htmlPath = path.join(hostDir, 'html', `${pageBase}.html`);
88
108
  const html = fs.existsSync(htmlPath) ? fs.readFileSync(htmlPath, 'utf8') : '';
89
- const inHtml = html.includes(`data-soma-id="${showKey[1]}"`);
109
+ const facts = factsOrNull(text);
110
+ const anchor = anchorOf(yamlKey.split('.')[0]);
111
+ if (!facts || (!facts.uiAnchors.has(anchor) && !facts.componentIds.has(anchor)
112
+ && !facts.eventAnchors.has(anchor))) return null;
113
+ const inHtml = html.includes(`data-soma-id="${anchor}"`)
114
+ || html.includes(`data-soma-id='${anchor}'`);
115
+ const isEvent = facts.eventAnchors.has(anchor) && yamlKey.includes('.');
90
116
  return {
91
117
  contents: {
92
118
  kind: 'markdown',
93
- value: `**锚点** \`${showKey[1]}\`\n\nHTML: ${inHtml ? '✓ 已声明' : '✗ 未找到'}`,
94
- },
95
- };
96
- }
97
-
98
- const eventAnchor = line.match(/^([\w@.-]+)\.([\w.]+)\s*:/);
99
- if (eventAnchor && fileName.endsWith('_event.yaml')) {
100
- return {
101
- contents: {
102
- kind: 'markdown',
103
- value: `**事件** \`${eventAnchor[1]}.${eventAnchor[2]}\`\n\n类型:Pipeline 事件锚点`,
119
+ value: isEvent
120
+ ? `**事件** \`${yamlKey}\`\n\nHTML 锚点: ${inHtml ? '✓ 已声明' : '✗ 未找到'}`
121
+ : `**锚点** \`${anchor}\`\n\nHTML: ${inHtml ? '✓ 已声明' : '✗ 未找到'}`,
104
122
  },
105
123
  };
106
124
  }
@@ -129,29 +147,15 @@ function getDefinition(hostDir, uri, text, position) {
129
147
 
130
148
  const htmlPath = path.join(hostDir, 'html', `${pageBase}.html`);
131
149
  const uiPath = path.join(hostDir, 'ui', `${pageBase}_ui.yaml`);
132
- const eventPath = path.join(hostDir, 'event', `${pageBase}_event.yaml`);
133
150
 
134
151
  if (fileName.endsWith('_ui.yaml')) {
135
- const keyMatch = currentLine.match(/^\s*([\w@.-]+)\s*:/);
136
- if (keyMatch && fs.existsSync(htmlPath)) {
137
- const html = fs.readFileSync(htmlPath, 'utf8');
138
- const loc = findAnchorInHtml(html, keyMatch[1]);
139
- if (!loc) return null;
140
- return {
141
- uri: `file://${htmlPath}`,
142
- range: {
143
- start: { line: loc.line, character: loc.character },
144
- end: { line: loc.line, character: loc.character + loc.length },
145
- },
146
- };
147
- }
148
- }
149
-
150
- if (fileName.endsWith('_event.yaml')) {
151
- const anchor = currentLine.match(/^\s*([\w@.-]+)\./);
152
- if (anchor && fs.existsSync(htmlPath)) {
152
+ const key = leadingYamlKey(currentLine);
153
+ const facts = factsOrNull(text);
154
+ const anchor = key ? anchorOf(key.split('.')[0]) : null;
155
+ if (anchor && facts && (facts.uiAnchors.has(anchor) || facts.componentIds.has(anchor)
156
+ || facts.eventAnchors.has(anchor)) && fs.existsSync(htmlPath)) {
153
157
  const html = fs.readFileSync(htmlPath, 'utf8');
154
- const loc = findAnchorInHtml(html, anchor[1]);
158
+ const loc = findAnchorInHtml(html, anchor);
155
159
  if (!loc) return null;
156
160
  return {
157
161
  uri: `file://${htmlPath}`,
@@ -167,6 +171,9 @@ function getDefinition(hostDir, uri, text, position) {
167
171
  const m = currentLine.match(/data-soma-id=["']([^"']+)["']/);
168
172
  if (m && fs.existsSync(uiPath)) {
169
173
  const uiYaml = fs.readFileSync(uiPath, 'utf8');
174
+ const facts = factsOrNull(uiYaml);
175
+ if (!facts || (!facts.uiAnchors.has(m[1]) && !facts.componentIds.has(m[1])
176
+ && !facts.eventAnchors.has(m[1]))) return null;
170
177
  const loc = findKeyInYaml(uiYaml, m[1]);
171
178
  if (!loc) return null;
172
179
  return {
@@ -0,0 +1,85 @@
1
+ const yaml = require('js-yaml');
2
+
3
+ function stripDeclarationSwitches(source) {
4
+ const lines = String(source ?? '').split('\n');
5
+ let open = -1;
6
+ let close = -1;
7
+ for (let index = 0; index < lines.length; index += 1) {
8
+ const text = lines[index].trim();
9
+ if (!text || text.startsWith('#')) continue;
10
+ if (open < 0) {
11
+ if (text !== '{{') throw new Error('SOMA DSL 声明必须以 `{{` 开关行开头');
12
+ open = index;
13
+ continue;
14
+ }
15
+ if (text === '}}') close = index;
16
+ }
17
+ if (open < 0 || close < 0 || close <= open) {
18
+ throw new Error('SOMA DSL 声明缺少成对的 `{{` / `}}` 开关行');
19
+ }
20
+ return lines.slice(open + 1, close).join('\n');
21
+ }
22
+
23
+ function parseSomaDeclaration(source) {
24
+ const doc = yaml.load(stripDeclarationSwitches(source));
25
+ if (!doc || typeof doc !== 'object' || Array.isArray(doc)) {
26
+ throw new Error('SOMA DSL 开关内必须是块映射');
27
+ }
28
+ if (!doc.page || typeof doc.page !== 'object' || Array.isArray(doc.page)) {
29
+ throw new Error('SOMA DSL 缺少 page 块');
30
+ }
31
+ return doc;
32
+ }
33
+
34
+ function anchorOf(key) {
35
+ const at = key.indexOf('@');
36
+ const dot = key.lastIndexOf('.');
37
+ const cut = at > 0 ? at : dot > 0 && ['when', 'model', 'class'].includes(key.slice(dot + 1)) ? dot : -1;
38
+ return cut > 0 ? key.slice(0, cut) : key;
39
+ }
40
+
41
+ function collectUiAnchors(ui, result = new Set()) {
42
+ if (!ui || typeof ui !== 'object' || Array.isArray(ui)) return result;
43
+ for (const [key, value] of Object.entries(ui)) {
44
+ if (key === 'each') continue;
45
+ result.add(anchorOf(key));
46
+ if (value && typeof value === 'object' && !Array.isArray(value) && typeof value.each === 'string') {
47
+ collectUiAnchors(value, result);
48
+ }
49
+ }
50
+ return result;
51
+ }
52
+
53
+ function collectEventAnchors(node, result = new Set()) {
54
+ if (!node || typeof node !== 'object' || Array.isArray(node)) return result;
55
+ for (const [key, value] of Object.entries(node)) {
56
+ if (typeof value === 'string') {
57
+ const anchor = key.split('.')[0];
58
+ if (anchor) result.add(anchor);
59
+ } else if (value && typeof value === 'object' && !Array.isArray(value)) {
60
+ collectEventAnchors(value, result);
61
+ }
62
+ }
63
+ return result;
64
+ }
65
+
66
+ function declarationFacts(source) {
67
+ const doc = parseSomaDeclaration(source);
68
+ const page = doc.page;
69
+ const componentIds = new Set(Object.keys(doc).filter((key) => key !== 'page'));
70
+ const uiAnchors = collectUiAnchors(page.ui);
71
+ const eventAnchors = collectEventAnchors(page.event?.on);
72
+ for (const id of componentIds) {
73
+ if (doc[id]?.event?.on && Object.keys(doc[id].event.on).length > 0) eventAnchors.add(id);
74
+ }
75
+ return { doc, page, componentIds, uiAnchors, eventAnchors };
76
+ }
77
+
78
+ module.exports = {
79
+ stripDeclarationSwitches,
80
+ parseSomaDeclaration,
81
+ anchorOf,
82
+ collectUiAnchors,
83
+ collectEventAnchors,
84
+ declarationFacts,
85
+ };