@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,1025 @@
1
+ import prettier from "prettier";
2
+ import { isIdentifierContinue, isIdentifierStart, utf16CodeUnits } from "../../core/charcode.js";
3
+ import { compilerAssert } from "../../core/diagnostics.js";
4
+ import { Keywords } from "../../core/scanner.js";
5
+ import { SyntaxKind, } from "../../core/types.js";
6
+ import { isArray } from "../../core/util.js";
7
+ import { commentHandler } from "./comment-handler.js";
8
+ import { needsParens } from "./needs-parens.js";
9
+ const { align, breakParent, group, hardline, ifBreak, indent, join, line, softline } = prettier.doc.builders;
10
+ const { isNextLineEmpty } = prettier.util;
11
+ export const typespecPrinter = {
12
+ print: printTypeSpec,
13
+ canAttachComment: canAttachComment,
14
+ printComment: printComment,
15
+ handleComments: commentHandler,
16
+ };
17
+ export function printTypeSpec(
18
+ // Path to the AST node to print
19
+ path, options, print) {
20
+ const node = path.getValue();
21
+ const directives = shouldPrintDirective(node) ? printDirectives(path, options, print) : "";
22
+ const printedNode = printNode(path, options, print);
23
+ const value = needsParens(path, options) ? ["(", printedNode, ")"] : printedNode;
24
+ const parts = [directives, value];
25
+ if (node.kind === SyntaxKind.TypeSpecScript) {
26
+ // For TypeSpecScript(root of typespec document) we had a new line at the end.
27
+ // This must be done here so the hardline entry can be the last item of the doc array returned by the printer
28
+ // so the markdown(and other embedded formatter) can omit that extra line.
29
+ parts.push(hardline);
30
+ }
31
+ return parts;
32
+ }
33
+ function shouldPrintDirective(node) {
34
+ // Model property handle printing directive itself.
35
+ return node.kind !== SyntaxKind.ModelProperty;
36
+ }
37
+ export function printNode(
38
+ // Path to the AST node to print
39
+ path, options, print) {
40
+ const node = path.getValue();
41
+ printDirectives(path, options, print);
42
+ switch (node.kind) {
43
+ // Root
44
+ case SyntaxKind.TypeSpecScript:
45
+ return [
46
+ printStatementSequence(path, options, print, "statements"),
47
+ ];
48
+ // Statements
49
+ case SyntaxKind.ImportStatement:
50
+ return [`import "${node.path.value}";`];
51
+ case SyntaxKind.UsingStatement:
52
+ return [`using `, path.call(print, "name"), `;`];
53
+ case SyntaxKind.OperationStatement:
54
+ return printOperationStatement(path, options, print);
55
+ case SyntaxKind.OperationSignatureDeclaration:
56
+ return printOperationSignatureDeclaration(path, options, print);
57
+ case SyntaxKind.OperationSignatureReference:
58
+ return printOperationSignatureReference(path, options, print);
59
+ case SyntaxKind.NamespaceStatement:
60
+ return printNamespaceStatement(path, options, print);
61
+ case SyntaxKind.ModelStatement:
62
+ return printModelStatement(path, options, print);
63
+ case SyntaxKind.ScalarStatement:
64
+ return printScalarStatement(path, options, print);
65
+ case SyntaxKind.AliasStatement:
66
+ return printAliasStatement(path, options, print);
67
+ case SyntaxKind.EnumStatement:
68
+ return printEnumStatement(path, options, print);
69
+ case SyntaxKind.UnionStatement:
70
+ return printUnionStatement(path, options, print);
71
+ case SyntaxKind.InterfaceStatement:
72
+ return printInterfaceStatement(path, options, print);
73
+ // Others.
74
+ case SyntaxKind.Identifier:
75
+ return printIdentifier(node, options);
76
+ case SyntaxKind.StringLiteral:
77
+ return printStringLiteral(path, options);
78
+ case SyntaxKind.NumericLiteral:
79
+ return printNumberLiteral(path, options);
80
+ case SyntaxKind.BooleanLiteral:
81
+ return printBooleanLiteral(path, options);
82
+ case SyntaxKind.ModelExpression:
83
+ return printModelExpression(path, options, print);
84
+ case SyntaxKind.ModelProperty:
85
+ return printModelProperty(path, options, print);
86
+ case SyntaxKind.DecoratorExpression:
87
+ return printDecorator(path, options, print);
88
+ case SyntaxKind.AugmentDecoratorStatement:
89
+ return printAugmentDecorator(path, options, print);
90
+ case SyntaxKind.DirectiveExpression:
91
+ return printDirective(path, options, print);
92
+ case SyntaxKind.UnionExpression:
93
+ return printUnion(path, options, print);
94
+ case SyntaxKind.IntersectionExpression:
95
+ return printIntersection(path, options, print);
96
+ case SyntaxKind.ArrayExpression:
97
+ return printArray(path, options, print);
98
+ case SyntaxKind.TupleExpression:
99
+ return printTuple(path, options, print);
100
+ case SyntaxKind.MemberExpression:
101
+ return printMemberExpression(path, options, print);
102
+ case SyntaxKind.EnumMember:
103
+ return printEnumMember(path, options, print);
104
+ case SyntaxKind.EnumSpreadMember:
105
+ return printEnumSpreadMember(path, options, print);
106
+ case SyntaxKind.UnionVariant:
107
+ return printUnionVariant(path, options, print);
108
+ case SyntaxKind.TypeReference:
109
+ return printTypeReference(path, options, print);
110
+ case SyntaxKind.TemplateParameterDeclaration:
111
+ return printTemplateParameterDeclaration(path, options, print);
112
+ case SyntaxKind.ModelSpreadProperty:
113
+ return printModelSpread(path, options, print);
114
+ case SyntaxKind.DecoratorDeclarationStatement:
115
+ return printDecoratorDeclarationStatement(path, options, print);
116
+ case SyntaxKind.FunctionDeclarationStatement:
117
+ return printFunctionDeclarationStatement(path, options, print);
118
+ case SyntaxKind.FunctionParameter:
119
+ return printFunctionParameterDeclaration(path, options, print);
120
+ case SyntaxKind.ExternKeyword:
121
+ return "extern";
122
+ case SyntaxKind.VoidKeyword:
123
+ return "void";
124
+ case SyntaxKind.NeverKeyword:
125
+ return "never";
126
+ case SyntaxKind.UnknownKeyword:
127
+ return "unknown";
128
+ case SyntaxKind.ProjectionStatement:
129
+ return printProjectionStatement(path, options, print);
130
+ case SyntaxKind.ProjectionModelSelector:
131
+ return "model";
132
+ case SyntaxKind.ProjectionOperationSelector:
133
+ return "op";
134
+ case SyntaxKind.ProjectionUnionSelector:
135
+ return "union";
136
+ case SyntaxKind.ProjectionInterfaceSelector:
137
+ return "interface";
138
+ case SyntaxKind.ProjectionEnumSelector:
139
+ return "enum";
140
+ case SyntaxKind.Projection:
141
+ return printProjection(path, options, print);
142
+ case SyntaxKind.ProjectionParameterDeclaration:
143
+ return printProjectionParameterDeclaration(path, options, print);
144
+ case SyntaxKind.ProjectionExpressionStatement:
145
+ return printProjectionExpressionStatement(path, options, print);
146
+ case SyntaxKind.ProjectionIfExpression:
147
+ return printProjectionIfExpressionNode(path, options, print);
148
+ case SyntaxKind.ProjectionBlockExpression:
149
+ return printProjectionBlockExpressionNode(path, options, print);
150
+ case SyntaxKind.ProjectionMemberExpression:
151
+ return printProjectionMemberExpression(path, options, print);
152
+ case SyntaxKind.ProjectionLogicalExpression:
153
+ case SyntaxKind.ProjectionEqualityExpression:
154
+ case SyntaxKind.ProjectionRelationalExpression:
155
+ case SyntaxKind.ProjectionArithmeticExpression:
156
+ return printProjectionLeftRightExpression(path, options, print);
157
+ case SyntaxKind.ProjectionUnaryExpression:
158
+ return printProjectionUnaryExpression(path, options, print);
159
+ case SyntaxKind.ProjectionCallExpression:
160
+ return printProjectionCallExpression(path, options, print);
161
+ case SyntaxKind.ProjectionLambdaExpression:
162
+ return printProjectionLambdaExpression(path, options, print);
163
+ case SyntaxKind.ProjectionLambdaParameterDeclaration:
164
+ return printProjectionLambdaParameterDeclaration(path, options, print);
165
+ case SyntaxKind.ProjectionModelExpression:
166
+ return printModelExpression(path, options, print);
167
+ case SyntaxKind.ProjectionModelProperty:
168
+ return printModelProperty(path, options, print);
169
+ case SyntaxKind.ProjectionModelSpreadProperty:
170
+ return printModelSpread(path, options, print);
171
+ case SyntaxKind.ProjectionTupleExpression:
172
+ return printTuple(path, options, print);
173
+ case SyntaxKind.ProjectionDecoratorReferenceExpression:
174
+ return path.call(print, "target");
175
+ case SyntaxKind.Return:
176
+ return printReturnExpression(path, options, print);
177
+ case SyntaxKind.Doc:
178
+ case SyntaxKind.DocText:
179
+ case SyntaxKind.DocParamTag:
180
+ case SyntaxKind.DocTemplateTag:
181
+ case SyntaxKind.DocReturnsTag:
182
+ case SyntaxKind.DocUnknownTag:
183
+ // https://github.com/microsoft/typespec/issues/1319 Tracks pretty-printing doc comments.
184
+ compilerAssert(false, "Currently, doc comments are only handled as regular comments and we do not opt in to parsing them so we shouldn't reach here.");
185
+ return "";
186
+ case SyntaxKind.JsSourceFile:
187
+ case SyntaxKind.EmptyStatement:
188
+ case SyntaxKind.InvalidStatement:
189
+ return getRawText(node, options);
190
+ default:
191
+ // Dummy const to ensure we handle all node types.
192
+ // If you get an error here, add a case for the new node type
193
+ // you added..
194
+ const _assertNever = node;
195
+ return getRawText(node, options);
196
+ }
197
+ }
198
+ export function printAliasStatement(path, options, print) {
199
+ const id = path.call(print, "id");
200
+ const template = printTemplateParameters(path, options, print, "templateParameters");
201
+ return ["alias ", id, template, " = ", path.call(print, "value"), ";"];
202
+ }
203
+ function printTemplateParameters(path, options, print, propertyName) {
204
+ const node = path.getValue();
205
+ const args = node[propertyName];
206
+ if (args.length === 0) {
207
+ return "";
208
+ }
209
+ const shouldHug = args.length === 1;
210
+ if (shouldHug) {
211
+ return ["<", join(", ", path.map(print, propertyName)), ">"];
212
+ }
213
+ else {
214
+ const body = indent([softline, join([", ", softline], path.map(print, propertyName))]);
215
+ return group(["<", body, softline, ">"]);
216
+ }
217
+ }
218
+ export function canAttachComment(node) {
219
+ const kind = node.kind;
220
+ return Boolean(kind &&
221
+ kind !== SyntaxKind.LineComment &&
222
+ kind !== SyntaxKind.BlockComment &&
223
+ !(node.flags & 8 /* NodeFlags.Synthetic */));
224
+ }
225
+ export function printComment(commentPath, options) {
226
+ const comment = commentPath.getValue();
227
+ comment.printed = true;
228
+ switch (comment.kind) {
229
+ case SyntaxKind.BlockComment:
230
+ return printBlockComment(commentPath, options);
231
+ case SyntaxKind.LineComment:
232
+ return `${options.originalText.slice(comment.pos, comment.end).trimRight()}`;
233
+ default:
234
+ throw new Error(`Not a comment: ${JSON.stringify(comment)}`);
235
+ }
236
+ }
237
+ function printBlockComment(commentPath, options) {
238
+ const comment = commentPath.getValue();
239
+ const rawComment = options.originalText.slice(comment.pos + 2, comment.end - 2);
240
+ if (isIndentableBlockComment(rawComment)) {
241
+ const printed = printIndentableBlockComment(rawComment);
242
+ return printed;
243
+ }
244
+ return ["/*", rawComment, "*/"];
245
+ }
246
+ function isIndentableBlockComment(rawComment) {
247
+ // If the comment has multiple lines and every line starts with a star
248
+ // we can fix the indentation of each line. The stars in the `/*` and
249
+ // `*/` delimiters are not included in the comment value, so add them
250
+ // back first.
251
+ const lines = `*${rawComment}*`.split("\n");
252
+ return lines.length > 1 && lines.every((line) => line.trim()[0] === "*");
253
+ }
254
+ function printIndentableBlockComment(rawComment) {
255
+ const lines = rawComment.split("\n");
256
+ return [
257
+ "/*",
258
+ join(hardline, lines.map((line, index) => index === 0
259
+ ? line.trimEnd()
260
+ : " " + (index < lines.length - 1 ? line.trim() : line.trimStart()))),
261
+ "*/",
262
+ ];
263
+ }
264
+ export function printDecorators(path, options, print, { tryInline }) {
265
+ const node = path.getValue();
266
+ if (node.decorators.length === 0) {
267
+ return { decorators: "", multiline: false };
268
+ }
269
+ const shouldBreak = !tryInline || node.decorators.length >= 3 || hasNewlineBetweenOrAfterDecorators(node, options);
270
+ const decorators = path.map((x) => [print(x), ifBreak(line, " ")], "decorators");
271
+ return {
272
+ decorators: group([shouldBreak ? breakParent : "", decorators]),
273
+ multiline: shouldBreak,
274
+ };
275
+ }
276
+ /**
277
+ * Check if there is already new lines in between the decorators of the node.
278
+ */
279
+ function hasNewlineBetweenOrAfterDecorators(node, options) {
280
+ return node.decorators.some((decorator) => prettier.util.hasNewline(options.originalText, decorator.end));
281
+ }
282
+ export function printDecorator(path, options, print) {
283
+ const args = printDecoratorArgs(path, options, print);
284
+ return ["@", path.call(print, "target"), args];
285
+ }
286
+ export function printAugmentDecorator(path, options, print) {
287
+ const args = printAugmentDecoratorArgs(path, options, print);
288
+ return ["@@", path.call(print, "target"), args, ";"];
289
+ }
290
+ function printAugmentDecoratorArgs(path, options, print) {
291
+ return [
292
+ "(",
293
+ group([
294
+ indent(join(", ", [
295
+ path.call(print, "targetType"),
296
+ ...path.map((arg) => [softline, print(arg)], "arguments"),
297
+ ])),
298
+ softline,
299
+ ]),
300
+ ")",
301
+ ];
302
+ }
303
+ export function printDirectives(path, options, print) {
304
+ const node = path.getValue();
305
+ if (node.directives === undefined || node.directives.length === 0) {
306
+ return "";
307
+ }
308
+ const directives = path.map((x) => [print(x), line], "directives");
309
+ return group([...directives, breakParent]);
310
+ }
311
+ export function printDirective(path, options, print) {
312
+ const args = printDirectiveArgs(path, options, print);
313
+ return ["#", path.call(print, "target"), " ", args];
314
+ }
315
+ function printDecoratorArgs(path, options, print) {
316
+ const node = path.getValue();
317
+ if (node.arguments.length === 0) {
318
+ return "";
319
+ }
320
+ // So that decorator with single object arguments have ( and { hugging.
321
+ // @deco({
322
+ // value: "foo"
323
+ // })
324
+ const shouldHug = node.arguments.length === 1 &&
325
+ (node.arguments[0].kind === SyntaxKind.ModelExpression ||
326
+ node.arguments[0].kind === SyntaxKind.StringLiteral);
327
+ if (shouldHug) {
328
+ return [
329
+ "(",
330
+ join(", ", path.map((arg) => [print(arg)], "arguments")),
331
+ ")",
332
+ ];
333
+ }
334
+ return [
335
+ "(",
336
+ group([
337
+ indent(join(", ", path.map((arg) => [softline, print(arg)], "arguments"))),
338
+ softline,
339
+ ]),
340
+ ")",
341
+ ];
342
+ }
343
+ export function printDirectiveArgs(path, options, print) {
344
+ const node = path.getValue();
345
+ if (node.arguments.length === 0) {
346
+ return "";
347
+ }
348
+ return join(" ", path.map((arg) => [print(arg)], "arguments"));
349
+ }
350
+ export function printEnumStatement(path, options, print) {
351
+ const { decorators } = printDecorators(path, options, print, { tryInline: false });
352
+ const id = path.call(print, "id");
353
+ return [decorators, "enum ", id, " ", printEnumBlock(path, options, print)];
354
+ }
355
+ function printEnumBlock(path, options, print) {
356
+ const node = path.getValue();
357
+ if (node.members.length === 0) {
358
+ return "{}";
359
+ }
360
+ return group([
361
+ "{",
362
+ indent([
363
+ hardline,
364
+ join(hardline, path.map((x) => [print(x), ","], "members")),
365
+ ]),
366
+ hardline,
367
+ "}",
368
+ ]);
369
+ }
370
+ export function printEnumMember(path, options, print) {
371
+ const node = path.getValue();
372
+ const id = path.call(print, "id");
373
+ const value = node.value ? [": ", path.call(print, "value")] : "";
374
+ const { decorators, multiline } = printDecorators(path, options, print, { tryInline: true });
375
+ const propertyIndex = path.stack[path.stack.length - 2];
376
+ const isNotFirst = typeof propertyIndex === "number" && propertyIndex > 0;
377
+ return [multiline && isNotFirst ? hardline : "", decorators, id, value];
378
+ }
379
+ function printEnumSpreadMember(path, options, print) {
380
+ return ["...", path.call(print, "target")];
381
+ }
382
+ export function printUnionStatement(path, options, print) {
383
+ const id = path.call(print, "id");
384
+ const { decorators } = printDecorators(path, options, print, { tryInline: false });
385
+ const generic = printTemplateParameters(path, options, print, "templateParameters");
386
+ return [decorators, "union ", id, generic, " ", printUnionVariantsBlock(path, options, print)];
387
+ }
388
+ export function printUnionVariantsBlock(path, options, print) {
389
+ const node = path.getValue();
390
+ if (node.options.length === 0) {
391
+ return "{}";
392
+ }
393
+ return group([
394
+ "{",
395
+ indent([
396
+ hardline,
397
+ join(hardline, path.map((x) => [print(x), ","], "options")),
398
+ ]),
399
+ hardline,
400
+ "}",
401
+ ]);
402
+ }
403
+ export function printUnionVariant(path, options, print) {
404
+ const id = path.call(print, "id");
405
+ const value = [": ", path.call(print, "value")];
406
+ const { decorators } = printDecorators(path, options, print, { tryInline: true });
407
+ return [decorators, id, value];
408
+ }
409
+ export function printInterfaceStatement(path, options, print) {
410
+ const id = path.call(print, "id");
411
+ const { decorators } = printDecorators(path, options, print, { tryInline: false });
412
+ const generic = printTemplateParameters(path, options, print, "templateParameters");
413
+ const extendList = printInterfaceExtends(path, options, print);
414
+ return [
415
+ decorators,
416
+ "interface ",
417
+ id,
418
+ generic,
419
+ extendList,
420
+ " ",
421
+ printInterfaceMembers(path, options, print),
422
+ ];
423
+ }
424
+ function printInterfaceExtends(path, options, print) {
425
+ const node = path.getValue();
426
+ if (node.extends.length === 0) {
427
+ return "";
428
+ }
429
+ const keyword = "extends ";
430
+ return [group(indent([line, keyword, indent(join([",", line], path.map(print, "extends")))]))];
431
+ }
432
+ export function printInterfaceMembers(path, options, print) {
433
+ const node = path.getValue();
434
+ const hasOperations = node.operations.length > 0;
435
+ const nodeHasComments = hasComments(node, CommentCheckFlags.Dangling);
436
+ if (!hasOperations && !nodeHasComments) {
437
+ return "{}";
438
+ }
439
+ const lastOperation = node.operations[node.operations.length - 1];
440
+ const parts = [];
441
+ path.each((operationPath) => {
442
+ const node = operationPath.getValue();
443
+ const printed = print(operationPath);
444
+ parts.push(printed);
445
+ if (node !== lastOperation) {
446
+ parts.push(hardline);
447
+ if (isNextLineEmpty(options.originalText, node, options.locEnd)) {
448
+ parts.push(hardline);
449
+ }
450
+ }
451
+ }, "operations");
452
+ const body = [hardline, parts];
453
+ if (nodeHasComments) {
454
+ body.push(printDanglingComments(path, options, { sameIndent: true }));
455
+ }
456
+ return group(["{", indent(body), hardline, "}"]);
457
+ }
458
+ function printDanglingComments(path, options, { sameIndent }) {
459
+ const node = path.getValue();
460
+ const parts = [];
461
+ if (!node || !node.comments) {
462
+ return "";
463
+ }
464
+ path.each((commentPath) => {
465
+ const comment = commentPath.getValue();
466
+ if (!comment.leading && !comment.trailing) {
467
+ parts.push(printComment(path, options));
468
+ }
469
+ }, "comments");
470
+ if (parts.length === 0) {
471
+ return "";
472
+ }
473
+ if (sameIndent) {
474
+ return join(hardline, parts);
475
+ }
476
+ return indent([hardline, join(hardline, parts)]);
477
+ }
478
+ /**
479
+ * Handle printing an intersection node.
480
+ * @example `Foo & Bar` or `{foo: string} & {bar: string}`
481
+ *
482
+ * @param path Prettier AST Path.
483
+ * @param options Prettier options
484
+ * @param print Prettier child print callback.
485
+ * @returns Prettier document.
486
+ */
487
+ export function printIntersection(path, options, print) {
488
+ const node = path.getValue();
489
+ const types = path.map(print, "options");
490
+ const result = [];
491
+ let wasIndented = false;
492
+ for (let i = 0; i < types.length; ++i) {
493
+ if (i === 0) {
494
+ result.push(types[i]);
495
+ }
496
+ else if (isModelNode(node.options[i - 1]) && isModelNode(node.options[i])) {
497
+ // If both are objects, don't indent
498
+ result.push([" & ", wasIndented ? indent(types[i]) : types[i]]);
499
+ }
500
+ else if (!isModelNode(node.options[i - 1]) && !isModelNode(node.options[i])) {
501
+ // If no object is involved, go to the next line if it breaks
502
+ result.push(indent([" &", line, types[i]]));
503
+ }
504
+ else {
505
+ // If you go from object to non-object or vis-versa, then inline it
506
+ if (i > 1) {
507
+ wasIndented = true;
508
+ }
509
+ result.push(" & ", i > 1 ? indent(types[i]) : types[i]);
510
+ }
511
+ }
512
+ return group(result);
513
+ }
514
+ function isModelNode(node) {
515
+ return node.kind === SyntaxKind.ModelExpression;
516
+ }
517
+ export function printArray(path, options, print) {
518
+ return [path.call(print, "elementType"), "[]"];
519
+ }
520
+ export function printTuple(path, options, print) {
521
+ return group([
522
+ "[",
523
+ indent(join(", ", path.map((arg) => [softline, print(arg)], "values"))),
524
+ softline,
525
+ "]",
526
+ ]);
527
+ }
528
+ export function printMemberExpression(path, options, print) {
529
+ const node = path.getValue();
530
+ return [node.base ? [path.call(print, "base"), "."] : "", path.call(print, "id")];
531
+ }
532
+ export function printModelExpression(path, options, print) {
533
+ const inBlock = isModelExpressionInBlock(path);
534
+ if (inBlock) {
535
+ return group(printModelPropertiesBlock(path, options, print));
536
+ }
537
+ else {
538
+ return group([
539
+ indent(join(", ", path.map((arg) => [softline, print(arg)], "properties"))),
540
+ softline,
541
+ ]);
542
+ }
543
+ }
544
+ export function printModelStatement(path, options, print) {
545
+ const node = path.getValue();
546
+ const id = path.call(print, "id");
547
+ const heritage = node.extends
548
+ ? [ifBreak(line, " "), "extends ", path.call(print, "extends")]
549
+ : "";
550
+ const isBase = node.is ? [ifBreak(line, " "), "is ", path.call(print, "is")] : "";
551
+ const generic = printTemplateParameters(path, options, print, "templateParameters");
552
+ const nodeHasComments = hasComments(node, CommentCheckFlags.Dangling);
553
+ const shouldPrintBody = nodeHasComments || !(node.properties.length === 0 && node.is);
554
+ const body = shouldPrintBody ? [" ", printModelPropertiesBlock(path, options, print)] : ";";
555
+ return [
556
+ printDecorators(path, options, print, { tryInline: false }).decorators,
557
+ "model ",
558
+ id,
559
+ generic,
560
+ group(indent(["", heritage, isBase])),
561
+ body,
562
+ ];
563
+ }
564
+ function printModelPropertiesBlock(path, options, print) {
565
+ var _a;
566
+ const node = path.getValue();
567
+ const hasProperties = node.properties && node.properties.length > 0;
568
+ const nodeHasComments = hasComments(node, CommentCheckFlags.Dangling);
569
+ if (!hasProperties && !nodeHasComments) {
570
+ return "{}";
571
+ }
572
+ const tryInline = ((_a = path.getParentNode()) === null || _a === void 0 ? void 0 : _a.kind) === SyntaxKind.TemplateParameterDeclaration;
573
+ const lineDoc = tryInline ? softline : hardline;
574
+ const seperator = isModelAValue(path) ? "," : ";";
575
+ const body = [
576
+ lineDoc,
577
+ join([seperator, lineDoc], path.map((x) => [print(x)], "properties")),
578
+ hasProperties ? ifBreak(seperator) : "",
579
+ ];
580
+ if (nodeHasComments) {
581
+ body.push(printDanglingComments(path, options, { sameIndent: true }));
582
+ }
583
+ return group(["{", indent(body), lineDoc, "}"]);
584
+ }
585
+ /**
586
+ * Figure out if this model is being used as a definition or value.
587
+ * @returns true if the model is used as a value(e.g. decorator value), false if it is used as a model definition.
588
+ */
589
+ function isModelAValue(path) {
590
+ let count = 0;
591
+ let node = path.getValue();
592
+ do {
593
+ switch (node.kind) {
594
+ case SyntaxKind.ModelStatement:
595
+ case SyntaxKind.AliasStatement:
596
+ case SyntaxKind.OperationStatement:
597
+ return false;
598
+ case SyntaxKind.DecoratorExpression:
599
+ return true;
600
+ }
601
+ } while ((node = path.getParentNode(count++)));
602
+ return true;
603
+ }
604
+ export function printModelProperty(path, options, print) {
605
+ const node = path.getValue();
606
+ const propertyIndex = path.stack[path.stack.length - 2];
607
+ const isNotFirst = typeof propertyIndex === "number" && propertyIndex > 0;
608
+ const { decorators, multiline } = printDecorators(path, options, print, {
609
+ tryInline: true,
610
+ });
611
+ const id = printIdentifier(node.id, options);
612
+ return [
613
+ multiline && isNotFirst ? hardline : "",
614
+ printDirectives(path, options, print),
615
+ decorators,
616
+ id,
617
+ node.optional ? "?: " : ": ",
618
+ path.call(print, "value"),
619
+ node.default ? [" = ", path.call(print, "default")] : "",
620
+ ];
621
+ }
622
+ function printIdentifier(id, options) {
623
+ return printId(id.sv);
624
+ }
625
+ export function printId(sv) {
626
+ if (needBacktick(sv)) {
627
+ const escapedString = sv
628
+ .replace(/\\/g, "\\\\")
629
+ .replace(/\n/g, "\\n")
630
+ .replace(/\r/g, "\\r")
631
+ .replace(/\t/g, "\\t")
632
+ .replace(/`/g, "\\`");
633
+ return `\`${escapedString}\``;
634
+ }
635
+ else {
636
+ return sv;
637
+ }
638
+ }
639
+ function needBacktick(sv) {
640
+ if (sv.length === 0) {
641
+ return false;
642
+ }
643
+ if (Keywords.has(sv)) {
644
+ return true;
645
+ }
646
+ let cp = sv.codePointAt(0);
647
+ if (!isIdentifierStart(cp)) {
648
+ return true;
649
+ }
650
+ let pos = 0;
651
+ do {
652
+ pos += utf16CodeUnits(cp);
653
+ } while (pos < sv.length && isIdentifierContinue((cp = sv.codePointAt(pos))));
654
+ return pos < sv.length;
655
+ }
656
+ function isModelExpressionInBlock(path) {
657
+ const parent = path.getParentNode();
658
+ switch (parent === null || parent === void 0 ? void 0 : parent.kind) {
659
+ case SyntaxKind.OperationSignatureDeclaration:
660
+ return parent.parameters !== path.getNode();
661
+ default:
662
+ return true;
663
+ }
664
+ }
665
+ export function printScalarStatement(path, options, print) {
666
+ const node = path.getValue();
667
+ const id = path.call(print, "id");
668
+ const template = printTemplateParameters(path, options, print, "templateParameters");
669
+ const heritage = node.extends
670
+ ? [ifBreak(line, " "), "extends ", path.call(print, "extends")]
671
+ : "";
672
+ return [
673
+ printDecorators(path, options, print, { tryInline: false }).decorators,
674
+ "scalar ",
675
+ id,
676
+ template,
677
+ group(indent(["", heritage])),
678
+ ";",
679
+ ];
680
+ }
681
+ export function printNamespaceStatement(path, options, print) {
682
+ const printNested = (currentPath, parentNames) => {
683
+ var _a;
684
+ const names = [...parentNames, currentPath.call(print, "id")];
685
+ const currentNode = currentPath.getNode();
686
+ if (!isArray(currentNode === null || currentNode === void 0 ? void 0 : currentNode.statements) &&
687
+ ((_a = currentNode === null || currentNode === void 0 ? void 0 : currentNode.statements) === null || _a === void 0 ? void 0 : _a.kind) === SyntaxKind.NamespaceStatement) {
688
+ return path.call((x) => printNested(x, names), "statements");
689
+ }
690
+ const suffix = (currentNode === null || currentNode === void 0 ? void 0 : currentNode.statements) === undefined
691
+ ? ";"
692
+ : [
693
+ " {",
694
+ indent([hardline, printStatementSequence(path, options, print, "statements")]),
695
+ hardline,
696
+ "}",
697
+ ];
698
+ const { decorators } = printDecorators(path, options, print, { tryInline: false });
699
+ return [decorators, `namespace `, join(".", names), suffix];
700
+ };
701
+ return printNested(path, []);
702
+ }
703
+ export function printOperationSignatureDeclaration(path, options, print) {
704
+ return ["(", path.call(print, "parameters"), "): ", path.call(print, "returnType")];
705
+ }
706
+ export function printOperationSignatureReference(path, options, print) {
707
+ return [" is ", path.call(print, "baseOperation")];
708
+ }
709
+ export function printOperationStatement(path, options, print) {
710
+ var _a;
711
+ const inInterface = ((_a = path.getParentNode()) === null || _a === void 0 ? void 0 : _a.kind) === SyntaxKind.InterfaceStatement;
712
+ const templateParams = printTemplateParameters(path, options, print, "templateParameters");
713
+ const { decorators } = printDecorators(path, options, print, {
714
+ tryInline: true,
715
+ });
716
+ return [
717
+ decorators,
718
+ inInterface ? "" : "op ",
719
+ path.call(print, "id"),
720
+ templateParams,
721
+ path.call(print, "signature"),
722
+ `;`,
723
+ ];
724
+ }
725
+ export function printStatementSequence(path, options, print, property) {
726
+ const node = path.getValue();
727
+ const parts = [];
728
+ const lastStatement = getLastStatement(node[property]);
729
+ path.each((statementPath) => {
730
+ const node = path.getValue();
731
+ if (node.kind === SyntaxKind.EmptyStatement) {
732
+ return;
733
+ }
734
+ const printed = print(statementPath);
735
+ parts.push(printed);
736
+ if (node !== lastStatement) {
737
+ parts.push(hardline);
738
+ if (isNextLineEmpty(options.originalText, node, options.locEnd)) {
739
+ parts.push(hardline);
740
+ }
741
+ }
742
+ }, property);
743
+ return parts;
744
+ }
745
+ function getLastStatement(statements) {
746
+ for (let i = statements.length - 1; i >= 0; i--) {
747
+ const statement = statements[i];
748
+ if (statement.kind !== SyntaxKind.EmptyStatement) {
749
+ return statement;
750
+ }
751
+ }
752
+ return undefined;
753
+ }
754
+ export function printUnion(path, options, print) {
755
+ const node = path.getValue();
756
+ const shouldHug = shouldHugType(node);
757
+ const types = path.map((typePath) => {
758
+ let printedType = print(typePath);
759
+ if (!shouldHug) {
760
+ printedType = align(2, printedType);
761
+ }
762
+ return printedType;
763
+ }, "options");
764
+ if (shouldHug) {
765
+ return join(" | ", types);
766
+ }
767
+ const shouldAddStartLine = true;
768
+ const code = [ifBreak([shouldAddStartLine ? line : "", "| "], ""), join([line, "| "], types)];
769
+ return group(indent(code));
770
+ }
771
+ function shouldHugType(node) {
772
+ if (node.kind === SyntaxKind.UnionExpression || node.kind === SyntaxKind.IntersectionExpression) {
773
+ return node.options.length < 4;
774
+ }
775
+ return false;
776
+ }
777
+ export function printTypeReference(path, options, print) {
778
+ const type = path.call(print, "target");
779
+ const template = printTemplateParameters(path, options, print, "arguments");
780
+ return [type, template];
781
+ }
782
+ function printTemplateParameterDeclaration(path, options, print) {
783
+ const node = path.getValue();
784
+ return [
785
+ path.call(print, "id"),
786
+ node.constraint ? [" extends ", path.call(print, "constraint")] : "",
787
+ node.default ? [" = ", path.call(print, "default")] : "",
788
+ ];
789
+ }
790
+ function printModelSpread(path, options, print) {
791
+ return ["...", path.call(print, "target")];
792
+ }
793
+ function printDecoratorDeclarationStatement(path, options, print) {
794
+ const id = path.call(print, "id");
795
+ const parameters = [
796
+ group([
797
+ indent(join(", ", [
798
+ [softline, path.call(print, "target")],
799
+ ...path.map((arg) => [softline, print(arg)], "parameters"),
800
+ ])),
801
+ softline,
802
+ ]),
803
+ ];
804
+ return [printModifiers(path, options, print), "dec ", id, "(", parameters, ")", ";"];
805
+ }
806
+ function printFunctionDeclarationStatement(path, options, print) {
807
+ const node = path.getValue();
808
+ const id = path.call(print, "id");
809
+ const parameters = [
810
+ group([
811
+ indent(join(", ", path.map((arg) => [softline, print(arg)], "parameters"))),
812
+ softline,
813
+ ]),
814
+ ];
815
+ const returnType = node.returnType ? [": ", path.call(print, "returnType")] : "";
816
+ return [printModifiers(path, options, print), "fn ", id, "(", parameters, ")", returnType, ";"];
817
+ }
818
+ function printFunctionParameterDeclaration(path, options, print) {
819
+ const node = path.getValue();
820
+ const id = path.call(print, "id");
821
+ const type = node.type ? [": ", path.call(print, "type")] : "";
822
+ return [
823
+ node.rest ? "..." : "",
824
+ printDirectives(path, options, print),
825
+ id,
826
+ node.optional ? "?" : "",
827
+ type,
828
+ ];
829
+ }
830
+ export function printModifiers(path, options, print) {
831
+ const node = path.getValue();
832
+ if (node.modifiers.length === 0) {
833
+ return "";
834
+ }
835
+ return path.map((x) => [print(x), " "], "modifiers");
836
+ }
837
+ function printStringLiteral(path, options) {
838
+ const node = path.getValue();
839
+ return getRawText(node, options);
840
+ }
841
+ function printNumberLiteral(path, options) {
842
+ const node = path.getValue();
843
+ return getRawText(node, options);
844
+ }
845
+ function printBooleanLiteral(path, options) {
846
+ const node = path.getValue();
847
+ return node.value ? "true" : "false";
848
+ }
849
+ function printProjectionStatement(path, options, print) {
850
+ const node = path.getValue();
851
+ const selector = path.call(print, "selector");
852
+ const id = path.call(print, "id");
853
+ const to = node.to ? [hardline, path.call(print, "to")] : "";
854
+ const from = node.from ? [hardline, path.call(print, "from")] : "";
855
+ const body = [to, from];
856
+ return [
857
+ "projection ",
858
+ selector,
859
+ "#",
860
+ id,
861
+ " {",
862
+ indent(body),
863
+ node.to || node.from ? hardline : "",
864
+ "}",
865
+ ];
866
+ }
867
+ function printProjection(path, options, print) {
868
+ const node = path.getValue();
869
+ const params = printProjectionParameters(path, options, print);
870
+ const body = printProjectionExpressionStatements(path, options, print, "body");
871
+ return [node.direction, params, " {", indent(body), hardline, "}"];
872
+ }
873
+ function printProjectionParameters(path, options, print) {
874
+ const node = path.getValue();
875
+ const params = node.parameters;
876
+ if (params.length === 0) {
877
+ return "";
878
+ }
879
+ const shouldHug = params.length === 1;
880
+ if (shouldHug) {
881
+ return ["(", printItemList(path, options, print, "parameters"), ")"];
882
+ }
883
+ else {
884
+ const body = indent([softline, join([", ", softline], path.map(print, "parameters"))]);
885
+ return group(["(", body, softline, ")"]);
886
+ }
887
+ }
888
+ function printProjectionExpressionStatements(path, options, print, key) {
889
+ const parts = [hardline];
890
+ const lastIndex = path.getValue()[key].length - 1;
891
+ path.each((statementPath, index) => {
892
+ const node = path.getValue();
893
+ if (node.kind === SyntaxKind.EmptyStatement) {
894
+ return;
895
+ }
896
+ const printed = print(statementPath);
897
+ parts.push(printed);
898
+ parts.push(";");
899
+ if (index < lastIndex) {
900
+ parts.push(hardline);
901
+ if (isNextLineEmpty(options.originalText, node, options.locEnd)) {
902
+ parts.push(hardline);
903
+ }
904
+ }
905
+ }, key);
906
+ return parts;
907
+ }
908
+ function printProjectionParameterDeclaration(path, options, print) {
909
+ return path.call(print, "id");
910
+ }
911
+ function printProjectionExpressionStatement(path, options, print) {
912
+ return path.call(print, "expr");
913
+ }
914
+ function printProjectionIfExpressionNode(path, options, print) {
915
+ const node = path.getValue();
916
+ const test = path.call(print, "test");
917
+ const consequent = path.call(print, "consequent");
918
+ const alternate = node.alternate ? [" else ", path.call(print, "alternate")] : "";
919
+ return ["if ", test, " ", consequent, alternate];
920
+ }
921
+ export function printProjectionBlockExpressionNode(path, options, print) {
922
+ const node = path.getValue();
923
+ if (node.statements.length === 0) {
924
+ return "{}";
925
+ }
926
+ return [
927
+ "{",
928
+ indent(printProjectionExpressionStatements(path, options, print, "statements")),
929
+ hardline,
930
+ "}",
931
+ ];
932
+ }
933
+ export function printProjectionMemberExpression(path, options, print) {
934
+ const node = path.getValue();
935
+ return [path.call(print, "base"), node.selector, path.call(print, "id")];
936
+ }
937
+ export function printProjectionLeftRightExpression(path, options, print) {
938
+ const node = path.getValue();
939
+ return [path.call(print, "left"), " ", node.op, " ", path.call(print, "right")];
940
+ }
941
+ export function printProjectionUnaryExpression(path, options, print) {
942
+ return ["!", path.call(print, "target")];
943
+ }
944
+ export function printProjectionCallExpression(path, options, print) {
945
+ const node = path.getValue();
946
+ const target = path.call(print, "target");
947
+ const params = printItemList(path, options, print, "arguments");
948
+ if (node.callKind === "method") {
949
+ return [target, "(", params, ")"];
950
+ }
951
+ else {
952
+ return [target, "<", params, ">"];
953
+ }
954
+ }
955
+ export function printProjectionLambdaExpression(path, options, print) {
956
+ return [
957
+ "(",
958
+ printItemList(path, options, print, "parameters"),
959
+ ")",
960
+ " => ",
961
+ path.call(print, "body"),
962
+ ];
963
+ }
964
+ export function printProjectionLambdaParameterDeclaration(path, options, print) {
965
+ return path.call(print, "id");
966
+ }
967
+ export function printReturnExpression(path, options, print) {
968
+ return ["return ", path.call(print, "value")];
969
+ }
970
+ function printItemList(path, options, print, key) {
971
+ return join(", ", path.map(print, key));
972
+ }
973
+ /**
974
+ * @param node Node that has postition information.
975
+ * @param options Prettier options
976
+ * @returns Raw text in the file for the given node.
977
+ */
978
+ function getRawText(node, options) {
979
+ return options.originalText.slice(node.pos, node.end);
980
+ }
981
+ function hasComments(node, flags) {
982
+ if (!node.comments || node.comments.length === 0) {
983
+ return false;
984
+ }
985
+ const test = getCommentTestFunction(flags);
986
+ return test ? node.comments.some(test) : true;
987
+ }
988
+ var CommentCheckFlags;
989
+ (function (CommentCheckFlags) {
990
+ /** Check comment is a leading comment */
991
+ CommentCheckFlags[CommentCheckFlags["Leading"] = 2] = "Leading";
992
+ /** Check comment is a trailing comment */
993
+ CommentCheckFlags[CommentCheckFlags["Trailing"] = 4] = "Trailing";
994
+ /** Check comment is a dangling comment */
995
+ CommentCheckFlags[CommentCheckFlags["Dangling"] = 8] = "Dangling";
996
+ /** Check comment is a block comment */
997
+ CommentCheckFlags[CommentCheckFlags["Block"] = 16] = "Block";
998
+ /** Check comment is a line comment */
999
+ CommentCheckFlags[CommentCheckFlags["Line"] = 32] = "Line";
1000
+ /** Check comment is a `prettier-ignore` comment */
1001
+ CommentCheckFlags[CommentCheckFlags["PrettierIgnore"] = 64] = "PrettierIgnore";
1002
+ /** Check comment is the first attached comment */
1003
+ CommentCheckFlags[CommentCheckFlags["First"] = 128] = "First";
1004
+ /** Check comment is the last attached comment */
1005
+ CommentCheckFlags[CommentCheckFlags["Last"] = 256] = "Last";
1006
+ })(CommentCheckFlags || (CommentCheckFlags = {}));
1007
+ function getCommentTestFunction(flags) {
1008
+ if (flags) {
1009
+ return (comment, index, comments) => !((flags & CommentCheckFlags.Leading && !comment.leading) ||
1010
+ (flags & CommentCheckFlags.Trailing && !comment.trailing) ||
1011
+ (flags & CommentCheckFlags.Dangling && (comment.leading || comment.trailing)) ||
1012
+ (flags & CommentCheckFlags.Block && !isBlockComment(comment)) ||
1013
+ (flags & CommentCheckFlags.Line && !isLineComment(comment)) ||
1014
+ (flags & CommentCheckFlags.First && index !== 0) ||
1015
+ (flags & CommentCheckFlags.Last && index !== comments.length - 1));
1016
+ }
1017
+ return undefined;
1018
+ }
1019
+ function isBlockComment(comment) {
1020
+ return comment.kind === SyntaxKind.BlockComment;
1021
+ }
1022
+ function isLineComment(comment) {
1023
+ return comment.kind === SyntaxKind.BlockComment;
1024
+ }
1025
+ //# sourceMappingURL=printer.js.map