tsc-ts 0.0.4 → 0.0.5

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 (584) hide show
  1. package/README.md +33 -1
  2. package/dist/api/compiler/ast-utilities.d.ts +82 -0
  3. package/dist/api/compiler/ast-utilities.js +462 -0
  4. package/dist/api/compiler/ast.d.ts +2350 -0
  5. package/dist/api/compiler/ast.js +11206 -0
  6. package/dist/api/compiler/astnav.d.ts +37 -0
  7. package/dist/api/compiler/astnav.js +767 -0
  8. package/dist/api/compiler/binder-utilities.d.ts +182 -0
  9. package/dist/api/compiler/binder-utilities.js +1240 -0
  10. package/dist/api/compiler/binder.d.ts +33 -0
  11. package/dist/api/compiler/binder.js +2358 -0
  12. package/dist/api/compiler/character-codes.d.ts +161 -0
  13. package/dist/api/compiler/character-codes.js +160 -0
  14. package/dist/api/compiler/checker-calls.d.ts +46 -0
  15. package/dist/api/compiler/checker-calls.js +1640 -0
  16. package/dist/api/compiler/checker-classes.d.ts +30 -0
  17. package/dist/api/compiler/checker-classes.js +582 -0
  18. package/dist/api/compiler/checker-contextual.d.ts +77 -0
  19. package/dist/api/compiler/checker-contextual.js +1136 -0
  20. package/dist/api/compiler/checker-declaration-order.d.ts +12 -0
  21. package/dist/api/compiler/checker-declaration-order.js +356 -0
  22. package/dist/api/compiler/checker-declarations.d.ts +33 -0
  23. package/dist/api/compiler/checker-declarations.js +888 -0
  24. package/dist/api/compiler/checker-declared-types.d.ts +48 -0
  25. package/dist/api/compiler/checker-declared-types.js +787 -0
  26. package/dist/api/compiler/checker-decorators.d.ts +26 -0
  27. package/dist/api/compiler/checker-decorators.js +562 -0
  28. package/dist/api/compiler/checker-deprecation.d.ts +33 -0
  29. package/dist/api/compiler/checker-deprecation.js +315 -0
  30. package/dist/api/compiler/checker-destructuring.d.ts +34 -0
  31. package/dist/api/compiler/checker-destructuring.js +584 -0
  32. package/dist/api/compiler/checker-elaboration.d.ts +15 -0
  33. package/dist/api/compiler/checker-elaboration.js +290 -0
  34. package/dist/api/compiler/checker-emit-helpers.d.ts +58 -0
  35. package/dist/api/compiler/checker-emit-helpers.js +154 -0
  36. package/dist/api/compiler/checker-emit-resolver.d.ts +75 -0
  37. package/dist/api/compiler/checker-emit-resolver.js +579 -0
  38. package/dist/api/compiler/checker-evaluator.d.ts +27 -0
  39. package/dist/api/compiler/checker-evaluator.js +188 -0
  40. package/dist/api/compiler/checker-expressions.d.ts +67 -0
  41. package/dist/api/compiler/checker-expressions.js +1351 -0
  42. package/dist/api/compiler/checker-features.d.ts +6 -0
  43. package/dist/api/compiler/checker-features.js +354 -0
  44. package/dist/api/compiler/checker-flow.d.ts +86 -0
  45. package/dist/api/compiler/checker-flow.js +169 -0
  46. package/dist/api/compiler/checker-functions.d.ts +43 -0
  47. package/dist/api/compiler/checker-functions.js +1002 -0
  48. package/dist/api/compiler/checker-generated-code.d.ts +25 -0
  49. package/dist/api/compiler/checker-generated-code.js +168 -0
  50. package/dist/api/compiler/checker-globals.d.ts +14 -0
  51. package/dist/api/compiler/checker-globals.js +37 -0
  52. package/dist/api/compiler/checker-grammar.d.ts +35 -0
  53. package/dist/api/compiler/checker-grammar.js +2573 -0
  54. package/dist/api/compiler/checker-heritage.d.ts +28 -0
  55. package/dist/api/compiler/checker-heritage.js +900 -0
  56. package/dist/api/compiler/checker-inference.d.ts +179 -0
  57. package/dist/api/compiler/checker-inference.js +2105 -0
  58. package/dist/api/compiler/checker-iteration.d.ts +115 -0
  59. package/dist/api/compiler/checker-iteration.js +857 -0
  60. package/dist/api/compiler/checker-javascript.d.ts +42 -0
  61. package/dist/api/compiler/checker-javascript.js +441 -0
  62. package/dist/api/compiler/checker-jsdoc.d.ts +30 -0
  63. package/dist/api/compiler/checker-jsdoc.js +320 -0
  64. package/dist/api/compiler/checker-jsx.d.ts +94 -0
  65. package/dist/api/compiler/checker-jsx.js +1433 -0
  66. package/dist/api/compiler/checker-literals.d.ts +6 -0
  67. package/dist/api/compiler/checker-literals.js +43 -0
  68. package/dist/api/compiler/checker-members.d.ts +198 -0
  69. package/dist/api/compiler/checker-members.js +2126 -0
  70. package/dist/api/compiler/checker-modules.d.ts +85 -0
  71. package/dist/api/compiler/checker-modules.js +2141 -0
  72. package/dist/api/compiler/checker-narrowing.d.ts +120 -0
  73. package/dist/api/compiler/checker-narrowing.js +2797 -0
  74. package/dist/api/compiler/checker-object-literals.d.ts +14 -0
  75. package/dist/api/compiler/checker-object-literals.js +479 -0
  76. package/dist/api/compiler/checker-operators.d.ts +33 -0
  77. package/dist/api/compiler/checker-operators.js +1040 -0
  78. package/dist/api/compiler/checker-overloads.d.ts +7 -0
  79. package/dist/api/compiler/checker-overloads.js +270 -0
  80. package/dist/api/compiler/checker-property-access.d.ts +34 -0
  81. package/dist/api/compiler/checker-property-access.js +672 -0
  82. package/dist/api/compiler/checker-services.d.ts +49 -0
  83. package/dist/api/compiler/checker-services.js +995 -0
  84. package/dist/api/compiler/checker-signatures.d.ts +55 -0
  85. package/dist/api/compiler/checker-signatures.js +788 -0
  86. package/dist/api/compiler/checker-source-file.d.ts +27 -0
  87. package/dist/api/compiler/checker-source-file.js +619 -0
  88. package/dist/api/compiler/checker-statements.d.ts +18 -0
  89. package/dist/api/compiler/checker-statements.js +226 -0
  90. package/dist/api/compiler/checker-super.d.ts +13 -0
  91. package/dist/api/compiler/checker-super.js +241 -0
  92. package/dist/api/compiler/checker-symbol-types.d.ts +67 -0
  93. package/dist/api/compiler/checker-symbol-types.js +1885 -0
  94. package/dist/api/compiler/checker-symbols.d.ts +181 -0
  95. package/dist/api/compiler/checker-symbols.js +1782 -0
  96. package/dist/api/compiler/checker-type-factory.d.ts +142 -0
  97. package/dist/api/compiler/checker-type-factory.js +2501 -0
  98. package/dist/api/compiler/checker-type-nodes.d.ts +26 -0
  99. package/dist/api/compiler/checker-type-nodes.js +828 -0
  100. package/dist/api/compiler/checker-type-operators.d.ts +109 -0
  101. package/dist/api/compiler/checker-type-operators.js +1842 -0
  102. package/dist/api/compiler/checker-types.d.ts +1027 -0
  103. package/dist/api/compiler/checker-types.js +1697 -0
  104. package/dist/api/compiler/checker-unused.d.ts +19 -0
  105. package/dist/api/compiler/checker-unused.js +524 -0
  106. package/dist/api/compiler/checker-utilities.d.ts +33 -0
  107. package/dist/api/compiler/checker-utilities.js +141 -0
  108. package/dist/api/compiler/checker.d.ts +491 -0
  109. package/dist/api/compiler/checker.js +912 -0
  110. package/dist/api/compiler/command-line.d.ts +32 -0
  111. package/dist/api/compiler/command-line.js +383 -0
  112. package/dist/api/compiler/compile-program.d.ts +118 -0
  113. package/dist/api/compiler/compile-program.js +384 -0
  114. package/dist/api/compiler/diagnostic-locales.d.ts +22 -0
  115. package/dist/api/compiler/diagnostic-locales.js +64 -0
  116. package/dist/api/compiler/diagnostic-messages.d.ts +2166 -0
  117. package/dist/api/compiler/diagnostic-messages.js +184 -0
  118. package/dist/api/compiler/diagnostics.d.ts +167 -0
  119. package/dist/api/compiler/diagnostics.js +820 -0
  120. package/dist/api/compiler/emitter.d.ts +71 -0
  121. package/dist/api/compiler/emitter.js +327 -0
  122. package/dist/api/compiler/file-include.d.ts +117 -0
  123. package/dist/api/compiler/file-include.js +536 -0
  124. package/dist/api/compiler/file-loader.d.ts +261 -0
  125. package/dist/api/compiler/file-loader.js +1257 -0
  126. package/dist/api/compiler/file-system.d.ts +51 -0
  127. package/dist/api/compiler/file-system.js +132 -0
  128. package/dist/api/compiler/flow.d.ts +60 -0
  129. package/dist/api/compiler/flow.js +45 -0
  130. package/dist/api/compiler/instantiation.d.ts +74 -0
  131. package/dist/api/compiler/instantiation.js +799 -0
  132. package/dist/api/compiler/jsdoc.d.ts +75 -0
  133. package/dist/api/compiler/jsdoc.js +1229 -0
  134. package/dist/api/compiler/link-tables.d.ts +36 -0
  135. package/dist/api/compiler/link-tables.js +83 -0
  136. package/dist/api/compiler/locale-data.d.ts +24 -0
  137. package/dist/api/compiler/locale-data.js +183 -0
  138. package/dist/api/compiler/locale.d.ts +4 -0
  139. package/dist/api/compiler/locale.js +317 -0
  140. package/dist/api/compiler/locales/cs-CZ.d.ts +2 -0
  141. package/dist/api/compiler/locales/cs-CZ.js +3 -0
  142. package/dist/api/compiler/locales/de-DE.d.ts +2 -0
  143. package/dist/api/compiler/locales/de-DE.js +3 -0
  144. package/dist/api/compiler/locales/es-ES.d.ts +2 -0
  145. package/dist/api/compiler/locales/es-ES.js +3 -0
  146. package/dist/api/compiler/locales/fr-FR.d.ts +2 -0
  147. package/dist/api/compiler/locales/fr-FR.js +3 -0
  148. package/dist/api/compiler/locales/it-IT.d.ts +2 -0
  149. package/dist/api/compiler/locales/it-IT.js +3 -0
  150. package/dist/api/compiler/locales/ja-JP.d.ts +2 -0
  151. package/dist/api/compiler/locales/ja-JP.js +3 -0
  152. package/dist/api/compiler/locales/ko-KR.d.ts +2 -0
  153. package/dist/api/compiler/locales/ko-KR.js +3 -0
  154. package/dist/api/compiler/locales/pl-PL.d.ts +2 -0
  155. package/dist/api/compiler/locales/pl-PL.js +3 -0
  156. package/dist/api/compiler/locales/pt-BR.d.ts +2 -0
  157. package/dist/api/compiler/locales/pt-BR.js +3 -0
  158. package/dist/api/compiler/locales/ru-RU.d.ts +2 -0
  159. package/dist/api/compiler/locales/ru-RU.js +3 -0
  160. package/dist/api/compiler/locales/tr-TR.d.ts +2 -0
  161. package/dist/api/compiler/locales/tr-TR.js +3 -0
  162. package/dist/api/compiler/locales/zh-CN.d.ts +2 -0
  163. package/dist/api/compiler/locales/zh-CN.js +3 -0
  164. package/dist/api/compiler/locales/zh-TW.d.ts +2 -0
  165. package/dist/api/compiler/locales/zh-TW.js +3 -0
  166. package/dist/api/compiler/module-resolution-types.d.ts +83 -0
  167. package/dist/api/compiler/module-resolution-types.js +176 -0
  168. package/dist/api/compiler/module-resolution.d.ts +71 -0
  169. package/dist/api/compiler/module-resolution.js +1598 -0
  170. package/dist/api/compiler/module-specifiers.d.ts +64 -0
  171. package/dist/api/compiler/module-specifiers.js +1101 -0
  172. package/dist/api/compiler/name-resolver.d.ts +43 -0
  173. package/dist/api/compiler/name-resolver.js +509 -0
  174. package/dist/api/compiler/node-builder-names.d.ts +32 -0
  175. package/dist/api/compiler/node-builder-names.js +854 -0
  176. package/dist/api/compiler/node-builder-pseudo.d.ts +36 -0
  177. package/dist/api/compiler/node-builder-pseudo.js +744 -0
  178. package/dist/api/compiler/node-builder-reuse.d.ts +4 -0
  179. package/dist/api/compiler/node-builder-reuse.js +647 -0
  180. package/dist/api/compiler/node-builder.d.ts +270 -0
  181. package/dist/api/compiler/node-builder.js +2645 -0
  182. package/dist/api/compiler/option-declarations.d.ts +90 -0
  183. package/dist/api/compiler/option-declarations.js +1460 -0
  184. package/dist/api/compiler/options.d.ts +289 -0
  185. package/dist/api/compiler/options.js +1197 -0
  186. package/dist/api/compiler/output-paths.d.ts +42 -0
  187. package/dist/api/compiler/output-paths.js +195 -0
  188. package/dist/api/compiler/package-json.d.ts +175 -0
  189. package/dist/api/compiler/package-json.js +1015 -0
  190. package/dist/api/compiler/parallel-parse.d.ts +219 -0
  191. package/dist/api/compiler/parallel-parse.js +537 -0
  192. package/dist/api/compiler/parser.d.ts +587 -0
  193. package/dist/api/compiler/parser.js +6407 -0
  194. package/dist/api/compiler/path.d.ts +91 -0
  195. package/dist/api/compiler/path.js +705 -0
  196. package/dist/api/compiler/printer/async-helpers.d.ts +22 -0
  197. package/dist/api/compiler/printer/async-helpers.js +91 -0
  198. package/dist/api/compiler/printer/auto-generate.d.ts +13 -0
  199. package/dist/api/compiler/printer/auto-generate.js +20 -0
  200. package/dist/api/compiler/printer/decorator-helpers.d.ts +24 -0
  201. package/dist/api/compiler/printer/decorator-helpers.js +118 -0
  202. package/dist/api/compiler/printer/emit-context.d.ts +154 -0
  203. package/dist/api/compiler/printer/emit-context.js +576 -0
  204. package/dist/api/compiler/printer/emit-flags.d.ts +60 -0
  205. package/dist/api/compiler/printer/emit-flags.js +2 -0
  206. package/dist/api/compiler/printer/factory-base.d.ts +171 -0
  207. package/dist/api/compiler/printer/factory-base.js +1505 -0
  208. package/dist/api/compiler/printer/factory.d.ts +147 -0
  209. package/dist/api/compiler/printer/factory.js +542 -0
  210. package/dist/api/compiler/printer/helpers.d.ts +27 -0
  211. package/dist/api/compiler/printer/helpers.js +164 -0
  212. package/dist/api/compiler/printer/name-generator.d.ts +41 -0
  213. package/dist/api/compiler/printer/name-generator.js +283 -0
  214. package/dist/api/compiler/printer/printer.d.ts +411 -0
  215. package/dist/api/compiler/printer/printer.js +4517 -0
  216. package/dist/api/compiler/printer/text-writer.d.ts +46 -0
  217. package/dist/api/compiler/printer/text-writer.js +172 -0
  218. package/dist/api/compiler/printer/utilities.d.ts +82 -0
  219. package/dist/api/compiler/printer/utilities.js +817 -0
  220. package/dist/api/compiler/program-checker.d.ts +156 -0
  221. package/dist/api/compiler/program-checker.js +526 -0
  222. package/dist/api/compiler/program-utilities.d.ts +52 -0
  223. package/dist/api/compiler/program-utilities.js +365 -0
  224. package/dist/api/compiler/program.d.ts +180 -0
  225. package/dist/api/compiler/program.js +1011 -0
  226. package/dist/api/compiler/pseudo-bigint.d.ts +5 -0
  227. package/dist/api/compiler/pseudo-bigint.js +61 -0
  228. package/dist/api/compiler/regexp-checker.d.ts +38 -0
  229. package/dist/api/compiler/regexp-checker.js +942 -0
  230. package/dist/api/compiler/relations.d.ts +263 -0
  231. package/dist/api/compiler/relations.js +4002 -0
  232. package/dist/api/compiler/reparser.d.ts +48 -0
  233. package/dist/api/compiler/reparser.js +1323 -0
  234. package/dist/api/compiler/scanner.d.ts +256 -0
  235. package/dist/api/compiler/scanner.js +2853 -0
  236. package/dist/api/compiler/side-state.d.ts +16 -0
  237. package/dist/api/compiler/side-state.js +89 -0
  238. package/dist/api/compiler/sourcemap.d.ts +55 -0
  239. package/dist/api/compiler/sourcemap.js +340 -0
  240. package/dist/api/compiler/spelling.d.ts +14 -0
  241. package/dist/api/compiler/spelling.js +128 -0
  242. package/dist/api/compiler/symbol-accessibility.d.ts +57 -0
  243. package/dist/api/compiler/symbol-accessibility.js +894 -0
  244. package/dist/api/compiler/symbol.d.ts +240 -0
  245. package/dist/api/compiler/symbol.js +228 -0
  246. package/dist/api/compiler/symlinks.d.ts +32 -0
  247. package/dist/api/compiler/symlinks.js +114 -0
  248. package/dist/api/compiler/syntax-kind.d.ts +389 -0
  249. package/dist/api/compiler/syntax-kind.js +2 -0
  250. package/dist/api/compiler/timeline.d.ts +9 -0
  251. package/dist/api/compiler/timeline.js +54 -0
  252. package/dist/api/compiler/transformers/alias-references.d.ts +39 -0
  253. package/dist/api/compiler/transformers/alias-references.js +676 -0
  254. package/dist/api/compiler/transformers/async-utilities.d.ts +51 -0
  255. package/dist/api/compiler/transformers/async-utilities.js +260 -0
  256. package/dist/api/compiler/transformers/async.d.ts +53 -0
  257. package/dist/api/compiler/transformers/async.js +645 -0
  258. package/dist/api/compiler/transformers/class-fields.d.ts +206 -0
  259. package/dist/api/compiler/transformers/class-fields.js +2418 -0
  260. package/dist/api/compiler/transformers/class-utilities.d.ts +46 -0
  261. package/dist/api/compiler/transformers/class-utilities.js +261 -0
  262. package/dist/api/compiler/transformers/commonjs-module.d.ts +103 -0
  263. package/dist/api/compiler/transformers/commonjs-module.js +1180 -0
  264. package/dist/api/compiler/transformers/const-enum-inliner.d.ts +11 -0
  265. package/dist/api/compiler/transformers/const-enum-inliner.js +36 -0
  266. package/dist/api/compiler/transformers/declarations-diagnostics.d.ts +52 -0
  267. package/dist/api/compiler/transformers/declarations-diagnostics.js +538 -0
  268. package/dist/api/compiler/transformers/declarations-pseudo.d.ts +160 -0
  269. package/dist/api/compiler/transformers/declarations-pseudo.js +797 -0
  270. package/dist/api/compiler/transformers/declarations-resolver.d.ts +152 -0
  271. package/dist/api/compiler/transformers/declarations-resolver.js +35 -0
  272. package/dist/api/compiler/transformers/declarations-syntactic.d.ts +119 -0
  273. package/dist/api/compiler/transformers/declarations-syntactic.js +1582 -0
  274. package/dist/api/compiler/transformers/declarations-tracker.d.ts +46 -0
  275. package/dist/api/compiler/transformers/declarations-tracker.js +197 -0
  276. package/dist/api/compiler/transformers/declarations.d.ts +175 -0
  277. package/dist/api/compiler/transformers/declarations.js +2380 -0
  278. package/dist/api/compiler/transformers/decorator-metadata.d.ts +54 -0
  279. package/dist/api/compiler/transformers/decorator-metadata.js +554 -0
  280. package/dist/api/compiler/transformers/decorator-utilities.d.ts +38 -0
  281. package/dist/api/compiler/transformers/decorator-utilities.js +238 -0
  282. package/dist/api/compiler/transformers/destructuring.d.ts +25 -0
  283. package/dist/api/compiler/transformers/destructuring.js +626 -0
  284. package/dist/api/compiler/transformers/downlevel.d.ts +6 -0
  285. package/dist/api/compiler/transformers/downlevel.js +45 -0
  286. package/dist/api/compiler/transformers/emit-resolver.d.ts +61 -0
  287. package/dist/api/compiler/transformers/emit-resolver.js +197 -0
  288. package/dist/api/compiler/transformers/es-decorators.d.ts +118 -0
  289. package/dist/api/compiler/transformers/es-decorators.js +1901 -0
  290. package/dist/api/compiler/transformers/exponentiation.d.ts +11 -0
  291. package/dist/api/compiler/transformers/exponentiation.js +76 -0
  292. package/dist/api/compiler/transformers/external-module-info.d.ts +35 -0
  293. package/dist/api/compiler/transformers/external-module-info.js +351 -0
  294. package/dist/api/compiler/transformers/for-await.d.ts +43 -0
  295. package/dist/api/compiler/transformers/for-await.js +469 -0
  296. package/dist/api/compiler/transformers/import-elision.d.ts +16 -0
  297. package/dist/api/compiler/transformers/import-elision.js +124 -0
  298. package/dist/api/compiler/transformers/jsx-entities.d.ts +1 -0
  299. package/dist/api/compiler/transformers/jsx-entities.js +256 -0
  300. package/dist/api/compiler/transformers/jsx.d.ts +71 -0
  301. package/dist/api/compiler/transformers/jsx.js +700 -0
  302. package/dist/api/compiler/transformers/legacy-decorators.d.ts +56 -0
  303. package/dist/api/compiler/transformers/legacy-decorators.js +564 -0
  304. package/dist/api/compiler/transformers/logical-assignment.d.ts +14 -0
  305. package/dist/api/compiler/transformers/logical-assignment.js +62 -0
  306. package/dist/api/compiler/transformers/module-utilities.d.ts +9 -0
  307. package/dist/api/compiler/transformers/module-utilities.js +36 -0
  308. package/dist/api/compiler/transformers/module.d.ts +49 -0
  309. package/dist/api/compiler/transformers/module.js +296 -0
  310. package/dist/api/compiler/transformers/named-evaluation.d.ts +26 -0
  311. package/dist/api/compiler/transformers/named-evaluation.js +266 -0
  312. package/dist/api/compiler/transformers/nullish-coalescing.d.ts +9 -0
  313. package/dist/api/compiler/transformers/nullish-coalescing.js +26 -0
  314. package/dist/api/compiler/transformers/object-rest-spread.d.ts +34 -0
  315. package/dist/api/compiler/transformers/object-rest-spread.js +411 -0
  316. package/dist/api/compiler/transformers/optional-catch.d.ts +9 -0
  317. package/dist/api/compiler/transformers/optional-catch.js +20 -0
  318. package/dist/api/compiler/transformers/optional-chain.d.ts +14 -0
  319. package/dist/api/compiler/transformers/optional-chain.js +188 -0
  320. package/dist/api/compiler/transformers/reference-resolver.d.ts +128 -0
  321. package/dist/api/compiler/transformers/reference-resolver.js +1155 -0
  322. package/dist/api/compiler/transformers/runtime-syntax.d.ts +71 -0
  323. package/dist/api/compiler/transformers/runtime-syntax.js +753 -0
  324. package/dist/api/compiler/transformers/subtree-facts.d.ts +9 -0
  325. package/dist/api/compiler/transformers/subtree-facts.js +203 -0
  326. package/dist/api/compiler/transformers/syntax-facts.d.ts +51 -0
  327. package/dist/api/compiler/transformers/syntax-facts.js +254 -0
  328. package/dist/api/compiler/transformers/tagged-template.d.ts +17 -0
  329. package/dist/api/compiler/transformers/tagged-template.js +148 -0
  330. package/dist/api/compiler/transformers/transformer.d.ts +17 -0
  331. package/dist/api/compiler/transformers/transformer.js +54 -0
  332. package/dist/api/compiler/transformers/type-eraser.d.ts +21 -0
  333. package/dist/api/compiler/transformers/type-eraser.js +346 -0
  334. package/dist/api/compiler/transformers/type-serialization.d.ts +20 -0
  335. package/dist/api/compiler/transformers/type-serialization.js +347 -0
  336. package/dist/api/compiler/transformers/using.d.ts +56 -0
  337. package/dist/api/compiler/transformers/using.js +453 -0
  338. package/dist/api/compiler/transformers/utilities.d.ts +50 -0
  339. package/dist/api/compiler/transformers/utilities.js +347 -0
  340. package/dist/api/compiler/transformers/visitor-base.d.ts +6 -0
  341. package/dist/api/compiler/transformers/visitor-base.js +817 -0
  342. package/dist/api/compiler/transformers/visitor-hooks.d.ts +29 -0
  343. package/dist/api/compiler/transformers/visitor-hooks.js +189 -0
  344. package/dist/api/compiler/tsconfig.d.ts +177 -0
  345. package/dist/api/compiler/tsconfig.js +1311 -0
  346. package/dist/api/compiler/type-printer.d.ts +15 -0
  347. package/dist/api/compiler/type-printer.js +74 -0
  348. package/dist/api/compiler/types.d.ts +87 -0
  349. package/dist/api/compiler/types.js +23 -0
  350. package/dist/api/compiler/unicode-identifiers.d.ts +2 -0
  351. package/dist/api/compiler/unicode-identifiers.js +209 -0
  352. package/dist/api/compiler/unicode-properties.d.ts +8 -0
  353. package/dist/api/compiler/unicode-properties.js +531 -0
  354. package/dist/api/compiler/vfs-match.d.ts +24 -0
  355. package/dist/api/compiler/vfs-match.js +587 -0
  356. package/dist/api/library/ast.d.ts +3 -2
  357. package/dist/api/library/ast.generated.d.ts +127 -139
  358. package/dist/api/library/checker.d.ts +51 -0
  359. package/dist/api/library/checker.js +318 -0
  360. package/dist/api/library/diagnostics.d.ts +13 -0
  361. package/dist/api/library/diagnostics.js +50 -0
  362. package/dist/api/library/disk.d.ts +13 -0
  363. package/dist/api/library/disk.js +66 -0
  364. package/dist/api/library/errors.d.ts +13 -0
  365. package/dist/api/library/errors.js +24 -0
  366. package/dist/api/library/host.d.ts +24 -0
  367. package/dist/api/library/host.js +52 -0
  368. package/dist/api/library/index.d.ts +5 -1
  369. package/dist/api/library/index.js +4 -0
  370. package/dist/api/library/nodes.d.ts +51 -0
  371. package/dist/api/library/nodes.js +196 -0
  372. package/dist/api/library/options.d.ts +144 -0
  373. package/dist/api/library/options.js +308 -0
  374. package/dist/api/library/parse.d.ts +19 -0
  375. package/dist/api/library/parse.js +41 -0
  376. package/dist/api/library/program.d.ts +21 -0
  377. package/dist/api/library/program.js +234 -0
  378. package/dist/api/library/types.d.ts +14 -1
  379. package/package.json +12 -8
  380. package/src/compiler/ast-utilities.ts +548 -0
  381. package/src/compiler/ast.ts +12862 -0
  382. package/src/compiler/astnav.ts +799 -0
  383. package/src/compiler/binder-utilities.ts +1392 -0
  384. package/src/compiler/binder.ts +2703 -0
  385. package/src/compiler/build-info.ts +1197 -0
  386. package/src/compiler/character-codes.ts +323 -0
  387. package/src/compiler/checker-calls.ts +2155 -0
  388. package/src/compiler/checker-classes.ts +706 -0
  389. package/src/compiler/checker-contextual.ts +1457 -0
  390. package/src/compiler/checker-declaration-order.ts +438 -0
  391. package/src/compiler/checker-declarations.ts +1194 -0
  392. package/src/compiler/checker-declared-types.ts +919 -0
  393. package/src/compiler/checker-decorators.ts +694 -0
  394. package/src/compiler/checker-deprecation.ts +359 -0
  395. package/src/compiler/checker-destructuring.ts +805 -0
  396. package/src/compiler/checker-elaboration.ts +477 -0
  397. package/src/compiler/checker-emit-helpers.ts +224 -0
  398. package/src/compiler/checker-emit-resolver.ts +769 -0
  399. package/src/compiler/checker-evaluator.ts +243 -0
  400. package/src/compiler/checker-expressions.ts +1831 -0
  401. package/src/compiler/checker-features.ts +361 -0
  402. package/src/compiler/checker-flow.ts +394 -0
  403. package/src/compiler/checker-functions.ts +1296 -0
  404. package/src/compiler/checker-generated-code.ts +227 -0
  405. package/src/compiler/checker-globals.ts +52 -0
  406. package/src/compiler/checker-grammar.ts +3584 -0
  407. package/src/compiler/checker-heritage.ts +1233 -0
  408. package/src/compiler/checker-inference.ts +2576 -0
  409. package/src/compiler/checker-iteration.ts +1206 -0
  410. package/src/compiler/checker-javascript.ts +502 -0
  411. package/src/compiler/checker-jsdoc.ts +360 -0
  412. package/src/compiler/checker-jsx.ts +1924 -0
  413. package/src/compiler/checker-literals.ts +61 -0
  414. package/src/compiler/checker-members.ts +2436 -0
  415. package/src/compiler/checker-modules.ts +2886 -0
  416. package/src/compiler/checker-narrowing.ts +3230 -0
  417. package/src/compiler/checker-object-literals.ts +575 -0
  418. package/src/compiler/checker-operators.ts +1422 -0
  419. package/src/compiler/checker-overloads.ts +328 -0
  420. package/src/compiler/checker-property-access.ts +906 -0
  421. package/src/compiler/checker-services.ts +1149 -0
  422. package/src/compiler/checker-signatures.ts +1032 -0
  423. package/src/compiler/checker-source-file.ts +732 -0
  424. package/src/compiler/checker-statements.ts +292 -0
  425. package/src/compiler/checker-super.ts +261 -0
  426. package/src/compiler/checker-symbol-types.ts +2107 -0
  427. package/src/compiler/checker-symbols.ts +2150 -0
  428. package/src/compiler/checker-type-factory.ts +2579 -0
  429. package/src/compiler/checker-type-nodes.ts +1033 -0
  430. package/src/compiler/checker-type-operators.ts +2502 -0
  431. package/src/compiler/checker-types.ts +2340 -0
  432. package/src/compiler/checker-unused.ts +642 -0
  433. package/src/compiler/checker-utilities.ts +161 -0
  434. package/src/compiler/checker.ts +1116 -0
  435. package/src/compiler/command-line.ts +436 -0
  436. package/src/compiler/compile-program.ts +439 -0
  437. package/src/compiler/decoder.ts +1209 -0
  438. package/src/compiler/diagnostic-locales.ts +86 -0
  439. package/src/compiler/diagnostic-messages.ts +2349 -0
  440. package/src/compiler/diagnostics.ts +897 -0
  441. package/src/compiler/emitter.ts +434 -0
  442. package/src/compiler/encoder.ts +1945 -0
  443. package/src/compiler/file-include.ts +673 -0
  444. package/src/compiler/file-loader.ts +1528 -0
  445. package/src/compiler/file-system.ts +143 -0
  446. package/src/compiler/flow.ts +89 -0
  447. package/src/compiler/help.ts +473 -0
  448. package/src/compiler/incremental.ts +2784 -0
  449. package/src/compiler/init.ts +141 -0
  450. package/src/compiler/instantiation.ts +894 -0
  451. package/src/compiler/jsdoc.ts +1386 -0
  452. package/src/compiler/link-tables.ts +98 -0
  453. package/src/compiler/locale-data.ts +203 -0
  454. package/src/compiler/locale.ts +307 -0
  455. package/src/compiler/locales/cs-CZ.ts +5 -0
  456. package/src/compiler/locales/de-DE.ts +5 -0
  457. package/src/compiler/locales/es-ES.ts +5 -0
  458. package/src/compiler/locales/fr-FR.ts +5 -0
  459. package/src/compiler/locales/it-IT.ts +5 -0
  460. package/src/compiler/locales/ja-JP.ts +5 -0
  461. package/src/compiler/locales/ko-KR.ts +5 -0
  462. package/src/compiler/locales/pl-PL.ts +5 -0
  463. package/src/compiler/locales/pt-BR.ts +5 -0
  464. package/src/compiler/locales/ru-RU.ts +5 -0
  465. package/src/compiler/locales/tr-TR.ts +5 -0
  466. package/src/compiler/locales/zh-CN.ts +5 -0
  467. package/src/compiler/locales/zh-TW.ts +5 -0
  468. package/src/compiler/module-resolution-types.ts +243 -0
  469. package/src/compiler/module-resolution.ts +2072 -0
  470. package/src/compiler/module-specifiers.ts +1491 -0
  471. package/src/compiler/name-resolver.ts +646 -0
  472. package/src/compiler/node-builder-names.ts +1069 -0
  473. package/src/compiler/node-builder-pseudo.ts +892 -0
  474. package/src/compiler/node-builder-reuse.ts +764 -0
  475. package/src/compiler/node-builder.ts +3425 -0
  476. package/src/compiler/option-declarations.ts +1560 -0
  477. package/src/compiler/options.ts +1576 -0
  478. package/src/compiler/output-paths.ts +334 -0
  479. package/src/compiler/package-json.ts +1083 -0
  480. package/src/compiler/parallel-check.ts +1951 -0
  481. package/src/compiler/parallel-parse.ts +590 -0
  482. package/src/compiler/parse-thread.ts +140 -0
  483. package/src/compiler/parser.ts +7716 -0
  484. package/src/compiler/path.ts +717 -0
  485. package/src/compiler/printer/async-helpers.ts +183 -0
  486. package/src/compiler/printer/auto-generate.ts +27 -0
  487. package/src/compiler/printer/decorator-helpers.ts +218 -0
  488. package/src/compiler/printer/emit-context.ts +679 -0
  489. package/src/compiler/printer/emit-flags.ts +64 -0
  490. package/src/compiler/printer/factory-base.ts +2577 -0
  491. package/src/compiler/printer/factory.ts +764 -0
  492. package/src/compiler/printer/helpers.ts +291 -0
  493. package/src/compiler/printer/name-generator.ts +372 -0
  494. package/src/compiler/printer/printer.ts +5072 -0
  495. package/src/compiler/printer/text-writer.ts +183 -0
  496. package/src/compiler/printer/utilities.ts +884 -0
  497. package/src/compiler/program-checker.ts +592 -0
  498. package/src/compiler/program-utilities.ts +435 -0
  499. package/src/compiler/program.ts +1419 -0
  500. package/src/compiler/pseudo-bigint.ts +62 -0
  501. package/src/compiler/regexp-checker.ts +1142 -0
  502. package/src/compiler/relations.ts +5149 -0
  503. package/src/compiler/reparser.ts +1566 -0
  504. package/src/compiler/scanner.ts +2951 -0
  505. package/src/compiler/show-config.ts +239 -0
  506. package/src/compiler/side-state.ts +125 -0
  507. package/src/compiler/sourcemap.ts +366 -0
  508. package/src/compiler/spelling.ts +122 -0
  509. package/src/compiler/symbol-accessibility.ts +1071 -0
  510. package/src/compiler/symbol.ts +439 -0
  511. package/src/compiler/symlinks.ts +137 -0
  512. package/src/compiler/syntax-kind.ts +391 -0
  513. package/src/compiler/thread-entry.ts +191 -0
  514. package/src/compiler/timeline.ts +53 -0
  515. package/src/compiler/transformers/alias-references.ts +757 -0
  516. package/src/compiler/transformers/async-utilities.ts +335 -0
  517. package/src/compiler/transformers/async.ts +838 -0
  518. package/src/compiler/transformers/class-fields.ts +2967 -0
  519. package/src/compiler/transformers/class-utilities.ts +324 -0
  520. package/src/compiler/transformers/commonjs-module.ts +1621 -0
  521. package/src/compiler/transformers/const-enum-inliner.ts +50 -0
  522. package/src/compiler/transformers/declarations-diagnostics.ts +832 -0
  523. package/src/compiler/transformers/declarations-pseudo.ts +966 -0
  524. package/src/compiler/transformers/declarations-resolver.ts +249 -0
  525. package/src/compiler/transformers/declarations-syntactic.ts +1859 -0
  526. package/src/compiler/transformers/declarations-tracker.ts +269 -0
  527. package/src/compiler/transformers/declarations.ts +3120 -0
  528. package/src/compiler/transformers/decorator-metadata.ts +697 -0
  529. package/src/compiler/transformers/decorator-utilities.ts +283 -0
  530. package/src/compiler/transformers/destructuring.ts +708 -0
  531. package/src/compiler/transformers/downlevel.ts +50 -0
  532. package/src/compiler/transformers/emit-resolver.ts +258 -0
  533. package/src/compiler/transformers/es-decorators.ts +2682 -0
  534. package/src/compiler/transformers/exponentiation.ts +80 -0
  535. package/src/compiler/transformers/external-module-info.ts +412 -0
  536. package/src/compiler/transformers/for-await.ts +687 -0
  537. package/src/compiler/transformers/import-elision.ts +152 -0
  538. package/src/compiler/transformers/jsx-entities.ts +256 -0
  539. package/src/compiler/transformers/jsx.ts +824 -0
  540. package/src/compiler/transformers/legacy-decorators.ts +726 -0
  541. package/src/compiler/transformers/logical-assignment.ts +75 -0
  542. package/src/compiler/transformers/module-utilities.ts +42 -0
  543. package/src/compiler/transformers/module.ts +474 -0
  544. package/src/compiler/transformers/named-evaluation.ts +389 -0
  545. package/src/compiler/transformers/nullish-coalescing.ts +35 -0
  546. package/src/compiler/transformers/object-rest-spread.ts +577 -0
  547. package/src/compiler/transformers/optional-catch.ts +29 -0
  548. package/src/compiler/transformers/optional-chain.ts +246 -0
  549. package/src/compiler/transformers/reference-resolver.ts +1201 -0
  550. package/src/compiler/transformers/runtime-syntax.ts +942 -0
  551. package/src/compiler/transformers/subtree-facts.ts +245 -0
  552. package/src/compiler/transformers/syntax-facts.ts +353 -0
  553. package/src/compiler/transformers/tagged-template.ts +167 -0
  554. package/src/compiler/transformers/transformer.ts +56 -0
  555. package/src/compiler/transformers/type-eraser.ts +520 -0
  556. package/src/compiler/transformers/type-serialization.ts +374 -0
  557. package/src/compiler/transformers/using.ts +613 -0
  558. package/src/compiler/transformers/utilities.ts +419 -0
  559. package/src/compiler/transformers/visitor-base.ts +1601 -0
  560. package/src/compiler/transformers/visitor-hooks.ts +252 -0
  561. package/src/compiler/tsc-build.ts +1418 -0
  562. package/src/compiler/tsc-cli.ts +666 -0
  563. package/src/compiler/tsc-compile.ts +437 -0
  564. package/src/compiler/tsc-watch.ts +854 -0
  565. package/src/compiler/tsconfig.ts +1574 -0
  566. package/src/compiler/type-printer.ts +110 -0
  567. package/src/compiler/types.ts +115 -0
  568. package/src/compiler/unicode-identifiers.ts +212 -0
  569. package/src/compiler/unicode-properties.ts +537 -0
  570. package/src/compiler/vfs-match.ts +597 -0
  571. package/src/compiler/xxh3.ts +650 -0
  572. package/src/library/ast.generated.ts +141 -167
  573. package/src/library/ast.ts +4 -1
  574. package/src/library/checker.ts +384 -0
  575. package/src/library/diagnostics.ts +72 -0
  576. package/src/library/disk.ts +73 -0
  577. package/src/library/errors.ts +26 -0
  578. package/src/library/host.ts +63 -0
  579. package/src/library/index.ts +21 -0
  580. package/src/library/nodes.ts +232 -0
  581. package/src/library/options.ts +449 -0
  582. package/src/library/parse.ts +63 -0
  583. package/src/library/program.ts +293 -0
  584. package/src/library/types.ts +14 -1
