@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,43 @@
1
+ /** The words the language reserves; they cannot name a property or a `let` unless written in backticks. */
2
+ export declare const EXPRESSION_KEYWORDS: readonly ["if", "then", "else", "let", "in", "match", "with", "or", "and", "not", "true", "false", "stored", "var"];
3
+ export type ExpressionKeyword = (typeof EXPRESSION_KEYWORDS)[number];
4
+ /** The most code points a name may have, after NFC normalization. @since 0.34.0 */
5
+ export declare const EXPRESSION_NAME_MAX_LENGTH = 256;
6
+ /** True when the code point `ch` (one code point, as a string) can start a bare name. @since 0.34.0 */
7
+ export declare function isExpressionNameStart(ch: string): boolean;
8
+ /** True when the code point `ch` can follow the first one of a bare name; never U+200C or U+200D. @since 0.34.0 */
9
+ export declare function isExpressionNamePart(ch: string): boolean;
10
+ /** `name` in the form names are stored and compared in: NFC. @since 0.34.0 */
11
+ export declare function normalizeExpressionName(name: string): string;
12
+ /**
13
+ * Why `name` cannot name a property or an option, or undefined when it can:
14
+ * it is empty, longer than 256 code points after NFC, or holds a control
15
+ * character, a line break or a bidirectional formatting character
16
+ * (U+202A to U+202E, U+2066 to U+2069). Any other name can be written,
17
+ * bare or in backticks.
18
+ *
19
+ * The length limit holds for a name being made or renamed. With
20
+ * `{ stored: true }`, for a name a stored document already holds, it does
21
+ * not: documents before 0.34.0 allowed identifiers of any length, and they
22
+ * still open.
23
+ * @since 0.34.0
24
+ */
25
+ export declare function expressionNameProblem(name: string, options?: {
26
+ readonly stored?: boolean;
27
+ }): string | undefined;
28
+ /**
29
+ * True when `name` can be written bare in an expression: a Unicode
30
+ * identifier (`ID_Start` or `_`, then `ID_Continue`, without U+200C or
31
+ * U+200D) that is not a keyword. Any other valid name is written in
32
+ * backticks (`formatExpressionName`).
33
+ * @since 0.34.0
34
+ */
35
+ export declare function isBareExpressionName(name: string): boolean;
36
+ /**
37
+ * How `name` is written in expression source: bare when it can be
38
+ * (`大`, `size`), and otherwise in backticks with `` ` `` and `\` escaped
39
+ * (`` `Primary / Hover` ``, `` `2x` ``, `` `if` ``). The lexer reads the
40
+ * result back as the same name.
41
+ * @since 0.34.0
42
+ */
43
+ export declare function formatExpressionName(name: string): string;
@@ -0,0 +1,99 @@
1
+ // Names in the expression language: property, option, `let` and field
2
+ // names. A name is written bare when it is a Unicode identifier (UAX #31:
3
+ // `ID_Start` or `_`, then `ID_Continue`, no zero-width joiner or non-joiner)
4
+ // and not a keyword; any other name is written in backticks, where `\``
5
+ // and `\\` are the only escapes. Names are compared after NFC
6
+ // normalization, case-sensitively.
7
+ /** The words the language reserves; they cannot name a property or a `let` unless written in backticks. */
8
+ export const EXPRESSION_KEYWORDS = [
9
+ "if",
10
+ "then",
11
+ "else",
12
+ "let",
13
+ "in",
14
+ "match",
15
+ "with",
16
+ "or",
17
+ "and",
18
+ "not",
19
+ "true",
20
+ "false",
21
+ "stored",
22
+ "var",
23
+ ];
24
+ /** The most code points a name may have, after NFC normalization. @since 0.34.0 */
25
+ export const EXPRESSION_NAME_MAX_LENGTH = 256;
26
+ const KEYWORDS = new Set(EXPRESSION_KEYWORDS);
27
+ const NAME_START = /^[\p{ID_Start}_]$/u;
28
+ const NAME_PART = /^\p{ID_Continue}$/u;
29
+ /** Control characters, line and paragraph separators, and the bidirectional embedding, override and isolate characters. */
30
+ const REFUSED = /[\p{Cc}\u2028\u2029\u202A-\u202E\u2066-\u2069]/u;
31
+ /** True when the code point `ch` (one code point, as a string) can start a bare name. @since 0.34.0 */
32
+ export function isExpressionNameStart(ch) {
33
+ return NAME_START.test(ch);
34
+ }
35
+ /** True when the code point `ch` can follow the first one of a bare name; never U+200C or U+200D. @since 0.34.0 */
36
+ export function isExpressionNamePart(ch) {
37
+ return ch !== "\u200C" && ch !== "\u200D" && NAME_PART.test(ch);
38
+ }
39
+ /** `name` in the form names are stored and compared in: NFC. @since 0.34.0 */
40
+ export function normalizeExpressionName(name) {
41
+ return name.normalize("NFC");
42
+ }
43
+ /**
44
+ * Why `name` cannot name a property or an option, or undefined when it can:
45
+ * it is empty, longer than 256 code points after NFC, or holds a control
46
+ * character, a line break or a bidirectional formatting character
47
+ * (U+202A to U+202E, U+2066 to U+2069). Any other name can be written,
48
+ * bare or in backticks.
49
+ *
50
+ * The length limit holds for a name being made or renamed. With
51
+ * `{ stored: true }`, for a name a stored document already holds, it does
52
+ * not: documents before 0.34.0 allowed identifiers of any length, and they
53
+ * still open.
54
+ * @since 0.34.0
55
+ */
56
+ export function expressionNameProblem(name, options = {}) {
57
+ if (name === "")
58
+ return "A name cannot be empty";
59
+ const length = [...normalizeExpressionName(name)].length;
60
+ if (options.stored !== true && length > EXPRESSION_NAME_MAX_LENGTH) {
61
+ return `A name can be at most ${String(EXPRESSION_NAME_MAX_LENGTH)} characters long; this one has ${String(length)}`;
62
+ }
63
+ const refused = REFUSED.exec(name);
64
+ if (refused !== null) {
65
+ const code = refused[0].codePointAt(0).toString(16).toUpperCase().padStart(4, "0");
66
+ return `A name cannot hold control characters, line breaks or bidirectional formatting characters; this one holds U+${code}`;
67
+ }
68
+ return undefined;
69
+ }
70
+ /**
71
+ * True when `name` can be written bare in an expression: a Unicode
72
+ * identifier (`ID_Start` or `_`, then `ID_Continue`, without U+200C or
73
+ * U+200D) that is not a keyword. Any other valid name is written in
74
+ * backticks (`formatExpressionName`).
75
+ * @since 0.34.0
76
+ */
77
+ export function isBareExpressionName(name) {
78
+ if (KEYWORDS.has(name))
79
+ return false;
80
+ let first = true;
81
+ for (const ch of name) {
82
+ if (first ? !isExpressionNameStart(ch) : !isExpressionNamePart(ch))
83
+ return false;
84
+ first = false;
85
+ }
86
+ return !first;
87
+ }
88
+ /**
89
+ * How `name` is written in expression source: bare when it can be
90
+ * (`大`, `size`), and otherwise in backticks with `` ` `` and `\` escaped
91
+ * (`` `Primary / Hover` ``, `` `2x` ``, `` `if` ``). The lexer reads the
92
+ * result back as the same name.
93
+ * @since 0.34.0
94
+ */
95
+ export function formatExpressionName(name) {
96
+ if (isBareExpressionName(name))
97
+ return name;
98
+ return `\`${name.replace(/[\\`]/g, (ch) => `\\${ch}`)}\``;
99
+ }
@@ -0,0 +1,15 @@
1
+ import type { Expression, ExpressionError } from "./ast.ts";
2
+ /** The longest expression source the parser accepts, in UTF-16 code units. */
3
+ export declare const EXPRESSION_SOURCE_LIMIT = 4000;
4
+ /** The deepest nesting of sub-expressions the parser accepts. */
5
+ export declare const EXPRESSION_DEPTH_LIMIT = 64;
6
+ /** What `parseExpression` gives back: a tree, or the first error. */
7
+ export type ExpressionParseResult = {
8
+ readonly ok: true;
9
+ readonly expression: Expression;
10
+ } | {
11
+ readonly ok: false;
12
+ readonly error: ExpressionError;
13
+ };
14
+ /** Parses source text into an expression tree, or reports the first syntax error. */
15
+ export declare function parseExpression(source: string): ExpressionParseResult;
@@ -0,0 +1,438 @@
1
+ // A recursive descent parser for the grammar in docs/components/design.md 8.4.
2
+ import { ExpressionSyntaxError, tokenizeExpression } from "./lexer.js";
3
+ import { formatExpressionName, isBareExpressionName } from "./names.js";
4
+ /** The longest expression source the parser accepts, in UTF-16 code units. */
5
+ export const EXPRESSION_SOURCE_LIMIT = 4000;
6
+ /** The deepest nesting of sub-expressions the parser accepts. */
7
+ export const EXPRESSION_DEPTH_LIMIT = 64;
8
+ /** Parses source text into an expression tree, or reports the first syntax error. */
9
+ export function parseExpression(source) {
10
+ if (source.length > EXPRESSION_SOURCE_LIMIT) {
11
+ return {
12
+ ok: false,
13
+ error: {
14
+ message: `The expression is ${source.length} characters long; the limit is ${EXPRESSION_SOURCE_LIMIT}.`,
15
+ span: { start: EXPRESSION_SOURCE_LIMIT, end: source.length },
16
+ },
17
+ };
18
+ }
19
+ try {
20
+ const parser = new Parser(tokenizeExpression(source));
21
+ const expression = parser.parseExpression();
22
+ parser.expectEnd();
23
+ const tooDeep = firstTooDeep(expression);
24
+ if (tooDeep !== undefined) {
25
+ return { ok: false, error: { message: `The expression nests deeper than ${EXPRESSION_DEPTH_LIMIT} levels.`, span: tooDeep.span } };
26
+ }
27
+ return { ok: true, expression };
28
+ }
29
+ catch (e) {
30
+ if (e instanceof ExpressionSyntaxError)
31
+ return { ok: false, error: e.error };
32
+ throw e;
33
+ }
34
+ }
35
+ function span(start, end) {
36
+ return { start: start.start, end: end.end };
37
+ }
38
+ function describe(token) {
39
+ switch (token.kind) {
40
+ case "end":
41
+ return "the end of the expression";
42
+ case "string":
43
+ return "a string";
44
+ case "number":
45
+ return "a number";
46
+ case "color":
47
+ return "a colour";
48
+ case "name":
49
+ return isBareExpressionName(token.value) ? `\`${token.value}\`` : `the name ${formatExpressionName(token.value)}`;
50
+ default:
51
+ return `\`${token.value}\``;
52
+ }
53
+ }
54
+ /** The direct sub-expressions of a node. */
55
+ function children(expression) {
56
+ switch (expression.kind) {
57
+ case "call":
58
+ return expression.args.map((a) => a.value);
59
+ case "list":
60
+ return expression.items;
61
+ case "field":
62
+ return [expression.target];
63
+ case "with":
64
+ return [expression.target, ...expression.updates.map((u) => u.value)];
65
+ case "unary":
66
+ return [expression.operand];
67
+ case "binary":
68
+ return [expression.left, expression.right];
69
+ case "if":
70
+ return [expression.condition, expression.then, expression.else];
71
+ case "let":
72
+ return [expression.value, expression.body];
73
+ case "match":
74
+ return [expression.subject, ...expression.arms.map((arm) => arm.body), ...(expression.otherwise === undefined ? [] : [expression.otherwise])];
75
+ default:
76
+ return [];
77
+ }
78
+ }
79
+ /**
80
+ * The first node below the depth limit, found without recursion. Operator and
81
+ * postfix chains (`1 + 1 + ...`, `a.b.c...`) build deep trees in a loop that
82
+ * `nested` does not see, and the checker walks trees recursively.
83
+ */
84
+ function firstTooDeep(root) {
85
+ const stack = [[root, 1]];
86
+ for (let entry = stack.pop(); entry !== undefined; entry = stack.pop()) {
87
+ const [expression, depth] = entry;
88
+ if (depth > EXPRESSION_DEPTH_LIMIT)
89
+ return expression;
90
+ const next = children(expression);
91
+ for (let i = next.length - 1; i >= 0; i -= 1)
92
+ stack.push([next[i], depth + 1]);
93
+ }
94
+ return undefined;
95
+ }
96
+ class Parser {
97
+ index = 0;
98
+ depth = 0;
99
+ tokens;
100
+ constructor(tokens) {
101
+ this.tokens = tokens;
102
+ }
103
+ get token() {
104
+ return this.tokens[this.index];
105
+ }
106
+ fail(message, at = this.token.span, hint) {
107
+ throw new ExpressionSyntaxError(hint === undefined ? { message, span: at } : { message, span: at, hint });
108
+ }
109
+ advance() {
110
+ const token = this.token;
111
+ if (token.kind !== "end")
112
+ this.index += 1;
113
+ return token;
114
+ }
115
+ isPunctuation(value) {
116
+ return this.token.kind === "punctuation" && this.token.value === value;
117
+ }
118
+ isKeyword(value) {
119
+ return this.token.kind === "keyword" && this.token.value === value;
120
+ }
121
+ takePunctuation(value) {
122
+ if (!this.isPunctuation(value))
123
+ this.fail(`Expected \`${value}\` but found ${describe(this.token)}.`);
124
+ return this.advance().span;
125
+ }
126
+ takeKeyword(value) {
127
+ if (!this.isKeyword(value))
128
+ this.fail(`Expected \`${value}\` but found ${describe(this.token)}.`);
129
+ return this.advance().span;
130
+ }
131
+ /** Takes a name; `before` is what comes right before it (`.` for an option or a field), for the hint. */
132
+ takeName(what, before = "") {
133
+ const token = this.token;
134
+ if (token.kind !== "name") {
135
+ if (token.kind === "keyword") {
136
+ this.fail(`\`${token.value}\` is a keyword and cannot be ${what}.`, token.span, `A name spelled like a keyword is written in backticks, as \`\` ${before}${formatExpressionName(token.value)} \`\`.`);
137
+ }
138
+ this.fail(`Expected ${what} but found ${describe(token)}.`);
139
+ }
140
+ this.advance();
141
+ return { name: token.value, span: token.span };
142
+ }
143
+ expectEnd() {
144
+ if (this.token.kind !== "end")
145
+ this.fail(`Unexpected ${describe(this.token)} after the expression.`);
146
+ }
147
+ /** Runs one nesting level of the grammar, counting it against the depth limit. */
148
+ nested(parse) {
149
+ this.depth += 1;
150
+ if (this.depth > EXPRESSION_DEPTH_LIMIT) {
151
+ this.fail(`The expression nests deeper than ${EXPRESSION_DEPTH_LIMIT} levels.`);
152
+ }
153
+ try {
154
+ return parse();
155
+ }
156
+ finally {
157
+ this.depth -= 1;
158
+ }
159
+ }
160
+ parseExpression() {
161
+ return this.nested(() => {
162
+ if (this.isKeyword("if"))
163
+ return this.parseIf();
164
+ if (this.isKeyword("let"))
165
+ return this.parseLet();
166
+ if (this.isKeyword("match"))
167
+ return this.parseMatch();
168
+ return this.parseBinary(0);
169
+ });
170
+ }
171
+ parseIf() {
172
+ const start = this.takeKeyword("if");
173
+ const condition = this.parseExpression();
174
+ this.takeKeyword("then");
175
+ const then = this.parseExpression();
176
+ this.takeKeyword("else");
177
+ const otherwise = this.parseExpression();
178
+ return { kind: "if", condition, then, else: otherwise, span: span(start, otherwise.span) };
179
+ }
180
+ parseLet() {
181
+ const start = this.takeKeyword("let");
182
+ const { name, span: nameSpan } = this.takeName("a name after `let`");
183
+ this.takePunctuation("=");
184
+ const value = this.parseExpression();
185
+ this.takeKeyword("in");
186
+ const body = this.parseExpression();
187
+ return { kind: "let", name, nameSpan, value, body, span: span(start, body.span) };
188
+ }
189
+ parsePattern() {
190
+ if (this.isPunctuation(".")) {
191
+ const dot = this.advance().span;
192
+ const { name, span: nameSpan } = this.takeName("an option name after `.`", ".");
193
+ return { kind: "option", name, span: span(dot, nameSpan) };
194
+ }
195
+ if (this.isKeyword("true") || this.isKeyword("false")) {
196
+ const token = this.advance();
197
+ return { kind: "boolean", value: token.kind === "keyword" && token.value === "true", span: token.span };
198
+ }
199
+ this.fail(`Expected a pattern such as \`.small\` or \`true\` but found ${describe(this.token)}.`);
200
+ }
201
+ parseMatch() {
202
+ const start = this.takeKeyword("match");
203
+ const subject = this.parseExpression();
204
+ this.takePunctuation("{");
205
+ const arms = [];
206
+ let otherwise;
207
+ for (;;) {
208
+ if (this.isPunctuation("}"))
209
+ break;
210
+ if (this.isKeyword("else")) {
211
+ this.advance();
212
+ this.takePunctuation("=>");
213
+ otherwise = this.parseExpression();
214
+ if (this.isPunctuation(","))
215
+ this.advance();
216
+ if (!this.isPunctuation("}"))
217
+ this.fail("`else =>` must be the last arm of a `match`.");
218
+ break;
219
+ }
220
+ const patterns = [this.parsePattern()];
221
+ while (this.isPunctuation(",")) {
222
+ this.advance();
223
+ patterns.push(this.parsePattern());
224
+ }
225
+ this.takePunctuation("=>");
226
+ const body = this.parseExpression();
227
+ arms.push({ patterns, body });
228
+ if (this.isPunctuation(",")) {
229
+ this.advance();
230
+ continue;
231
+ }
232
+ if (!this.isPunctuation("}"))
233
+ this.fail(`Expected \`,\` or \`}\` after a match arm but found ${describe(this.token)}.`);
234
+ }
235
+ const close = this.takePunctuation("}");
236
+ if (arms.length === 0 && otherwise === undefined) {
237
+ this.fail("A `match` needs at least one arm.", span(start, close));
238
+ }
239
+ const node = { kind: "match", subject, arms, span: span(start, close) };
240
+ return otherwise === undefined ? node : { ...node, otherwise };
241
+ }
242
+ static LEVELS = [
243
+ ["or"],
244
+ ["and"],
245
+ // `not` sits between `and` and the comparisons; parseBinary handles it at this level.
246
+ [],
247
+ ["==", "!=", "<", "<=", ">", ">="],
248
+ ["++"],
249
+ ["+", "-"],
250
+ ["*", "/"],
251
+ ];
252
+ operatorAt(level) {
253
+ const token = this.token;
254
+ if (token.kind !== "punctuation" && token.kind !== "keyword")
255
+ return undefined;
256
+ const ops = Parser.LEVELS[level];
257
+ return ops.find((op) => op === token.value);
258
+ }
259
+ parseBinary(level) {
260
+ if (level >= Parser.LEVELS.length)
261
+ return this.parseUnary();
262
+ if (level === 2)
263
+ return this.parseNot();
264
+ let left = this.parseBinary(level + 1);
265
+ const isComparison = level === 3;
266
+ for (;;) {
267
+ const operator = this.operatorAt(level);
268
+ if (operator === undefined)
269
+ break;
270
+ const operatorSpan = this.advance().span;
271
+ const right = this.parseBinary(level + 1);
272
+ left = { kind: "binary", operator, operatorSpan, left, right, span: span(left.span, right.span) };
273
+ if (isComparison) {
274
+ const next = this.operatorAt(level);
275
+ if (next !== undefined) {
276
+ this.fail(`\`${next}\` cannot follow \`${operator}\` without parentheses.`, this.token.span, "Compare two values at a time, joined with `and`.");
277
+ }
278
+ break;
279
+ }
280
+ }
281
+ return left;
282
+ }
283
+ parseNot() {
284
+ if (this.isKeyword("not")) {
285
+ const start = this.advance().span;
286
+ const operand = this.nested(() => this.parseNot());
287
+ return { kind: "unary", operator: "not", operand, span: span(start, operand.span) };
288
+ }
289
+ return this.parseBinary(3);
290
+ }
291
+ parseUnary() {
292
+ if (this.isPunctuation("-")) {
293
+ const start = this.advance().span;
294
+ const operand = this.nested(() => this.parseUnary());
295
+ return { kind: "unary", operator: "-", operand, span: span(start, operand.span) };
296
+ }
297
+ return this.parsePostfix();
298
+ }
299
+ parsePostfix() {
300
+ let target = this.parseAtom();
301
+ for (;;) {
302
+ if (this.isPunctuation(".")) {
303
+ this.advance();
304
+ const { name, span: nameSpan } = this.takeName("a field name after `.`", ".");
305
+ target = { kind: "field", target, name, nameSpan, span: span(target.span, nameSpan) };
306
+ continue;
307
+ }
308
+ if (this.isKeyword("with")) {
309
+ this.advance();
310
+ this.takePunctuation("{");
311
+ const updates = [];
312
+ while (!this.isPunctuation("}")) {
313
+ const { name, span: nameSpan } = this.takeName("a field name");
314
+ this.takePunctuation(":");
315
+ const value = this.parseExpression();
316
+ updates.push({ name, nameSpan, value });
317
+ if (this.isPunctuation(","))
318
+ this.advance();
319
+ else if (!this.isPunctuation("}"))
320
+ this.fail(`Expected \`,\` or \`}\` but found ${describe(this.token)}.`);
321
+ }
322
+ const close = this.takePunctuation("}");
323
+ if (updates.length === 0)
324
+ this.fail("`with { }` changes no field.", span(target.span, close));
325
+ target = { kind: "with", target, updates, span: span(target.span, close) };
326
+ continue;
327
+ }
328
+ return target;
329
+ }
330
+ }
331
+ parseArguments() {
332
+ const args = [];
333
+ let sawNamed = false;
334
+ while (!this.isPunctuation(")")) {
335
+ const next = this.tokens[this.index + 1];
336
+ if (this.token.kind === "name" && next?.kind === "punctuation" && next.value === ":") {
337
+ const { name, span: nameSpan } = this.takeName("an argument name");
338
+ this.advance();
339
+ const value = this.parseExpression();
340
+ args.push({ name, nameSpan, value });
341
+ sawNamed = true;
342
+ }
343
+ else {
344
+ const value = this.parseExpression();
345
+ if (sawNamed)
346
+ this.fail("Positional arguments must come before named ones.", value.span);
347
+ args.push({ value });
348
+ }
349
+ if (this.isPunctuation(","))
350
+ this.advance();
351
+ else if (!this.isPunctuation(")"))
352
+ this.fail(`Expected \`,\` or \`)\` but found ${describe(this.token)}.`);
353
+ }
354
+ return args;
355
+ }
356
+ parseAtom() {
357
+ const token = this.token;
358
+ switch (token.kind) {
359
+ case "number": {
360
+ this.advance();
361
+ return token.unit === undefined
362
+ ? { kind: "number", value: token.value, span: token.span }
363
+ : { kind: "number", value: token.value, unit: token.unit, span: token.span };
364
+ }
365
+ case "string":
366
+ this.advance();
367
+ return { kind: "string", value: token.value, span: token.span };
368
+ case "color":
369
+ this.advance();
370
+ return { kind: "color", value: token.value, span: token.span };
371
+ case "name": {
372
+ this.advance();
373
+ if (this.isPunctuation("(")) {
374
+ this.advance();
375
+ const args = this.parseArguments();
376
+ const close = this.takePunctuation(")");
377
+ return { kind: "call", callee: token.value, calleeSpan: token.span, args, span: span(token.span, close) };
378
+ }
379
+ return { kind: "name", name: token.value, span: token.span };
380
+ }
381
+ case "keyword":
382
+ switch (token.value) {
383
+ case "true":
384
+ case "false":
385
+ this.advance();
386
+ return { kind: "boolean", value: token.value === "true", span: token.span };
387
+ case "stored":
388
+ this.advance();
389
+ return { kind: "stored", span: token.span };
390
+ case "var": {
391
+ this.advance();
392
+ this.takePunctuation("(");
393
+ const name = this.token;
394
+ if (name.kind !== "string")
395
+ this.fail(`\`var\` takes a string such as \`"Brand/Primary"\` but found ${describe(name)}.`);
396
+ this.advance();
397
+ const close = this.takePunctuation(")");
398
+ return { kind: "variable", name: name.value, nameSpan: name.span, span: span(token.span, close) };
399
+ }
400
+ default:
401
+ this.fail(`Unexpected \`${token.value}\`.`);
402
+ }
403
+ // falls through only after fail(), which throws
404
+ case "punctuation":
405
+ switch (token.value) {
406
+ case ".": {
407
+ this.advance();
408
+ const { name, span: nameSpan } = this.takeName("an option name after `.`", ".");
409
+ return { kind: "option", name, span: span(token.span, nameSpan) };
410
+ }
411
+ case "(": {
412
+ this.advance();
413
+ const inner = this.parseExpression();
414
+ this.takePunctuation(")");
415
+ return inner;
416
+ }
417
+ case "[": {
418
+ this.advance();
419
+ const items = [];
420
+ while (!this.isPunctuation("]")) {
421
+ items.push(this.parseExpression());
422
+ if (this.isPunctuation(","))
423
+ this.advance();
424
+ else if (!this.isPunctuation("]"))
425
+ this.fail(`Expected \`,\` or \`]\` but found ${describe(this.token)}.`);
426
+ }
427
+ const close = this.takePunctuation("]");
428
+ return { kind: "list", items, span: span(token.span, close) };
429
+ }
430
+ default:
431
+ this.fail(`Unexpected \`${token.value}\`.`);
432
+ }
433
+ // falls through only after fail(), which throws
434
+ case "end":
435
+ this.fail("The expression ends too early.");
436
+ }
437
+ }
438
+ }
@@ -0,0 +1,34 @@
1
+ import { type ExpressionEnvironment } from "./check.ts";
2
+ import type { ExpressionType } from "./types.ts";
3
+ /** What to rename. Names are the ones in force before the rename. */
4
+ export type ExpressionRename = {
5
+ readonly kind: "property";
6
+ readonly from: string;
7
+ readonly to: string;
8
+ } | {
9
+ readonly kind: "option";
10
+ readonly enum: string;
11
+ readonly from: string;
12
+ readonly to: string;
13
+ } | {
14
+ readonly kind: "variable";
15
+ readonly from: string;
16
+ readonly to: string;
17
+ };
18
+ /** The rewritten source and how many references changed. */
19
+ export interface ExpressionRenameResult {
20
+ readonly source: string;
21
+ readonly count: number;
22
+ }
23
+ /**
24
+ * Rewrites every reference to the renamed properties, options or variables
25
+ * in the source text. The environment and expected type describe the field
26
+ * as it is before the rename, so `.large` can be told apart between enums.
27
+ * Several renames are found in one check of the original source, so a
28
+ * reference half-way through a batch never meets names the environment
29
+ * does not know yet. Source that does not parse is returned unchanged; type
30
+ * errors elsewhere in the expression do not stop the references that do
31
+ * resolve from being rewritten. A `let` name that shadows a property is not
32
+ * a reference to it.
33
+ */
34
+ export declare function renameInExpression(source: string, expected: ExpressionType, environment: ExpressionEnvironment, renames: ExpressionRename | readonly ExpressionRename[]): ExpressionRenameResult;
@@ -0,0 +1,46 @@
1
+ // The rename rewrite (docs/components/design.md 8.5): renaming a property,
2
+ // an enum option or a variable replaces only those references' characters
3
+ // in the source text and leaves everything else, spacing included, alone.
4
+ import { checkExpression } from "./check.js";
5
+ import { formatExpressionName } from "./names.js";
6
+ import { parseExpression } from "./parser.js";
7
+ function matches(reference, rename) {
8
+ if (reference.kind !== rename.kind || reference.name !== rename.from)
9
+ return false;
10
+ return rename.kind !== "option" || reference.enum === rename.enum;
11
+ }
12
+ function stringLiteral(value) {
13
+ return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n")}"`;
14
+ }
15
+ /**
16
+ * Rewrites every reference to the renamed properties, options or variables
17
+ * in the source text. The environment and expected type describe the field
18
+ * as it is before the rename, so `.large` can be told apart between enums.
19
+ * Several renames are found in one check of the original source, so a
20
+ * reference half-way through a batch never meets names the environment
21
+ * does not know yet. Source that does not parse is returned unchanged; type
22
+ * errors elsewhere in the expression do not stop the references that do
23
+ * resolve from being rewritten. A `let` name that shadows a property is not
24
+ * a reference to it.
25
+ */
26
+ export function renameInExpression(source, expected, environment, renames) {
27
+ const list = Array.isArray(renames) ? renames : [renames];
28
+ const parsed = parseExpression(source);
29
+ if (!parsed.ok)
30
+ return { source, count: 0 };
31
+ const { references } = checkExpression(parsed.expression, expected, environment);
32
+ const targets = [];
33
+ for (const reference of references) {
34
+ const rename = list.find((candidate) => matches(reference, candidate));
35
+ if (rename === undefined)
36
+ continue;
37
+ const replacement = rename.kind === "option" ? `.${formatExpressionName(rename.to)}` : rename.kind === "variable" ? stringLiteral(rename.to) : formatExpressionName(rename.to);
38
+ targets.push({ reference, replacement });
39
+ }
40
+ targets.sort((a, b) => b.reference.span.start - a.reference.span.start);
41
+ let result = source;
42
+ for (const { reference, replacement } of targets) {
43
+ result = result.slice(0, reference.span.start) + replacement + result.slice(reference.span.end);
44
+ }
45
+ return { source: result, count: targets.length };
46
+ }