tsc-ts 0.0.9 → 0.1.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 (373) hide show
  1. package/README.md +2 -2
  2. package/bin/tsc-ts.js +70 -1
  3. package/dist/api/compiler/ast-utilities.d.ts +19 -1
  4. package/dist/api/compiler/ast-utilities.js +180 -95
  5. package/dist/api/compiler/ast.d.ts +22 -15
  6. package/dist/api/compiler/ast.js +1026 -995
  7. package/dist/api/compiler/astnav.js +15 -15
  8. package/dist/api/compiler/binder-utilities.js +186 -186
  9. package/dist/api/compiler/binder.js +290 -263
  10. package/dist/api/compiler/character-codes.js +2 -2
  11. package/dist/api/compiler/checker-calls.js +178 -124
  12. package/dist/api/compiler/checker-classes.d.ts +14 -3
  13. package/dist/api/compiler/checker-classes.js +134 -90
  14. package/dist/api/compiler/checker-contextual.js +64 -61
  15. package/dist/api/compiler/checker-declaration-order.js +79 -79
  16. package/dist/api/compiler/checker-declarations.d.ts +1 -0
  17. package/dist/api/compiler/checker-declarations.js +160 -136
  18. package/dist/api/compiler/checker-declared-types.js +67 -65
  19. package/dist/api/compiler/checker-decorators.js +77 -77
  20. package/dist/api/compiler/checker-deprecation.d.ts +8 -3
  21. package/dist/api/compiler/checker-deprecation.js +76 -55
  22. package/dist/api/compiler/checker-destructuring.js +47 -45
  23. package/dist/api/compiler/checker-elaboration.js +24 -24
  24. package/dist/api/compiler/checker-emit-helpers.js +4 -4
  25. package/dist/api/compiler/checker-emit-resolver.d.ts +4 -2
  26. package/dist/api/compiler/checker-emit-resolver.js +25 -13
  27. package/dist/api/compiler/checker-evaluator.js +9 -9
  28. package/dist/api/compiler/checker-expressions.js +244 -211
  29. package/dist/api/compiler/checker-functions.js +113 -107
  30. package/dist/api/compiler/checker-generated-code.d.ts +0 -11
  31. package/dist/api/compiler/checker-generated-code.js +28 -52
  32. package/dist/api/compiler/checker-grammar.d.ts +1 -0
  33. package/dist/api/compiler/checker-grammar.js +472 -440
  34. package/dist/api/compiler/checker-heritage.js +91 -93
  35. package/dist/api/compiler/checker-inference.d.ts +77 -5
  36. package/dist/api/compiler/checker-inference.js +487 -107
  37. package/dist/api/compiler/checker-iteration.d.ts +0 -10
  38. package/dist/api/compiler/checker-iteration.js +35 -52
  39. package/dist/api/compiler/checker-javascript.js +14 -14
  40. package/dist/api/compiler/checker-jsdoc.d.ts +8 -10
  41. package/dist/api/compiler/checker-jsdoc.js +67 -58
  42. package/dist/api/compiler/checker-jsx.js +105 -88
  43. package/dist/api/compiler/checker-members.d.ts +9 -2
  44. package/dist/api/compiler/checker-members.js +212 -75
  45. package/dist/api/compiler/checker-modules.d.ts +15 -6
  46. package/dist/api/compiler/checker-modules.js +469 -309
  47. package/dist/api/compiler/checker-narrowing.js +273 -189
  48. package/dist/api/compiler/checker-object-literals.js +25 -22
  49. package/dist/api/compiler/checker-operators.js +126 -118
  50. package/dist/api/compiler/checker-overloads.js +27 -27
  51. package/dist/api/compiler/checker-property-access.js +59 -55
  52. package/dist/api/compiler/checker-services.d.ts +1 -0
  53. package/dist/api/compiler/checker-services.js +222 -210
  54. package/dist/api/compiler/checker-signatures.js +65 -49
  55. package/dist/api/compiler/checker-source-file.d.ts +5 -0
  56. package/dist/api/compiler/checker-source-file.js +154 -130
  57. package/dist/api/compiler/checker-statements.js +33 -32
  58. package/dist/api/compiler/checker-super.js +50 -50
  59. package/dist/api/compiler/checker-symbol-types.js +109 -93
  60. package/dist/api/compiler/checker-symbols.d.ts +13 -1
  61. package/dist/api/compiler/checker-symbols.js +264 -149
  62. package/dist/api/compiler/checker-type-factory.d.ts +5 -0
  63. package/dist/api/compiler/checker-type-factory.js +145 -36
  64. package/dist/api/compiler/checker-type-nodes.d.ts +1 -0
  65. package/dist/api/compiler/checker-type-nodes.js +179 -141
  66. package/dist/api/compiler/checker-type-operators.d.ts +2 -0
  67. package/dist/api/compiler/checker-type-operators.js +110 -63
  68. package/dist/api/compiler/checker-types.d.ts +80 -3
  69. package/dist/api/compiler/checker-types.js +121 -2
  70. package/dist/api/compiler/checker-unused.js +114 -118
  71. package/dist/api/compiler/checker-utilities.js +11 -11
  72. package/dist/api/compiler/checker.d.ts +104 -31
  73. package/dist/api/compiler/checker.js +181 -33
  74. package/dist/api/compiler/command-line.d.ts +1 -3
  75. package/dist/api/compiler/command-line.js +47 -44
  76. package/dist/api/compiler/compile-program.d.ts +42 -3
  77. package/dist/api/compiler/compile-program.js +139 -27
  78. package/dist/api/compiler/diagnostic-messages.d.ts +1849 -1774
  79. package/dist/api/compiler/diagnostic-messages.js +128 -122
  80. package/dist/api/compiler/diagnostics.d.ts +22 -0
  81. package/dist/api/compiler/diagnostics.js +93 -11
  82. package/dist/api/compiler/emitter.d.ts +7 -1
  83. package/dist/api/compiler/emitter.js +53 -11
  84. package/dist/api/compiler/file-include.d.ts +2 -1
  85. package/dist/api/compiler/file-include.js +11 -7
  86. package/dist/api/compiler/file-loader.d.ts +21 -1
  87. package/dist/api/compiler/file-loader.js +201 -48
  88. package/dist/api/compiler/instantiation.d.ts +7 -0
  89. package/dist/api/compiler/instantiation.js +101 -49
  90. package/dist/api/compiler/jsdoc.js +23 -23
  91. package/dist/api/compiler/link-tables.d.ts +48 -18
  92. package/dist/api/compiler/link-tables.js +80 -37
  93. package/dist/api/compiler/locales/cs-CZ.d.ts +1 -1
  94. package/dist/api/compiler/locales/cs-CZ.js +1 -1
  95. package/dist/api/compiler/locales/de-DE.d.ts +1 -1
  96. package/dist/api/compiler/locales/de-DE.js +1 -1
  97. package/dist/api/compiler/locales/es-ES.d.ts +1 -1
  98. package/dist/api/compiler/locales/es-ES.js +1 -1
  99. package/dist/api/compiler/locales/fr-FR.d.ts +1 -1
  100. package/dist/api/compiler/locales/fr-FR.js +1 -1
  101. package/dist/api/compiler/locales/it-IT.d.ts +1 -1
  102. package/dist/api/compiler/locales/it-IT.js +1 -1
  103. package/dist/api/compiler/locales/ja-JP.d.ts +1 -1
  104. package/dist/api/compiler/locales/ja-JP.js +1 -1
  105. package/dist/api/compiler/locales/ko-KR.d.ts +1 -1
  106. package/dist/api/compiler/locales/ko-KR.js +1 -1
  107. package/dist/api/compiler/locales/pl-PL.d.ts +1 -1
  108. package/dist/api/compiler/locales/pl-PL.js +1 -1
  109. package/dist/api/compiler/locales/pt-BR.d.ts +1 -1
  110. package/dist/api/compiler/locales/pt-BR.js +1 -1
  111. package/dist/api/compiler/locales/ru-RU.d.ts +1 -1
  112. package/dist/api/compiler/locales/ru-RU.js +1 -1
  113. package/dist/api/compiler/locales/tr-TR.d.ts +1 -1
  114. package/dist/api/compiler/locales/tr-TR.js +1 -1
  115. package/dist/api/compiler/locales/zh-CN.d.ts +1 -1
  116. package/dist/api/compiler/locales/zh-CN.js +1 -1
  117. package/dist/api/compiler/locales/zh-TW.d.ts +1 -1
  118. package/dist/api/compiler/locales/zh-TW.js +1 -1
  119. package/dist/api/compiler/module-resolution-types.js +6 -6
  120. package/dist/api/compiler/module-resolution.d.ts +10 -1
  121. package/dist/api/compiler/module-resolution.js +115 -84
  122. package/dist/api/compiler/module-specifiers.d.ts +15 -0
  123. package/dist/api/compiler/module-specifiers.js +24 -18
  124. package/dist/api/compiler/name-resolver.js +74 -65
  125. package/dist/api/compiler/node-builder-names.d.ts +11 -3
  126. package/dist/api/compiler/node-builder-names.js +210 -90
  127. package/dist/api/compiler/node-builder-pseudo.js +29 -31
  128. package/dist/api/compiler/node-builder-reuse.js +23 -22
  129. package/dist/api/compiler/node-builder.d.ts +18 -1
  130. package/dist/api/compiler/node-builder.js +191 -102
  131. package/dist/api/compiler/option-declarations.d.ts +2 -6
  132. package/dist/api/compiler/option-declarations.js +315 -375
  133. package/dist/api/compiler/options.d.ts +15 -33
  134. package/dist/api/compiler/options.js +21 -54
  135. package/dist/api/compiler/output-paths.d.ts +4 -1
  136. package/dist/api/compiler/output-paths.js +6 -11
  137. package/dist/api/compiler/package-json.d.ts +3 -3
  138. package/dist/api/compiler/package-json.js +11 -10
  139. package/dist/api/compiler/parallel-parse.d.ts +3 -2
  140. package/dist/api/compiler/parallel-parse.js +17 -5
  141. package/dist/api/compiler/parser.d.ts +40 -19
  142. package/dist/api/compiler/parser.js +308 -216
  143. package/dist/api/compiler/path.d.ts +9 -0
  144. package/dist/api/compiler/path.js +20 -3
  145. package/dist/api/compiler/printer/emit-context.d.ts +5 -1
  146. package/dist/api/compiler/printer/emit-context.js +20 -1
  147. package/dist/api/compiler/printer/factory-base.d.ts +2 -2
  148. package/dist/api/compiler/printer/factory-base.js +7 -3
  149. package/dist/api/compiler/printer/factory.js +3 -1
  150. package/dist/api/compiler/printer/helpers.js +10 -1
  151. package/dist/api/compiler/printer/name-generator.d.ts +1 -1
  152. package/dist/api/compiler/printer/name-generator.js +35 -16
  153. package/dist/api/compiler/printer/printer.d.ts +49 -2
  154. package/dist/api/compiler/printer/printer.js +539 -293
  155. package/dist/api/compiler/printer/utilities.js +149 -147
  156. package/dist/api/compiler/program-checker.d.ts +12 -2
  157. package/dist/api/compiler/program-checker.js +88 -34
  158. package/dist/api/compiler/program-utilities.d.ts +3 -1
  159. package/dist/api/compiler/program-utilities.js +28 -15
  160. package/dist/api/compiler/program.d.ts +4 -6
  161. package/dist/api/compiler/program.js +74 -87
  162. package/dist/api/compiler/pseudo-bigint.d.ts +2 -0
  163. package/dist/api/compiler/pseudo-bigint.js +18 -0
  164. package/dist/api/compiler/regexp-checker.d.ts +1 -1
  165. package/dist/api/compiler/regexp-checker.js +93 -76
  166. package/dist/api/compiler/relations.d.ts +27 -4
  167. package/dist/api/compiler/relations.js +347 -180
  168. package/dist/api/compiler/reparser.js +60 -56
  169. package/dist/api/compiler/scanner.d.ts +19 -2
  170. package/dist/api/compiler/scanner.js +123 -108
  171. package/dist/api/compiler/shared-results.d.ts +463 -0
  172. package/dist/api/compiler/shared-results.js +2128 -0
  173. package/dist/api/compiler/sourcemap.js +2 -2
  174. package/dist/api/compiler/symbol-accessibility.js +87 -74
  175. package/dist/api/compiler/symbol.d.ts +23 -14
  176. package/dist/api/compiler/symbol.js +23 -11
  177. package/dist/api/compiler/syntax-kind.d.ts +198 -197
  178. package/dist/api/compiler/transformers/alias-references.js +247 -139
  179. package/dist/api/compiler/transformers/async-utilities.js +25 -25
  180. package/dist/api/compiler/transformers/async.d.ts +0 -1
  181. package/dist/api/compiler/transformers/async.js +64 -77
  182. package/dist/api/compiler/transformers/class-fields.d.ts +1 -0
  183. package/dist/api/compiler/transformers/class-fields.js +81 -78
  184. package/dist/api/compiler/transformers/commonjs-module.js +18 -15
  185. package/dist/api/compiler/transformers/const-enum-inliner.js +1 -1
  186. package/dist/api/compiler/transformers/declarations-diagnostics.js +247 -244
  187. package/dist/api/compiler/transformers/declarations-pseudo.js +100 -100
  188. package/dist/api/compiler/transformers/declarations-resolver.d.ts +0 -3
  189. package/dist/api/compiler/transformers/declarations-syntactic.d.ts +1 -2
  190. package/dist/api/compiler/transformers/declarations-syntactic.js +134 -156
  191. package/dist/api/compiler/transformers/declarations-tracker.js +18 -18
  192. package/dist/api/compiler/transformers/declarations.d.ts +4 -1
  193. package/dist/api/compiler/transformers/declarations.js +305 -238
  194. package/dist/api/compiler/transformers/decorator-metadata.d.ts +1 -1
  195. package/dist/api/compiler/transformers/decorator-metadata.js +53 -45
  196. package/dist/api/compiler/transformers/decorator-utilities.js +24 -24
  197. package/dist/api/compiler/transformers/destructuring.js +19 -18
  198. package/dist/api/compiler/transformers/emit-resolver.js +1 -1
  199. package/dist/api/compiler/transformers/es-decorators.js +55 -55
  200. package/dist/api/compiler/transformers/external-module-info.js +3 -3
  201. package/dist/api/compiler/transformers/for-await.js +25 -25
  202. package/dist/api/compiler/transformers/import-elision.js +14 -14
  203. package/dist/api/compiler/transformers/jsx.js +12 -12
  204. package/dist/api/compiler/transformers/legacy-decorators.js +17 -17
  205. package/dist/api/compiler/transformers/module.js +8 -8
  206. package/dist/api/compiler/transformers/named-evaluation.js +4 -4
  207. package/dist/api/compiler/transformers/object-rest-spread.js +20 -20
  208. package/dist/api/compiler/transformers/optional-chain.js +10 -10
  209. package/dist/api/compiler/transformers/reference-resolver.d.ts +12 -1
  210. package/dist/api/compiler/transformers/reference-resolver.js +131 -69
  211. package/dist/api/compiler/transformers/runtime-syntax.js +35 -31
  212. package/dist/api/compiler/transformers/subtree-facts.js +47 -47
  213. package/dist/api/compiler/transformers/syntax-facts.js +56 -58
  214. package/dist/api/compiler/transformers/type-eraser.js +73 -66
  215. package/dist/api/compiler/transformers/type-serialization.js +24 -24
  216. package/dist/api/compiler/transformers/using.js +12 -12
  217. package/dist/api/compiler/transformers/utilities.js +77 -77
  218. package/dist/api/compiler/transformers/visitor-base.js +172 -172
  219. package/dist/api/compiler/transformers/visitor-hooks.js +6 -2
  220. package/dist/api/compiler/tsconfig.d.ts +10 -2
  221. package/dist/api/compiler/tsconfig.js +136 -48
  222. package/dist/api/compiler/type-printer.js +40 -3
  223. package/dist/api/compiler/types.d.ts +2 -1
  224. package/dist/api/library/ast.generated.d.ts +42 -39
  225. package/dist/api/library/ast.generated.js +1 -1
  226. package/dist/api/library/compiler-options.generated.d.ts +111 -100
  227. package/dist/api/library/compiler-options.generated.js +1 -1
  228. package/dist/api/library/disk.js +12 -1
  229. package/dist/api/library/enums.generated.d.ts +206 -206
  230. package/dist/api/library/enums.generated.js +206 -206
  231. package/dist/api/library/nodes.js +26 -26
  232. package/package.json +9 -9
  233. package/src/compiler/ast-utilities.ts +99 -7
  234. package/src/compiler/ast.ts +58 -26
  235. package/src/compiler/binder.ts +39 -4
  236. package/src/compiler/character-codes.ts +2 -2
  237. package/src/compiler/checker-calls.ts +79 -9
  238. package/src/compiler/checker-classes.ts +78 -24
  239. package/src/compiler/checker-contextual.ts +6 -1
  240. package/src/compiler/checker-declarations.ts +31 -4
  241. package/src/compiler/checker-declared-types.ts +8 -5
  242. package/src/compiler/checker-decorators.ts +1 -1
  243. package/src/compiler/checker-deprecation.ts +29 -8
  244. package/src/compiler/checker-destructuring.ts +10 -3
  245. package/src/compiler/checker-emit-helpers.ts +1 -0
  246. package/src/compiler/checker-emit-resolver.ts +16 -6
  247. package/src/compiler/checker-expressions.ts +79 -46
  248. package/src/compiler/checker-functions.ts +21 -6
  249. package/src/compiler/checker-generated-code.ts +2 -22
  250. package/src/compiler/checker-grammar.ts +136 -62
  251. package/src/compiler/checker-heritage.ts +22 -21
  252. package/src/compiler/checker-inference.ts +514 -82
  253. package/src/compiler/checker-iteration.ts +18 -31
  254. package/src/compiler/checker-javascript.ts +1 -1
  255. package/src/compiler/checker-jsdoc.ts +50 -27
  256. package/src/compiler/checker-jsx.ts +26 -4
  257. package/src/compiler/checker-members.ts +200 -56
  258. package/src/compiler/checker-modules.ts +332 -65
  259. package/src/compiler/checker-narrowing.ts +98 -19
  260. package/src/compiler/checker-object-literals.ts +3 -0
  261. package/src/compiler/checker-operators.ts +11 -4
  262. package/src/compiler/checker-property-access.ts +7 -2
  263. package/src/compiler/checker-services.ts +39 -12
  264. package/src/compiler/checker-signatures.ts +15 -1
  265. package/src/compiler/checker-source-file.ts +39 -13
  266. package/src/compiler/checker-statements.ts +3 -2
  267. package/src/compiler/checker-symbol-types.ts +29 -6
  268. package/src/compiler/checker-symbols.ts +158 -15
  269. package/src/compiler/checker-type-factory.ts +124 -23
  270. package/src/compiler/checker-type-nodes.ts +46 -3
  271. package/src/compiler/checker-type-operators.ts +83 -26
  272. package/src/compiler/checker-types.ts +147 -4
  273. package/src/compiler/checker-unused.ts +9 -14
  274. package/src/compiler/checker.ts +203 -39
  275. package/src/compiler/command-line.ts +28 -28
  276. package/src/compiler/compile-program.ts +167 -27
  277. package/src/compiler/decoder.ts +13 -4
  278. package/src/compiler/diagnostic-messages.ts +1973 -1892
  279. package/src/compiler/diagnostics.ts +84 -3
  280. package/src/compiler/emitter.ts +57 -8
  281. package/src/compiler/encoder.ts +23 -8
  282. package/src/compiler/file-include.ts +9 -3
  283. package/src/compiler/file-loader.ts +197 -38
  284. package/src/compiler/help.ts +10 -14
  285. package/src/compiler/incremental.ts +165 -30
  286. package/src/compiler/init.ts +2 -7
  287. package/src/compiler/instantiation.ts +77 -22
  288. package/src/compiler/jsdoc.ts +3 -1
  289. package/src/compiler/link-tables.ts +82 -37
  290. package/src/compiler/locales/cs-CZ.ts +1 -1
  291. package/src/compiler/locales/de-DE.ts +1 -1
  292. package/src/compiler/locales/es-ES.ts +1 -1
  293. package/src/compiler/locales/fr-FR.ts +1 -1
  294. package/src/compiler/locales/it-IT.ts +1 -1
  295. package/src/compiler/locales/ja-JP.ts +1 -1
  296. package/src/compiler/locales/ko-KR.ts +1 -1
  297. package/src/compiler/locales/pl-PL.ts +1 -1
  298. package/src/compiler/locales/pt-BR.ts +1 -1
  299. package/src/compiler/locales/ru-RU.ts +1 -1
  300. package/src/compiler/locales/tr-TR.ts +1 -1
  301. package/src/compiler/locales/zh-CN.ts +1 -1
  302. package/src/compiler/locales/zh-TW.ts +1 -1
  303. package/src/compiler/module-resolution.ts +44 -8
  304. package/src/compiler/module-specifiers.ts +62 -20
  305. package/src/compiler/name-resolver.ts +9 -1
  306. package/src/compiler/node-builder-names.ts +236 -54
  307. package/src/compiler/node-builder-pseudo.ts +4 -7
  308. package/src/compiler/node-builder-reuse.ts +5 -4
  309. package/src/compiler/node-builder.ts +130 -31
  310. package/src/compiler/option-declarations.ts +33 -103
  311. package/src/compiler/options.ts +23 -62
  312. package/src/compiler/output-paths.ts +5 -18
  313. package/src/compiler/package-entrypoints.ts +345 -0
  314. package/src/compiler/package-json.ts +4 -4
  315. package/src/compiler/parallel-check.ts +326 -52
  316. package/src/compiler/parallel-emit.ts +320 -0
  317. package/src/compiler/parallel-parse.ts +16 -5
  318. package/src/compiler/parse-thread.ts +3 -0
  319. package/src/compiler/parser.ts +156 -39
  320. package/src/compiler/path.ts +22 -2
  321. package/src/compiler/printer/emit-context.ts +21 -0
  322. package/src/compiler/printer/factory-base.ts +10 -3
  323. package/src/compiler/printer/factory.ts +4 -1
  324. package/src/compiler/printer/helpers.ts +10 -1
  325. package/src/compiler/printer/name-generator.ts +24 -2
  326. package/src/compiler/printer/printer.ts +266 -30
  327. package/src/compiler/printer/utilities.ts +8 -2
  328. package/src/compiler/program-checker.ts +57 -5
  329. package/src/compiler/program-utilities.ts +23 -10
  330. package/src/compiler/program.ts +18 -26
  331. package/src/compiler/pseudo-bigint.ts +17 -0
  332. package/src/compiler/regexp-checker.ts +63 -30
  333. package/src/compiler/relations.ts +240 -77
  334. package/src/compiler/reparser.ts +7 -2
  335. package/src/compiler/scanner.ts +109 -79
  336. package/src/compiler/shared-results.ts +2414 -0
  337. package/src/compiler/show-config.ts +18 -1
  338. package/src/compiler/sourcemap.ts +2 -2
  339. package/src/compiler/symbol-accessibility.ts +26 -3
  340. package/src/compiler/symbol.ts +46 -24
  341. package/src/compiler/syntax-kind.ts +188 -187
  342. package/src/compiler/thread-entry.ts +64 -4
  343. package/src/compiler/transformers/alias-references.ts +123 -4
  344. package/src/compiler/transformers/async.ts +2 -17
  345. package/src/compiler/transformers/class-fields.ts +13 -12
  346. package/src/compiler/transformers/commonjs-module.ts +11 -7
  347. package/src/compiler/transformers/declarations-diagnostics.ts +5 -3
  348. package/src/compiler/transformers/declarations-pseudo.ts +2 -4
  349. package/src/compiler/transformers/declarations-resolver.ts +0 -3
  350. package/src/compiler/transformers/declarations-syntactic.ts +1 -21
  351. package/src/compiler/transformers/declarations.ts +134 -48
  352. package/src/compiler/transformers/decorator-metadata.ts +9 -1
  353. package/src/compiler/transformers/destructuring.ts +4 -3
  354. package/src/compiler/transformers/external-module-info.ts +1 -1
  355. package/src/compiler/transformers/jsx.ts +2 -1
  356. package/src/compiler/transformers/reference-resolver.ts +83 -19
  357. package/src/compiler/transformers/runtime-syntax.ts +6 -1
  358. package/src/compiler/transformers/syntax-facts.ts +2 -7
  359. package/src/compiler/transformers/type-eraser.ts +10 -1
  360. package/src/compiler/transformers/type-serialization.ts +3 -2
  361. package/src/compiler/transformers/visitor-base.ts +2 -1
  362. package/src/compiler/transformers/visitor-hooks.ts +9 -4
  363. package/src/compiler/tsc-build.ts +66 -9
  364. package/src/compiler/tsc-cli.ts +7 -0
  365. package/src/compiler/tsc-compile.ts +284 -4
  366. package/src/compiler/tsc-watch.ts +74 -45
  367. package/src/compiler/tsconfig.ts +176 -19
  368. package/src/compiler/type-printer.ts +36 -2
  369. package/src/compiler/types.ts +2 -1
  370. package/src/library/ast.generated.ts +57 -41
  371. package/src/library/compiler-options.generated.ts +117 -103
  372. package/src/library/disk.ts +10 -1
  373. package/src/library/enums.generated.ts +196 -196
