@moondraft/import 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (337) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/LICENSE +34 -0
  3. package/README.md +36 -2
  4. package/SOURCE.md +394 -0
  5. package/dist/adapter/convert.d.ts +144 -0
  6. package/dist/adapter/convert.js +2811 -0
  7. package/dist/adapter/figma-descriptions.d.ts +7 -0
  8. package/dist/adapter/figma-descriptions.js +34 -0
  9. package/dist/adapter/figma-grids.d.ts +12 -0
  10. package/dist/adapter/figma-grids.js +175 -0
  11. package/dist/adapter/figma-handoff.d.ts +12 -0
  12. package/dist/adapter/figma-handoff.js +167 -0
  13. package/dist/adapter/figma-hyperlinks.d.ts +13 -0
  14. package/dist/adapter/figma-hyperlinks.js +77 -0
  15. package/dist/adapter/figma.d.ts +12 -0
  16. package/dist/adapter/figma.js +40 -0
  17. package/dist/adapter/file-components.d.ts +20 -0
  18. package/dist/adapter/file-components.js +352 -0
  19. package/dist/adapter/file-resources.d.ts +29 -0
  20. package/dist/adapter/file-resources.js +851 -0
  21. package/dist/adapter/image-crop.d.ts +18 -0
  22. package/dist/adapter/image-crop.js +30 -0
  23. package/dist/adapter/index.d.ts +6 -0
  24. package/dist/adapter/index.js +9 -0
  25. package/dist/adapter/link.d.ts +49 -0
  26. package/dist/adapter/link.js +892 -0
  27. package/dist/adapter/payload.d.ts +31 -0
  28. package/dist/adapter/payload.js +78 -0
  29. package/dist/adapter/shapes.d.ts +31 -0
  30. package/dist/adapter/shapes.js +75 -0
  31. package/dist/adapter/variables.d.ts +45 -0
  32. package/dist/adapter/variables.js +106 -0
  33. package/dist/figma-file.d.ts +38 -0
  34. package/dist/figma-file.js +97 -0
  35. package/dist/figma-worker.d.ts +11 -0
  36. package/dist/figma-worker.js +12 -0
  37. package/dist/figma.d.ts +26 -0
  38. package/dist/figma.js +230 -0
  39. package/dist/index.d.ts +29 -0
  40. package/dist/index.js +50 -0
  41. package/dist/report.d.ts +42 -0
  42. package/dist/report.js +142 -0
  43. package/dist/serialize.d.ts +7 -0
  44. package/dist/serialize.js +79 -0
  45. package/dist/svg.d.ts +33 -0
  46. package/dist/svg.js +44 -0
  47. package/dist/syntax.d.ts +8 -0
  48. package/dist/syntax.js +19 -0
  49. package/dist/taco/figma-import/clipboard.d.ts +25 -0
  50. package/dist/taco/figma-import/clipboard.js +87 -0
  51. package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
  52. package/dist/taco/figma-import/clipboardSize.js +107 -0
  53. package/dist/taco/figma-import/container/compression.d.ts +9 -0
  54. package/dist/taco/figma-import/container/compression.js +174 -0
  55. package/dist/taco/figma-import/container/error.d.ts +15 -0
  56. package/dist/taco/figma-import/container/error.js +19 -0
  57. package/dist/taco/figma-import/container/kiwiContainer.d.ts +41 -0
  58. package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
  59. package/dist/taco/figma-import/container/limits.d.ts +22 -0
  60. package/dist/taco/figma-import/container/limits.js +22 -0
  61. package/dist/taco/figma-import/container/zip.d.ts +14 -0
  62. package/dist/taco/figma-import/container/zip.js +154 -0
  63. package/dist/taco/figma-import/figFile.d.ts +34 -0
  64. package/dist/taco/figma-import/figFile.js +91 -0
  65. package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
  66. package/dist/taco/figma-import/figFileDocument.js +100 -0
  67. package/dist/taco/figma-import/import/blobs.d.ts +10 -0
  68. package/dist/taco/figma-import/import/blobs.js +23 -0
  69. package/dist/taco/figma-import/import/carry.d.ts +24 -0
  70. package/dist/taco/figma-import/import/carry.js +66 -0
  71. package/dist/taco/figma-import/import/componentProps.d.ts +90 -0
  72. package/dist/taco/figma-import/import/componentProps.js +329 -0
  73. package/dist/taco/figma-import/import/componentSets.d.ts +85 -0
  74. package/dist/taco/figma-import/import/componentSets.js +647 -0
  75. package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
  76. package/dist/taco/figma-import/import/converters/effects.js +127 -0
  77. package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
  78. package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
  79. package/dist/taco/figma-import/import/converters/exportSettings.d.ts +4 -0
  80. package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
  81. package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
  82. package/dist/taco/figma-import/import/converters/frame.js +63 -0
  83. package/dist/taco/figma-import/import/converters/gradient.d.ts +74 -0
  84. package/dist/taco/figma-import/import/converters/gradient.js +108 -0
  85. package/dist/taco/figma-import/import/converters/group.d.ts +19 -0
  86. package/dist/taco/figma-import/import/converters/group.js +30 -0
  87. package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
  88. package/dist/taco/figma-import/import/converters/image.js +94 -0
  89. package/dist/taco/figma-import/import/converters/layout.d.ts +33 -0
  90. package/dist/taco/figma-import/import/converters/layout.js +187 -0
  91. package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
  92. package/dist/taco/figma-import/import/converters/line.js +40 -0
  93. package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
  94. package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
  95. package/dist/taco/figma-import/import/converters/section.d.ts +8 -0
  96. package/dist/taco/figma-import/import/converters/section.js +29 -0
  97. package/dist/taco/figma-import/import/converters/shared.d.ts +71 -0
  98. package/dist/taco/figma-import/import/converters/shared.js +372 -0
  99. package/dist/taco/figma-import/import/converters/text.d.ts +54 -0
  100. package/dist/taco/figma-import/import/converters/text.js +707 -0
  101. package/dist/taco/figma-import/import/converters/vector.d.ts +105 -0
  102. package/dist/taco/figma-import/import/converters/vector.js +609 -0
  103. package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +114 -0
  104. package/dist/taco/figma-import/import/converters/vectorNetwork.js +503 -0
  105. package/dist/taco/figma-import/import/fields.d.ts +15 -0
  106. package/dist/taco/figma-import/import/fields.js +29 -0
  107. package/dist/taco/figma-import/import/instances.d.ts +184 -0
  108. package/dist/taco/figma-import/import/instances.js +1530 -0
  109. package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
  110. package/dist/taco/figma-import/import/nestingCycles.js +488 -0
  111. package/dist/taco/figma-import/import/slots.d.ts +56 -0
  112. package/dist/taco/figma-import/import/slots.js +96 -0
  113. package/dist/taco/figma-import/import/styles.d.ts +48 -0
  114. package/dist/taco/figma-import/import/styles.js +48 -0
  115. package/dist/taco/figma-import/import/tree.d.ts +66 -0
  116. package/dist/taco/figma-import/import/tree.js +194 -0
  117. package/dist/taco/figma-import/import/types.d.ts +232 -0
  118. package/dist/taco/figma-import/import/types.js +33 -0
  119. package/dist/taco/figma-import/import/variables.d.ts +137 -0
  120. package/dist/taco/figma-import/import/variables.js +769 -0
  121. package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
  122. package/dist/taco/figma-import/import/variantTrees.js +520 -0
  123. package/dist/taco/figma-import/import/walk.d.ts +207 -0
  124. package/dist/taco/figma-import/import/walk.js +1464 -0
  125. package/dist/taco/figma-import/index.d.ts +22 -0
  126. package/dist/taco/figma-import/index.js +13 -0
  127. package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
  128. package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
  129. package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
  130. package/dist/taco/figma-import/kiwi/error.js +29 -0
  131. package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
  132. package/dist/taco/figma-import/kiwi/limits.js +18 -0
  133. package/dist/taco/figma-import/kiwi/message.d.ts +12 -0
  134. package/dist/taco/figma-import/kiwi/message.js +147 -0
  135. package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
  136. package/dist/taco/figma-import/kiwi/schema.js +206 -0
  137. package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
  138. package/dist/taco/figma-import/kiwi/types.js +18 -0
  139. package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
  140. package/dist/taco/model/agent-hidden-fields.js +29 -0
  141. package/dist/taco/model/asset-users.d.ts +23 -0
  142. package/dist/taco/model/asset-users.js +128 -0
  143. package/dist/taco/model/auto-layout-lines.d.ts +32 -0
  144. package/dist/taco/model/auto-layout-lines.js +70 -0
  145. package/dist/taco/model/codec.d.ts +444 -0
  146. package/dist/taco/model/codec.js +5665 -0
  147. package/dist/taco/model/color.d.ts +11 -0
  148. package/dist/taco/model/color.js +61 -0
  149. package/dist/taco/model/comment-operations.d.ts +102 -0
  150. package/dist/taco/model/comment-operations.js +227 -0
  151. package/dist/taco/model/component-evaluation.d.ts +96 -0
  152. package/dist/taco/model/component-evaluation.js +555 -0
  153. package/dist/taco/model/component-expressions.d.ts +242 -0
  154. package/dist/taco/model/component-expressions.js +1082 -0
  155. package/dist/taco/model/component-properties.d.ts +111 -0
  156. package/dist/taco/model/component-properties.js +339 -0
  157. package/dist/taco/model/component-samples.d.ts +83 -0
  158. package/dist/taco/model/component-samples.js +290 -0
  159. package/dist/taco/model/component-validity.d.ts +85 -0
  160. package/dist/taco/model/component-validity.js +397 -0
  161. package/dist/taco/model/components.d.ts +155 -0
  162. package/dist/taco/model/components.js +505 -0
  163. package/dist/taco/model/connector-ends.d.ts +20 -0
  164. package/dist/taco/model/connector-ends.js +188 -0
  165. package/dist/taco/model/connector-geometry.d.ts +74 -0
  166. package/dist/taco/model/connector-geometry.js +293 -0
  167. package/dist/taco/model/corner-radii.d.ts +26 -0
  168. package/dist/taco/model/corner-radii.js +47 -0
  169. package/dist/taco/model/derivations.d.ts +81 -0
  170. package/dist/taco/model/derivations.js +151 -0
  171. package/dist/taco/model/export-settings.d.ts +53 -0
  172. package/dist/taco/model/export-settings.js +156 -0
  173. package/dist/taco/model/expressions/ast.d.ts +153 -0
  174. package/dist/taco/model/expressions/ast.js +6 -0
  175. package/dist/taco/model/expressions/cache.d.ts +23 -0
  176. package/dist/taco/model/expressions/cache.js +43 -0
  177. package/dist/taco/model/expressions/check.d.ts +72 -0
  178. package/dist/taco/model/expressions/check.js +517 -0
  179. package/dist/taco/model/expressions/completion.d.ts +61 -0
  180. package/dist/taco/model/expressions/completion.js +421 -0
  181. package/dist/taco/model/expressions/evaluate.d.ts +32 -0
  182. package/dist/taco/model/expressions/evaluate.js +519 -0
  183. package/dist/taco/model/expressions/forms.d.ts +84 -0
  184. package/dist/taco/model/expressions/forms.js +200 -0
  185. package/dist/taco/model/expressions/index.d.ts +14 -0
  186. package/dist/taco/model/expressions/index.js +17 -0
  187. package/dist/taco/model/expressions/lexer.d.ts +42 -0
  188. package/dist/taco/model/expressions/lexer.js +213 -0
  189. package/dist/taco/model/expressions/library.d.ts +33 -0
  190. package/dist/taco/model/expressions/library.js +117 -0
  191. package/dist/taco/model/expressions/names.d.ts +43 -0
  192. package/dist/taco/model/expressions/names.js +99 -0
  193. package/dist/taco/model/expressions/parser.d.ts +15 -0
  194. package/dist/taco/model/expressions/parser.js +438 -0
  195. package/dist/taco/model/expressions/rename.d.ts +34 -0
  196. package/dist/taco/model/expressions/rename.js +46 -0
  197. package/dist/taco/model/expressions/source.d.ts +15 -0
  198. package/dist/taco/model/expressions/source.js +161 -0
  199. package/dist/taco/model/expressions/types.d.ts +119 -0
  200. package/dist/taco/model/expressions/types.js +102 -0
  201. package/dist/taco/model/expressions/values.d.ts +97 -0
  202. package/dist/taco/model/expressions/values.js +160 -0
  203. package/dist/taco/model/geometry-limits.d.ts +16 -0
  204. package/dist/taco/model/geometry-limits.js +67 -0
  205. package/dist/taco/model/geometry.d.ts +47 -0
  206. package/dist/taco/model/geometry.js +193 -0
  207. package/dist/taco/model/group-bounds.d.ts +38 -0
  208. package/dist/taco/model/group-bounds.js +144 -0
  209. package/dist/taco/model/id-table.d.ts +37 -0
  210. package/dist/taco/model/id-table.js +80 -0
  211. package/dist/taco/model/index.d.ts +25 -0
  212. package/dist/taco/model/index.js +27 -0
  213. package/dist/taco/model/instance-resolver.d.ts +376 -0
  214. package/dist/taco/model/instance-resolver.js +1778 -0
  215. package/dist/taco/model/layer-syntax.d.ts +340 -0
  216. package/dist/taco/model/layer-syntax.js +3506 -0
  217. package/dist/taco/model/layout/constraints.d.ts +84 -0
  218. package/dist/taco/model/layout/constraints.js +445 -0
  219. package/dist/taco/model/layout/frame-layout.d.ts +46 -0
  220. package/dist/taco/model/layout/frame-layout.js +618 -0
  221. package/dist/taco/model/layout/node-sizing.d.ts +163 -0
  222. package/dist/taco/model/layout/node-sizing.js +486 -0
  223. package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
  224. package/dist/taco/model/legacy-connector-caps.js +70 -0
  225. package/dist/taco/model/linear-call.d.ts +48 -0
  226. package/dist/taco/model/linear-call.js +58 -0
  227. package/dist/taco/model/new-nodes.d.ts +145 -0
  228. package/dist/taco/model/new-nodes.js +277 -0
  229. package/dist/taco/model/node-builder.d.ts +449 -0
  230. package/dist/taco/model/node-builder.js +2294 -0
  231. package/dist/taco/model/node-table-watch.d.ts +36 -0
  232. package/dist/taco/model/node-table-watch.js +63 -0
  233. package/dist/taco/model/node-transform.d.ts +39 -0
  234. package/dist/taco/model/node-transform.js +79 -0
  235. package/dist/taco/model/node-users-index.d.ts +10 -0
  236. package/dist/taco/model/node-users-index.js +82 -0
  237. package/dist/taco/model/operation-references.d.ts +80 -0
  238. package/dist/taco/model/operation-references.js +399 -0
  239. package/dist/taco/model/operations.d.ts +610 -0
  240. package/dist/taco/model/operations.js +2882 -0
  241. package/dist/taco/model/overlay-table.d.ts +71 -0
  242. package/dist/taco/model/overlay-table.js +171 -0
  243. package/dist/taco/model/override-remap.d.ts +69 -0
  244. package/dist/taco/model/override-remap.js +230 -0
  245. package/dist/taco/model/parent-table.d.ts +53 -0
  246. package/dist/taco/model/parent-table.js +193 -0
  247. package/dist/taco/model/problem-collector.d.ts +27 -0
  248. package/dist/taco/model/problem-collector.js +101 -0
  249. package/dist/taco/model/sha256.d.ts +10 -0
  250. package/dist/taco/model/sha256.js +79 -0
  251. package/dist/taco/model/shape-geometry.d.ts +96 -0
  252. package/dist/taco/model/shape-geometry.js +368 -0
  253. package/dist/taco/model/shared-node-table.d.ts +97 -0
  254. package/dist/taco/model/shared-node-table.js +546 -0
  255. package/dist/taco/model/slots.d.ts +113 -0
  256. package/dist/taco/model/slots.js +281 -0
  257. package/dist/taco/model/stroke-settings.d.ts +37 -0
  258. package/dist/taco/model/stroke-settings.js +90 -0
  259. package/dist/taco/model/svg-path-import.d.ts +89 -0
  260. package/dist/taco/model/svg-path-import.js +219 -0
  261. package/dist/taco/model/text-measurement.d.ts +142 -0
  262. package/dist/taco/model/text-measurement.js +846 -0
  263. package/dist/taco/model/text-reads.d.ts +66 -0
  264. package/dist/taco/model/text-reads.js +128 -0
  265. package/dist/taco/model/text-sizer.d.ts +358 -0
  266. package/dist/taco/model/text-sizer.js +996 -0
  267. package/dist/taco/model/theme-users.d.ts +59 -0
  268. package/dist/taco/model/theme-users.js +312 -0
  269. package/dist/taco/model/thumbnail.d.ts +15 -0
  270. package/dist/taco/model/thumbnail.js +34 -0
  271. package/dist/taco/model/types.d.ts +2369 -0
  272. package/dist/taco/model/types.js +366 -0
  273. package/dist/taco/model/unknown-property.d.ts +5 -0
  274. package/dist/taco/model/unknown-property.js +7 -0
  275. package/dist/taco/model/validated-documents.d.ts +22 -0
  276. package/dist/taco/model/validated-documents.js +22 -0
  277. package/dist/taco/model/variable-modes.d.ts +61 -0
  278. package/dist/taco/model/variable-modes.js +438 -0
  279. package/dist/taco/model/variable-references.d.ts +54 -0
  280. package/dist/taco/model/variable-references.js +183 -0
  281. package/dist/taco/model/variables.d.ts +24 -0
  282. package/dist/taco/model/variables.js +151 -0
  283. package/dist/taco/model/vector-network.d.ts +34 -0
  284. package/dist/taco/model/vector-network.js +411 -0
  285. package/dist/taco/svg-import/import-svg.d.ts +36 -0
  286. package/dist/taco/svg-import/import-svg.js +729 -0
  287. package/dist/taco/svg-import/index.d.ts +3 -0
  288. package/dist/taco/svg-import/index.js +3 -0
  289. package/dist/taco/svg-import/limits/limits.d.ts +79 -0
  290. package/dist/taco/svg-import/limits/limits.js +160 -0
  291. package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
  292. package/dist/taco/svg-import/paint/gradients.js +429 -0
  293. package/dist/taco/svg-import/paint/paint.d.ts +32 -0
  294. package/dist/taco/svg-import/paint/paint.js +162 -0
  295. package/dist/taco/svg-import/paths/paths.d.ts +58 -0
  296. package/dist/taco/svg-import/paths/paths.js +205 -0
  297. package/dist/taco/svg-import/public-types.d.ts +279 -0
  298. package/dist/taco/svg-import/public-types.js +56 -0
  299. package/dist/taco/svg-import/report/report.d.ts +44 -0
  300. package/dist/taco/svg-import/report/report.js +79 -0
  301. package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
  302. package/dist/taco/svg-import/reuse/reuse.js +840 -0
  303. package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
  304. package/dist/taco/svg-import/shapes/shapes.js +386 -0
  305. package/dist/taco/svg-import/structure/effects.d.ts +26 -0
  306. package/dist/taco/svg-import/structure/effects.js +618 -0
  307. package/dist/taco/svg-import/structure/image.d.ts +24 -0
  308. package/dist/taco/svg-import/structure/image.js +495 -0
  309. package/dist/taco/svg-import/structure/walk.d.ts +85 -0
  310. package/dist/taco/svg-import/structure/walk.js +544 -0
  311. package/dist/taco/svg-import/style/color.d.ts +40 -0
  312. package/dist/taco/svg-import/style/color.js +275 -0
  313. package/dist/taco/svg-import/style/computed.d.ts +214 -0
  314. package/dist/taco/svg-import/style/computed.js +1534 -0
  315. package/dist/taco/svg-import/style/css.d.ts +127 -0
  316. package/dist/taco/svg-import/style/css.js +541 -0
  317. package/dist/taco/svg-import/style/media.d.ts +25 -0
  318. package/dist/taco/svg-import/style/media.js +307 -0
  319. package/dist/taco/svg-import/style/properties.d.ts +43 -0
  320. package/dist/taco/svg-import/style/properties.js +135 -0
  321. package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
  322. package/dist/taco/svg-import/style/selector-reach.js +169 -0
  323. package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
  324. package/dist/taco/svg-import/style/selector-validity.js +307 -0
  325. package/dist/taco/svg-import/text/text.d.ts +29 -0
  326. package/dist/taco/svg-import/text/text.js +545 -0
  327. package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
  328. package/dist/taco/svg-import/transform/matrix.js +229 -0
  329. package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
  330. package/dist/taco/svg-import/tree/element-data.js +155 -0
  331. package/dist/taco/svg-import/tree/elements.d.ts +36 -0
  332. package/dist/taco/svg-import/tree/elements.js +121 -0
  333. package/dist/taco/svg-import/units/length.d.ts +43 -0
  334. package/dist/taco/svg-import/units/length.js +125 -0
  335. package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
  336. package/dist/taco/svg-import/units/viewbox.js +77 -0
  337. package/package.json +55 -4
