index.d.cts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. interface CodegenOptions {
  2. singleQuotes?: boolean;
  3. }
  4. type ESMImport = string | {
  5. name: string;
  6. as?: string;
  7. };
  8. type ESMExport = string | {
  9. name: string;
  10. as?: string;
  11. };
  12. interface ESMCodeGenOptions extends CodegenOptions {
  13. attributes?: {
  14. type: string;
  15. };
  16. /** @deprecated use attributes */
  17. assert?: {
  18. type: string;
  19. };
  20. }
  21. interface DynamicImportOptions extends ESMCodeGenOptions {
  22. comment?: string;
  23. wrapper?: boolean;
  24. interopDefault?: boolean;
  25. }
  26. /**
  27. * Generate an ESM `import` statement.
  28. *
  29. * @example
  30. *
  31. * ```js
  32. * genImport("pkg", "foo");
  33. * // ~> `import foo from "pkg";`
  34. *
  35. * genImport("pkg", ["foo"]);
  36. * // ~> `import { foo } from "pkg";`
  37. *
  38. * genImport("pkg", ["a", "b"]);
  39. * // ~> `import { a, b } from "pkg`;
  40. *
  41. * genImport("pkg", [{ name: "default", as: "bar" }]);
  42. * // ~> `import { default as bar } from "pkg`;
  43. *
  44. * genImport("pkg", [{ name: "foo", as: "bar" }]);
  45. * // ~> `import { foo as bar } from "pkg`;
  46. *
  47. * genImport("pkg", "foo", { attributes: { type: "json" } });
  48. * // ~> `import foo from "pkg" with { type: "json" };
  49. *
  50. * genExport("pkg", "foo");
  51. * // ~> `export foo from "pkg";`
  52. *
  53. * genExport("pkg", ["a", "b"]);
  54. * // ~> `export { a, b } from "pkg";`
  55. *
  56. * // export * as bar from "pkg"
  57. * genExport("pkg", { name: "*", as: "bar" });
  58. * // ~> `export * as bar from "pkg";`
  59. * ```
  60. *
  61. * @group ESM
  62. */
  63. declare function genImport(specifier: string, imports?: ESMImport | ESMImport[], options?: ESMCodeGenOptions): string;
  64. /**
  65. * Generate an ESM `import type` statement.
  66. *
  67. * @group ESM
  68. */
  69. declare function genTypeImport(specifier: string, imports: ESMImport[], options?: ESMCodeGenOptions): string;
  70. /**
  71. * Generate an ESM `export` statement.
  72. *
  73. * @group ESM
  74. */
  75. declare function genExport(specifier: string, exports?: ESMExport | ESMExport[], options?: ESMCodeGenOptions): string;
  76. /**
  77. * Generate an ESM dynamic `import()` statement.
  78. *
  79. * @group ESM
  80. */
  81. declare function genDynamicImport(specifier: string, options?: DynamicImportOptions): string;
  82. interface GenObjectOptions extends CodegenOptions {
  83. preserveTypes?: boolean;
  84. }
  85. /**
  86. * Serialize an object to a string.
  87. *
  88. * Values are not escaped or quoted.
  89. *
  90. * @example
  91. *
  92. * ```js
  93. * genObjectFromRaw({ foo: "bar", test: '() => import("pkg")' })
  94. * // ~> `{ foo: bar, test: () => import("pkg") }`
  95. * ```
  96. *
  97. * @group serialization
  98. */
  99. declare function genObjectFromRaw(object: Record<string, any>, indent?: string, options?: GenObjectOptions): string;
  100. /**
  101. * Serialize an object to a string.
  102. *
  103. * Values are escaped and quoted if necessary.
  104. *
  105. * @example
  106. *
  107. * ```js
  108. * genObjectFromValues({ foo: "bar" })
  109. * // ~> `{ foo: "bar" }`
  110. * ```
  111. *
  112. * @group serialization
  113. */
  114. declare function genObjectFromValues(obj: Record<string, any>, indent?: string, options?: GenObjectOptions): string;
  115. /**
  116. * Serialize an array to a string.
  117. *
  118. * Values are not escaped or quoted.
  119. *
  120. * @example
  121. *
  122. * ```js
  123. * genArrayFromRaw([1, 2, 3])
  124. * // ~> `[1, 2, 3]`
  125. * ```
  126. *
  127. * @group serialization
  128. */
  129. declare function genArrayFromRaw(array: any[], indent?: string, options?: GenObjectOptions): string;
  130. /**
  131. * Serialize an array of key-value pairs to a string.
  132. *
  133. * Values are not escaped or quoted.
  134. *
  135. * @group serialization
  136. */
  137. declare function genObjectFromRawEntries(array: [key: string, value: any][], indent?: string, options?: GenObjectOptions): string;
  138. /**
  139. * Generate a string with double or single quotes and handle escapes.
  140. *
  141. * @group string
  142. */
  143. declare function genString(input: string, options?: CodegenOptions): string;
  144. /**
  145. * Escape a string for use in a javascript string.
  146. *
  147. * @group string
  148. */
  149. declare function escapeString(id: string): string;
  150. /**
  151. * Generate a safe javascript variable name.
  152. *
  153. * @group string
  154. */
  155. declare function genSafeVariableName(name: string): string;
  156. type TypeObject = {
  157. [key: string]: string | TypeObject;
  158. };
  159. interface GenInterfaceOptions {
  160. extends?: string | string[];
  161. export?: boolean;
  162. }
  163. /**
  164. * Generate a typescript `export type` statement.
  165. *
  166. * @group Typescript
  167. */
  168. declare function genTypeExport(specifier: string, imports: ESMImport[], options?: ESMCodeGenOptions): string;
  169. /**
  170. * Generate an typescript `typeof import()` statement for default import.
  171. *
  172. * @group Typescript
  173. */
  174. declare function genInlineTypeImport(specifier: string, name?: string, options?: ESMCodeGenOptions): string;
  175. /**
  176. * Generate typescript object type.
  177. *
  178. * @group Typescript
  179. */
  180. declare function genTypeObject(object: TypeObject, indent?: string): string;
  181. /**
  182. * Generate typescript interface.
  183. *
  184. * @group Typescript
  185. */
  186. declare function genInterface(name: string, contents?: TypeObject, options?: GenInterfaceOptions, indent?: string): string;
  187. /**
  188. * Generate typescript `declare module` augmentation.
  189. *
  190. * @group Typescript
  191. */
  192. declare function genAugmentation(specifier: string, interfaces?: Record<string, TypeObject | [TypeObject, Omit<GenInterfaceOptions, "export">]>): string;
  193. /**
  194. * Wrap an array of strings in delimiters.
  195. *
  196. * @group utils
  197. */
  198. declare function wrapInDelimiters(lines: string[], indent?: string, delimiters?: string, withComma?: boolean): string;
  199. /**
  200. * Generate a safe javascript variable name for an object key.
  201. *
  202. * @group utils
  203. */
  204. declare function genObjectKey(key: string): string;
  205. export { type CodegenOptions, type DynamicImportOptions, type ESMCodeGenOptions, type ESMExport, type ESMImport, type GenInterfaceOptions, type GenObjectOptions, type TypeObject, escapeString, genArrayFromRaw, genAugmentation, genDynamicImport, genExport, genImport, genInlineTypeImport, genInterface, genObjectFromRaw, genObjectFromRawEntries, genObjectFromValues, genObjectKey, genSafeVariableName, genString, genTypeExport, genTypeImport, genTypeObject, wrapInDelimiters };