@typespec/compiler 0.41.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 (352) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +9 -0
  3. package/cmd/tsp-server.js +3 -0
  4. package/cmd/tsp.js +3 -0
  5. package/dist/cmd/runner.d.ts +9 -0
  6. package/dist/cmd/runner.d.ts.map +1 -0
  7. package/dist/cmd/runner.js +62 -0
  8. package/dist/cmd/runner.js.map +1 -0
  9. package/dist/config/config-interpolation.d.ts +11 -0
  10. package/dist/config/config-interpolation.d.ts.map +1 -0
  11. package/dist/config/config-interpolation.js +109 -0
  12. package/dist/config/config-interpolation.js.map +1 -0
  13. package/dist/config/config-loader.d.ts +25 -0
  14. package/dist/config/config-loader.d.ts.map +1 -0
  15. package/dist/config/config-loader.js +182 -0
  16. package/dist/config/config-loader.js.map +1 -0
  17. package/dist/config/config-schema.d.ts +4 -0
  18. package/dist/config/config-schema.d.ts.map +1 -0
  19. package/dist/config/config-schema.js +84 -0
  20. package/dist/config/config-schema.js.map +1 -0
  21. package/dist/config/index.d.ts +3 -0
  22. package/dist/config/index.d.ts.map +1 -0
  23. package/dist/config/index.js +3 -0
  24. package/dist/config/index.js.map +1 -0
  25. package/dist/config/types.d.ts +79 -0
  26. package/dist/config/types.d.ts.map +1 -0
  27. package/dist/config/types.js +2 -0
  28. package/dist/config/types.js.map +1 -0
  29. package/dist/core/binder.d.ts +29 -0
  30. package/dist/core/binder.d.ts.map +1 -0
  31. package/dist/core/binder.js +455 -0
  32. package/dist/core/binder.js.map +1 -0
  33. package/dist/core/charcode.d.ts +130 -0
  34. package/dist/core/charcode.d.ts.map +1 -0
  35. package/dist/core/charcode.js +115 -0
  36. package/dist/core/charcode.js.map +1 -0
  37. package/dist/core/checker.d.ts +138 -0
  38. package/dist/core/checker.d.ts.map +1 -0
  39. package/dist/core/checker.js +4070 -0
  40. package/dist/core/checker.js.map +1 -0
  41. package/dist/core/cli/args.d.ts +18 -0
  42. package/dist/core/cli/args.d.ts.map +1 -0
  43. package/dist/core/cli/args.js +108 -0
  44. package/dist/core/cli/args.js.map +1 -0
  45. package/dist/core/cli/cli.d.ts +2 -0
  46. package/dist/core/cli/cli.d.ts.map +1 -0
  47. package/dist/core/cli/cli.js +477 -0
  48. package/dist/core/cli/cli.js.map +1 -0
  49. package/dist/core/cli/index.d.ts +2 -0
  50. package/dist/core/cli/index.d.ts.map +1 -0
  51. package/dist/core/cli/index.js +2 -0
  52. package/dist/core/cli/index.js.map +1 -0
  53. package/dist/core/decorator-utils.d.ts +108 -0
  54. package/dist/core/decorator-utils.d.ts.map +1 -0
  55. package/dist/core/decorator-utils.js +326 -0
  56. package/dist/core/decorator-utils.js.map +1 -0
  57. package/dist/core/diagnostics.d.ts +93 -0
  58. package/dist/core/diagnostics.d.ts.map +1 -0
  59. package/dist/core/diagnostics.js +315 -0
  60. package/dist/core/diagnostics.js.map +1 -0
  61. package/dist/core/emitter-utils.d.ts +14 -0
  62. package/dist/core/emitter-utils.d.ts.map +1 -0
  63. package/dist/core/emitter-utils.js +16 -0
  64. package/dist/core/emitter-utils.js.map +1 -0
  65. package/dist/core/formatter-fs.d.ts +21 -0
  66. package/dist/core/formatter-fs.d.ts.map +1 -0
  67. package/dist/core/formatter-fs.js +79 -0
  68. package/dist/core/formatter-fs.js.map +1 -0
  69. package/dist/core/formatter.d.ts +10 -0
  70. package/dist/core/formatter.d.ts.map +1 -0
  71. package/dist/core/formatter.js +24 -0
  72. package/dist/core/formatter.js.map +1 -0
  73. package/dist/core/helpers/discriminator-utils.d.ts +14 -0
  74. package/dist/core/helpers/discriminator-utils.d.ts.map +1 -0
  75. package/dist/core/helpers/discriminator-utils.js +169 -0
  76. package/dist/core/helpers/discriminator-utils.js.map +1 -0
  77. package/dist/core/helpers/index.d.ts +6 -0
  78. package/dist/core/helpers/index.d.ts.map +1 -0
  79. package/dist/core/helpers/index.js +6 -0
  80. package/dist/core/helpers/index.js.map +1 -0
  81. package/dist/core/helpers/operation-utils.d.ts +15 -0
  82. package/dist/core/helpers/operation-utils.d.ts.map +1 -0
  83. package/dist/core/helpers/operation-utils.js +35 -0
  84. package/dist/core/helpers/operation-utils.js.map +1 -0
  85. package/dist/core/helpers/projected-names-utils.d.ts +20 -0
  86. package/dist/core/helpers/projected-names-utils.d.ts.map +1 -0
  87. package/dist/core/helpers/projected-names-utils.js +40 -0
  88. package/dist/core/helpers/projected-names-utils.js.map +1 -0
  89. package/dist/core/helpers/type-name-utils.d.ts +14 -0
  90. package/dist/core/helpers/type-name-utils.d.ts.map +1 -0
  91. package/dist/core/helpers/type-name-utils.js +122 -0
  92. package/dist/core/helpers/type-name-utils.js.map +1 -0
  93. package/dist/core/helpers/usage-resolver.d.ts +20 -0
  94. package/dist/core/helpers/usage-resolver.d.ts.map +1 -0
  95. package/dist/core/helpers/usage-resolver.js +108 -0
  96. package/dist/core/helpers/usage-resolver.js.map +1 -0
  97. package/dist/core/index.d.ts +26 -0
  98. package/dist/core/index.d.ts.map +1 -0
  99. package/dist/core/index.js +26 -0
  100. package/dist/core/index.js.map +1 -0
  101. package/dist/core/install.d.ts +2 -0
  102. package/dist/core/install.d.ts.map +1 -0
  103. package/dist/core/install.js +26 -0
  104. package/dist/core/install.js.map +1 -0
  105. package/dist/core/library.d.ts +34 -0
  106. package/dist/core/library.d.ts.map +1 -0
  107. package/dist/core/library.js +100 -0
  108. package/dist/core/library.js.map +1 -0
  109. package/dist/core/logger/console-sink.browser.d.ts +4 -0
  110. package/dist/core/logger/console-sink.browser.d.ts.map +1 -0
  111. package/dist/core/logger/console-sink.browser.js +14 -0
  112. package/dist/core/logger/console-sink.browser.js.map +1 -0
  113. package/dist/core/logger/console-sink.d.ts +9 -0
  114. package/dist/core/logger/console-sink.d.ts.map +1 -0
  115. package/dist/core/logger/console-sink.js +81 -0
  116. package/dist/core/logger/console-sink.js.map +1 -0
  117. package/dist/core/logger/index.d.ts +3 -0
  118. package/dist/core/logger/index.d.ts.map +1 -0
  119. package/dist/core/logger/index.js +3 -0
  120. package/dist/core/logger/index.js.map +1 -0
  121. package/dist/core/logger/logger.d.ts +7 -0
  122. package/dist/core/logger/logger.d.ts.map +1 -0
  123. package/dist/core/logger/logger.js +32 -0
  124. package/dist/core/logger/logger.js.map +1 -0
  125. package/dist/core/logger/tracer.d.ts +3 -0
  126. package/dist/core/logger/tracer.d.ts.map +1 -0
  127. package/dist/core/logger/tracer.js +91 -0
  128. package/dist/core/logger/tracer.js.map +1 -0
  129. package/dist/core/manifest.d.ts +21 -0
  130. package/dist/core/manifest.d.ts.map +1 -0
  131. package/dist/core/manifest.js +8 -0
  132. package/dist/core/manifest.js.map +1 -0
  133. package/dist/core/messages.d.ts +2253 -0
  134. package/dist/core/messages.d.ts.map +1 -0
  135. package/dist/core/messages.js +749 -0
  136. package/dist/core/messages.js.map +1 -0
  137. package/dist/core/module-resolver.d.ts +84 -0
  138. package/dist/core/module-resolver.d.ts.map +1 -0
  139. package/dist/core/module-resolver.js +170 -0
  140. package/dist/core/module-resolver.js.map +1 -0
  141. package/dist/core/node-host.browser.d.ts +2 -0
  142. package/dist/core/node-host.browser.d.ts.map +1 -0
  143. package/dist/core/node-host.browser.js +2 -0
  144. package/dist/core/node-host.browser.js.map +1 -0
  145. package/dist/core/node-host.d.ts +7 -0
  146. package/dist/core/node-host.d.ts.map +1 -0
  147. package/dist/core/node-host.js +43 -0
  148. package/dist/core/node-host.js.map +1 -0
  149. package/dist/core/nonascii.d.ts +2 -0
  150. package/dist/core/nonascii.d.ts.map +1 -0
  151. package/dist/core/nonascii.js +712 -0
  152. package/dist/core/nonascii.js.map +1 -0
  153. package/dist/core/options.d.ts +46 -0
  154. package/dist/core/options.d.ts.map +1 -0
  155. package/dist/core/options.js +2 -0
  156. package/dist/core/options.js.map +1 -0
  157. package/dist/core/parser.d.ts +17 -0
  158. package/dist/core/parser.d.ts.map +1 -0
  159. package/dist/core/parser.js +2561 -0
  160. package/dist/core/parser.js.map +1 -0
  161. package/dist/core/path-utils.d.ts +193 -0
  162. package/dist/core/path-utils.d.ts.map +1 -0
  163. package/dist/core/path-utils.js +427 -0
  164. package/dist/core/path-utils.js.map +1 -0
  165. package/dist/core/program.d.ts +104 -0
  166. package/dist/core/program.d.ts.map +1 -0
  167. package/dist/core/program.js +898 -0
  168. package/dist/core/program.js.map +1 -0
  169. package/dist/core/projection-members.d.ts +8 -0
  170. package/dist/core/projection-members.d.ts.map +1 -0
  171. package/dist/core/projection-members.js +387 -0
  172. package/dist/core/projection-members.js.map +1 -0
  173. package/dist/core/projector.d.ts +26 -0
  174. package/dist/core/projector.d.ts.map +1 -0
  175. package/dist/core/projector.js +523 -0
  176. package/dist/core/projector.js.map +1 -0
  177. package/dist/core/scanner.d.ts +125 -0
  178. package/dist/core/scanner.d.ts.map +1 -0
  179. package/dist/core/scanner.js +1012 -0
  180. package/dist/core/scanner.js.map +1 -0
  181. package/dist/core/schema-validator.d.ts +6 -0
  182. package/dist/core/schema-validator.d.ts.map +1 -0
  183. package/dist/core/schema-validator.js +38 -0
  184. package/dist/core/schema-validator.js.map +1 -0
  185. package/dist/core/semantic-walker.d.ts +53 -0
  186. package/dist/core/semantic-walker.d.ts.map +1 -0
  187. package/dist/core/semantic-walker.js +355 -0
  188. package/dist/core/semantic-walker.js.map +1 -0
  189. package/dist/core/type-utils.d.ts +49 -0
  190. package/dist/core/type-utils.d.ts.map +1 -0
  191. package/dist/core/type-utils.js +95 -0
  192. package/dist/core/type-utils.js.map +1 -0
  193. package/dist/core/types.d.ts +1345 -0
  194. package/dist/core/types.d.ts.map +1 -0
  195. package/dist/core/types.js +105 -0
  196. package/dist/core/types.js.map +1 -0
  197. package/dist/core/util.d.ts +150 -0
  198. package/dist/core/util.d.ts.map +1 -0
  199. package/dist/core/util.js +417 -0
  200. package/dist/core/util.js.map +1 -0
  201. package/dist/emitter-framework/asset-emitter.d.ts +5 -0
  202. package/dist/emitter-framework/asset-emitter.d.ts.map +1 -0
  203. package/dist/emitter-framework/asset-emitter.js +610 -0
  204. package/dist/emitter-framework/asset-emitter.js.map +1 -0
  205. package/dist/emitter-framework/builders/array-builder.d.ts +7 -0
  206. package/dist/emitter-framework/builders/array-builder.d.ts.map +1 -0
  207. package/dist/emitter-framework/builders/array-builder.js +45 -0
  208. package/dist/emitter-framework/builders/array-builder.js.map +1 -0
  209. package/dist/emitter-framework/builders/object-builder.d.ts +9 -0
  210. package/dist/emitter-framework/builders/object-builder.d.ts.map +1 -0
  211. package/dist/emitter-framework/builders/object-builder.js +30 -0
  212. package/dist/emitter-framework/builders/object-builder.js.map +1 -0
  213. package/dist/emitter-framework/builders/string-builder.d.ts +13 -0
  214. package/dist/emitter-framework/builders/string-builder.d.ts.map +1 -0
  215. package/dist/emitter-framework/builders/string-builder.js +98 -0
  216. package/dist/emitter-framework/builders/string-builder.js.map +1 -0
  217. package/dist/emitter-framework/custom-key-map.d.ts +29 -0
  218. package/dist/emitter-framework/custom-key-map.d.ts.map +1 -0
  219. package/dist/emitter-framework/custom-key-map.js +62 -0
  220. package/dist/emitter-framework/custom-key-map.js.map +1 -0
  221. package/dist/emitter-framework/index.d.ts +8 -0
  222. package/dist/emitter-framework/index.d.ts.map +1 -0
  223. package/dist/emitter-framework/index.js +8 -0
  224. package/dist/emitter-framework/index.js.map +1 -0
  225. package/dist/emitter-framework/placeholder.d.ts +12 -0
  226. package/dist/emitter-framework/placeholder.d.ts.map +1 -0
  227. package/dist/emitter-framework/placeholder.js +27 -0
  228. package/dist/emitter-framework/placeholder.js.map +1 -0
  229. package/dist/emitter-framework/type-emitter.d.ts +314 -0
  230. package/dist/emitter-framework/type-emitter.d.ts.map +1 -0
  231. package/dist/emitter-framework/type-emitter.js +612 -0
  232. package/dist/emitter-framework/type-emitter.js.map +1 -0
  233. package/dist/emitter-framework/types.d.ts +124 -0
  234. package/dist/emitter-framework/types.d.ts.map +1 -0
  235. package/dist/emitter-framework/types.js +39 -0
  236. package/dist/emitter-framework/types.js.map +1 -0
  237. package/dist/formatter/index.d.ts +11 -0
  238. package/dist/formatter/index.d.ts.map +1 -0
  239. package/dist/formatter/index.js +28 -0
  240. package/dist/formatter/index.js.map +1 -0
  241. package/dist/formatter/parser.d.ts +16 -0
  242. package/dist/formatter/parser.d.ts.map +1 -0
  243. package/dist/formatter/parser.js +23 -0
  244. package/dist/formatter/parser.js.map +1 -0
  245. package/dist/formatter/print/comment-handler.d.ts +7 -0
  246. package/dist/formatter/print/comment-handler.d.ts.map +1 -0
  247. package/dist/formatter/print/comment-handler.js +81 -0
  248. package/dist/formatter/print/comment-handler.js.map +1 -0
  249. package/dist/formatter/print/index.d.ts +3 -0
  250. package/dist/formatter/print/index.d.ts.map +1 -0
  251. package/dist/formatter/print/index.js +3 -0
  252. package/dist/formatter/print/index.js.map +1 -0
  253. package/dist/formatter/print/needs-parens.d.ts +10 -0
  254. package/dist/formatter/print/needs-parens.d.ts.map +1 -0
  255. package/dist/formatter/print/needs-parens.js +27 -0
  256. package/dist/formatter/print/needs-parens.js.map +1 -0
  257. package/dist/formatter/print/printer.d.ts +62 -0
  258. package/dist/formatter/print/printer.d.ts.map +1 -0
  259. package/dist/formatter/print/printer.js +1025 -0
  260. package/dist/formatter/print/printer.js.map +1 -0
  261. package/dist/formatter/print/types.d.ts +9 -0
  262. package/dist/formatter/print/types.d.ts.map +1 -0
  263. package/dist/formatter/print/types.js +2 -0
  264. package/dist/formatter/print/types.js.map +1 -0
  265. package/dist/init/index.d.ts +2 -0
  266. package/dist/init/index.d.ts.map +1 -0
  267. package/dist/init/index.js +2 -0
  268. package/dist/init/index.js.map +1 -0
  269. package/dist/init/init-template.d.ts +45 -0
  270. package/dist/init/init-template.d.ts.map +1 -0
  271. package/dist/init/init-template.js +45 -0
  272. package/dist/init/init-template.js.map +1 -0
  273. package/dist/init/init.d.ts +49 -0
  274. package/dist/init/init.d.ts.map +1 -0
  275. package/dist/init/init.js +224 -0
  276. package/dist/init/init.js.map +1 -0
  277. package/dist/lib/decorators.d.ts +222 -0
  278. package/dist/lib/decorators.d.ts.map +1 -0
  279. package/dist/lib/decorators.js +745 -0
  280. package/dist/lib/decorators.js.map +1 -0
  281. package/dist/lib/service.d.ts +38 -0
  282. package/dist/lib/service.d.ts.map +1 -0
  283. package/dist/lib/service.js +78 -0
  284. package/dist/lib/service.js.map +1 -0
  285. package/dist/manifest.js +4 -0
  286. package/dist/server/completion.d.ts +10 -0
  287. package/dist/server/completion.d.ts.map +1 -0
  288. package/dist/server/completion.js +207 -0
  289. package/dist/server/completion.js.map +1 -0
  290. package/dist/server/index.d.ts +3 -0
  291. package/dist/server/index.d.ts.map +1 -0
  292. package/dist/server/index.js +3 -0
  293. package/dist/server/index.js.map +1 -0
  294. package/dist/server/language-config.d.ts +195 -0
  295. package/dist/server/language-config.d.ts.map +1 -0
  296. package/dist/server/language-config.js +102 -0
  297. package/dist/server/language-config.js.map +1 -0
  298. package/dist/server/server.d.ts +2 -0
  299. package/dist/server/server.d.ts.map +1 -0
  300. package/dist/server/server.js +131 -0
  301. package/dist/server/server.js.map +1 -0
  302. package/dist/server/serverlib.d.ts +72 -0
  303. package/dist/server/serverlib.d.ts.map +1 -0
  304. package/dist/server/serverlib.js +1034 -0
  305. package/dist/server/serverlib.js.map +1 -0
  306. package/dist/server/symbol-structure.d.ts +4 -0
  307. package/dist/server/symbol-structure.d.ts.map +1 -0
  308. package/dist/server/symbol-structure.js +121 -0
  309. package/dist/server/symbol-structure.js.map +1 -0
  310. package/dist/server/tmlanguage.d.ts +3 -0
  311. package/dist/server/tmlanguage.d.ts.map +1 -0
  312. package/dist/server/tmlanguage.js +713 -0
  313. package/dist/server/tmlanguage.js.map +1 -0
  314. package/dist/server/type-details.d.ts +11 -0
  315. package/dist/server/type-details.d.ts.map +1 -0
  316. package/dist/server/type-details.js +72 -0
  317. package/dist/server/type-details.js.map +1 -0
  318. package/dist/server/type-signature.d.ts +2 -0
  319. package/dist/server/type-signature.d.ts.map +1 -0
  320. package/dist/server/type-signature.js +108 -0
  321. package/dist/server/type-signature.js.map +1 -0
  322. package/dist/testing/expect.d.ts +49 -0
  323. package/dist/testing/expect.d.ts.map +1 -0
  324. package/dist/testing/expect.js +83 -0
  325. package/dist/testing/expect.js.map +1 -0
  326. package/dist/testing/index.d.ts +6 -0
  327. package/dist/testing/index.d.ts.map +1 -0
  328. package/dist/testing/index.js +6 -0
  329. package/dist/testing/index.js.map +1 -0
  330. package/dist/testing/test-host.d.ts +14 -0
  331. package/dist/testing/test-host.d.ts.map +1 -0
  332. package/dist/testing/test-host.js +271 -0
  333. package/dist/testing/test-host.js.map +1 -0
  334. package/dist/testing/test-server-host.d.ts +39 -0
  335. package/dist/testing/test-server-host.d.ts.map +1 -0
  336. package/dist/testing/test-server-host.js +112 -0
  337. package/dist/testing/test-server-host.js.map +1 -0
  338. package/dist/testing/test-utils.d.ts +22 -0
  339. package/dist/testing/test-utils.d.ts.map +1 -0
  340. package/dist/testing/test-utils.js +60 -0
  341. package/dist/testing/test-utils.js.map +1 -0
  342. package/dist/testing/types.d.ts +69 -0
  343. package/dist/testing/types.d.ts.map +1 -0
  344. package/dist/testing/types.js +7 -0
  345. package/dist/testing/types.js.map +1 -0
  346. package/dist/typespec.tmLanguage +1760 -0
  347. package/lib/decorators.tsp +420 -0
  348. package/lib/lib.tsp +158 -0
  349. package/lib/main.tsp +4 -0
  350. package/lib/projected-names.tsp +94 -0
  351. package/lib/reflection.tsp +10 -0
  352. package/package.json +119 -0