@@ -0,0 +1,81 @@
1
+ import type { DesignDocument, NodeId } from "./types.ts";
2
+ /**
3
+ * Where a frozen document came from: the document it was made from and the
4
+ * nodes whose entries differ between the two. Every other node entry is the
5
+ * very same object in both. `applyOperations` records one for each document
6
+ * it returns, and `recordDerivedNodes` for documents built outside it.
7
+ *
8
+ * This module is internal to the model package: it is not exported from the
9
+ * package index.
10
+ */
11
+ export interface Derivation {
12
+ /** The document the operations were applied to. Weak, so a chain of edits does not keep every older document. */
13
+ readonly base: WeakRef<DesignDocument>;
14
+ /** Every node whose entry differs between `base` and the derived document. */
15
+ readonly changedNodeIds: readonly NodeId[];
16
+ /**
17
+ * The nodes the operations inserted or removed, a node removed and put
18
+ * back included, which moves it in the table order. Undefined when not
19
+ * recorded: every changed node may then have been.
20
+ */
21
+ readonly insertedOrRemovedNodeIds?: readonly NodeId[];
22
+ }
23
+ /** For each recorded document, the document it came from and the nodes it changed. */
24
+ export declare const derivations: WeakMap<import("./types.ts").DesignDocumentV1, Derivation>;
25
+ /**
26
+ * How often an index kept for each document (the instances of each component)
27
+ * was built by reading every node instead of from the index of a document it
28
+ * was derived from. Tests read it; it is not exported from the package.
29
+ */
30
+ export declare const derivedIndexStats: {
31
+ instanceIndexWholeDocumentScans: number;
32
+ };
33
+ /** How many recorded steps a walk back from one document follows at most. */
34
+ export declare const MAX_DERIVATION_STEPS = 8;
35
+ /**
36
+ * Records where each new document of `replaced` came from, for documents
37
+ * replaced by copies with the same node table (as the store does when it
38
+ * gives every commit's document new comment threads). Each old document's
39
+ * derivations are followed back to the nearest document that was replaced
40
+ * too, and the new one is recorded as derived from that one's replacement,
41
+ * with the nodes changed on the way. Without this a copy has no derivation,
42
+ * and comparing it with another copy (an undo) reads every node.
43
+ * A key may map to itself: a new document made from an old one is then
44
+ * linked to the old one's replacement in the same way.
45
+ */
46
+ export declare function carryDerivations(replaced: ReadonlyMap<DesignDocument, DesignDocument>): void;
47
+ /**
48
+ * Walks back from `document` through its recorded derivations, at most
49
+ * `MAX_DERIVATION_STEPS` steps, to the nearest document (`document` itself
50
+ * included) that `accepts` approves. Returns that document and the change
51
+ * lists on the way to it: every node whose entry can differ between it and
52
+ * `document` is in one of them. Undefined when no such document is found.
53
+ */
54
+ export declare function nearestDerivationBase(document: DesignDocument, accepts: (candidate: DesignDocument) => boolean): {
55
+ readonly base: DesignDocument;
56
+ readonly changedNodeIds: readonly (readonly NodeId[])[];
57
+ } | undefined;
58
+ /**
59
+ * Records `derived` as made in one step from the nearest document back along
60
+ * its derivations that `isAnchor` accepts, with every node changed on the way
61
+ * (and the nodes inserted or removed, when every step recorded them).
62
+ *
63
+ * Each step holds its base only weakly. A document made in several steps
64
+ * (a layout view: each round of corrections and its layout) has steps in
65
+ * between that nothing else holds, and once the garbage collector takes one,
66
+ * a walk back from `derived` stops there: a diff with a document made from
67
+ * the same anchor then reads every node. Call it right after `derived` is
68
+ * made, while every step is still there. Does nothing when no anchor is found
69
+ * or the nearest one is `derived`'s own base. Returns whether it recorded.
70
+ */
71
+ export declare function shortenDerivation(derived: DesignDocument, isAnchor: (candidate: DesignDocument) => boolean): boolean;
72
+ /**
73
+ * Whether `nodes` can no longer change: a frozen table, or a preview's table
74
+ * shared with its stored document (`ApplyOperationsOptions.shareNodeTable`),
75
+ * or a sealed shared table (`sharedTableDraft`), which `Object.isFrozen`
76
+ * does not report as frozen. Tables like these keep
77
+ * their parent table (`parentMapOf`) and other per-table caches.
78
+ */
79
+ export declare function isFrozenNodeMap(nodes: DesignDocument["nodes"]): boolean;
80
+ /** Notes a node map the caller has just frozen. */
81
+ export declare function rememberFrozenNodeMap(nodes: DesignDocument["nodes"]): void;
@@ -0,0 +1,151 @@
1
+ import { isSealedOverlay } from "./overlay-table.js";
2
+ import { isSealedSharedTable } from "./shared-node-table.js";
3
+ /** For each recorded document, the document it came from and the nodes it changed. */
4
+ export const derivations = new WeakMap();
5
+ /**
6
+ * How often an index kept for each document (the instances of each component)
7
+ * was built by reading every node instead of from the index of a document it
8
+ * was derived from. Tests read it; it is not exported from the package.
9
+ */
10
+ export const derivedIndexStats = { instanceIndexWholeDocumentScans: 0 };
11
+ /** How many recorded steps a walk back from one document follows at most. */
12
+ export const MAX_DERIVATION_STEPS = 8;
13
+ /**
14
+ * Records where each new document of `replaced` came from, for documents
15
+ * replaced by copies with the same node table (as the store does when it
16
+ * gives every commit's document new comment threads). Each old document's
17
+ * derivations are followed back to the nearest document that was replaced
18
+ * too, and the new one is recorded as derived from that one's replacement,
19
+ * with the nodes changed on the way. Without this a copy has no derivation,
20
+ * and comparing it with another copy (an undo) reads every node.
21
+ * A key may map to itself: a new document made from an old one is then
22
+ * linked to the old one's replacement in the same way.
23
+ */
24
+ export function carryDerivations(replaced) {
25
+ const carried = [];
26
+ for (const [before, after] of replaced) {
27
+ if (after.nodes !== before.nodes)
28
+ continue;
29
+ const changedNodeIds = new Set();
30
+ let document = before;
31
+ for (let step = 0; step < MAX_DERIVATION_STEPS; step += 1) {
32
+ const derivation = derivations.get(document);
33
+ const base = derivation?.base.deref();
34
+ if (derivation === undefined || base === undefined)
35
+ break;
36
+ for (const id of derivation.changedNodeIds)
37
+ changedNodeIds.add(id);
38
+ const replacement = replaced.get(base);
39
+ // A replacement with another node table is not the same nodes, so nothing is carried through it.
40
+ if (replacement !== undefined && replacement.nodes !== base.nodes)
41
+ break;
42
+ if (replacement !== undefined && replacement !== after) {
43
+ carried.push([after, { base: new WeakRef(replacement), changedNodeIds: Object.freeze([...changedNodeIds]) }]);
44
+ break;
45
+ }
46
+ document = base;
47
+ }
48
+ }
49
+ for (const [document, derivation] of carried)
50
+ derivations.set(document, derivation);
51
+ }
52
+ /**
53
+ * Walks back from `document` through its recorded derivations, at most
54
+ * `MAX_DERIVATION_STEPS` steps, to the nearest document (`document` itself
55
+ * included) that `accepts` approves. Returns that document and the change
56
+ * lists on the way to it: every node whose entry can differ between it and
57
+ * `document` is in one of them. Undefined when no such document is found.
58
+ */
59
+ export function nearestDerivationBase(document, accepts) {
60
+ const changedNodeIds = [];
61
+ let candidate = document;
62
+ for (let step = 0; candidate !== undefined; step += 1) {
63
+ if (accepts(candidate))
64
+ return { base: candidate, changedNodeIds };
65
+ const derivation = derivations.get(candidate);
66
+ if (derivation === undefined || step === MAX_DERIVATION_STEPS)
67
+ return undefined;
68
+ changedNodeIds.push(derivation.changedNodeIds);
69
+ candidate = derivation.base.deref();
70
+ }
71
+ return undefined;
72
+ }
73
+ /**
74
+ * How many steps `shortenDerivation` follows back at most. Larger than
75
+ * `MAX_DERIVATION_STEPS`: a layout view is made in several steps (each round
76
+ * of corrections and the layout that follows), and the walk that shortens
77
+ * them runs once, when the view is made.
78
+ */
79
+ const MAX_SHORTENED_STEPS = 64;
80
+ /**
81
+ * Records `derived` as made in one step from the nearest document back along
82
+ * its derivations that `isAnchor` accepts, with every node changed on the way
83
+ * (and the nodes inserted or removed, when every step recorded them).
84
+ *
85
+ * Each step holds its base only weakly. A document made in several steps
86
+ * (a layout view: each round of corrections and its layout) has steps in
87
+ * between that nothing else holds, and once the garbage collector takes one,
88
+ * a walk back from `derived` stops there: a diff with a document made from
89
+ * the same anchor then reads every node. Call it right after `derived` is
90
+ * made, while every step is still there. Does nothing when no anchor is found
91
+ * or the nearest one is `derived`'s own base. Returns whether it recorded.
92
+ */
93
+ export function shortenDerivation(derived, isAnchor) {
94
+ const changed = new Set();
95
+ let insertedOrRemoved = new Set();
96
+ let candidate = derived;
97
+ for (let step = 0; step < MAX_SHORTENED_STEPS; step += 1) {
98
+ const derivation = derivations.get(candidate);
99
+ const base = derivation?.base.deref();
100
+ if (derivation === undefined || base === undefined)
101
+ return false;
102
+ for (const id of derivation.changedNodeIds)
103
+ changed.add(id);
104
+ if (derivation.insertedOrRemovedNodeIds === undefined)
105
+ insertedOrRemoved = undefined;
106
+ else if (insertedOrRemoved !== undefined)
107
+ for (const id of derivation.insertedOrRemovedNodeIds)
108
+ insertedOrRemoved.add(id);
109
+ if (isAnchor(base)) {
110
+ if (step === 0)
111
+ return false;
112
+ derivations.set(derived, {
113
+ base: new WeakRef(base),
114
+ changedNodeIds: Object.freeze([...changed]),
115
+ ...(insertedOrRemoved === undefined ? {} : { insertedOrRemovedNodeIds: Object.freeze([...insertedOrRemoved]) }),
116
+ });
117
+ return true;
118
+ }
119
+ candidate = base;
120
+ }
121
+ return false;
122
+ }
123
+ /**
124
+ * Node maps known to be frozen. `Object.isFrozen` reads every property of
125
+ * a large object, so a document of thousands of nodes would pay for it on
126
+ * each diff; a frozen object stays frozen, so the answer is kept.
127
+ */
128
+ const frozenNodeMaps = new WeakSet();
129
+ /**
130
+ * Whether `nodes` can no longer change: a frozen table, or a preview's table
131
+ * shared with its stored document (`ApplyOperationsOptions.shareNodeTable`),
132
+ * or a sealed shared table (`sharedTableDraft`), which `Object.isFrozen`
133
+ * does not report as frozen. Tables like these keep
134
+ * their parent table (`parentMapOf`) and other per-table caches.
135
+ */
136
+ export function isFrozenNodeMap(nodes) {
137
+ if (frozenNodeMaps.has(nodes))
138
+ return true;
139
+ // A sealed overlay (a preview's table over its document's) is closed to
140
+ // writes as a frozen table is, though `Object.isFrozen` cannot say so.
141
+ // So is a sealed shared table (`sharedTableDraft`), a Proxy over an object
142
+ // its family writes.
143
+ if (!Object.isFrozen(nodes) && !isSealedOverlay(nodes) && !isSealedSharedTable(nodes))
144
+ return false;
145
+ frozenNodeMaps.add(nodes);
146
+ return true;
147
+ }
148
+ /** Notes a node map the caller has just frozen. */
149
+ export function rememberFrozenNodeMap(nodes) {
150
+ frozenNodeMaps.add(nodes);
151
+ }
@@ -0,0 +1,53 @@
1
+ import type { ExportConstraint, ExportFormat, ExportQuality, ExportSetting } from "./types.ts";
2
+ /** Largest multiplier a scale constraint may hold. */
3
+ export declare const MAX_EXPORT_SCALE = 100;
4
+ /** Largest pixel width or height a size constraint may hold; the renderer's own limit. */
5
+ export declare const MAX_EXPORT_DIMENSION = 16384;
6
+ /** Longest suffix, in UTF-16 code units. */
7
+ export declare const MAX_EXPORT_SUFFIX_LENGTH = 100;
8
+ /** Most rows one node may hold. */
9
+ export declare const MAX_EXPORT_SETTINGS = 64;
10
+ /** The quality a row starts with when it switches to `format`. */
11
+ export declare function defaultExportQuality(format: ExportFormat): ExportQuality;
12
+ /** True when the format ignores the size constraint and exports at 1x. */
13
+ export declare function isVectorExportFormat(format: ExportFormat): format is "svg" | "pdf";
14
+ /** The reason a constraint is invalid, or `null` when it is valid. */
15
+ export declare function exportConstraintProblem(constraint: ExportConstraint): string | null;
16
+ /** The reason a suffix is invalid, or `null` when it is valid. */
17
+ export declare function exportSuffixProblem(suffix: string): string | null;
18
+ /**
19
+ * The row the Export section's + button adds. The first row is PNG at 1x
20
+ * with no suffix. Each later row copies the last row's format and options
21
+ * and takes the next whole scale, with a matching "@Nx" suffix, as Figma does.
22
+ */
23
+ export declare function nextExportSetting(existing: readonly ExportSetting[]): ExportSetting;
24
+ /** A constraint as the scale field shows it: "2x", "512w" or "512h". */
25
+ export declare function formatExportConstraint(constraint: ExportConstraint): string;
26
+ /**
27
+ * Reads what a person typed in the scale field. "2", "2x" and "2X" are
28
+ * multipliers, "512w" a fixed width and "512h" a fixed height. Returns `null`
29
+ * when the text is not a valid constraint.
30
+ */
31
+ export declare function parseExportConstraint(text: string): ExportConstraint | null;
32
+ /** The file extension for a format; JPEG files use ".jpg", as Figma's do. */
33
+ export declare function exportFileExtension(format: ExportFormat): string;
34
+ /**
35
+ * The part of a layer name that is safe in a file name on every common
36
+ * system. Path separators and reserved characters become "-".
37
+ */
38
+ export declare function exportFileStem(name: string): string;
39
+ /** `<layer name><suffix>.<extension>`, as Figma names each exported file. */
40
+ export declare function exportFileName(name: string, setting: Pick<ExportSetting, "format" | "suffix">): string;
41
+ /**
42
+ * Makes every file name in one export unique. A repeated name gets " 2",
43
+ * " 3" and so on before its extension, so no download replaces another.
44
+ */
45
+ export declare function uniqueExportFileNames(names: readonly string[]): readonly string[];
46
+ /**
47
+ * A valid XML id made from a layer name: spaces become "_", other characters
48
+ * outside letters, digits, "_", "-" and "." are dropped, and a name that does
49
+ * not start with a letter or "_" gets a leading "_". `used` holds ids already
50
+ * in the file; a repeated id gets "_2", "_3" and so on. The result is added
51
+ * to `used`.
52
+ */
53
+ export declare function uniqueSvgId(name: string, used: Set<string>): string;
@@ -0,0 +1,156 @@
1
+ /** Largest multiplier a scale constraint may hold. */
2
+ export const MAX_EXPORT_SCALE = 100;
3
+ /** Largest pixel width or height a size constraint may hold; the renderer's own limit. */
4
+ export const MAX_EXPORT_DIMENSION = 16384;
5
+ /** Longest suffix, in UTF-16 code units. */
6
+ export const MAX_EXPORT_SUFFIX_LENGTH = 100;
7
+ /** Most rows one node may hold. */
8
+ export const MAX_EXPORT_SETTINGS = 64;
9
+ /** The quality a row starts with when it switches to `format`. */
10
+ export function defaultExportQuality(format) {
11
+ // Figma exports JPEG at High and PDF at Medium by default.
12
+ return format === "pdf" ? "medium" : "high";
13
+ }
14
+ /** True when the format ignores the size constraint and exports at 1x. */
15
+ export function isVectorExportFormat(format) {
16
+ return format === "svg" || format === "pdf";
17
+ }
18
+ /** The reason a constraint is invalid, or `null` when it is valid. */
19
+ export function exportConstraintProblem(constraint) {
20
+ const { value } = constraint;
21
+ if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
22
+ return "An export size must be a positive number";
23
+ }
24
+ if (constraint.type === "scale") {
25
+ return value > MAX_EXPORT_SCALE
26
+ ? `An export scale must be at most ${MAX_EXPORT_SCALE}x`
27
+ : null;
28
+ }
29
+ if (!Number.isInteger(value) || value > MAX_EXPORT_DIMENSION) {
30
+ return `An export width or height must be a whole number of pixels up to ${MAX_EXPORT_DIMENSION}`;
31
+ }
32
+ return null;
33
+ }
34
+ /** The reason a suffix is invalid, or `null` when it is valid. */
35
+ export function exportSuffixProblem(suffix) {
36
+ if (suffix.length > MAX_EXPORT_SUFFIX_LENGTH) {
37
+ return `An export suffix must be at most ${MAX_EXPORT_SUFFIX_LENGTH} characters`;
38
+ }
39
+ // Control characters cannot appear in a file name.
40
+ // eslint-disable-next-line no-control-regex
41
+ if (/[\u0000-\u001f\u007f]/.test(suffix)) {
42
+ return "An export suffix cannot contain control characters";
43
+ }
44
+ return null;
45
+ }
46
+ /**
47
+ * The row the Export section's + button adds. The first row is PNG at 1x
48
+ * with no suffix. Each later row copies the last row's format and options
49
+ * and takes the next whole scale, with a matching "@Nx" suffix, as Figma does.
50
+ */
51
+ export function nextExportSetting(existing) {
52
+ const last = existing.at(-1);
53
+ if (last === undefined) {
54
+ return {
55
+ format: "png",
56
+ constraint: { type: "scale", value: 1 },
57
+ suffix: "",
58
+ quality: defaultExportQuality("png"),
59
+ resampling: "detailed",
60
+ ignoreOverlappingLayers: true,
61
+ includeIdAttribute: false,
62
+ };
63
+ }
64
+ const largestScale = existing.reduce((largest, setting) => setting.constraint.type === "scale"
65
+ ? Math.max(largest, setting.constraint.value)
66
+ : largest, 0);
67
+ const scale = Math.min(MAX_EXPORT_SCALE, Math.max(1, Math.floor(largestScale) + 1));
68
+ return {
69
+ ...last,
70
+ constraint: { type: "scale", value: scale },
71
+ suffix: `@${formatExportNumber(scale)}x`,
72
+ };
73
+ }
74
+ /** Formats a constraint value without trailing zeros: 0.5, 1, 1.5. */
75
+ function formatExportNumber(value) {
76
+ return String(Number(value.toFixed(4)));
77
+ }
78
+ /** A constraint as the scale field shows it: "2x", "512w" or "512h". */
79
+ export function formatExportConstraint(constraint) {
80
+ const unit = constraint.type === "scale" ? "x" : constraint.type === "width" ? "w" : "h";
81
+ return `${formatExportNumber(constraint.value)}${unit}`;
82
+ }
83
+ /**
84
+ * Reads what a person typed in the scale field. "2", "2x" and "2X" are
85
+ * multipliers, "512w" a fixed width and "512h" a fixed height. Returns `null`
86
+ * when the text is not a valid constraint.
87
+ */
88
+ export function parseExportConstraint(text) {
89
+ const match = /^\s*(\d+(?:\.\d+)?|\.\d+)\s*([xwh]?)\s*$/i.exec(text);
90
+ if (match === null)
91
+ return null;
92
+ const value = Number(match[1]);
93
+ const unit = (match[2] ?? "").toLowerCase();
94
+ const constraint = unit === "w"
95
+ ? { type: "width", value }
96
+ : unit === "h"
97
+ ? { type: "height", value }
98
+ : { type: "scale", value };
99
+ return exportConstraintProblem(constraint) === null ? constraint : null;
100
+ }
101
+ /** The file extension for a format; JPEG files use ".jpg", as Figma's do. */
102
+ export function exportFileExtension(format) {
103
+ return format === "jpeg" ? "jpg" : format;
104
+ }
105
+ /**
106
+ * The part of a layer name that is safe in a file name on every common
107
+ * system. Path separators and reserved characters become "-".
108
+ */
109
+ export function exportFileStem(name) {
110
+ // eslint-disable-next-line no-control-regex
111
+ const cleaned = name.trim().replace(/[\\/:*?"<>|\u0000-\u001f\u007f]+/g, "-").replace(/\s+/g, " ");
112
+ return cleaned.length === 0 ? "export" : cleaned.slice(0, 120);
113
+ }
114
+ /** `<layer name><suffix>.<extension>`, as Figma names each exported file. */
115
+ export function exportFileName(name, setting) {
116
+ // eslint-disable-next-line no-control-regex
117
+ const suffix = setting.suffix.replace(/[\\/:*?"<>|\u0000-\u001f\u007f]+/g, "-");
118
+ return `${exportFileStem(name)}${suffix}.${exportFileExtension(setting.format)}`;
119
+ }
120
+ /**
121
+ * Makes every file name in one export unique. A repeated name gets " 2",
122
+ * " 3" and so on before its extension, so no download replaces another.
123
+ */
124
+ export function uniqueExportFileNames(names) {
125
+ const used = new Set();
126
+ return names.map((name) => {
127
+ let candidate = name;
128
+ const dot = name.lastIndexOf(".");
129
+ const stem = dot <= 0 ? name : name.slice(0, dot);
130
+ const extension = dot <= 0 ? "" : name.slice(dot);
131
+ for (let index = 2; used.has(candidate.toLowerCase()); index += 1) {
132
+ candidate = `${stem} ${index}${extension}`;
133
+ }
134
+ used.add(candidate.toLowerCase());
135
+ return candidate;
136
+ });
137
+ }
138
+ /**
139
+ * A valid XML id made from a layer name: spaces become "_", other characters
140
+ * outside letters, digits, "_", "-" and "." are dropped, and a name that does
141
+ * not start with a letter or "_" gets a leading "_". `used` holds ids already
142
+ * in the file; a repeated id gets "_2", "_3" and so on. The result is added
143
+ * to `used`.
144
+ */
145
+ export function uniqueSvgId(name, used) {
146
+ let base = name.trim().replace(/\s+/g, "_").replace(/[^\p{L}\p{N}_.-]/gu, "");
147
+ if (base.length === 0)
148
+ base = "layer";
149
+ if (!/^[\p{L}_]/u.test(base))
150
+ base = `_${base}`;
151
+ let candidate = base;
152
+ for (let index = 2; used.has(candidate); index += 1)
153
+ candidate = `${base}_${index}`;
154
+ used.add(candidate);
155
+ return candidate;
156
+ }
@@ -0,0 +1,153 @@
1
+ /** A half-open range of UTF-16 offsets in an expression's source text. */
2
+ export interface ExpressionSpan {
3
+ readonly start: number;
4
+ readonly end: number;
5
+ }
6
+ /** A parse or type error, with the text it points at and an optional fix. */
7
+ export interface ExpressionError {
8
+ readonly message: string;
9
+ readonly span: ExpressionSpan;
10
+ /** A short suggested fix, for example "write `8px`". */
11
+ readonly hint?: string;
12
+ }
13
+ /** The unit suffix of a number literal: `8px`, `50%`, `45deg`. */
14
+ export type ExpressionUnit = "px" | "%" | "deg";
15
+ /** The two-operand operators, from `or` (lowest precedence) to `/` (highest). */
16
+ export type ExpressionBinaryOperator = "or" | "and" | "==" | "!=" | "<" | "<=" | ">" | ">=" | "++" | "+" | "-" | "*" | "/";
17
+ /** An sRGB colour with every channel from 0 to 1, as a `#rrggbb[aa]` literal parses to. */
18
+ export interface ExpressionColor {
19
+ readonly red: number;
20
+ readonly green: number;
21
+ readonly blue: number;
22
+ readonly alpha: number;
23
+ }
24
+ /** One argument of a call: positional, or named with `name: value`. */
25
+ export interface ExpressionArgument {
26
+ readonly name?: string;
27
+ readonly nameSpan?: ExpressionSpan;
28
+ readonly value: Expression;
29
+ }
30
+ /** One `name: value` pair inside `with { ... }`. */
31
+ export interface ExpressionFieldUpdate {
32
+ readonly name: string;
33
+ readonly nameSpan: ExpressionSpan;
34
+ readonly value: Expression;
35
+ }
36
+ /** A `match` arm pattern: an enum option (`.small`) or a boolean (`true`). */
37
+ export type ExpressionPattern = {
38
+ readonly kind: "option";
39
+ readonly name: string;
40
+ readonly span: ExpressionSpan;
41
+ } | {
42
+ readonly kind: "boolean";
43
+ readonly value: boolean;
44
+ readonly span: ExpressionSpan;
45
+ };
46
+ /** One arm of a `match`: one or more patterns and the value they give. */
47
+ export interface ExpressionMatchArm {
48
+ readonly patterns: readonly ExpressionPattern[];
49
+ readonly body: Expression;
50
+ }
51
+ /** A node of the expression syntax tree. `kind` tells the construct. */
52
+ export type Expression = {
53
+ readonly kind: "boolean";
54
+ readonly value: boolean;
55
+ readonly span: ExpressionSpan;
56
+ } | {
57
+ readonly kind: "number";
58
+ readonly value: number;
59
+ /** Absent for a plain `number`. */
60
+ readonly unit?: ExpressionUnit;
61
+ readonly span: ExpressionSpan;
62
+ } | {
63
+ readonly kind: "string";
64
+ readonly value: string;
65
+ readonly span: ExpressionSpan;
66
+ } | {
67
+ readonly kind: "color";
68
+ readonly value: ExpressionColor;
69
+ readonly span: ExpressionSpan;
70
+ }
71
+ /** An enum option literal, `.large`. Its enum comes from the expected type. */
72
+ | {
73
+ readonly kind: "option";
74
+ readonly name: string;
75
+ readonly span: ExpressionSpan;
76
+ }
77
+ /** A component property or a `let` name. */
78
+ | {
79
+ readonly kind: "name";
80
+ readonly name: string;
81
+ readonly span: ExpressionSpan;
82
+ }
83
+ /** The component's base value of the field being computed. */
84
+ | {
85
+ readonly kind: "stored";
86
+ readonly span: ExpressionSpan;
87
+ }
88
+ /** A document variable, `var("Collection/Name")`; `nameSpan` covers the string literal. */
89
+ | {
90
+ readonly kind: "variable";
91
+ readonly name: string;
92
+ readonly nameSpan: ExpressionSpan;
93
+ readonly span: ExpressionSpan;
94
+ }
95
+ /** A constructor or standard library call. */
96
+ | {
97
+ readonly kind: "call";
98
+ readonly callee: string;
99
+ readonly calleeSpan: ExpressionSpan;
100
+ readonly args: readonly ExpressionArgument[];
101
+ readonly span: ExpressionSpan;
102
+ } | {
103
+ readonly kind: "list";
104
+ readonly items: readonly Expression[];
105
+ readonly span: ExpressionSpan;
106
+ }
107
+ /** A record field read, `pad.top`. */
108
+ | {
109
+ readonly kind: "field";
110
+ readonly target: Expression;
111
+ readonly name: string;
112
+ readonly nameSpan: ExpressionSpan;
113
+ readonly span: ExpressionSpan;
114
+ }
115
+ /** A record copy with some fields changed, `stored with { left: 0px }`. */
116
+ | {
117
+ readonly kind: "with";
118
+ readonly target: Expression;
119
+ readonly updates: readonly ExpressionFieldUpdate[];
120
+ readonly span: ExpressionSpan;
121
+ } | {
122
+ readonly kind: "unary";
123
+ readonly operator: "-" | "not";
124
+ readonly operand: Expression;
125
+ readonly span: ExpressionSpan;
126
+ } | {
127
+ readonly kind: "binary";
128
+ readonly operator: ExpressionBinaryOperator;
129
+ readonly operatorSpan: ExpressionSpan;
130
+ readonly left: Expression;
131
+ readonly right: Expression;
132
+ readonly span: ExpressionSpan;
133
+ } | {
134
+ readonly kind: "if";
135
+ readonly condition: Expression;
136
+ readonly then: Expression;
137
+ readonly else: Expression;
138
+ readonly span: ExpressionSpan;
139
+ } | {
140
+ readonly kind: "let";
141
+ readonly name: string;
142
+ readonly nameSpan: ExpressionSpan;
143
+ readonly value: Expression;
144
+ readonly body: Expression;
145
+ readonly span: ExpressionSpan;
146
+ } | {
147
+ readonly kind: "match";
148
+ readonly subject: Expression;
149
+ readonly arms: readonly ExpressionMatchArm[];
150
+ /** The `else => value` arm, when written. */
151
+ readonly otherwise?: Expression;
152
+ readonly span: ExpressionSpan;
153
+ };
@@ -0,0 +1,6 @@
1
+ // The syntax tree of the component expression language (docs/components/design.md 8.4).
2
+ //
3
+ // This file is a contract: the evaluator (part 2) and the editor (step 3.2)
4
+ // read these shapes. Every node carries the span of its source text so
5
+ // errors and renames can point at exact characters.
6
+ export {};
@@ -0,0 +1,23 @@
1
+ import { type ExpressionCheckResult, type ExpressionEnvironment } from "./check.ts";
2
+ import type { Expression } from "./ast.ts";
3
+ import { type ExpressionType } from "./types.ts";
4
+ /** A parsed and checked expression. `expression` is absent after a syntax error. */
5
+ export interface CheckedExpression {
6
+ readonly expression?: Expression;
7
+ readonly result: ExpressionCheckResult;
8
+ }
9
+ /**
10
+ * Keeps parsed and checked expressions per source text for one version of
11
+ * a checking environment. Bump `version` whenever the properties, the
12
+ * variables or the components change; the cache then starts over.
13
+ */
14
+ export declare class ExpressionCache {
15
+ private version;
16
+ private readonly entries;
17
+ /** Parses and checks `source` against the field's type, or returns the cached result for this version. */
18
+ check(source: string, expected: ExpressionType, environment: ExpressionEnvironment, version: string | number): CheckedExpression;
19
+ /** How many expressions are cached for the current version. */
20
+ get size(): number;
21
+ /** Forgets everything. */
22
+ clear(): void;
23
+ }
@@ -0,0 +1,43 @@
1
+ // Parse-and-check caching (docs/components/design.md 8.6, "Parse once").
2
+ // Parsed and checked forms are kept per source text and per version of the
3
+ // checking environment, so a component's expressions are checked once after
4
+ // loading or an edit and read from the cache at every later resolve.
5
+ import { checkExpression } from "./check.js";
6
+ import { parseExpression } from "./parser.js";
7
+ import { formatExpressionType } from "./types.js";
8
+ /**
9
+ * Keeps parsed and checked expressions per source text for one version of
10
+ * a checking environment. Bump `version` whenever the properties, the
11
+ * variables or the components change; the cache then starts over.
12
+ */
13
+ export class ExpressionCache {
14
+ version;
15
+ entries = new Map();
16
+ /** Parses and checks `source` against the field's type, or returns the cached result for this version. */
17
+ check(source, expected, environment, version) {
18
+ if (version !== this.version) {
19
+ this.entries.clear();
20
+ this.version = version;
21
+ }
22
+ const stored = environment.storedType === undefined ? "-" : formatExpressionType(environment.storedType);
23
+ const key = `${formatExpressionType(expected)}\n${stored}\n${source}`;
24
+ let entry = this.entries.get(key);
25
+ if (entry === undefined) {
26
+ const parsed = parseExpression(source);
27
+ entry = parsed.ok
28
+ ? { expression: parsed.expression, result: checkExpression(parsed.expression, expected, environment) }
29
+ : { result: { ok: false, errors: [parsed.error], references: [] } };
30
+ this.entries.set(key, entry);
31
+ }
32
+ return entry;
33
+ }
34
+ /** How many expressions are cached for the current version. */
35
+ get size() {
36
+ return this.entries.size;
37
+ }
38
+ /** Forgets everything. */
39
+ clear() {
40
+ this.entries.clear();
41
+ this.version = undefined;
42
+ }
43
+ }