@@ -141,6 +141,12 @@ import {
141
141
  } from './symbol.js';
142
142
 
143
143
  export function getTypeOfSymbol(c: Checker, symbol: Symbol): Type {
144
+ // TSC_TS_SHARE: checker threads share the value types of variables and properties (shared-results.ts).
145
+ if (c.threadSharing !== undefined && isSharedValueSymbol(symbol)) {
146
+ const sharing = c.threadSharing;
147
+ if (sharing.computing === symbol) sharing.computing = undefined;
148
+ else return threadTypeOfSymbol(c, symbol);
149
+ }
144
150
  const checkFlags = symbol.checkFlags;
145
151
  if ((checkFlags & CheckFlags.DeferredType) !== 0) return getTypeOfSymbolWithDeferredType(c, symbol);
146
152
  if ((checkFlags & CheckFlags.Instantiated) !== 0) return getTypeOfInstantiatedSymbol(c, symbol);
@@ -277,7 +283,13 @@ function getTypeOfVariableOrParameterOrPropertyWorker(c: Checker, symbol: Symbol
277
283
  case SyntaxKind.PropertySignature:
278
284
  case SyntaxKind.VariableDeclaration:
279
285
  case SyntaxKind.BindingElement:
280
- result = getWidenedTypeForVariableLikeDeclaration(c, declaration, true);
286
+ // A parameter of a context sensitive function may get another type when its context is known, so
287
+ // its widening errors are left to that.
288
+ result = getWidenedTypeForVariableLikeDeclaration(
289
+ c,
290
+ declaration,
291
+ !isParameterOfContextSensitiveSignature(c, symbol),
292
+ );
281
293
  break;
282
294
  case SyntaxKind.PropertyAssignment:
283
295
  case SyntaxKind.ShorthandPropertyAssignment:
@@ -684,11 +696,21 @@ export function reportImplicitAny(
684
696
  return;
685
697
  }
686
698
  if (c.noImplicitAny && (declaration.flags & NodeFlags.Reparsed) !== 0) {
687
- c.error(
688
- declaration,
689
- Diagnostics.This_overload_implicitly_returns_the_type_0_because_it_lacks_a_return_type_annotation,
690
- typeAsString,
691
- );
699
+ // A JSDoc `@overload`.
700
+ const nameText = declarationNameToString(file, getNameOfDeclaration(declaration));
701
+ if (nameText !== '')
702
+ c.error(
703
+ declaration,
704
+ Diagnostics.X_0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type,
705
+ nameText,
706
+ typeAsString,
707
+ );
708
+ else
709
+ c.error(
710
+ declaration,
711
+ Diagnostics.This_overload_implicitly_returns_the_type_0_because_it_lacks_a_return_type_annotation,
712
+ typeAsString,
713
+ );
692
714
  return;
693
715
  }
694
716
  if (!c.noImplicitAny)
@@ -980,6 +1002,7 @@ import { getParameterTypeOfFullSignature } from './checker-jsdoc.js';
980
1002
  import { isFunctionLikeDeclarationKind, isInJSFile } from './ast-utilities.js';
981
1003
  import { isEmptyArrayLiteralType } from './relations.js';
982
1004
  import { isCheckJsEnabledForFile } from './program-utilities.js';
1005
+ import { isSharedValueSymbol, threadTypeOfSymbol } from './shared-results.js';
983
1006
 
984
1007
  function getDeclarationModifierFlags(c: Checker, symbol: Symbol): ModifierFlags {
985
1008
  return getDeclarationModifierFlagsFromSymbol(c, symbol, false);
@@ -5,14 +5,16 @@ import { isTypeAssignableToKindEx } from './checker-type-operators.js';
5
5
  import {
6
6
  BinaryExpression,
7
7
  type ElementAccessExpression,
8
+ type ExportAssignment,
8
9
  type Identifier,
9
10
  ModuleDeclaration,
10
11
  type Node,
11
12
  NodeFlags,
12
- type PatternAmbientModule,
13
+ PatternAmbientModule,
13
14
  type QualifiedName,
14
15
  type SourceFile,
15
16
  Tristate,
17
+ type TypeReferenceNode,
16
18
  getExpression,
17
19
  getInitializer,
18
20
  getMembers as getMemberNodes,
@@ -61,7 +63,12 @@ import { getDeclaredTypeOfSymbol } from './checker-declared-types.js';
61
63
  import { symbolToString } from './type-printer.js';
62
64
  import { getTypeOfSymbol } from './checker-symbol-types.js';
63
65
  import { getPropertyOfType } from './checker-members.js';
64
- import { checkComputedPropertyName, checkExpressionCached, entityNameToString } from './checker-expressions.js';
66
+ import {
67
+ checkComputedPropertyName,
68
+ checkExpressionCached,
69
+ entityNameToString,
70
+ isConstTypeReference,
71
+ } from './checker-expressions.js';
65
72
  import {
66
73
  getLateBindableNameOfDeclaration,
67
74
  getPropertyNameFromType,
@@ -74,6 +81,7 @@ import {
74
81
  markAliasDeclarationOfNameIfTypeOnly,
75
82
  getExportsOfModuleWorker,
76
83
  getTargetOfAliasDeclaration,
84
+ getTypeOfModuleImportAttributes,
77
85
  getTypeOnlyAliasDeclaration,
78
86
  isTypeOnlyImportOrExportDeclaration,
79
87
  resolveExternalModuleNameWorker,
@@ -81,6 +89,7 @@ import {
81
89
  resolveIndirectionAlias,
82
90
  } from './checker-modules.js';
83
91
  import { isJSAliasDeclaration } from './checker-javascript.js';
92
+ import { isTypeIdenticalTo } from './relations.js';
84
93
  import { getFeatureMap } from './checker-features.js';
85
94
  import { NodeBuilderInternalFlags } from './transformers/declarations-resolver.js';
86
95
  import {
@@ -95,6 +104,7 @@ import {
95
104
  Symbol,
96
105
  SymbolFlags,
97
106
  type SymbolTable,
107
+ tryGetAmbientModuleNameFromSymbolName,
98
108
  } from './symbol.js';
99
109
 
100
110
  // ---- Name resolution
@@ -332,6 +342,13 @@ function isES2015OrLaterConstructorName(name: string): boolean {
332
342
  );
333
343
  }
334
344
 
345
+ /**
346
+ * The name-not-found message of an identifier resolved from its own location, chosen only once the name
347
+ * isn't found (`onFailedToResolveSymbol`). Choosing it up front, as tsgo does, compares every resolved
348
+ * identifier's text with the list of names `getCannotFindNameDiagnosticForName` has messages for.
349
+ */
350
+ export const cannotFindNameAtLocation = -1 as DiagnosticMessage;
351
+
335
352
  function onFailedToResolveSymbol(
336
353
  c: Checker,
337
354
  location: Node | undefined,
@@ -339,8 +356,11 @@ function onFailedToResolveSymbol(
339
356
  meaning: SymbolFlags,
340
357
  message: DiagnosticMessage,
341
358
  ): void {
359
+ if (message === cannotFindNameAtLocation) message = getCannotFindNameDiagnosticForName(c, location!);
342
360
  let declarationName = name;
343
361
  if (location !== undefined) {
362
+ // The `const` of a `const` assertion is not a name.
363
+ if (isConstTypeReferenceName(location)) return;
344
364
  if (location.parent !== undefined && location.parent.kind === SyntaxKind.JSDocLink) return;
345
365
  if (
346
366
  checkAndReportErrorForMissingPrefix(c, location, name) ||
@@ -446,8 +466,11 @@ function checkAndReportErrorForExtendingInterface(c: Checker, location: Node): b
446
466
  function getEntityNameForExtendingInterface(node: Node): Node | undefined {
447
467
  switch (node.kind) {
448
468
  case SyntaxKind.Identifier:
469
+ case SyntaxKind.QualifiedName:
449
470
  case SyntaxKind.PropertyAccessExpression:
450
471
  return node.parent !== undefined ? getEntityNameForExtendingInterface(node.parent) : undefined;
472
+ case SyntaxKind.TypeReference:
473
+ return (node as TypeReferenceNode).typeName;
451
474
  case SyntaxKind.ExpressionWithTypeArguments: {
452
475
  const expression = getExpression(node)!;
453
476
  return isEntityNameExpression(expression, false) ? expression : undefined;
@@ -575,7 +598,7 @@ function checkAndReportErrorForUsingTypeAsNamespace(
575
598
  if (symbol === undefined) return false;
576
599
  const parent = location.parent;
577
600
  if (parent !== undefined && parent.kind === SyntaxKind.QualifiedName) {
578
- const propName = (parent as QualifiedName).right.text;
601
+ const propName = getText((parent as QualifiedName).right);
579
602
  if (getPropertyOfType(c, getDeclaredTypeOfSymbol(c, symbol), propName) !== undefined) {
580
603
  c.error(
581
604
  parent,
@@ -602,6 +625,34 @@ function checkAndReportErrorForExportingPrimitiveType(c: Checker, location: Node
602
625
  return false;
603
626
  }
604
627
 
628
+ /** Whether `node` is the `const` of a `const` assertion (`x as const`, `<const>x`). */
629
+ function isConstTypeReferenceName(node: Node): boolean {
630
+ const parent = node.parent;
631
+ return (
632
+ node.kind === SyntaxKind.Identifier &&
633
+ parent !== undefined &&
634
+ isConstTypeReference(parent) &&
635
+ parent.parent !== undefined &&
636
+ (parent.parent.kind === SyntaxKind.AsExpression || parent.parent.kind === SyntaxKind.TypeAssertionExpression)
637
+ );
638
+ }
639
+
640
+ /** Whether `node` is the expression of an `export =` or `export default`, or the leftmost name of it. */
641
+ function isExportAssignmentExpressionName(node: Node): boolean {
642
+ let current = node;
643
+ while (
644
+ current.parent !== undefined &&
645
+ (current.parent.kind === SyntaxKind.PropertyAccessExpression || current.parent.kind === SyntaxKind.QualifiedName)
646
+ )
647
+ current = current.parent;
648
+ const parent = current.parent;
649
+ return (
650
+ parent !== undefined &&
651
+ parent.kind === SyntaxKind.ExportAssignment &&
652
+ (parent as ExportAssignment).expression === current
653
+ );
654
+ }
655
+
605
656
  function checkAndReportErrorForUsingNamespaceAsTypeOrValue(
606
657
  c: Checker,
607
658
  location: Node,
@@ -614,7 +665,9 @@ function checkAndReportErrorForUsingNamespaceAsTypeOrValue(
614
665
  resolveName(c, location, name, SymbolFlags.NamespaceModule, undefined, false, false),
615
666
  );
616
667
  if (symbol !== undefined) {
617
- c.error(location, Diagnostics.Cannot_use_namespace_0_as_a_value, name);
668
+ // checkExportAssignment decides whether `export = ns` is an error.
669
+ if (!isExportAssignmentExpressionName(location))
670
+ c.error(location, Diagnostics.Cannot_use_namespace_0_as_a_value, name);
618
671
  return true;
619
672
  }
620
673
  } else if ((meaning & (SymbolFlags.Type & ~SymbolFlags.Value)) !== 0) {
@@ -670,6 +723,8 @@ function checkAndReportErrorForUsingTypeAsValue(
670
723
  resolveName(c, location, name, SymbolFlags.Type & ~SymbolFlags.Value, undefined, false, false),
671
724
  );
672
725
  if (symbol !== undefined && (getSymbolFlags(c, symbol) & SymbolFlags.Value) === 0) {
726
+ // checkExportAssignment decides whether `export = T` of a type is an error.
727
+ if (isExportAssignmentExpressionName(location)) return true;
673
728
  if (isES2015OrLaterConstructorName(name))
674
729
  c.error(
675
730
  location,
@@ -836,6 +891,7 @@ export class MergedGlobals {
836
891
  readonly ambientModuleSymbols: Symbol[];
837
892
  readonly patternAmbientModules: PatternAmbientModule[];
838
893
  readonly patternAmbientModuleAugmentations: Map<string, Symbol>;
894
+ readonly patternAmbientModuleAugmentationTargets: Map<string, Symbol>;
839
895
  /** Whether the global `undefined` is the checker's own (no declaration of `undefined` merged). */
840
896
  readonly ownUndefined: boolean;
841
897
  /** Ids of the symbols merged into each shared symbol. */
@@ -854,6 +910,7 @@ export class MergedGlobals {
854
910
  this.ambientModuleSymbols = c.ambientModuleSymbols.slice();
855
911
  this.patternAmbientModules = c.patternAmbientModules.slice();
856
912
  this.patternAmbientModuleAugmentations = new Map(c.patternAmbientModuleAugmentations);
913
+ this.patternAmbientModuleAugmentationTargets = new Map(c.patternAmbientModuleAugmentationTargets);
857
914
  for (const [id, target] of this.mergedSymbols) {
858
915
  let ids = this.sources.get(target);
859
916
  if (ids === undefined) {
@@ -970,6 +1027,8 @@ export function initializeGlobals(c: Checker): void {
970
1027
  for (const pattern of merged.patternAmbientModules) c.patternAmbientModules.push(pattern);
971
1028
  for (const [name, symbol] of merged.patternAmbientModuleAugmentations)
972
1029
  c.patternAmbientModuleAugmentations.set(name, symbol);
1030
+ for (const [name, symbol] of merged.patternAmbientModuleAugmentationTargets)
1031
+ c.patternAmbientModuleAugmentationTargets.set(name, symbol);
973
1032
  addMergeDiagnostics(c, merged, 0);
974
1033
  if (merged.ownUndefined) setGlobal(c, 'undefined', c.undefinedSymbol);
975
1034
  return;
@@ -1016,6 +1075,7 @@ export function mergeAmbientModulesAndAugmentations(c: Checker): void {
1016
1075
  return;
1017
1076
  }
1018
1077
  for (const symbol of c.ambientModuleSymbols) mergeGlobalSymbol(c, symbol);
1078
+ mergePatternAmbientModules(c);
1019
1079
  for (const file of c.files)
1020
1080
  for (const augmentation of file.moduleAugmentations)
1021
1081
  if (augmentation.parent !== undefined && !isGlobalScopeAugmentation(augmentation.parent))
@@ -1023,7 +1083,7 @@ export function mergeAmbientModulesAndAugmentations(c: Checker): void {
1023
1083
  }
1024
1084
 
1025
1085
  export function isAmbientModuleSymbolName(name: string): boolean {
1026
- return name.length >= 2 && name.charCodeAt(0) === 0x22 && name.charCodeAt(name.length - 1) === 0x22;
1086
+ return tryGetAmbientModuleNameFromSymbolName(name) !== undefined;
1027
1087
  }
1028
1088
 
1029
1089
  function isTypeDeclaration(node: Node): boolean {
@@ -1053,6 +1113,44 @@ function mergeGlobalSymbol(c: Checker, symbol: Symbol): void {
1053
1113
  );
1054
1114
  }
1055
1115
 
1116
+ /** Merges the pattern ambient modules with the same pattern and identical import attributes types. */
1117
+ function mergePatternAmbientModules(c: Checker): void {
1118
+ const modules = c.patternAmbientModules;
1119
+ if (modules.length === 0) return;
1120
+ const groupsByPattern = new Map<string, number[]>();
1121
+ const grouped: PatternAmbientModule[] = [];
1122
+ for (const module of modules) {
1123
+ const attributesType = getTypeOfModuleImportAttributes(c, module.symbol);
1124
+ let groups = groupsByPattern.get(module.text);
1125
+ let groupIndex = -1;
1126
+ if (groups !== undefined)
1127
+ for (const index of groups)
1128
+ if (isTypeIdenticalTo(c, attributesType, getTypeOfModuleImportAttributes(c, grouped[index]!.symbol))) {
1129
+ groupIndex = index;
1130
+ break;
1131
+ }
1132
+ if (groupIndex === -1) {
1133
+ if (groups === undefined) {
1134
+ groups = [];
1135
+ groupsByPattern.set(module.text, groups);
1136
+ }
1137
+ groups.push(grouped.length);
1138
+ grouped.push(new PatternAmbientModule(module.text, module.starIndex, module.symbol));
1139
+ } else {
1140
+ const group = grouped[groupIndex]!;
1141
+ grouped[groupIndex] = new PatternAmbientModule(
1142
+ group.text,
1143
+ group.starIndex,
1144
+ mergeSymbol(c, group.symbol, module.symbol, false),
1145
+ );
1146
+ }
1147
+ }
1148
+ for (const module of modules)
1149
+ if (c.globals.has(module.symbol.name)) setGlobal(c, module.symbol.name, getMergedSymbol(c, module.symbol));
1150
+ modules.length = 0;
1151
+ for (const module of grouped) modules.push(module);
1152
+ }
1153
+
1056
1154
  function mergeModuleAugmentation(c: Checker, moduleName: Node): void {
1057
1155
  const moduleNode = moduleName.parent as ModuleDeclaration;
1058
1156
  const augmentationSymbol = moduleNode.symbol!;
@@ -1067,14 +1165,25 @@ function mergeModuleAugmentation(c: Checker, moduleName: Node): void {
1067
1165
  (moduleNode.parent!.flags & NodeFlags.Ambient) === 0
1068
1166
  ? Diagnostics.Invalid_module_name_in_augmentation_module_0_cannot_be_found
1069
1167
  : undefined;
1070
- let mainModule = resolveExternalModuleNameWorker(c, moduleName, moduleName, moduleNotFoundError, false, true);
1168
+ // Module augmentations can't have import attributes.
1169
+ let mainModule = resolveExternalModuleNameWorker(
1170
+ c,
1171
+ moduleName,
1172
+ moduleName,
1173
+ moduleNotFoundError,
1174
+ false,
1175
+ true,
1176
+ undefined,
1177
+ );
1071
1178
  if (mainModule === undefined) return;
1072
1179
  mainModule = resolveExternalModuleSymbol(c, mainModule, false)!;
1073
1180
  if ((mainModule.flags & SymbolFlags.Namespace) !== 0) {
1074
1181
  for (const pattern of c.patternAmbientModules)
1075
- if (pattern.symbol === mainModule) {
1182
+ if (getMergedSymbol(c, pattern.symbol) === mainModule) {
1183
+ // Merging the pattern into the augmentation keeps the pattern's own exports unchanged.
1076
1184
  const merged = mergeSymbol(c, augmentationSymbol, mainModule, true);
1077
1185
  c.patternAmbientModuleAugmentations.set(getText(moduleName), merged);
1186
+ c.patternAmbientModuleAugmentationTargets.set(getText(moduleName), mainModule);
1078
1187
  return;
1079
1188
  }
1080
1189
  // Exports the module gets through `export *` live on the re-exported symbols; merge into those.
@@ -1231,11 +1340,7 @@ function hasMatchingDiagnostic(diagnostics: readonly Diagnostic[], a: Diagnostic
1231
1340
  }
1232
1341
 
1233
1342
  function lookupOrIssueError(c: Checker, location: Node, message: DiagnosticMessage, args: string[]) {
1234
- const diagnostic = createDiagnosticForNode(location, message, args);
1235
- const existing = c.diagnostics.lookup(diagnostic);
1236
- if (existing !== undefined) return existing;
1237
- c.addDiagnostic(diagnostic);
1238
- return diagnostic;
1343
+ return c.addDiagnostic(createDiagnosticForNode(location, message, args));
1239
1344
  }
1240
1345
 
1241
1346
  export function getExcludedSymbolFlags(flags: SymbolFlags): SymbolFlags {
@@ -1786,6 +1891,42 @@ export function getNamedMembers(c: Checker, members: SymbolTable | undefined, co
1786
1891
  return contained.length === 0 ? rest : contained.concat(rest);
1787
1892
  }
1788
1893
 
1894
+ /**
1895
+ * `getNamedMembers(c, table, container)` for a table that holds `members` in this order under their names, as
1896
+ * positions in `members`; undefined where a member is an alias (`symbolIsValue` then resolves it) or a name repeats.
1897
+ */
1898
+ export function getNamedMemberPositions(
1899
+ c: Checker,
1900
+ members: Symbol[],
1901
+ container: Symbol | undefined,
1902
+ ): number[] | undefined {
1903
+ const isClassOrInterface =
1904
+ container !== undefined && (container.flags & (SymbolFlags.Class | SymbolFlags.Interface)) !== 0;
1905
+ const names = new Set<string>();
1906
+ const contained: number[] = [];
1907
+ const rest: number[] = [];
1908
+ for (let i = 0; i < members.length; i++) {
1909
+ const symbol = members[i]!;
1910
+ if ((symbol.flags & SymbolFlags.Alias) !== 0 || names.has(symbol.name)) return undefined;
1911
+ names.add(symbol.name);
1912
+ if (isReservedMemberName(symbol.name) || !symbolIsValue(c, symbol)) continue;
1913
+ if (isClassOrInterface && isDeclarationContainedBy(symbol, container!)) contained.push(i);
1914
+ else rest.push(i);
1915
+ }
1916
+ sortPositions(c, members, contained);
1917
+ sortPositions(c, members, rest);
1918
+ return contained.length === 0 ? rest : contained.concat(rest);
1919
+ }
1920
+
1921
+ /** `sortSymbols` for positions in `members`. */
1922
+ function sortPositions(c: Checker, members: Symbol[], positions: number[]): void {
1923
+ for (let i = 1; i < positions.length; i++)
1924
+ if (compareSymbols(c, members[positions[i - 1]!]!, members[positions[i]!]!) > 0) {
1925
+ positions.sort((a, b) => compareSymbols(c, members[a]!, members[b]!));
1926
+ return;
1927
+ }
1928
+ }
1929
+
1789
1930
  /** Sorts symbols by declaration order; members are usually in that order already. */
1790
1931
  function sortSymbols(c: Checker, symbols: Symbol[]): void {
1791
1932
  for (let i = 1; i < symbols.length; i++)
@@ -1821,7 +1962,9 @@ export function resolveEntityName(
1821
1962
  ? undefined
1822
1963
  : meaning === SymbolFlags.Namespace || (name.flags & NodeFlags.Synthesized) !== 0
1823
1964
  ? Diagnostics.Cannot_find_namespace_0
1824
- : getCannotFindNameDiagnosticForName(c, getFirstIdentifier(name));
1965
+ : location === undefined
1966
+ ? cannotFindNameAtLocation
1967
+ : getCannotFindNameDiagnosticForName(c, getFirstIdentifier(name));
1825
1968
  const resolveLocation = location ?? name;
1826
1969
  const text = getText(name);
1827
1970
  if (meaning === SymbolFlags.Namespace) {
@@ -1931,7 +2074,7 @@ function resolveQualifiedName(
1931
2074
  qualified.right,
1932
2075
  Diagnostics.Cannot_access_0_1_because_0_is_a_type_but_not_a_namespace_Did_you_mean_to_retrieve_the_type_of_the_property_1_in_0_with_0_1,
1933
2076
  symbolToString(c, getMergedSymbol(c, exportedType)),
1934
- qualified.right.text,
2077
+ getText(qualified.right),
1935
2078
  );
1936
2079
  return undefined;
1937
2080
  }
@@ -1947,7 +2090,7 @@ function tryGetQualifiedNameAsValue(c: Checker, node: Node): Symbol | undefined
1947
2090
  if (symbol === undefined) return undefined;
1948
2091
  let current: Node = id;
1949
2092
  while (current.parent!.kind === SyntaxKind.QualifiedName) {
1950
- symbol = getPropertyOfType(c, getTypeOfSymbol(c, symbol), (current.parent as QualifiedName).right.text);
2093
+ symbol = getPropertyOfType(c, getTypeOfSymbol(c, symbol), getText((current.parent as QualifiedName).right));
1951
2094
  if (symbol === undefined) return undefined;
1952
2095
  current = current.parent!;
1953
2096
  }
@@ -8,6 +8,7 @@ import { getAliasForTypeNode, getTypeArgumentsOfDeferredReference } from './chec
8
8
  import { TypeSystemPropertyName, popTypeResolution, pushTypeResolution } from './checker-symbols.js';
9
9
  import { isGenericIndexType } from './checker-type-operators.js';
10
10
  import { compareCodePoints } from './diagnostics.js';
11
+ import { utf8Length } from './character-codes.js';
11
12
  import { Diagnostics } from './diagnostic-messages.js';
12
13
  import type { Checker } from './checker.js';
13
14
  import {
@@ -79,6 +80,7 @@ import { addOptionality } from './checker-symbol-types.js';
79
80
  import { getSourceFileOfNode } from './binder-utilities.js';
80
81
  import { isPartOfTypeNode } from './transformers/alias-references.js';
81
82
  import { CheckFlags, declarationCount, getSymbolId, type Symbol, SymbolFlags, type SymbolTable } from './symbol.js';
83
+ import { comparePseudoBigInts } from './pseudo-bigint.js';
82
84
 
83
85
  export const enum UnionReduction {
84
86
  None = 0,
@@ -500,6 +502,9 @@ export function compareNodes(c: Checker, n1: Node | undefined, n2: Node | undefi
500
502
  if (n1 === n2) return 0;
501
503
  if (n1 === undefined) return 1;
502
504
  if (n2 === undefined) return -1;
505
+ // Siblings are in one file: most compared declarations are members of one declaration.
506
+ const parent = n1.parent;
507
+ if (parent !== undefined && parent === n2.parent) return n1.pos - n2.pos;
503
508
  const s1 = sourceFileOf(n1);
504
509
  const s2 = sourceFileOf(n2);
505
510
  if (s1 !== s2) return (c.fileIndex.get(s1) ?? 0) - (c.fileIndex.get(s2) ?? 0);
@@ -562,11 +567,12 @@ function getTypeNameSymbol(t: Type): Symbol | undefined {
562
567
  function compareTypeNames(c: Checker, t1: Type, t2: Type): number {
563
568
  const s1 = getTypeNameSymbol(t1);
564
569
  const s2 = getTypeNameSymbol(t2);
565
- if (s1 === s2)
566
- return t1.alias !== undefined ? compareTypeLists(c, t1.alias.typeArguments, t2.alias!.typeArguments) : 0;
570
+ if (s1 === s2) return compareTypeLists(c, t1.alias?.typeArguments, t2.alias?.typeArguments);
567
571
  if (s1 === undefined) return 1;
568
572
  if (s2 === undefined) return -1;
569
- return compareCodePoints(s1.name, s2.name);
573
+ const r = compareCodePoints(s1.name, s2.name);
574
+ // Distinct declarations with the same name stay together before alias arguments or structure decide.
575
+ return r !== 0 ? r : compareSymbols(c, s1, s2);
570
576
  }
571
577
 
572
578
  function compareTypeLists(c: Checker, s1: readonly Type[] | undefined, s2: readonly Type[] | undefined): number {
@@ -800,8 +806,17 @@ export function compareTypes(c: Checker, t1: Type, t2: Type): number {
800
806
  if ((flags & TypeFlags.Singleton) !== 0) {
801
807
  // Only distinguished by ids.
802
808
  } else if ((flags & TypeFlags.Object) !== 0) {
803
- r = compareSymbols(c, t1.symbol, t2.symbol);
804
- if (r !== 0) return r;
809
+ if ((t1.objectFlags & t2.objectFlags & ObjectFlags.InstantiationExpressionType) !== 0) {
810
+ // Instantiation expression types are ordered by their source declaration, since one expression
811
+ // can produce a type for each union constituent, then by the expression, without symbol ids.
812
+ r = compareNodes(c, t1.symbol?.declaration, t2.symbol?.declaration);
813
+ if (r !== 0) return r;
814
+ r = compareNodes(c, (t1 as InstantiationExpressionType).node, (t2 as InstantiationExpressionType).node);
815
+ if (r !== 0) return r;
816
+ } else {
817
+ r = compareSymbols(c, t1.symbol, t2.symbol);
818
+ if (r !== 0) return r;
819
+ }
805
820
  if ((t1.objectFlags & ObjectFlags.Reference) !== 0 && (t2.objectFlags & ObjectFlags.Reference) !== 0) {
806
821
  const r1 = t1 as TypeReference;
807
822
  const r2 = t2 as TypeReference;
@@ -823,9 +838,28 @@ export function compareTypes(c: Checker, t1: Type, t2: Type): number {
823
838
  } else if ((t1.objectFlags & ObjectFlags.Reference) !== 0) return -1;
824
839
  else if ((t2.objectFlags & ObjectFlags.Reference) !== 0) return 1;
825
840
  else {
841
+ // Unnamed non-reference object types are ordered by kind and instantiation data.
826
842
  r = (t1.objectFlags & ObjectFlags.ObjectTypeKindMask) - (t2.objectFlags & ObjectFlags.ObjectTypeKindMask);
827
843
  if (r !== 0) return r;
828
- r = compareTypeMappers(c, (t1 as ObjectType).mapper, (t2 as ObjectType).mapper);
844
+ if ((t1.objectFlags & ObjectFlags.ReverseMapped) !== 0) {
845
+ const r1 = t1 as ReverseMappedType;
846
+ const r2 = t2 as ReverseMappedType;
847
+ r = compareTypes(c, r1.source!, r2.source!);
848
+ if (r !== 0) return r;
849
+ r = compareTypes(c, r1.mappedType!, r2.mappedType!);
850
+ if (r !== 0) return r;
851
+ r = compareTypes(c, r1.constraintType!, r2.constraintType!);
852
+ if (r !== 0) return r;
853
+ }
854
+ let m1 = (t1 as ObjectType).mapper;
855
+ let m2 = (t2 as ObjectType).mapper;
856
+ if ((t1.objectFlags & ObjectFlags.Mapped) !== 0) {
857
+ // instantiateAnonymousType prepends a mapping to a fresh type parameter; compare the
858
+ // instantiation itself rather than the identity of that parameter.
859
+ if (m1 instanceof CompositeTypeMapper) m1 = m1.m2;
860
+ if (m2 instanceof CompositeTypeMapper) m2 = m2.m2;
861
+ }
862
+ r = compareTypeMappers(c, m1, m2);
829
863
  if (r !== 0) return r;
830
864
  }
831
865
  } else if ((flags & TypeFlags.Union) !== 0) {
@@ -852,6 +886,9 @@ export function compareTypes(c: Checker, t1: Type, t2: Type): number {
852
886
  } else if ((flags & TypeFlags.NumberLiteral) !== 0) {
853
887
  r = compareNumbers((t1 as LiteralType).numberValue, (t2 as LiteralType).numberValue);
854
888
  if (r !== 0) return r;
889
+ } else if ((flags & TypeFlags.BigIntLiteral) !== 0) {
890
+ r = comparePseudoBigInts((t1 as LiteralType).stringValue, (t2 as LiteralType).stringValue);
891
+ if (r !== 0) return r;
855
892
  } else if ((flags & TypeFlags.BooleanLiteral) !== 0) {
856
893
  const b1 = (t1 as LiteralType).stringValue === 'true';
857
894
  const b2 = (t2 as LiteralType).stringValue === 'true';
@@ -998,7 +1035,8 @@ export function unionContainsSortedTypes(c: Checker, union: UnionType, types: re
998
1035
  export function insertType(c: Checker, types: Type[], t: Type): boolean {
999
1036
  const index = binarySearchTypes(c, types, t);
1000
1037
  if (index >= 0) return false;
1001
- types.splice(~index, 0, t);
1038
+ if (~index === types.length) types.push(t);
1039
+ else types.splice(~index, 0, t);
1002
1040
  return true;
1003
1041
  }
1004
1042
 
@@ -1107,7 +1145,30 @@ export function filterType(c: Checker, type: Type, f: (t: Type) => boolean): Typ
1107
1145
  }
1108
1146
 
1109
1147
  export function removeType(c: Checker, type: Type, target: Type): Type {
1110
- return filterType(c, type, (t) => t !== target);
1148
+ if ((type.flags & TypeFlags.Union) === 0) return type === target ? c.neverType : type;
1149
+ const union = type as UnionType;
1150
+ const origin = union.origin;
1151
+ // An origin that lists the target keeps the rest of its constituents.
1152
+ if (
1153
+ origin !== undefined &&
1154
+ (origin.flags & TypeFlags.Union) !== 0 &&
1155
+ containsType(c, (origin as UnionType).types, target)
1156
+ )
1157
+ return filterType(c, type, (t) => t !== target);
1158
+ const types = union.types;
1159
+ const index =
1160
+ types.length < linearTypeSearchLimit ? indexOfType(types, target, 0) : binarySearchTypes(c, types, target);
1161
+ if (index < 0) return type;
1162
+ if (types.length === 2) return types[1 - index]!;
1163
+ const filtered: Type[] = [];
1164
+ for (let i = 0; i < types.length; i++) if (i !== index) filtered.push(types[i]!);
1165
+ return getUnionTypeFromSortedList(
1166
+ c,
1167
+ filtered,
1168
+ union.objectFlags & (ObjectFlags.PrimitiveUnion | ObjectFlags.ContainsIntersections),
1169
+ undefined,
1170
+ undefined,
1171
+ );
1111
1172
  }
1112
1173
 
1113
1174
  export function countTypes(type: Type): number {
@@ -1214,7 +1275,8 @@ function insertTypeNearLast(c: Checker, set: TypeSet, t: Type): void {
1214
1275
  if (r < 0) low = middle + 1;
1215
1276
  else high = middle - 1;
1216
1277
  }
1217
- types.splice(low, 0, t);
1278
+ if (low === types.length) types.push(t);
1279
+ else types.splice(low, 0, t);
1218
1280
  set.last = low;
1219
1281
  }
1220
1282
 
@@ -1971,21 +2033,26 @@ function intersectUnionsOfPrimitiveTypes(c: Checker, types: Type[]): boolean {
1971
2033
  }
1972
2034
 
1973
2035
  function eachUnionContains(c: Checker, unionTypes: Type[], t: Type): boolean {
1974
- for (const type of unionTypes) {
1975
- const u = type as UnionType;
1976
- if (unionContainsType(c, u, t)) continue;
1977
- if (t === c.missingType) return unionContainsType(c, u, c.undefinedType);
1978
- if (t === c.undefinedType) return unionContainsType(c, u, c.missingType);
1979
- let primitive: Type | undefined;
1980
- if ((t.flags & TypeFlags.StringLiteral) !== 0) primitive = c.stringType;
1981
- else if ((t.flags & (TypeFlags.Enum | TypeFlags.NumberLiteral)) !== 0) primitive = c.numberType;
1982
- else if ((t.flags & TypeFlags.BigIntLiteral) !== 0) primitive = c.bigintType;
1983
- else if ((t.flags & TypeFlags.UniqueESSymbol) !== 0) primitive = c.esSymbolType;
1984
- if (primitive === undefined || !unionContainsType(c, u, primitive)) return false;
1985
- }
2036
+ for (const type of unionTypes) if (!unionContainsTypeOrPrimitive(c, type as UnionType, t, true)) return false;
1986
2037
  return true;
1987
2038
  }
1988
2039
 
2040
+ /**
2041
+ * Whether `union` contains `t`, `undefined` where `t` is the missing type and the other way around, or
2042
+ * the primitive type of literal `t` (of a unique symbol only with `matchSymbol`).
2043
+ */
2044
+ export function unionContainsTypeOrPrimitive(c: Checker, union: UnionType, t: Type, matchSymbol: boolean): boolean {
2045
+ if (unionContainsType(c, union, t)) return true;
2046
+ if (t === c.missingType) return unionContainsType(c, union, c.undefinedType);
2047
+ if (t === c.undefinedType) return unionContainsType(c, union, c.missingType);
2048
+ let primitive: Type | undefined;
2049
+ if ((t.flags & TypeFlags.StringLiteral) !== 0) primitive = c.stringType;
2050
+ else if ((t.flags & (TypeFlags.Enum | TypeFlags.NumberLiteral)) !== 0) primitive = c.numberType;
2051
+ else if ((t.flags & TypeFlags.BigIntLiteral) !== 0) primitive = c.bigintType;
2052
+ else if ((t.flags & TypeFlags.UniqueESSymbol) !== 0 && matchSymbol) primitive = c.esSymbolType;
2053
+ return primitive !== undefined && unionContainsType(c, union, primitive);
2054
+ }
2055
+
1989
2056
  export function getCrossProductUnionSize(types: readonly Type[]): number {
1990
2057
  let size = 1;
1991
2058
  for (const t of types) {
@@ -2551,7 +2618,13 @@ export function getTemplateLiteralType(c: Checker, texts: string[], types: Type[
2551
2618
  }
2552
2619
  if (types.includes(c.wildcardType)) return c.wildcardType;
2553
2620
  const builder = new TemplateBuilder(c, texts[0]!);
2554
- if (!builder.addSpans(texts, types)) return c.stringType;
2621
+ if (!builder.addSpans(texts, types)) {
2622
+ if (builder.tooLarge) {
2623
+ c.error(c.currentNode, Diagnostics.Type_instantiation_is_excessively_deep_and_possibly_infinite);
2624
+ return c.errorType;
2625
+ }
2626
+ return c.stringType;
2627
+ }
2555
2628
  if (builder.newTypes.length === 0) return getStringLiteralType(c, builder.text);
2556
2629
  builder.newTexts.push(builder.text);
2557
2630
  const newTexts = builder.newTexts;
@@ -2570,15 +2643,30 @@ export function getTemplateLiteralType(c: Checker, texts: string[], types: Type[
2570
2643
  return type;
2571
2644
  }
2572
2645
 
2646
+ /**
2647
+ * Limits on the size of a template literal type that getTemplateLiteralType builds, in UTF-8 bytes of text
2648
+ * and in placeholders. Recursive instantiations such as `Recur<any, `${S}_${S}`>` double the text or the
2649
+ * placeholders on every step and exhaust memory long before the tail recursion limit of conditional types.
2650
+ */
2651
+ const maxTemplateLiteralTypeLength = 50_000_000;
2652
+ const maxTemplateLiteralTypeSpans = 100_000;
2653
+
2573
2654
  class TemplateBuilder {
2574
2655
  readonly c: Checker;
2575
2656
  text: string;
2576
2657
  readonly newTypes: Type[] = [];
2577
2658
  readonly newTexts: string[] = [];
2659
+ /** UTF-8 bytes of `text`. */
2660
+ private textBytes: number;
2661
+ /** UTF-8 bytes of the texts already moved into `newTexts`. */
2662
+ private movedBytes = 0;
2663
+ /** Whether `addSpans` stopped because the result passed a size limit. */
2664
+ tooLarge = false;
2578
2665
 
2579
2666
  constructor(c: Checker, text: string) {
2580
2667
  this.c = c;
2581
2668
  this.text = text;
2669
+ this.textBytes = utf8Length(text);
2582
2670
  }
2583
2671
 
2584
2672
  addSpans(texts: string[], types: Type[]): boolean {
@@ -2586,17 +2674,30 @@ class TemplateBuilder {
2586
2674
  for (let i = 0; i < types.length; i++) {
2587
2675
  const t = types[i]!;
2588
2676
  if ((t.flags & (TypeFlags.Literal | TypeFlags.Null | TypeFlags.Undefined)) !== 0) {
2589
- this.text += getTemplateStringForType(c, t) + texts[i + 1]!;
2677
+ const added = getTemplateStringForType(c, t) + texts[i + 1]!;
2678
+ this.text += added;
2679
+ this.textBytes += utf8Length(added);
2590
2680
  } else if ((t.flags & TypeFlags.TemplateLiteral) !== 0) {
2591
2681
  const template = t as TemplateLiteralType;
2592
2682
  this.text += template.texts[0]!;
2683
+ this.textBytes += utf8Length(template.texts[0]!);
2593
2684
  if (!this.addSpans(template.texts, template.types)) return false;
2594
2685
  this.text += texts[i + 1]!;
2686
+ this.textBytes += utf8Length(texts[i + 1]!);
2595
2687
  } else if (isGenericIndexType(c, t) || isPatternLiteralPlaceholderType(c, t)) {
2596
2688
  this.newTypes.push(t);
2597
2689
  this.newTexts.push(this.text);
2690
+ this.movedBytes += this.textBytes;
2598
2691
  this.text = texts[i + 1]!;
2692
+ this.textBytes = utf8Length(this.text);
2599
2693
  } else return false;
2694
+ if (
2695
+ this.movedBytes + this.textBytes > maxTemplateLiteralTypeLength ||
2696
+ this.newTypes.length > maxTemplateLiteralTypeSpans
2697
+ ) {
2698
+ this.tooLarge = true;
2699
+ return false;
2700
+ }
2600
2701
  }
2601
2702
  return true;
2602
2703
  }