@@ -0,0 +1,824 @@
1
+ // Transforms JSX into calls of the JSX factory (`jsx: react`) or of the automatic runtime
2
+ // (`react-jsx`, `react-jsxdev`), importing the runtime's functions.
3
+ import {
4
+ BindingElement,
5
+ BindingPattern,
6
+ Identifier,
7
+ ImportClause,
8
+ ImportDeclaration,
9
+ ImportSpecifier,
10
+ JsxAttribute,
11
+ JsxElement,
12
+ JsxExpression,
13
+ JsxFragment,
14
+ JsxNamespacedName,
15
+ JsxSelfClosingElement,
16
+ JsxSpreadAttribute,
17
+ JsxText,
18
+ ModuleDeclaration,
19
+ NamedImports,
20
+ type Node,
21
+ NodeFlags,
22
+ type NodeList,
23
+ ObjectLiteralExpression,
24
+ PropertyAssignment,
25
+ QualifiedName,
26
+ SourceFile,
27
+ SpreadAssignment,
28
+ SpreadElement,
29
+ StringLiteral,
30
+ ArrayLiteralExpression,
31
+ VariableDeclaration,
32
+ getTagName,
33
+ } from '../ast.js';
34
+ import { isExternalOrCommonJSModule } from '../binder-utilities.js';
35
+ import { isLineBreak, isWhiteSpaceSingleLine } from '../character-codes.js';
36
+ import { type CompilerOptions, JsxEmit, ScriptTarget, getEmitScriptTarget } from '../options.js';
37
+ import type { EmitContext } from '../printer/emit-context.js';
38
+ import { EmitFlags, GeneratedIdentifierFlags } from '../printer/emit-flags.js';
39
+ import type { NodeFactory } from '../printer/factory.js';
40
+ import { isPrologueDirective, lineOf } from '../printer/utilities.js';
41
+ import { getJSXImplicitImportBase, getJSXRuntimeImport } from '../program-utilities.js';
42
+ import { isIdentifierText, skipTrivia } from '../scanner.js';
43
+ import { SyntaxKind } from '../syntax-kind.js';
44
+ import { TokenFlags } from '../types.js';
45
+ import type { EmitResolver } from './emit-resolver.js';
46
+ import { entities } from './jsx-entities.js';
47
+ import { SubtreeFacts, type SyntaxFacts } from './syntax-facts.js';
48
+ import { Transformer } from './transformer.js';
49
+ import { isExternalModule } from './utilities.js';
50
+
51
+ /** The names imported from one runtime module, by imported name. */
52
+ class RuntimeImports {
53
+ readonly source: string;
54
+ readonly specifiers: Map<string, ImportSpecifier>;
55
+
56
+ constructor(source: string) {
57
+ this.source = source;
58
+ this.specifiers = new Map<string, ImportSpecifier>();
59
+ }
60
+
61
+ /** The specifiers ordered by imported name, then local name. */
62
+ sorted(): ImportSpecifier[] {
63
+ const result: ImportSpecifier[] = [];
64
+ for (const specifier of this.specifiers.values()) result.push(specifier);
65
+ result.sort(compareSpecifiers);
66
+ return result;
67
+ }
68
+ }
69
+
70
+ export class JsxTransformer extends Transformer {
71
+ readonly options: CompilerOptions;
72
+ readonly facts: SyntaxFacts;
73
+ readonly resolver: EmitResolver;
74
+ private file: SourceFile | undefined;
75
+ /** The automatic runtime's module, or `''` for the classic factory. */
76
+ private importSource: string;
77
+ private fileNameDeclaration: VariableDeclaration | undefined;
78
+ private runtimeImports: RuntimeImports[];
79
+ /** Whether the node being visited is a child of a JSX element. */
80
+ private inChild: boolean;
81
+
82
+ constructor(context: EmitContext, options: CompilerOptions, facts: SyntaxFacts, resolver: EmitResolver) {
83
+ super(context);
84
+ this.options = options;
85
+ this.facts = facts;
86
+ this.resolver = resolver;
87
+ this.file = undefined;
88
+ this.importSource = '';
89
+ this.fileNameDeclaration = undefined;
90
+ this.runtimeImports = [];
91
+ this.inChild = false;
92
+ }
93
+
94
+ visit(node: Node): Node | undefined {
95
+ if (!this.facts.has(node, SubtreeFacts.ContainsJsx)) return node;
96
+ switch (node.kind) {
97
+ case SyntaxKind.SourceFile:
98
+ this.inChild = false;
99
+ return this.visitSourceFile(node as SourceFile);
100
+ case SyntaxKind.JsxElement:
101
+ return this.visitJsxElement(node as JsxElement);
102
+ case SyntaxKind.JsxSelfClosingElement:
103
+ return this.visitJsxSelfClosingElement(node as JsxSelfClosingElement);
104
+ case SyntaxKind.JsxFragment:
105
+ return this.visitJsxFragment(node as JsxFragment);
106
+ case SyntaxKind.JsxText:
107
+ this.inChild = false;
108
+ return this.visitJsxText(node as JsxText);
109
+ case SyntaxKind.JsxExpression:
110
+ this.inChild = false;
111
+ return this.visitJsxExpression(node as JsxExpression);
112
+ }
113
+ this.inChild = false;
114
+ return this.visitEachChild(node);
115
+ }
116
+
117
+ private visitSourceFile(file: SourceFile): Node {
118
+ if (file.isDeclarationFile) return file;
119
+ const context = this.context;
120
+ const factory = this.factory;
121
+ this.file = file;
122
+ this.importSource = getJSXImplicitImportBase(this.options, file);
123
+ this.fileNameDeclaration = undefined;
124
+ this.runtimeImports = [];
125
+ let visited = this.visitEachChild(file) as SourceFile;
126
+ const helpers = context.readEmitHelpers();
127
+ for (let i = 0; i < helpers.length; i++) context.addEmitHelper(visited, helpers[i]);
128
+ let statements = visited.statements.nodes;
129
+ let updated = false;
130
+ const fileNameDeclaration: VariableDeclaration | undefined = this.fileNameDeclaration;
131
+ if (fileNameDeclaration !== undefined) {
132
+ statements = this.insertAfterPrologue(
133
+ statements,
134
+ factory.createVariableStatement(
135
+ undefined,
136
+ factory.createVariableDeclarationList(factory.createNodeList([fileNameDeclaration as Node]), NodeFlags.Const),
137
+ ),
138
+ );
139
+ updated = true;
140
+ }
141
+ if (this.runtimeImports.length > 0) {
142
+ const module = isExternalModule(file);
143
+ if (module || isExternalOrCommonJSModule(file)) {
144
+ updated = true;
145
+ for (let i = 0; i < this.runtimeImports.length; i++) {
146
+ const imports = this.runtimeImports[i];
147
+ statements = this.insertAfterPrologue(
148
+ statements,
149
+ module ? this.createImport(imports) : this.createRequire(imports),
150
+ );
151
+ }
152
+ }
153
+ }
154
+ if (updated) visited = factory.updateSourceFile(file, factory.createNodeList(statements), file.endOfFileToken);
155
+ this.file = undefined;
156
+ this.importSource = '';
157
+ this.fileNameDeclaration = undefined;
158
+ this.runtimeImports = [];
159
+ return visited;
160
+ }
161
+
162
+ /** `import { jsx as _jsx } from "react/jsx-runtime";` */
163
+ private createImport(imports: RuntimeImports): Node {
164
+ const factory = this.factory;
165
+ const specifiers = imports.sorted();
166
+ const named = factory.created(new NamedImports(factory.createNodeList(specifiers)));
167
+ for (let i = 0; i < specifiers.length; i++) specifiers[i].parent = named;
168
+ const clause = factory.created(new ImportClause(undefined, undefined, named));
169
+ named.parent = clause;
170
+ const declaration = factory.created(
171
+ new ImportDeclaration(
172
+ SyntaxKind.ImportDeclaration,
173
+ undefined,
174
+ clause,
175
+ factory.createStringLiteral(imports.source, TokenFlags.None),
176
+ undefined,
177
+ ),
178
+ );
179
+ clause.parent = declaration;
180
+ return declaration;
181
+ }
182
+
183
+ /** `const { jsx: _jsx } = require("react/jsx-runtime");` */
184
+ private createRequire(imports: RuntimeImports): Node {
185
+ const factory = this.factory;
186
+ const specifiers = imports.sorted();
187
+ const elements: Node[] = [];
188
+ for (let i = 0; i < specifiers.length; i++)
189
+ elements.push(
190
+ factory.created(new BindingElement(undefined, specifiers[i].propertyName, specifiers[i].name, undefined)),
191
+ );
192
+ const pattern = factory.created(
193
+ new BindingPattern(SyntaxKind.ObjectBindingPattern, factory.createNodeList(elements)),
194
+ );
195
+ const require = factory.createCallExpression(factory.createIdentifier('require'), [
196
+ factory.createStringLiteral(imports.source, TokenFlags.None),
197
+ ]);
198
+ return factory.createVariableStatement(
199
+ undefined,
200
+ factory.createVariableDeclarationList(
201
+ factory.createNodeList([factory.createVariableDeclaration(pattern, undefined, undefined, require)]),
202
+ NodeFlags.Const,
203
+ ),
204
+ );
205
+ }
206
+
207
+ private insertAfterPrologue(statements: Node[], statement: Node): Node[] {
208
+ let index = 0;
209
+ while (
210
+ index < statements.length &&
211
+ (isPrologueDirective(statements[index]) ||
212
+ (this.context.getEmitFlags(statements[index]) & EmitFlags.CustomPrologue) !== 0)
213
+ )
214
+ index++;
215
+ const result = statements.slice(0, index);
216
+ result.push(statement);
217
+ for (let i = index; i < statements.length; i++) result.push(statements[i]);
218
+ return result;
219
+ }
220
+
221
+ /** `_jsxFileName`, declared at the top of the file on first use. */
222
+ private currentFileNameExpression(): Node {
223
+ if (this.fileNameDeclaration === undefined) {
224
+ const factory = this.factory;
225
+ this.fileNameDeclaration = factory.createVariableDeclaration(
226
+ factory.createUniqueName(
227
+ '_jsxFileName',
228
+ GeneratedIdentifierFlags.Optimistic | GeneratedIdentifierFlags.FileLevel,
229
+ ),
230
+ undefined,
231
+ undefined,
232
+ factory.createStringLiteral(this.file!.fileName, TokenFlags.None),
233
+ );
234
+ }
235
+ return this.fileNameDeclaration.name;
236
+ }
237
+
238
+ /** The local name of `name` imported from the runtime, added to the file's imports on first use. */
239
+ private implicitImport(name: string): Identifier {
240
+ let source = this.importSource;
241
+ if (name !== 'createElement') source = getJSXRuntimeImport(source, this.options);
242
+ let imports: RuntimeImports | undefined = undefined;
243
+ for (let i = 0; i < this.runtimeImports.length; i++)
244
+ if (this.runtimeImports[i].source === source) imports = this.runtimeImports[i];
245
+ if (imports === undefined) {
246
+ imports = new RuntimeImports(source);
247
+ this.runtimeImports.push(imports);
248
+ }
249
+ const existing = imports.specifiers.get(name);
250
+ if (existing !== undefined) return existing.name;
251
+ const factory = this.factory;
252
+ const local = factory.createUniqueName(
253
+ '_' + name,
254
+ GeneratedIdentifierFlags.Optimistic |
255
+ GeneratedIdentifierFlags.FileLevel |
256
+ GeneratedIdentifierFlags.AllowNameSubstitution,
257
+ );
258
+ const specifier = factory.created(new ImportSpecifier(false, factory.createIdentifier(name), local));
259
+ this.resolver.setReferencedImportDeclaration(local, specifier);
260
+ imports.specifiers.set(name, specifier);
261
+ return local;
262
+ }
263
+
264
+ private factoryCallee(isStaticChildren: boolean): Identifier {
265
+ if (this.options.jsx === JsxEmit.ReactJSXDev) return this.implicitImport('jsxDEV');
266
+ return this.implicitImport(isStaticChildren ? 'jsxs' : 'jsx');
267
+ }
268
+
269
+ private shouldUseCreateElement(attributes: NodeList): boolean {
270
+ return this.importSource === '' || hasKeyAfterPropsSpread(attributes);
271
+ }
272
+
273
+ private visitJsxElement(element: JsxElement): Node {
274
+ const opening = element.openingElement;
275
+ const pos = skipTrivia(this.file!.text, element.pos);
276
+ if (this.shouldUseCreateElement(opening.attributes.properties))
277
+ return this.visitCreateElement(opening, opening.attributes.properties, element.children, pos, element.end);
278
+ return this.visitElementJsx(opening, opening.attributes.properties, element.children, pos, element.end);
279
+ }
280
+
281
+ private visitJsxSelfClosingElement(element: JsxSelfClosingElement): Node {
282
+ const pos = skipTrivia(this.file!.text, element.pos);
283
+ if (this.shouldUseCreateElement(element.attributes.properties))
284
+ return this.visitCreateElement(element, element.attributes.properties, undefined, pos, element.end);
285
+ return this.visitElementJsx(element, element.attributes.properties, undefined, pos, element.end);
286
+ }
287
+
288
+ private visitJsxFragment(fragment: JsxFragment): Node {
289
+ const pos = skipTrivia(this.file!.text, fragment.pos);
290
+ if (this.importSource === '')
291
+ return this.visitFragmentCreateElement(fragment.openingFragment, fragment.children, pos, fragment.end);
292
+ return this.visitFragmentJsx(fragment.children, pos, fragment.end);
293
+ }
294
+
295
+ private visitChild(node: Node): Node | undefined {
296
+ const saved = this.inChild;
297
+ this.inChild = true;
298
+ const result = this.visit(node);
299
+ this.inChild = saved;
300
+ return result;
301
+ }
302
+
303
+ /** `children: x` or `children: [x, y]`, or undefined when there are none. */
304
+ private childrenProperty(children: Node[]): Node | undefined {
305
+ const factory = this.factory;
306
+ const semantic = getSemanticJsxChildren(children);
307
+ if (semantic.length === 1 && !isSpreadChild(semantic[0])) {
308
+ const result = this.visitChild(semantic[0]);
309
+ return result === undefined
310
+ ? undefined
311
+ : factory.createPropertyAssignment(factory.createIdentifier('children'), result);
312
+ }
313
+ const results: Node[] = [];
314
+ for (let i = 0; i < semantic.length; i++) {
315
+ const result = this.visitChild(semantic[i]);
316
+ if (result === undefined) continue;
317
+ // The array is printed on one line.
318
+ this.context.setEmitFlags(result, this.context.getEmitFlags(result) & ~EmitFlags.StartOnNewLine);
319
+ results.push(result);
320
+ }
321
+ if (results.length === 0) return undefined;
322
+ return factory.createPropertyAssignment(
323
+ factory.createIdentifier('children'),
324
+ factory.created(new ArrayLiteralExpression(factory.createNodeList(results), false)),
325
+ );
326
+ }
327
+
328
+ /** The tag of an opening or self-closing element, as an expression. */
329
+ private tagName(element: Node): Node {
330
+ const tagName = getTagName(element)!;
331
+ const factory = this.factory;
332
+ if (tagName instanceof Identifier && isIntrinsicJsxName(tagName.text))
333
+ return factory.createStringLiteral(tagName.text, TokenFlags.None);
334
+ if (tagName instanceof JsxNamespacedName)
335
+ return factory.createStringLiteral(tagName.namespace.text + ':' + tagName.name.text, TokenFlags.None);
336
+ return factory.createExpressionFromEntityName(tagName);
337
+ }
338
+
339
+ private visitElementJsx(
340
+ element: Node,
341
+ attributes: NodeList,
342
+ children: NodeList | undefined,
343
+ pos: number,
344
+ end: number,
345
+ ): Node {
346
+ const factory = this.factory;
347
+ const tagName = this.tagName(element);
348
+ const childrenProperty =
349
+ children !== undefined && children.nodes.length > 0 ? this.childrenProperty(children.nodes) : undefined;
350
+ let key: JsxAttribute | undefined = undefined;
351
+ let attrs = attributes.nodes;
352
+ for (let i = 0; i < attrs.length; i++) {
353
+ const attribute = attrs[i];
354
+ if (attribute instanceof JsxAttribute && attribute.name instanceof Identifier && attribute.name.text === 'key') {
355
+ key = attribute;
356
+ attrs = attrs.slice(0, i).concat(attrs.slice(i + 1));
357
+ break;
358
+ }
359
+ }
360
+ let props: Node;
361
+ if (attrs.length > 0) props = this.attributesToObject(attrs, childrenProperty);
362
+ else props = factory.createObjectLiteral(childrenProperty !== undefined ? [childrenProperty] : [], false);
363
+ return this.createJsxCall(tagName, props, key, children, pos, end);
364
+ }
365
+
366
+ private visitFragmentJsx(children: NodeList, pos: number, end: number): Node {
367
+ const factory = this.factory;
368
+ let props: Node | undefined = undefined;
369
+ if (children.nodes.length > 0) {
370
+ const property = this.childrenProperty(children.nodes);
371
+ if (property !== undefined) props = factory.createObjectLiteral([property], false);
372
+ }
373
+ if (props === undefined) props = factory.createObjectLiteral([], false);
374
+ return this.createJsxCall(this.implicitImport('Fragment'), props, undefined, children, pos, end);
375
+ }
376
+
377
+ /** `_jsx(type, props, key)`, with `jsxDEV`'s static-children flag, source and `this`. */
378
+ private createJsxCall(
379
+ tagName: Node,
380
+ props: Node,
381
+ key: JsxAttribute | undefined,
382
+ children: NodeList | undefined,
383
+ pos: number,
384
+ end: number,
385
+ ): Node {
386
+ const factory = this.factory;
387
+ const semantic = children !== undefined ? getSemanticJsxChildren(children.nodes) : [];
388
+ const isStaticChildren = semantic.length > 1 || (semantic.length === 1 && isSpreadChild(semantic[0]));
389
+ const args: Node[] = [tagName, props];
390
+ if (key !== undefined) args.push(this.attributeInitializer(key.initializer));
391
+ if (this.options.jsx === JsxEmit.ReactJSXDev) {
392
+ const file = this.context.mostOriginal(this.file!);
393
+ if (file instanceof SourceFile) {
394
+ // `jsxDEV(type, props, key, isStaticChildren, source, self)`
395
+ if (key === undefined) args.push(factory.createVoidZero());
396
+ args.push(factory.createKeywordExpression(isStaticChildren ? SyntaxKind.TrueKeyword : SyntaxKind.FalseKeyword));
397
+ const lineStarts = file.lineStarts();
398
+ const line = lineOf(lineStarts, pos);
399
+ args.push(
400
+ factory.createObjectLiteral(
401
+ [
402
+ factory.createPropertyAssignment(factory.createIdentifier('fileName'), this.currentFileNameExpression()),
403
+ factory.createPropertyAssignment(
404
+ factory.createIdentifier('lineNumber'),
405
+ factory.createNumericLiteral(String(line + 1)),
406
+ ),
407
+ factory.createPropertyAssignment(
408
+ factory.createIdentifier('columnNumber'),
409
+ factory.createNumericLiteral(String(pos - lineStarts[line] + 1)),
410
+ ),
411
+ ],
412
+ false,
413
+ ),
414
+ );
415
+ args.push(factory.createThisExpression());
416
+ }
417
+ }
418
+ const call = factory.createCallExpression(this.factoryCallee(isStaticChildren), args);
419
+ call.pos = pos | 0;
420
+ call.end = end | 0;
421
+ if (this.inChild) this.context.addEmitFlags(call, EmitFlags.StartOnNewLine);
422
+ return call;
423
+ }
424
+
425
+ /** The props object; below ES2018, spreads become `Object.assign` calls. */
426
+ private attributesToObject(attributes: Node[], childrenProperty: Node | undefined): Node {
427
+ const factory = this.factory;
428
+ if (getEmitScriptTarget(this.options) >= ScriptTarget.ES2018) {
429
+ const props: Node[] = [];
430
+ for (let i = 0; i < attributes.length; i++) {
431
+ const attribute = attributes[i];
432
+ if (attribute instanceof JsxSpreadAttribute) {
433
+ const spread = this.spreadAttributeToProperties(attribute as JsxSpreadAttribute);
434
+ for (let j = 0; j < spread.length; j++) props.push(spread[j]);
435
+ } else props.push(this.attributeToProperty(attribute as JsxAttribute));
436
+ }
437
+ if (childrenProperty !== undefined) props.push(childrenProperty);
438
+ return factory.createObjectLiteral(props, false);
439
+ }
440
+ return this.attributesToAssign(attributes, childrenProperty);
441
+ }
442
+
443
+ private attributesToAssign(attributes: Node[], childrenProperty: Node | undefined): Node {
444
+ const factory = this.factory;
445
+ const segments = new AssignSegments();
446
+ for (let i = 0; i < attributes.length; i++) {
447
+ const attribute = attributes[i];
448
+ if (attribute instanceof JsxSpreadAttribute) {
449
+ const expression = attribute.expression;
450
+ // An inline object literal is flattened as if its properties were attributes.
451
+ if (expression instanceof ObjectLiteralExpression && !hasProto(expression)) {
452
+ const properties = expression.properties.nodes;
453
+ for (let j = 0; j < properties.length; j++) {
454
+ const property = properties[j];
455
+ if (property instanceof SpreadAssignment) {
456
+ segments.flush(factory);
457
+ segments.push(this.visit(property.expression));
458
+ continue;
459
+ }
460
+ const visited = this.visit(property);
461
+ if (visited !== undefined) segments.properties.push(visited);
462
+ }
463
+ continue;
464
+ }
465
+ segments.flush(factory);
466
+ segments.push(this.visit(expression));
467
+ continue;
468
+ }
469
+ segments.properties.push(this.attributeToProperty(attribute as JsxAttribute));
470
+ }
471
+ if (childrenProperty !== undefined) segments.properties.push(childrenProperty);
472
+ segments.flush(factory);
473
+ let expressions = segments.expressions;
474
+ // The factory expects a fresh object, so the first argument of `Object.assign` is a literal.
475
+ if (expressions.length > 0 && !(expressions[0] instanceof ObjectLiteralExpression))
476
+ expressions = [factory.createObjectLiteral([], false) as Node].concat(expressions);
477
+ if (expressions.length === 1) return expressions[0];
478
+ return factory.createGlobalMethodCall('Object', 'assign', expressions);
479
+ }
480
+
481
+ private spreadAttributeToProperties(attribute: JsxSpreadAttribute): Node[] {
482
+ const expression = attribute.expression;
483
+ if (expression instanceof ObjectLiteralExpression && !hasProto(expression))
484
+ return this.visitArray(expression.properties.nodes);
485
+ const visited = this.visit(expression);
486
+ return [this.factory.created(new SpreadAssignment(visited!))];
487
+ }
488
+
489
+ private attributeToProperty(attribute: JsxAttribute): Node {
490
+ const name = this.attributeName(attribute);
491
+ return this.factory.createPropertyAssignment(name, this.attributeInitializer(attribute.initializer));
492
+ }
493
+
494
+ /** The attribute's name, quoted when it isn't an identifier. */
495
+ private attributeName(attribute: JsxAttribute): Node {
496
+ const name = attribute.name;
497
+ if (name instanceof Identifier) {
498
+ if (isIdentifierText(name.text)) return name;
499
+ return this.factory.createStringLiteral(name.text, TokenFlags.None);
500
+ }
501
+ const namespaced = name as JsxNamespacedName;
502
+ return this.factory.createStringLiteral(namespaced.namespace.text + ':' + namespaced.name.text, TokenFlags.None);
503
+ }
504
+
505
+ private attributeInitializer(node: Node | undefined): Node {
506
+ const factory = this.factory;
507
+ if (node === undefined) return factory.createTrue();
508
+ if (node instanceof StringLiteral) {
509
+ // Recreated so the printer escapes what the JSX string had literally, keeping its quotes.
510
+ const literal = factory.createStringLiteral(decodeEntities(node.text), node.tokenFlags);
511
+ literal.pos = node.pos;
512
+ literal.end = node.end;
513
+ return literal;
514
+ }
515
+ if (node instanceof JsxExpression) {
516
+ if (node.expression === undefined) return factory.createTrue();
517
+ return this.visit(node.expression)!;
518
+ }
519
+ if (
520
+ node.kind === SyntaxKind.JsxElement ||
521
+ node.kind === SyntaxKind.JsxSelfClosingElement ||
522
+ node.kind === SyntaxKind.JsxFragment
523
+ ) {
524
+ this.inChild = false;
525
+ return this.visit(node)!;
526
+ }
527
+ throw new Error('Unhandled JSX attribute initializer');
528
+ }
529
+
530
+ // The classic factory
531
+
532
+ /** `React`, or `ns.React` when it names an export of an enclosing namespace. */
533
+ private createReactNamespace(namespace: string, parent: Node): Node {
534
+ const factory = this.factory;
535
+ const react = factory.createIdentifier(namespace === '' ? 'React' : namespace);
536
+ // Resolved as if in the parse tree, from the element's scope.
537
+ react.flags &= ~NodeFlags.Synthesized;
538
+ react.parent = this.context.parseNode(parent);
539
+ const container = this.resolver.getReferencedExportContainer(react, false);
540
+ if (container instanceof ModuleDeclaration)
541
+ return factory.createPropertyAccessExpression(factory.createGeneratedNameForNode(container, 0, '', ''), react);
542
+ return react;
543
+ }
544
+
545
+ private expressionFromEntityName(entity: Node, parent: Node): Node {
546
+ if (entity instanceof QualifiedName) {
547
+ const left = this.expressionFromEntityName(entity.left, parent);
548
+ return this.factory.createPropertyAccessExpression(left, this.factory.createIdentifier(entity.right.text));
549
+ }
550
+ return this.createReactNamespace((entity as Identifier).text, parent);
551
+ }
552
+
553
+ private pseudoFactoryExpression(parent: Node, entity: Node | undefined, member: string): Node {
554
+ if (entity !== undefined) return this.expressionFromEntityName(entity, parent);
555
+ const namespace = this.options.reactNamespace;
556
+ return this.factory.createPropertyAccessExpression(
557
+ this.createReactNamespace(namespace !== undefined ? namespace : '', parent),
558
+ this.factory.createIdentifier(member),
559
+ );
560
+ }
561
+
562
+ private factoryExpression(parent: Node): Node {
563
+ return this.pseudoFactoryExpression(parent, this.resolver.getJsxFactoryEntity(this.file!), 'createElement');
564
+ }
565
+
566
+ private fragmentFactoryExpression(parent: Node): Node {
567
+ return this.pseudoFactoryExpression(parent, this.resolver.getJsxFragmentFactoryEntity(this.file!), 'Fragment');
568
+ }
569
+
570
+ private visitCreateElement(
571
+ element: Node,
572
+ attributes: NodeList,
573
+ children: NodeList | undefined,
574
+ pos: number,
575
+ end: number,
576
+ ): Node {
577
+ const factory = this.factory;
578
+ const tagName = this.tagName(element);
579
+ // React wants `null` when there are no attributes.
580
+ const props =
581
+ attributes.nodes.length > 0
582
+ ? this.attributesToObject(attributes.nodes, undefined)
583
+ : factory.createKeywordExpression(SyntaxKind.NullKeyword);
584
+ const callee = this.importSource === '' ? this.factoryExpression(element) : this.implicitImport('createElement');
585
+ return this.createElementCall(callee, tagName, props, children, pos, end);
586
+ }
587
+
588
+ private visitFragmentCreateElement(fragment: Node, children: NodeList, pos: number, end: number): Node {
589
+ const tagName = this.fragmentFactoryExpression(fragment);
590
+ const callee = this.factoryExpression(fragment);
591
+ return this.createElementCall(
592
+ callee,
593
+ tagName,
594
+ this.factory.createKeywordExpression(SyntaxKind.NullKeyword),
595
+ children,
596
+ pos,
597
+ end,
598
+ );
599
+ }
600
+
601
+ /** `React.createElement(type, props, ...children)` */
602
+ private createElementCall(
603
+ callee: Node,
604
+ tagName: Node,
605
+ props: Node,
606
+ children: NodeList | undefined,
607
+ pos: number,
608
+ end: number,
609
+ ): Node {
610
+ const args: Node[] = [tagName, props];
611
+ if (children !== undefined) {
612
+ const first = args.length;
613
+ for (let i = 0; i < children.nodes.length; i++) {
614
+ const result = this.visitChild(children.nodes[i]);
615
+ if (result !== undefined) args.push(result);
616
+ }
617
+ // Several children each start on a new line.
618
+ if (args.length - first > 1)
619
+ for (let i = first; i < args.length; i++) this.context.addEmitFlags(args[i], EmitFlags.StartOnNewLine);
620
+ }
621
+ const call = this.factory.createCallExpression(callee, args);
622
+ call.pos = pos | 0;
623
+ call.end = end | 0;
624
+ if (this.inChild) this.context.addEmitFlags(call, EmitFlags.StartOnNewLine);
625
+ return call;
626
+ }
627
+
628
+ // Children
629
+
630
+ private visitJsxText(text: JsxText): Node | undefined {
631
+ const fixed = fixupWhitespaceAndDecodeEntities(text.text);
632
+ return fixed === '' ? undefined : this.factory.createStringLiteral(fixed, TokenFlags.None);
633
+ }
634
+
635
+ private visitJsxExpression(expression: JsxExpression): Node | undefined {
636
+ const visited = this.visitNode(expression.expression);
637
+ if (expression.dotDotDotToken !== undefined) return this.factory.created(new SpreadElement(visited!));
638
+ return visited;
639
+ }
640
+ }
641
+
642
+ /** Arguments of `Object.assign`: object literals of consecutive properties and spread expressions. */
643
+ class AssignSegments {
644
+ readonly expressions: Node[];
645
+ properties: Node[];
646
+
647
+ constructor() {
648
+ this.expressions = [];
649
+ this.properties = [];
650
+ }
651
+
652
+ push(expression: Node | undefined): void {
653
+ if (expression !== undefined) this.expressions.push(expression);
654
+ }
655
+
656
+ flush(factory: NodeFactory): void {
657
+ if (this.properties.length === 0) return;
658
+ this.expressions.push(factory.createObjectLiteral(this.properties, false));
659
+ this.properties = [];
660
+ }
661
+ }
662
+
663
+ function compareSpecifiers(a: ImportSpecifier, b: ImportSpecifier): number {
664
+ const x = (a.propertyName as Identifier).text;
665
+ const y = (b.propertyName as Identifier).text;
666
+ if (x !== y) return x < y ? -1 : 1;
667
+ const p = a.name.text;
668
+ const q = b.name.text;
669
+ return p === q ? 0 : p < q ? -1 : 1;
670
+ }
671
+
672
+ function isIntrinsicJsxName(name: string): boolean {
673
+ if (name.length === 0) return false;
674
+ const ch = name.charCodeAt(0);
675
+ return (ch >= 0x61 && ch <= 0x7a) || name.indexOf('-') >= 0;
676
+ }
677
+
678
+ function isSpreadChild(node: Node): boolean {
679
+ return node instanceof JsxExpression && node.dotDotDotToken !== undefined;
680
+ }
681
+
682
+ /** Children other than whitespace-only text and empty expressions. */
683
+ function getSemanticJsxChildren(children: Node[]): Node[] {
684
+ const result: Node[] = [];
685
+ for (let i = 0; i < children.length; i++) {
686
+ const child = children[i];
687
+ if (child instanceof JsxExpression && child.expression === undefined) continue;
688
+ if (child instanceof JsxText && child.containsOnlyTriviaWhiteSpaces) continue;
689
+ result.push(child);
690
+ }
691
+ return result;
692
+ }
693
+
694
+ /** The react `jsx`/`jsxs` functions fall back to `createElement` for a `key` after a spread. */
695
+ function hasKeyAfterPropsSpread(attributes: NodeList): boolean {
696
+ let spread = false;
697
+ const nodes = attributes.nodes;
698
+ for (let i = 0; i < nodes.length; i++) {
699
+ const attribute = nodes[i];
700
+ if (attribute instanceof JsxSpreadAttribute) {
701
+ const expression = attribute.expression;
702
+ if (!(expression instanceof ObjectLiteralExpression) || hasSpreadAssignment(expression)) spread = true;
703
+ } else if (
704
+ spread &&
705
+ attribute instanceof JsxAttribute &&
706
+ attribute.name instanceof Identifier &&
707
+ attribute.name.text === 'key'
708
+ )
709
+ return true;
710
+ }
711
+ return false;
712
+ }
713
+
714
+ function hasSpreadAssignment(object: ObjectLiteralExpression): boolean {
715
+ const properties = object.properties.nodes;
716
+ for (let i = 0; i < properties.length; i++) if (properties[i] instanceof SpreadAssignment) return true;
717
+ return false;
718
+ }
719
+
720
+ function hasProto(object: ObjectLiteralExpression): boolean {
721
+ const properties = object.properties.nodes;
722
+ for (let i = 0; i < properties.length; i++) {
723
+ const property = properties[i];
724
+ if (!(property instanceof PropertyAssignment)) continue;
725
+ const name = property.name;
726
+ if (name instanceof StringLiteral && name.text === '__proto__') return true;
727
+ if (name instanceof Identifier && name.text === '__proto__') return true;
728
+ }
729
+ return false;
730
+ }
731
+
732
+ /**
733
+ * Trims whitespace at the start and end of each line, drops empty lines and joins the rest
734
+ * with spaces, decoding entities. Whitespace next to a tag on the same line is kept.
735
+ */
736
+ export function fixupWhitespaceAndDecodeEntities(text: string): string {
737
+ let result = '';
738
+ let initial = true;
739
+ // The first line keeps leading whitespace but is dropped when it is all whitespace.
740
+ let firstNonWhitespace = 0;
741
+ let lastNonWhitespace = -1;
742
+ for (let i = 0; i < text.length; i++) {
743
+ const ch = text.charCodeAt(i);
744
+ if (isLineBreak(ch)) {
745
+ if (firstNonWhitespace !== -1 && lastNonWhitespace !== -1) {
746
+ if (!initial) result += ' ';
747
+ result += decodeEntities(text.substring(firstNonWhitespace, lastNonWhitespace + 1));
748
+ initial = false;
749
+ }
750
+ firstNonWhitespace = -1;
751
+ } else if (!isWhiteSpaceSingleLine(ch)) {
752
+ lastNonWhitespace = i;
753
+ if (firstNonWhitespace === -1) firstNonWhitespace = i;
754
+ }
755
+ }
756
+ if (firstNonWhitespace !== -1) {
757
+ if (!initial) result += ' ';
758
+ result += decodeEntities(text.substring(firstNonWhitespace));
759
+ }
760
+ return result;
761
+ }
762
+
763
+ /** Replaces entities such as `&nbsp;`, `&#123;` and `&#x1F600;` with the characters they encode. */
764
+ export function decodeEntities(text: string): string {
765
+ let at = text.indexOf('&');
766
+ if (at < 0) return text;
767
+ let result = '';
768
+ for (;;) {
769
+ result += text.substring(0, at);
770
+ text = text.substring(at);
771
+ let semi = text.indexOf(';');
772
+ if (semi < 0) break;
773
+ // An `&` before the `;` starts no entity of the earlier `&`.
774
+ for (;;) {
775
+ const next = text.substring(1, semi).indexOf('&');
776
+ if (next < 0) break;
777
+ result += text.substring(0, next + 1);
778
+ text = text.substring(next + 1);
779
+ semi -= next + 1;
780
+ }
781
+ const decoded = decodeEntity(text.substring(1, semi));
782
+ result += decoded >= 0 ? codePointToString(decoded) : text.substring(0, semi + 1);
783
+ text = text.substring(semi + 1);
784
+ at = text.indexOf('&');
785
+ if (at < 0) break;
786
+ }
787
+ return result + text;
788
+ }
789
+
790
+ /** The code point an entity name such as `amp` or `#x26` stands for, or -1. */
791
+ function decodeEntity(entity: string): number {
792
+ if (entity.length === 0) return -1;
793
+ if (entity.charCodeAt(0) !== 0x23 /* # */) {
794
+ const value = entities.get(entity);
795
+ return value !== undefined ? value : -1;
796
+ }
797
+ let digits = entity.substring(1);
798
+ let hex = false;
799
+ if (digits.length > 0 && digits.charCodeAt(0) === 0x78 /* x */) {
800
+ hex = true;
801
+ digits = digits.substring(1);
802
+ }
803
+ if (digits.length === 0) return -1;
804
+ let value = 0;
805
+ for (let i = 0; i < digits.length; i++) {
806
+ const ch = digits.charCodeAt(i);
807
+ let digit = -1;
808
+ if (ch >= 0x30 && ch <= 0x39) digit = ch - 0x30;
809
+ else if (hex && ch >= 0x61 && ch <= 0x66) digit = ch - 0x61 + 10;
810
+ else if (hex && ch >= 0x41 && ch <= 0x46) digit = ch - 0x41 + 10;
811
+ if (digit < 0) return -1;
812
+ value = value * (hex ? 16 : 10) + digit;
813
+ // Values beyond 32 bits don't parse.
814
+ if (value > 0x7fffffff) return -1;
815
+ }
816
+ return value;
817
+ }
818
+
819
+ function codePointToString(code: number): string {
820
+ // Lone surrogates are kept; values beyond Unicode become U+FFFD.
821
+ if (code > 0x10ffff) return '�';
822
+ if (code >= 0xd800 && code <= 0xdfff) return String.fromCharCode(code);
823
+ return String.fromCodePoint(code);
824
+ }