@@ -0,0 +1,898 @@
1
+ var _a, _b;
2
+ import { createAssetEmitter } from "../emitter-framework/asset-emitter.js";
3
+ import { createBinder } from "./binder.js";
4
+ import { createChecker } from "./checker.js";
5
+ import { compilerAssert, createSourceFile } from "./diagnostics.js";
6
+ import { getLibraryUrlsLoaded } from "./library.js";
7
+ import { createLogger } from "./logger/index.js";
8
+ import { createTracer } from "./logger/tracer.js";
9
+ import { MANIFEST } from "./manifest.js";
10
+ import { createDiagnostic } from "./messages.js";
11
+ import { resolveModule, } from "./module-resolver.js";
12
+ import { isImportStatement, parse, parseStandaloneTypeReference } from "./parser.js";
13
+ import { getDirectoryPath, joinPaths, resolvePath } from "./path-utils.js";
14
+ import { createProjector } from "./projector.js";
15
+ import { NoTarget, SyntaxKind, } from "./types.js";
16
+ import { deepEquals, doIO, ExternalError, findProjectRoot, isDefined, loadFile, mapEquals, mutate, resolveTspMain, } from "./util.js";
17
+ export function isProjectedProgram(program) {
18
+ return "projector" in program;
19
+ }
20
+ class StateMap extends Map {
21
+ }
22
+ class StateSet extends Map {
23
+ }
24
+ class StateMapView {
25
+ constructor(state, projector) {
26
+ this.state = state;
27
+ this.projector = projector;
28
+ this[_a] = "StateMap";
29
+ }
30
+ has(t) {
31
+ var _c, _d;
32
+ return (_d = (_c = this.dispatch(t)) === null || _c === void 0 ? void 0 : _c.has(t)) !== null && _d !== void 0 ? _d : false;
33
+ }
34
+ set(t, v) {
35
+ this.dispatch(t).set(t, v);
36
+ return this;
37
+ }
38
+ get(t) {
39
+ return this.dispatch(t).get(t);
40
+ }
41
+ delete(t) {
42
+ return this.dispatch(t).delete(t);
43
+ }
44
+ forEach(cb, thisArg) {
45
+ this.dispatch().forEach(cb, thisArg);
46
+ return this;
47
+ }
48
+ get size() {
49
+ return this.dispatch().size;
50
+ }
51
+ clear() {
52
+ return this.dispatch().clear();
53
+ }
54
+ entries() {
55
+ return this.dispatch().entries();
56
+ }
57
+ values() {
58
+ return this.dispatch().values();
59
+ }
60
+ keys() {
61
+ return this.dispatch().keys();
62
+ }
63
+ [Symbol.iterator]() {
64
+ return this.entries();
65
+ }
66
+ dispatch(keyType) {
67
+ const key = keyType ? keyType.projector : this.projector;
68
+ if (!this.state.has(key)) {
69
+ this.state.set(key, new Map());
70
+ }
71
+ return this.state.get(key);
72
+ }
73
+ }
74
+ _a = Symbol.toStringTag;
75
+ class StateSetView {
76
+ constructor(state, projector) {
77
+ this.state = state;
78
+ this.projector = projector;
79
+ this[_b] = "StateSet";
80
+ }
81
+ has(t) {
82
+ var _c, _d;
83
+ return (_d = (_c = this.dispatch(t)) === null || _c === void 0 ? void 0 : _c.has(t)) !== null && _d !== void 0 ? _d : false;
84
+ }
85
+ add(t) {
86
+ this.dispatch(t).add(t);
87
+ return this;
88
+ }
89
+ delete(t) {
90
+ return this.dispatch(t).delete(t);
91
+ }
92
+ forEach(cb, thisArg) {
93
+ this.dispatch().forEach(cb, thisArg);
94
+ return this;
95
+ }
96
+ get size() {
97
+ return this.dispatch().size;
98
+ }
99
+ clear() {
100
+ return this.dispatch().clear();
101
+ }
102
+ values() {
103
+ return this.dispatch().values();
104
+ }
105
+ keys() {
106
+ return this.dispatch().keys();
107
+ }
108
+ entries() {
109
+ return this.dispatch().entries();
110
+ }
111
+ [Symbol.iterator]() {
112
+ return this.values();
113
+ }
114
+ dispatch(keyType) {
115
+ const key = keyType ? keyType.projector : this.projector;
116
+ if (!this.state.has(key)) {
117
+ this.state.set(key, new Set());
118
+ }
119
+ return this.state.get(key);
120
+ }
121
+ }
122
+ _b = Symbol.toStringTag;
123
+ export function projectProgram(program, projections, startNode) {
124
+ return createProjector(program, projections, startNode);
125
+ }
126
+ export async function compile(host, mainFile, options = {}, oldProgram // NOTE: deliberately separate from options to avoid memory leak by chaining all old programs together.
127
+ ) {
128
+ const validateCbs = [];
129
+ const stateMaps = new Map();
130
+ const stateSets = new Map();
131
+ const diagnostics = [];
132
+ const seenSourceFiles = new Set();
133
+ const duplicateSymbols = new Set();
134
+ const emitters = [];
135
+ const requireImports = new Map();
136
+ const loadedLibraries = new Map();
137
+ let error = false;
138
+ const logger = createLogger({ sink: host.logSink });
139
+ const tracer = createTracer(logger, { filter: options.trace });
140
+ const program = {
141
+ checker: undefined,
142
+ compilerOptions: resolveOptions(options),
143
+ sourceFiles: new Map(),
144
+ jsSourceFiles: new Map(),
145
+ literalTypes: new Map(),
146
+ host,
147
+ diagnostics,
148
+ emitters,
149
+ loadTypeSpecScript,
150
+ getOption,
151
+ stateMaps,
152
+ stateSets,
153
+ tracer,
154
+ trace,
155
+ ...createStateAccessors(stateMaps, stateSets),
156
+ reportDiagnostic,
157
+ reportDiagnostics,
158
+ reportDuplicateSymbols,
159
+ hasError() {
160
+ return error;
161
+ },
162
+ onValidate(cb) {
163
+ validateCbs.push(cb);
164
+ },
165
+ getGlobalNamespaceType,
166
+ resolveTypeReference,
167
+ };
168
+ trace("compiler.options", JSON.stringify(options, null, 2));
169
+ function trace(area, message) {
170
+ tracer.trace(area, message);
171
+ }
172
+ const binder = createBinder(program);
173
+ if (!(options === null || options === void 0 ? void 0 : options.nostdlib)) {
174
+ await loadStandardLibrary(program);
175
+ }
176
+ const resolvedMain = await resolveTypeSpecEntrypoint(mainFile);
177
+ // Load additional imports prior to compilation
178
+ if (resolvedMain && options.additionalImports) {
179
+ const importScript = options.additionalImports.map((i) => `import "${i}";`).join("\n");
180
+ const sourceFile = createSourceFile(importScript, joinPaths(getDirectoryPath(resolvedMain), `__additional_imports`));
181
+ await loadTypeSpecScript(sourceFile);
182
+ }
183
+ if (resolvedMain) {
184
+ await loadMain(resolvedMain, options);
185
+ }
186
+ if (resolvedMain) {
187
+ let emit = options.emit;
188
+ let emitterOptions = options.options;
189
+ if (options.emitters) {
190
+ emit !== null && emit !== void 0 ? emit : (emit = Object.keys(options.emitters));
191
+ emitterOptions !== null && emitterOptions !== void 0 ? emitterOptions : (emitterOptions = options.emitters);
192
+ }
193
+ await loadEmitters(resolvedMain, emit !== null && emit !== void 0 ? emit : [], emitterOptions !== null && emitterOptions !== void 0 ? emitterOptions : {});
194
+ }
195
+ if (oldProgram &&
196
+ mapEquals(oldProgram.sourceFiles, program.sourceFiles) &&
197
+ deepEquals(oldProgram.compilerOptions, program.compilerOptions)) {
198
+ return oldProgram;
199
+ }
200
+ // let GC reclaim old program, we do not reuse it beyond this point.
201
+ oldProgram = undefined;
202
+ program.checker = createChecker(program);
203
+ program.checker.checkProgram();
204
+ if (program.hasError()) {
205
+ return program;
206
+ }
207
+ for (const cb of validateCbs) {
208
+ try {
209
+ await cb(program);
210
+ }
211
+ catch (error) {
212
+ if (options.designTimeBuild) {
213
+ program.reportDiagnostic(createDiagnostic({
214
+ code: "on-validate-fail",
215
+ format: { error: error.stack },
216
+ target: NoTarget,
217
+ }));
218
+ }
219
+ else {
220
+ throw error;
221
+ }
222
+ }
223
+ }
224
+ for (const [requiredImport, emitterName] of requireImports) {
225
+ if (!loadedLibraries.has(requiredImport)) {
226
+ program.reportDiagnostic(createDiagnostic({
227
+ code: "missing-import",
228
+ format: { requiredImport, emitterName },
229
+ target: NoTarget,
230
+ }));
231
+ }
232
+ }
233
+ await validateLoadedLibraries();
234
+ if (program.hasError()) {
235
+ return program;
236
+ }
237
+ for (const instance of emitters) {
238
+ await runEmitter(instance);
239
+ }
240
+ return program;
241
+ /**
242
+ * Validate the libraries loaded during the compilation process are compatible.
243
+ */
244
+ async function validateLoadedLibraries() {
245
+ const loadedRoots = new Set();
246
+ // Check all the files that were loaded
247
+ for (const fileUrl of getLibraryUrlsLoaded()) {
248
+ const root = await findProjectRoot(host, host.fileURLToPath(fileUrl));
249
+ if (root) {
250
+ loadedRoots.add(root);
251
+ }
252
+ }
253
+ const libraries = new Map([...loadedLibraries.entries()]);
254
+ const incompatibleLibraries = new Map();
255
+ for (const root of loadedRoots) {
256
+ const packageJsonPath = joinPaths(root, "package.json");
257
+ try {
258
+ const packageJson = JSON.parse((await host.readFile(packageJsonPath)).text);
259
+ const found = libraries.get(packageJson.name);
260
+ if (found && found.path !== root && found.manifest.version !== packageJson.version) {
261
+ let incompatibleIndex = incompatibleLibraries.get(packageJson.name);
262
+ if (incompatibleIndex === undefined) {
263
+ incompatibleIndex = [found];
264
+ incompatibleLibraries.set(packageJson.name, incompatibleIndex);
265
+ }
266
+ incompatibleIndex.push({ path: root, manifest: packageJson });
267
+ }
268
+ }
269
+ catch { }
270
+ }
271
+ for (const [name, incompatibleLibs] of incompatibleLibraries) {
272
+ reportDiagnostic(createDiagnostic({
273
+ code: "incompatible-library",
274
+ format: {
275
+ name: name,
276
+ versionMap: incompatibleLibs
277
+ .map((x) => ` - Version: "${x.manifest.version}" installed at "${x.path}"`)
278
+ .join("\n"),
279
+ },
280
+ target: NoTarget,
281
+ }));
282
+ }
283
+ }
284
+ async function loadStandardLibrary(program) {
285
+ for (const dir of host.getLibDirs()) {
286
+ await loadDirectory(dir, NoTarget);
287
+ }
288
+ }
289
+ async function loadDirectory(dir, diagnosticTarget) {
290
+ const mainFile = await resolveTypeSpecEntrypointForDir(dir);
291
+ await loadTypeSpecFile(mainFile, diagnosticTarget);
292
+ return mainFile;
293
+ }
294
+ async function loadTypeSpecFile(path, diagnosticTarget) {
295
+ if (seenSourceFiles.has(path)) {
296
+ return;
297
+ }
298
+ seenSourceFiles.add(path);
299
+ const file = await doIO(host.readFile, path, program.reportDiagnostic, {
300
+ diagnosticTarget,
301
+ });
302
+ if (file) {
303
+ await loadTypeSpecScript(file);
304
+ }
305
+ }
306
+ async function loadJsFile(path, diagnosticTarget) {
307
+ const sourceFile = program.jsSourceFiles.get(path);
308
+ if (sourceFile !== undefined) {
309
+ return sourceFile;
310
+ }
311
+ const file = createSourceFile("", path);
312
+ const exports = await doIO(host.getJsImport, path, program.reportDiagnostic, {
313
+ diagnosticTarget,
314
+ jsDiagnosticTarget: { file, pos: 0, end: 0 },
315
+ });
316
+ if (!exports) {
317
+ return undefined;
318
+ }
319
+ return {
320
+ kind: SyntaxKind.JsSourceFile,
321
+ id: {
322
+ kind: SyntaxKind.Identifier,
323
+ sv: "",
324
+ pos: 0,
325
+ end: 0,
326
+ symbol: undefined,
327
+ flags: 8 /* NodeFlags.Synthetic */,
328
+ },
329
+ esmExports: exports,
330
+ file,
331
+ namespaceSymbols: [],
332
+ symbol: undefined,
333
+ pos: 0,
334
+ end: 0,
335
+ flags: 0 /* NodeFlags.None */,
336
+ };
337
+ }
338
+ /**
339
+ * Import the Javascript files decorator and lifecycle hooks.
340
+ */
341
+ async function importJsFile(path, diagnosticTarget) {
342
+ const file = await loadJsFile(path, diagnosticTarget);
343
+ if (file !== undefined) {
344
+ program.jsSourceFiles.set(path, file);
345
+ binder.bindJsSourceFile(file);
346
+ }
347
+ }
348
+ async function loadTypeSpecScript(file) {
349
+ // This is not a diagnostic because the compiler should never reuse the same path.
350
+ // It's the caller's responsibility to use unique paths.
351
+ if (program.sourceFiles.has(file.path)) {
352
+ throw new RangeError("Duplicate script path: " + file.path);
353
+ }
354
+ const script = parseOrReuse(file);
355
+ program.reportDiagnostics(script.parseDiagnostics);
356
+ program.sourceFiles.set(file.path, script);
357
+ binder.bindSourceFile(script);
358
+ await loadScriptImports(script);
359
+ return script;
360
+ }
361
+ function parseOrReuse(file) {
362
+ var _c, _d, _e;
363
+ const old = (_c = oldProgram === null || oldProgram === void 0 ? void 0 : oldProgram.sourceFiles.get(file.path)) !== null && _c !== void 0 ? _c : (_d = host === null || host === void 0 ? void 0 : host.parseCache) === null || _d === void 0 ? void 0 : _d.get(file);
364
+ if ((old === null || old === void 0 ? void 0 : old.file) === file && deepEquals(old.parseOptions, options.parseOptions)) {
365
+ return old;
366
+ }
367
+ const script = parse(file, options.parseOptions);
368
+ (_e = host.parseCache) === null || _e === void 0 ? void 0 : _e.set(file, script);
369
+ return script;
370
+ }
371
+ async function loadScriptImports(file) {
372
+ // collect imports
373
+ const basedir = getDirectoryPath(file.file.path);
374
+ await loadImports(file.statements.filter(isImportStatement).map((x) => ({ path: x.path.value, target: x })), basedir);
375
+ }
376
+ async function loadImports(imports, relativeTo) {
377
+ // collect imports
378
+ for (const { path, target } of imports) {
379
+ await loadImport(path, target, relativeTo);
380
+ }
381
+ }
382
+ async function loadImport(path, target, relativeTo) {
383
+ const library = await resolveTypeSpecLibrary(path, relativeTo, target);
384
+ if (library === undefined) {
385
+ return;
386
+ }
387
+ if (library.type === "module") {
388
+ loadedLibraries.set(library.manifest.name, {
389
+ path: library.path,
390
+ manifest: library.manifest,
391
+ });
392
+ trace("import-resolution.library", `Loading library "${path}" from "${library.mainFile}"`);
393
+ }
394
+ const importFilePath = library.type === "module" ? library.mainFile : library.path;
395
+ const isDirectory = (await host.stat(importFilePath)).isDirectory();
396
+ if (isDirectory) {
397
+ return await loadDirectory(importFilePath, target);
398
+ }
399
+ const sourceFileKind = host.getSourceFileKind(importFilePath);
400
+ switch (sourceFileKind) {
401
+ case "js":
402
+ return await importJsFile(importFilePath, target);
403
+ case "typespec":
404
+ return await loadTypeSpecFile(importFilePath, target);
405
+ default:
406
+ program.reportDiagnostic(createDiagnostic({ code: "invalid-import", target }));
407
+ }
408
+ }
409
+ async function loadEmitters(mainFile, emitterNameOrPaths, emitterOptions) {
410
+ var _c;
411
+ const emitterThatShouldExists = new Set(Object.keys(emitterOptions));
412
+ for (const emitterNameOrPath of emitterNameOrPaths) {
413
+ const emitter = await loadEmitter(mainFile, emitterNameOrPath, emitterOptions);
414
+ if (emitter) {
415
+ emitters.push(emitter);
416
+ emitterThatShouldExists.delete((_c = emitter.metadata.name) !== null && _c !== void 0 ? _c : emitterNameOrPath);
417
+ }
418
+ }
419
+ // This is the emitter names under options that haven't been used. We need to check if it points to an emitter that wasn't loaded
420
+ for (const emitterName of emitterThatShouldExists) {
421
+ // attempt to resolve a node module with this name
422
+ const [module, _] = await resolveEmitterModuleAndEntrypoint(mainFile, emitterName);
423
+ if ((module === null || module === void 0 ? void 0 : module.entrypoint) === undefined) {
424
+ program.reportDiagnostic(createDiagnostic({
425
+ code: "emitter-not-found",
426
+ format: { emitterName },
427
+ target: NoTarget,
428
+ }));
429
+ }
430
+ }
431
+ }
432
+ async function resolveEmitterModuleAndEntrypoint(mainFile, emitterNameOrPath) {
433
+ const basedir = getDirectoryPath(mainFile);
434
+ // attempt to resolve a node module with this name
435
+ const [module, diagnostics] = await resolveJSLibrary(emitterNameOrPath, basedir);
436
+ if (!module) {
437
+ return [undefined, diagnostics];
438
+ }
439
+ const entrypoint = module.type === "file" ? module.path : module.mainFile;
440
+ const file = await loadJsFile(entrypoint, NoTarget);
441
+ return [{ module, entrypoint: file }, []];
442
+ }
443
+ async function loadEmitter(mainFile, emitterNameOrPath, emittersOptions) {
444
+ var _c, _d, _e, _f;
445
+ const [resolution, diagnostics] = await resolveEmitterModuleAndEntrypoint(mainFile, emitterNameOrPath);
446
+ if (resolution === undefined) {
447
+ program.reportDiagnostics(diagnostics);
448
+ return undefined;
449
+ }
450
+ const { module, entrypoint } = resolution;
451
+ if (entrypoint === undefined) {
452
+ program.reportDiagnostic(createDiagnostic({
453
+ code: "invalid-emitter",
454
+ format: { emitterPackage: emitterNameOrPath },
455
+ target: NoTarget,
456
+ }));
457
+ return undefined;
458
+ }
459
+ const emitFunction = entrypoint.esmExports.$onEmit;
460
+ const libDefinition = entrypoint.esmExports.$lib;
461
+ const metadata = computeLibraryMetadata(module);
462
+ let { "emitter-output-dir": emitterOutputDir, ...emitterOptions } = (_d = emittersOptions[(_c = metadata.name) !== null && _c !== void 0 ? _c : emitterNameOrPath]) !== null && _d !== void 0 ? _d : {};
463
+ if (emitterOutputDir === undefined) {
464
+ emitterOutputDir = [options.outputDir, metadata.name].filter(isDefined).join("/");
465
+ }
466
+ if (libDefinition === null || libDefinition === void 0 ? void 0 : libDefinition.requireImports) {
467
+ for (const lib of libDefinition.requireImports) {
468
+ requireImports.set(lib, libDefinition.name);
469
+ }
470
+ }
471
+ if (emitFunction !== undefined) {
472
+ if ((_e = libDefinition === null || libDefinition === void 0 ? void 0 : libDefinition.emitter) === null || _e === void 0 ? void 0 : _e.options) {
473
+ const diagnostics = (_f = libDefinition === null || libDefinition === void 0 ? void 0 : libDefinition.emitterOptionValidator) === null || _f === void 0 ? void 0 : _f.validate(emitterOptions, NoTarget);
474
+ if (diagnostics && diagnostics.length > 0) {
475
+ program.reportDiagnostics(diagnostics);
476
+ return;
477
+ }
478
+ }
479
+ return {
480
+ main: entrypoint.file.path,
481
+ emitFunction,
482
+ metadata,
483
+ emitterOutputDir,
484
+ options: emitterOptions,
485
+ };
486
+ }
487
+ else {
488
+ program.reportDiagnostic(createDiagnostic({
489
+ code: "invalid-emitter",
490
+ format: { emitterPackage: emitterNameOrPath },
491
+ target: NoTarget,
492
+ }));
493
+ return undefined;
494
+ }
495
+ }
496
+ function computeLibraryMetadata(module) {
497
+ var _c, _d;
498
+ if (module.type === "file") {
499
+ return {};
500
+ }
501
+ const metadata = {
502
+ name: module.manifest.name,
503
+ };
504
+ if (module.manifest.homepage) {
505
+ metadata.homepage = module.manifest.homepage;
506
+ }
507
+ if ((_c = module.manifest.bugs) === null || _c === void 0 ? void 0 : _c.url) {
508
+ metadata.bugs = { url: (_d = module.manifest.bugs) === null || _d === void 0 ? void 0 : _d.url };
509
+ }
510
+ return metadata;
511
+ }
512
+ /**
513
+ * @param emitter Emitter ref to run
514
+ */
515
+ async function runEmitter(emitter) {
516
+ var _c, _d, _e;
517
+ const context = {
518
+ program,
519
+ emitterOutputDir: emitter.emitterOutputDir,
520
+ options: emitter.options,
521
+ getAssetEmitter(TypeEmitterClass) {
522
+ return createAssetEmitter(program, TypeEmitterClass, this);
523
+ },
524
+ };
525
+ try {
526
+ await emitter.emitFunction(context);
527
+ }
528
+ catch (error) {
529
+ const msg = [`Emitter "${(_c = emitter.metadata.name) !== null && _c !== void 0 ? _c : emitter.main}" failed!`];
530
+ if ((_d = emitter.metadata.bugs) === null || _d === void 0 ? void 0 : _d.url) {
531
+ msg.push(`File issue at ${(_e = emitter.metadata.bugs) === null || _e === void 0 ? void 0 : _e.url}`);
532
+ }
533
+ else {
534
+ msg.push(`Please contact emitter author to report this issue.`);
535
+ }
536
+ msg.push("");
537
+ if ("stack" in error) {
538
+ msg.push(error.stack);
539
+ }
540
+ else {
541
+ msg.push(String(error));
542
+ }
543
+ throw new ExternalError(msg.join("\n"));
544
+ }
545
+ }
546
+ /**
547
+ * resolves a module specifier like "myLib" to an absolute path where we can find the main of
548
+ * that module, e.g. "/typespec/node_modules/myLib/main.tsp".
549
+ */
550
+ async function resolveTypeSpecLibrary(specifier, baseDir, target) {
551
+ try {
552
+ return await resolveModule(getResolveModuleHost(), specifier, {
553
+ baseDir,
554
+ directoryIndexFiles: ["main.tsp", "index.mjs", "index.js"],
555
+ resolveMain(pkg) {
556
+ var _c;
557
+ // this lets us follow node resolve semantics more-or-less exactly
558
+ // but using tspMain instead of main.
559
+ return (_c = resolveTspMain(pkg)) !== null && _c !== void 0 ? _c : pkg.main;
560
+ },
561
+ });
562
+ }
563
+ catch (e) {
564
+ if (e.code === "MODULE_NOT_FOUND") {
565
+ program.reportDiagnostic(createDiagnostic({ code: "import-not-found", format: { path: specifier }, target }));
566
+ return undefined;
567
+ }
568
+ else if (e.code === "INVALID_MAIN") {
569
+ program.reportDiagnostic(createDiagnostic({
570
+ code: "library-invalid",
571
+ format: { path: specifier },
572
+ messageId: "tspMain",
573
+ target,
574
+ }));
575
+ return undefined;
576
+ }
577
+ else {
578
+ throw e;
579
+ }
580
+ }
581
+ }
582
+ /**
583
+ * resolves a module specifier like "myLib" to an absolute path where we can find the main of
584
+ * that module, e.g. "/typespec/node_modules/myLib/dist/lib.js".
585
+ */
586
+ async function resolveJSLibrary(specifier, baseDir) {
587
+ try {
588
+ return [await resolveModule(getResolveModuleHost(), specifier, { baseDir }), []];
589
+ }
590
+ catch (e) {
591
+ if (e.code === "MODULE_NOT_FOUND") {
592
+ return [
593
+ undefined,
594
+ [
595
+ createDiagnostic({
596
+ code: "import-not-found",
597
+ format: { path: specifier },
598
+ target: NoTarget,
599
+ }),
600
+ ],
601
+ ];
602
+ }
603
+ else if (e.code === "INVALID_MAIN") {
604
+ return [
605
+ undefined,
606
+ [
607
+ createDiagnostic({
608
+ code: "library-invalid",
609
+ format: { path: specifier },
610
+ target: NoTarget,
611
+ }),
612
+ ],
613
+ ];
614
+ }
615
+ else {
616
+ throw e;
617
+ }
618
+ }
619
+ }
620
+ function getResolveModuleHost() {
621
+ return {
622
+ realpath: host.realpath,
623
+ stat: host.stat,
624
+ readFile: async (path) => {
625
+ const file = await host.readFile(path);
626
+ return file.text;
627
+ },
628
+ };
629
+ }
630
+ /**
631
+ * Resolve the path to the main file
632
+ * @param path path to the entrypoint of the program. Can be the main.tsp, folder containing main.tsp or a project/library root.
633
+ * @returns Absolute path to the entrypoint.
634
+ */
635
+ async function resolveTypeSpecEntrypoint(path) {
636
+ const resolvedPath = resolvePath(path);
637
+ const mainStat = await doIO(host.stat, resolvedPath, program.reportDiagnostic);
638
+ if (!mainStat) {
639
+ return undefined;
640
+ }
641
+ if (mainStat.isDirectory()) {
642
+ return resolveTypeSpecEntrypointForDir(resolvedPath);
643
+ }
644
+ else {
645
+ return resolvedPath;
646
+ }
647
+ }
648
+ async function resolveTypeSpecEntrypointForDir(dir) {
649
+ const pkgJsonPath = resolvePath(dir, "package.json");
650
+ const [pkg] = await loadFile(host, pkgJsonPath, JSON.parse, program.reportDiagnostic, {
651
+ allowFileNotFound: true,
652
+ });
653
+ const tspMain = resolveTspMain(pkg);
654
+ if (tspMain !== undefined) {
655
+ return resolvePath(dir, tspMain);
656
+ }
657
+ // Back Compat: if main.cadl exist, return main.cadl
658
+ let mainFile = resolvePath(dir, "main.cadl");
659
+ const stat = await doIO(() => host.stat(mainFile), mainFile, () => { });
660
+ // if not found, use the normal resolution.
661
+ if ((stat === null || stat === void 0 ? void 0 : stat.isFile()) !== true) {
662
+ mainFile = resolvePath(dir, "main.tsp");
663
+ }
664
+ return mainFile;
665
+ }
666
+ /**
667
+ * Load the main file from the given path
668
+ * @param mainPath Absolute path to the main file.
669
+ * @param options Compiler options.
670
+ * @returns
671
+ */
672
+ async function loadMain(mainPath, options) {
673
+ await checkForCompilerVersionMismatch(mainPath);
674
+ const sourceFileKind = host.getSourceFileKind(mainPath);
675
+ switch (sourceFileKind) {
676
+ case "js":
677
+ return await importJsFile(mainPath, NoTarget);
678
+ case "typespec":
679
+ return await loadTypeSpecFile(mainPath, NoTarget);
680
+ default:
681
+ program.reportDiagnostic(createDiagnostic({ code: "invalid-main", target: NoTarget }));
682
+ }
683
+ }
684
+ // It's important that we use the compiler version that resolves locally
685
+ // from the input TypeSpec source location. Otherwise, there will be undefined
686
+ // runtime behavior when decorators and handlers expect a
687
+ // different version of typespec than the current one. Abort the compilation
688
+ // with an error if the TypeSpec entry point resolves to a different local
689
+ // compiler.
690
+ async function checkForCompilerVersionMismatch(mainPath) {
691
+ const baseDir = getDirectoryPath(mainPath);
692
+ let actual;
693
+ try {
694
+ const resolved = await resolveModule({
695
+ realpath: host.realpath,
696
+ stat: host.stat,
697
+ readFile: async (path) => {
698
+ const file = await host.readFile(path);
699
+ return file.text;
700
+ },
701
+ }, "@typespec/compiler", { baseDir });
702
+ compilerAssert(resolved.type === "module", `Expected to have resolved "@typespec/compiler" to a node module.`);
703
+ actual = resolved;
704
+ }
705
+ catch (err) {
706
+ if (err.code === "MODULE_NOT_FOUND" || err.code === "INVALID_MAIN") {
707
+ return true; // no local typespec, ok to use any compiler
708
+ }
709
+ throw err;
710
+ }
711
+ const expected = resolvePath(await host.realpath(host.fileURLToPath(import.meta.url)), "../index.js");
712
+ if (actual.mainFile !== expected && MANIFEST.version !== actual.manifest.version) {
713
+ // we have resolved node_modules/@typespec/compiler/dist/core/index.js and we want to get
714
+ // to the shim executable node_modules/.bin/tsp-server
715
+ const betterTypeSpecServerPath = resolvePath(actual.path, ".bin/tsp-server");
716
+ program.reportDiagnostic(createDiagnostic({
717
+ code: "compiler-version-mismatch",
718
+ format: { basedir: baseDir, betterTypeSpecServerPath, actual: actual.mainFile, expected },
719
+ target: NoTarget,
720
+ }));
721
+ return false;
722
+ }
723
+ return true;
724
+ }
725
+ function getOption(key) {
726
+ return (options.miscOptions || {})[key];
727
+ }
728
+ function reportDiagnostic(diagnostic) {
729
+ if (shouldSuppress(diagnostic)) {
730
+ return;
731
+ }
732
+ if (options.warningAsError && diagnostic.severity === "warning") {
733
+ diagnostic = { ...diagnostic, severity: "error" };
734
+ }
735
+ if (diagnostic.severity === "error") {
736
+ error = true;
737
+ }
738
+ diagnostics.push(diagnostic);
739
+ }
740
+ function reportDiagnostics(newDiagnostics) {
741
+ for (const diagnostic of newDiagnostics) {
742
+ reportDiagnostic(diagnostic);
743
+ }
744
+ }
745
+ function shouldSuppress(diagnostic) {
746
+ const { target } = diagnostic;
747
+ if (diagnostic.code === "error") {
748
+ diagnostics.push(diagnostic);
749
+ return false;
750
+ }
751
+ if (target === NoTarget || target === undefined) {
752
+ return false;
753
+ }
754
+ if ("file" in target) {
755
+ return false; // No global file suppress yet.
756
+ }
757
+ const node = getNode(target);
758
+ if (node === undefined) {
759
+ return false; // Can't find target cannot be suppressed.
760
+ }
761
+ const suppressing = findDirectiveSuppressingOnNode(diagnostic.code, node);
762
+ if (suppressing) {
763
+ if (diagnostic.severity === "error") {
764
+ // Cannot suppress errors.
765
+ diagnostics.push({
766
+ severity: "error",
767
+ code: "suppress-error",
768
+ message: "Errors cannot be suppressed.",
769
+ target: suppressing.node,
770
+ });
771
+ return false;
772
+ }
773
+ else {
774
+ return true;
775
+ }
776
+ }
777
+ return false;
778
+ }
779
+ function findDirectiveSuppressingOnNode(code, node) {
780
+ let current = node;
781
+ do {
782
+ if (current.directives) {
783
+ const directive = findDirectiveSuppressingCode(code, current.directives);
784
+ if (directive) {
785
+ return directive;
786
+ }
787
+ }
788
+ } while ((current = current.parent));
789
+ return undefined;
790
+ }
791
+ /**
792
+ * Returns the directive node that is suppressing this code.
793
+ * @param code Code to check for suppression.
794
+ * @param directives List of directives.
795
+ * @returns Directive suppressing this code if found, `undefined` otherwise
796
+ */
797
+ function findDirectiveSuppressingCode(code, directives) {
798
+ for (const directive of directives.map((x) => parseDirective(x))) {
799
+ if (directive.name === "suppress") {
800
+ if (directive.code === code) {
801
+ return directive;
802
+ }
803
+ }
804
+ }
805
+ return undefined;
806
+ }
807
+ function parseDirective(node) {
808
+ const args = node.arguments.map((x) => {
809
+ return x.kind === SyntaxKind.Identifier ? x.sv : x.value;
810
+ });
811
+ switch (node.target.sv) {
812
+ case "suppress":
813
+ return { name: "suppress", code: args[0], message: args[1], node };
814
+ default:
815
+ throw new Error("Unexpected directive name.");
816
+ }
817
+ }
818
+ function getNode(target) {
819
+ if (!("kind" in target)) {
820
+ // symbol
821
+ if (target.flags & 524288 /* SymbolFlags.Using */) {
822
+ return target.symbolSource.declarations[0];
823
+ }
824
+ return target.declarations[0]; // handle multiple decls
825
+ }
826
+ else if (typeof target.kind === "number") {
827
+ // node
828
+ return target;
829
+ }
830
+ else {
831
+ // type
832
+ return target.node;
833
+ }
834
+ }
835
+ function reportDuplicateSymbols(symbols) {
836
+ if (!symbols) {
837
+ return;
838
+ }
839
+ for (const set of symbols.duplicates.values()) {
840
+ for (const symbol of set) {
841
+ if (!duplicateSymbols.has(symbol)) {
842
+ duplicateSymbols.add(symbol);
843
+ const name = symbol.flags & 524288 /* SymbolFlags.Using */ ? symbol.symbolSource.name : symbol.name;
844
+ reportDiagnostic(createDiagnostic({
845
+ code: "duplicate-symbol",
846
+ format: { name },
847
+ target: symbol,
848
+ }));
849
+ }
850
+ }
851
+ }
852
+ }
853
+ function getGlobalNamespaceType() {
854
+ return program.checker.getGlobalNamespaceType();
855
+ }
856
+ function resolveTypeReference(reference) {
857
+ const [node, parseDiagnostics] = parseStandaloneTypeReference(reference);
858
+ if (parseDiagnostics.length > 0) {
859
+ return [undefined, parseDiagnostics];
860
+ }
861
+ const binder = createBinder(program);
862
+ binder.bindNode(node);
863
+ mutate(node).parent = program.checker.getGlobalNamespaceNode();
864
+ return program.checker.resolveTypeReference(node);
865
+ }
866
+ }
867
+ export function createStateAccessors(stateMaps, stateSets, projector) {
868
+ function stateMap(key) {
869
+ let m = stateMaps.get(key);
870
+ if (!m) {
871
+ m = new StateMap();
872
+ stateMaps.set(key, m);
873
+ }
874
+ return new StateMapView(m, projector);
875
+ }
876
+ function stateSet(key) {
877
+ let s = stateSets.get(key);
878
+ if (!s) {
879
+ s = new StateSet();
880
+ stateSets.set(key, s);
881
+ }
882
+ return new StateSetView(s, projector);
883
+ }
884
+ return { stateMap, stateSet };
885
+ }
886
+ /**
887
+ * Resolve compiler options from input options.
888
+ */
889
+ function resolveOptions(options) {
890
+ var _c;
891
+ const outputDir = (_c = options.outputDir) !== null && _c !== void 0 ? _c : options.outputPath;
892
+ return {
893
+ ...options,
894
+ outputDir,
895
+ outputPath: outputDir,
896
+ };
897
+ }
898
+ //# sourceMappingURL=program.js.map