@ts-for-gir/lib 3.3.0 → 4.0.0-beta.2

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 (313) hide show
  1. package/README.md +0 -1
  2. package/lib/conflict-resolver.d.ts +2 -39
  3. package/lib/conflict-resolver.js +4 -228
  4. package/lib/conflict-resolver.js.map +1 -1
  5. package/lib/constants.d.ts +3 -7
  6. package/lib/constants.js +6 -16
  7. package/lib/constants.js.map +1 -1
  8. package/lib/dependency-manager.d.ts +3 -5
  9. package/lib/dependency-manager.js +10 -17
  10. package/lib/dependency-manager.js.map +1 -1
  11. package/lib/formatters/default.d.ts +4 -0
  12. package/lib/formatters/default.js +7 -0
  13. package/lib/formatters/default.js.map +1 -0
  14. package/lib/formatters/formatter.d.ts +3 -0
  15. package/lib/formatters/formatter.js +3 -0
  16. package/lib/formatters/formatter.js.map +1 -0
  17. package/lib/formatters/json.d.ts +4 -0
  18. package/lib/formatters/json.js +7 -0
  19. package/lib/formatters/json.js.map +1 -0
  20. package/lib/generators/dts/gio.d.ts +2 -0
  21. package/lib/generators/dts/gio.js +9 -0
  22. package/lib/generators/dts/gio.js.map +1 -0
  23. package/lib/generators/dts/glib.d.ts +2 -0
  24. package/lib/generators/dts/glib.js +514 -0
  25. package/lib/generators/dts/glib.js.map +1 -0
  26. package/lib/generators/dts/gobject.d.ts +2 -0
  27. package/lib/generators/dts/gobject.js +117 -0
  28. package/lib/generators/dts/gobject.js.map +1 -0
  29. package/lib/generators/dts-inline.d.ts +8 -0
  30. package/lib/generators/dts-inline.js +59 -0
  31. package/lib/generators/dts-inline.js.map +1 -0
  32. package/lib/generators/dts-modules.d.ts +8 -0
  33. package/lib/generators/dts-modules.js +100 -0
  34. package/lib/generators/dts-modules.js.map +1 -0
  35. package/lib/generators/dts.d.ts +45 -0
  36. package/lib/generators/dts.js +682 -0
  37. package/lib/generators/dts.js.map +1 -0
  38. package/lib/generators/generator.d.ts +45 -0
  39. package/lib/generators/generator.js +9 -0
  40. package/lib/generators/generator.js.map +1 -0
  41. package/lib/generators/index.d.ts +5 -0
  42. package/lib/generators/index.js +6 -0
  43. package/lib/generators/index.js.map +1 -0
  44. package/lib/generators/json.d.ts +256 -0
  45. package/lib/generators/json.js +869 -0
  46. package/lib/generators/json.js.map +1 -0
  47. package/lib/generics/clutter.d.ts +26 -0
  48. package/lib/generics/clutter.js +49 -0
  49. package/lib/generics/clutter.js.map +1 -0
  50. package/lib/generics/generify.d.ts +2 -0
  51. package/lib/generics/generify.js +39 -0
  52. package/lib/generics/generify.js.map +1 -0
  53. package/lib/generics/gio.d.ts +7 -0
  54. package/lib/generics/gio.js +51 -0
  55. package/lib/generics/gio.js.map +1 -0
  56. package/lib/generics/glib.d.ts +7 -0
  57. package/lib/generics/glib.js +18 -0
  58. package/lib/generics/glib.js.map +1 -0
  59. package/lib/generics/meta.d.ts +21 -0
  60. package/lib/generics/meta.js +26 -0
  61. package/lib/generics/meta.js.map +1 -0
  62. package/lib/generics/st.d.ts +16 -0
  63. package/lib/generics/st.js +92 -0
  64. package/lib/generics/st.js.map +1 -0
  65. package/lib/generics/visitor.d.ts +20 -0
  66. package/lib/generics/visitor.js +259 -0
  67. package/lib/generics/visitor.js.map +1 -0
  68. package/lib/gir/alias.d.ts +24 -0
  69. package/lib/gir/alias.js +43 -0
  70. package/lib/gir/alias.js.map +1 -0
  71. package/lib/gir/base.d.ts +62 -0
  72. package/lib/gir/base.js +95 -0
  73. package/lib/gir/base.js.map +1 -0
  74. package/lib/gir/class.d.ts +189 -0
  75. package/lib/gir/class.js +1132 -0
  76. package/lib/gir/class.js.map +1 -0
  77. package/lib/gir/const.d.ts +25 -0
  78. package/lib/gir/const.js +43 -0
  79. package/lib/gir/const.js.map +1 -0
  80. package/lib/gir/enum.d.ts +45 -0
  81. package/lib/gir/enum.js +164 -0
  82. package/lib/gir/enum.js.map +1 -0
  83. package/lib/gir/function.d.ts +199 -0
  84. package/lib/gir/function.js +683 -0
  85. package/lib/gir/function.js.map +1 -0
  86. package/lib/gir/generics.d.ts +5 -0
  87. package/lib/gir/generics.js +57 -0
  88. package/lib/gir/generics.js.map +1 -0
  89. package/lib/gir/namespace.d.ts +21 -0
  90. package/lib/gir/namespace.js +57 -0
  91. package/lib/gir/namespace.js.map +1 -0
  92. package/lib/gir/nodes.d.ts +11 -0
  93. package/lib/gir/nodes.js +12 -0
  94. package/lib/gir/nodes.js.map +1 -0
  95. package/lib/gir/property.d.ts +60 -0
  96. package/lib/gir/property.js +128 -0
  97. package/lib/gir/property.js.map +1 -0
  98. package/lib/gir/registry.d.ts +51 -0
  99. package/lib/gir/registry.js +144 -0
  100. package/lib/gir/registry.js.map +1 -0
  101. package/lib/gir/signal.d.ts +33 -0
  102. package/lib/gir/signal.js +165 -0
  103. package/lib/gir/signal.js.map +1 -0
  104. package/lib/gir/util.d.ts +49 -0
  105. package/lib/gir/util.js +556 -0
  106. package/lib/gir/util.js.map +1 -0
  107. package/lib/gir-factory.d.ts +2 -2
  108. package/lib/gir-factory.js +8 -44
  109. package/lib/gir-factory.js.map +1 -1
  110. package/lib/gir-module.d.ts +61 -221
  111. package/lib/gir-module.js +387 -2106
  112. package/lib/gir-module.js.map +1 -1
  113. package/lib/gir.d.ts +251 -0
  114. package/lib/gir.js +638 -0
  115. package/lib/gir.js.map +1 -0
  116. package/lib/index.d.ts +5 -4
  117. package/lib/index.js +7 -4
  118. package/lib/index.js.map +1 -1
  119. package/lib/injection/callbacks/index.d.ts +0 -1
  120. package/lib/injection/callbacks/index.js +0 -1
  121. package/lib/injection/callbacks/index.js.map +1 -1
  122. package/lib/injection/classes/index.d.ts +0 -1
  123. package/lib/injection/classes/index.js +0 -1
  124. package/lib/injection/classes/index.js.map +1 -1
  125. package/lib/injection/injector.d.ts +2 -3
  126. package/lib/injection/injector.js +6 -8
  127. package/lib/injection/injector.js.map +1 -1
  128. package/lib/injections/gee08.d.ts +7 -0
  129. package/lib/injections/gee08.js +68 -0
  130. package/lib/injections/gee08.js.map +1 -0
  131. package/lib/injections/gee1.d.ts +7 -0
  132. package/lib/injections/gee1.js +28 -0
  133. package/lib/injections/gee1.js.map +1 -0
  134. package/lib/injections/gio.d.ts +7 -0
  135. package/lib/injections/gio.js +431 -0
  136. package/lib/injections/gio.js.map +1 -0
  137. package/lib/injections/glib.d.ts +7 -0
  138. package/lib/injections/glib.js +204 -0
  139. package/lib/injections/glib.js.map +1 -0
  140. package/lib/injections/gobject.d.ts +7 -0
  141. package/lib/injections/gobject.js +479 -0
  142. package/lib/injections/gobject.js.map +1 -0
  143. package/lib/injections/inject.d.ts +4 -0
  144. package/lib/injections/inject.js +28 -0
  145. package/lib/injections/inject.js.map +1 -0
  146. package/lib/injections/tracker1.d.ts +7 -0
  147. package/lib/injections/tracker1.js +30 -0
  148. package/lib/injections/tracker1.js.map +1 -0
  149. package/lib/library-version.js.map +1 -1
  150. package/lib/logger.d.ts +1 -3
  151. package/lib/logger.js +3 -8
  152. package/lib/logger.js.map +1 -1
  153. package/lib/messages.d.ts +1 -4
  154. package/lib/messages.js +1 -3
  155. package/lib/messages.js.map +1 -1
  156. package/lib/registry.d.ts +9 -0
  157. package/lib/registry.js +13 -0
  158. package/lib/registry.js.map +1 -0
  159. package/lib/symtable.js +1 -1
  160. package/lib/symtable.js.map +1 -1
  161. package/lib/transformation.d.ts +4 -5
  162. package/lib/transformation.js +31 -127
  163. package/lib/transformation.js.map +1 -1
  164. package/lib/types/generate-config.d.ts +4 -11
  165. package/lib/types/gir-alias-element.d.ts +3 -12
  166. package/lib/types/gir-bitfield-element.d.ts +3 -14
  167. package/lib/types/gir-callable-param-element.d.ts +3 -31
  168. package/lib/types/gir-callable-params.d.ts +7 -0
  169. package/lib/types/gir-callable-params.js +2 -0
  170. package/lib/types/gir-callable-params.js.map +1 -0
  171. package/lib/types/gir-callable-return.d.ts +3 -20
  172. package/lib/types/gir-callback-element.d.ts +3 -12
  173. package/lib/types/gir-class-element.d.ts +3 -34
  174. package/lib/types/gir-constant-element.d.ts +3 -14
  175. package/lib/types/gir-constructor-element.d.ts +3 -5
  176. package/lib/types/gir-enum-element.d.ts +3 -16
  177. package/lib/types/gir-field-element.d.ts +3 -16
  178. package/lib/types/gir-function-element.d.ts +3 -5
  179. package/lib/types/gir-instance-parameter.d.ts +2 -17
  180. package/lib/types/gir-interface-element.d.ts +4 -19
  181. package/lib/types/gir-member-element.d.ts +4 -16
  182. package/lib/types/gir-method-element.d.ts +4 -10
  183. package/lib/types/gir-namespace.d.ts +14 -17
  184. package/lib/types/gir-property-element.d.ts +3 -22
  185. package/lib/types/gir-record-element.d.ts +3 -23
  186. package/lib/types/gir-repository.d.ts +3 -14
  187. package/lib/types/gir-signal.d.ts +3 -18
  188. package/lib/types/gir-union-element.d.ts +3 -16
  189. package/lib/types/gir-virtual-method.d.ts +3 -7
  190. package/lib/types/index.d.ts +3 -54
  191. package/lib/types/index.js +3 -54
  192. package/lib/types/index.js.map +1 -1
  193. package/lib/types/parsed-gir.d.ts +3 -2
  194. package/lib/types/transformations.d.ts +1 -8
  195. package/lib/types/user-config.d.ts +0 -11
  196. package/lib/types.d.ts +23 -0
  197. package/lib/types.js +2 -0
  198. package/lib/types.js.map +1 -0
  199. package/lib/util.d.ts +20 -0
  200. package/lib/util.js +67 -0
  201. package/lib/util.js.map +1 -0
  202. package/lib/utils.d.ts +2 -30
  203. package/lib/utils.js +0 -56
  204. package/lib/utils.js.map +1 -1
  205. package/lib/validators/class.d.ts +7 -0
  206. package/lib/validators/class.js +217 -0
  207. package/lib/validators/class.js.map +1 -0
  208. package/lib/validators/interface.d.ts +5 -0
  209. package/lib/validators/interface.js +16 -0
  210. package/lib/validators/interface.js.map +1 -0
  211. package/lib/visitor.d.ts +35 -0
  212. package/lib/visitor.js +29 -0
  213. package/lib/visitor.js.map +1 -0
  214. package/package.json +14 -10
  215. package/lib/gir-parser.d.ts +0 -2
  216. package/lib/gir-parser.js +0 -69
  217. package/lib/gir-parser.js.map +0 -1
  218. package/lib/injection/callbacks/node-gtk/index.d.ts +0 -2
  219. package/lib/injection/callbacks/node-gtk/index.js +0 -2
  220. package/lib/injection/callbacks/node-gtk/index.js.map +0 -1
  221. package/lib/injection/classes/node-gtk/gdk-4.0.d.ts +0 -2
  222. package/lib/injection/classes/node-gtk/gdk-4.0.js +0 -32
  223. package/lib/injection/classes/node-gtk/gdk-4.0.js.map +0 -1
  224. package/lib/injection/classes/node-gtk/gio-2.0.d.ts +0 -2
  225. package/lib/injection/classes/node-gtk/gio-2.0.js +0 -14
  226. package/lib/injection/classes/node-gtk/gio-2.0.js.map +0 -1
  227. package/lib/injection/classes/node-gtk/glib-2.0.d.ts +0 -2
  228. package/lib/injection/classes/node-gtk/glib-2.0.js +0 -19
  229. package/lib/injection/classes/node-gtk/glib-2.0.js.map +0 -1
  230. package/lib/injection/classes/node-gtk/gobject-2.0.d.ts +0 -2
  231. package/lib/injection/classes/node-gtk/gobject-2.0.js +0 -66
  232. package/lib/injection/classes/node-gtk/gobject-2.0.js.map +0 -1
  233. package/lib/injection/classes/node-gtk/graphene-1.0.d.ts +0 -2
  234. package/lib/injection/classes/node-gtk/graphene-1.0.js +0 -133
  235. package/lib/injection/classes/node-gtk/graphene-1.0.js.map +0 -1
  236. package/lib/injection/classes/node-gtk/gtk-3.0.d.ts +0 -2
  237. package/lib/injection/classes/node-gtk/gtk-3.0.js +0 -31
  238. package/lib/injection/classes/node-gtk/gtk-3.0.js.map +0 -1
  239. package/lib/injection/classes/node-gtk/gtk-4.0.d.ts +0 -2
  240. package/lib/injection/classes/node-gtk/gtk-4.0.js +0 -52
  241. package/lib/injection/classes/node-gtk/gtk-4.0.js.map +0 -1
  242. package/lib/injection/classes/node-gtk/index.d.ts +0 -2
  243. package/lib/injection/classes/node-gtk/index.js +0 -19
  244. package/lib/injection/classes/node-gtk/index.js.map +0 -1
  245. package/lib/injection/classes/node-gtk/pango-1.0.d.ts +0 -2
  246. package/lib/injection/classes/node-gtk/pango-1.0.js +0 -31
  247. package/lib/injection/classes/node-gtk/pango-1.0.js.map +0 -1
  248. package/lib/types/build-type.d.ts +0 -1
  249. package/lib/types/build-type.js +0 -2
  250. package/lib/types/build-type.js.map +0 -1
  251. package/lib/types/environment.d.ts +0 -1
  252. package/lib/types/environment.js +0 -2
  253. package/lib/types/environment.js.map +0 -1
  254. package/lib/types/gir-any-type.d.ts +0 -5
  255. package/lib/types/gir-any-type.js +0 -2
  256. package/lib/types/gir-any-type.js.map +0 -1
  257. package/lib/types/gir-array-type.d.ts +0 -20
  258. package/lib/types/gir-array-type.js +0 -3
  259. package/lib/types/gir-array-type.js.map +0 -1
  260. package/lib/types/gir-boolean.d.ts +0 -1
  261. package/lib/types/gir-boolean.js +0 -2
  262. package/lib/types/gir-boolean.js.map +0 -1
  263. package/lib/types/gir-boxed-element.d.ts +0 -15
  264. package/lib/types/gir-boxed-element.js +0 -2
  265. package/lib/types/gir-boxed-element.js.map +0 -1
  266. package/lib/types/gir-c-include.d.ts +0 -7
  267. package/lib/types/gir-c-include.js +0 -2
  268. package/lib/types/gir-c-include.js.map +0 -1
  269. package/lib/types/gir-callable-attrs.d.ts +0 -15
  270. package/lib/types/gir-callable-attrs.js +0 -3
  271. package/lib/types/gir-callable-attrs.js.map +0 -1
  272. package/lib/types/gir-callable-param.d.ts +0 -7
  273. package/lib/types/gir-callable-param.js +0 -2
  274. package/lib/types/gir-callable-param.js.map +0 -1
  275. package/lib/types/gir-direction.d.ts +0 -7
  276. package/lib/types/gir-direction.js +0 -9
  277. package/lib/types/gir-direction.js.map +0 -1
  278. package/lib/types/gir-doc-element.d.ts +0 -68
  279. package/lib/types/gir-doc-element.js +0 -2
  280. package/lib/types/gir-doc-element.js.map +0 -1
  281. package/lib/types/gir-implements.d.ts +0 -8
  282. package/lib/types/gir-implements.js +0 -2
  283. package/lib/types/gir-implements.js.map +0 -1
  284. package/lib/types/gir-include.d.ts +0 -9
  285. package/lib/types/gir-include.js +0 -2
  286. package/lib/types/gir-include.js.map +0 -1
  287. package/lib/types/gir-info-attrs.d.ts +0 -13
  288. package/lib/types/gir-info-attrs.js +0 -2
  289. package/lib/types/gir-info-attrs.js.map +0 -1
  290. package/lib/types/gir-info-elements.d.ts +0 -5
  291. package/lib/types/gir-info-elements.js +0 -2
  292. package/lib/types/gir-info-elements.js.map +0 -1
  293. package/lib/types/gir-package.d.ts +0 -7
  294. package/lib/types/gir-package.js +0 -2
  295. package/lib/types/gir-package.js.map +0 -1
  296. package/lib/types/gir-prerequisite.d.ts +0 -5
  297. package/lib/types/gir-prerequisite.js +0 -2
  298. package/lib/types/gir-prerequisite.js.map +0 -1
  299. package/lib/types/gir-transfer-ownership-type.d.ts +0 -5
  300. package/lib/types/gir-transfer-ownership-type.js +0 -7
  301. package/lib/types/gir-transfer-ownership-type.js.map +0 -1
  302. package/lib/types/gir-transfer-ownership.d.ts +0 -9
  303. package/lib/types/gir-transfer-ownership.js +0 -2
  304. package/lib/types/gir-transfer-ownership.js.map +0 -1
  305. package/lib/types/gir-type.d.ts +0 -14
  306. package/lib/types/gir-type.js +0 -2
  307. package/lib/types/gir-type.js.map +0 -1
  308. package/lib/types/gir-var-args.d.ts +0 -5
  309. package/lib/types/gir-var-args.js +0 -2
  310. package/lib/types/gir-var-args.js.map +0 -1
  311. package/lib/types/module-type.d.ts +0 -1
  312. package/lib/types/module-type.js +0 -2
  313. package/lib/types/module-type.js.map +0 -1
@@ -0,0 +1,869 @@
1
+ import { FormatGenerator } from "./generator.js";
2
+ import { IntrospectedBaseClass, IntrospectedRecord, IntrospectedInterface, IntrospectedClass } from "../gir/class.js";
3
+ import { IntrospectedConstant } from "../gir/const.js";
4
+ import { IntrospectedEnum, IntrospectedError } from "../gir/enum.js";
5
+ import { IntrospectedSignalType } from "../gir/signal.js";
6
+ import { IntrospectedFunction, IntrospectedConstructor, IntrospectedFunctionParameter, IntrospectedCallback } from "../gir/function.js";
7
+ import { IntrospectedClassFunction, IntrospectedStaticClassFunction, IntrospectedVirtualClassFunction } from "../gir/function.js";
8
+ import { sanitizeIdentifierName, isInvalid, resolveDirectedType } from "../gir/util.js";
9
+ import { NativeType, AnyType, VoidType, StringType, NumberType, ArrayType, TypeIdentifier, OrType, TupleType, NullableType, ClosureType, AnyFunctionType, TypeConflict } from "../gir.js";
10
+ import { GirDirection } from "@gi.ts/parser";
11
+ import { IntrospectedAlias } from "../gir/alias.js";
12
+ function generateType(type) {
13
+ if (type instanceof TypeIdentifier) {
14
+ return {
15
+ kind: "identifier" /* TypeKind.identifier */,
16
+ name: type.name,
17
+ namespace: type.namespace
18
+ };
19
+ }
20
+ else if (type instanceof NativeType) {
21
+ return {
22
+ kind: "native" /* TypeKind.native */,
23
+ type: type.expression()
24
+ };
25
+ }
26
+ else if (type instanceof ClosureType) {
27
+ return {
28
+ kind: "closure" /* TypeKind.closure */,
29
+ type: generateType(type.type),
30
+ user_data: type.user_data
31
+ };
32
+ }
33
+ else if (type instanceof ArrayType) {
34
+ return {
35
+ kind: "array" /* TypeKind.array */,
36
+ type: generateType(type.type),
37
+ depth: type.arrayDepth
38
+ };
39
+ }
40
+ else if (type instanceof NullableType) {
41
+ return {
42
+ kind: "null" /* TypeKind.nulled */,
43
+ type: generateType(type.type)
44
+ };
45
+ }
46
+ else if (type instanceof TypeConflict) {
47
+ // Type conflicts aren't considered in JSON outputs.
48
+ return generateType(type.type);
49
+ }
50
+ else if (type instanceof TupleType) {
51
+ return {
52
+ kind: "tuple" /* TypeKind.tuple */,
53
+ types: type.types.map(t => generateType(t))
54
+ };
55
+ }
56
+ else if (type instanceof OrType) {
57
+ return {
58
+ kind: "or" /* TypeKind.or */,
59
+ types: type.types.map(t => generateType(t))
60
+ };
61
+ }
62
+ else {
63
+ return {
64
+ kind: "native" /* TypeKind.native */,
65
+ type: "any"
66
+ };
67
+ }
68
+ }
69
+ function capitalize(str) {
70
+ if (str.length === 0) {
71
+ return "";
72
+ }
73
+ if (str.length === 1) {
74
+ return str[0].toUpperCase();
75
+ }
76
+ return str[0].toUpperCase() + str.substring(1).toLowerCase();
77
+ }
78
+ export class JsonGenerator extends FormatGenerator {
79
+ constructor(namespace, options) {
80
+ super(namespace, options);
81
+ }
82
+ /**
83
+ * Intelligently reformats # and () references
84
+ * to handle c-prefixes and namespacing.
85
+ *
86
+ * @param doc
87
+ */
88
+ generateDoc(doc) {
89
+ const { namespace } = this;
90
+ function resolveClass(ns, className) {
91
+ let classes = ns.getMembers(className);
92
+ let plural = false;
93
+ if (classes.length === 0 && className.endsWith("Class")) {
94
+ classes = ns.getMembers(className.slice(0, -5));
95
+ }
96
+ if (classes.length === 0 && className.endsWith("Iface")) {
97
+ classes = ns.getMembers(className.slice(0, -5));
98
+ }
99
+ if (classes.length === 0 && className.endsWith("Interface")) {
100
+ classes = ns.getMembers(className.slice(0, -9));
101
+ }
102
+ if (classes.length === 0 && className.endsWith("s")) {
103
+ plural = true;
104
+ classes = ns.getMembers(className.slice(0, -1));
105
+ }
106
+ return [classes[0] ?? null, plural];
107
+ }
108
+ function formatReference(identifier, member_name, punc) {
109
+ const parts = identifier
110
+ .split(/([A-Z])/)
111
+ .filter(p => p != "")
112
+ .reduce((prev, next) => {
113
+ if (next.toUpperCase() === next) {
114
+ prev.push(`${next}`);
115
+ }
116
+ else {
117
+ const lastCapital = prev.pop();
118
+ prev.push(`${lastCapital}${next}`);
119
+ }
120
+ return prev;
121
+ }, []);
122
+ const [base_part] = parts;
123
+ const [, , namespaces, className] = parts.slice(1).reduce(([underscore, camel, ns, selected], next) => {
124
+ const next_underscore = [underscore, next.toLowerCase()].join("_");
125
+ const namespaces = namespace.getImportsForCPrefix(next_underscore);
126
+ const nextCamel = camel + capitalize(next);
127
+ if (namespaces.length > 0) {
128
+ return [next_underscore, nextCamel, namespaces, capitalize(next)];
129
+ }
130
+ return [next_underscore, nextCamel, ns, selected + capitalize(next)];
131
+ }, [
132
+ base_part.toLowerCase(),
133
+ capitalize(base_part),
134
+ namespace.getImportsForCPrefix(base_part.toLowerCase()),
135
+ ""
136
+ ]);
137
+ let ns = namespaces.find(n => n.hasSymbol(className));
138
+ if (!ns) {
139
+ ns = namespaces.find(n => {
140
+ const [c] = resolveClass(n, className);
141
+ return c != null;
142
+ });
143
+ }
144
+ if (ns) {
145
+ const is_prop = punc === ":";
146
+ const modified_name = is_prop ? member_name.replace(/[\-]/g, "_") : member_name;
147
+ const [clazz, plural] = resolveClass(ns, className);
148
+ if (clazz instanceof IntrospectedBaseClass || clazz instanceof IntrospectedEnum) {
149
+ const r = `#${plural ? "{" : ""}${ns.name}.${clazz.name}${punc ? `${punc}${modified_name}` : ""}${plural ? "}s" : ""}`;
150
+ return r;
151
+ }
152
+ return `#${ns.name}${punc ? ` (${punc}${modified_name})` : ""}`;
153
+ }
154
+ else {
155
+ return null;
156
+ }
157
+ }
158
+ function formatFunctionReference(func, upper = false) {
159
+ // namespace_class_do_thing()
160
+ const parts = func.toLowerCase().split("_");
161
+ // ['namespace', 'class', 'do', 'thing']
162
+ const [base_part] = parts;
163
+ // ['namespace']
164
+ const namespaceBase = [
165
+ base_part.toLowerCase(),
166
+ capitalize(base_part),
167
+ namespace.getImportsForCPrefix(base_part.toLowerCase()),
168
+ 0
169
+ ];
170
+ // ['namespace', 'Namespace', { Namespace }, -1]
171
+ const [, , namespaces, i] = parts.slice(1).reduce(([prev, camel, currentNamespaces, selected], next, i) => {
172
+ const underscore = [prev, next.toLowerCase()].join("_");
173
+ const namespaces = namespace.getImportsForCPrefix(underscore);
174
+ const identifier = camel + capitalize(next);
175
+ // We've found namespace(s) which matches the c_prefix
176
+ if (namespaces.length > 0) {
177
+ return [underscore, identifier, namespaces, i];
178
+ }
179
+ return [underscore, identifier, currentNamespaces, selected];
180
+ }, namespaceBase);
181
+ // If no namespaces are found for the function's c_prefix, we return the original reference.
182
+ if (namespaces.length === 0) {
183
+ return null;
184
+ }
185
+ // ['class', 'do', 'thing']
186
+ const nameParts = parts.slice(i + 1);
187
+ // 'class_do_thing'
188
+ const functionName = nameParts.join("_");
189
+ const functionNamespace = namespaces.find(n => n.hasSymbol(functionName.toLowerCase()));
190
+ const constNamespace = namespaces.find(n => n.hasSymbol(functionName.toUpperCase()));
191
+ const enumNamespace = namespaces.find(n => n.enum_constants.has(func.toUpperCase()));
192
+ if (functionNamespace) {
193
+ const [member = null] = functionNamespace.getMembers(functionName.toLowerCase());
194
+ if (member instanceof IntrospectedFunction) {
195
+ return `${functionNamespace.name}.${member.name}`;
196
+ }
197
+ return null;
198
+ }
199
+ else if (constNamespace) {
200
+ const [member = null] = constNamespace.getMembers(functionName.toUpperCase());
201
+ if (member instanceof IntrospectedConstant) {
202
+ return `${constNamespace.name}.${member.name}`;
203
+ }
204
+ return null;
205
+ }
206
+ else if (enumNamespace) {
207
+ const constantInfo = enumNamespace.enum_constants.get(func.toUpperCase());
208
+ if (constantInfo) {
209
+ const [enumName, memberName] = constantInfo;
210
+ const [klass = null] = enumNamespace.getMembers(enumName);
211
+ if (klass instanceof IntrospectedEnum) {
212
+ return `${enumNamespace.name}.${klass.name}.${memberName.toUpperCase()}`;
213
+ }
214
+ }
215
+ return null;
216
+ }
217
+ else {
218
+ // ['class', 'do', 'thing']
219
+ const { selectedClassName, resolvedNamespace, selectedIndex } = parts.slice(i + 1).reduce(({ className, selectedClassName, resolvedNamespace, selectedIndex }, next, i) => {
220
+ // Class
221
+ const identifier = `${className}${capitalize(next)}`;
222
+ const withSymbol = namespaces.find(n => n.hasSymbol(identifier));
223
+ if (withSymbol) {
224
+ // { className: Class, resolvedNamespace: {Namespace}, selectedIndex: 0 }
225
+ return {
226
+ className: identifier,
227
+ selectedClassName: identifier,
228
+ resolvedNamespace: withSymbol,
229
+ selectedIndex: i
230
+ };
231
+ }
232
+ return { className: identifier, selectedClassName, resolvedNamespace, selectedIndex };
233
+ }, {
234
+ className: "",
235
+ selectedClassName: "",
236
+ resolvedNamespace: null,
237
+ selectedIndex: -1
238
+ });
239
+ if (resolvedNamespace && selectedIndex >= 0) {
240
+ const nextIndex = i + selectedIndex + 1 /* (slice omits first index) */ + 1; /* (the next index) */
241
+ const functionName = parts.slice(nextIndex).join("_");
242
+ const [klass] = resolveClass(resolvedNamespace, selectedClassName);
243
+ if (klass instanceof IntrospectedBaseClass || klass instanceof IntrospectedEnum) {
244
+ return `${resolvedNamespace.name}.${klass.name}.${upper ? functionName.toUpperCase() : functionName}`;
245
+ }
246
+ return `${resolvedNamespace.name}.${selectedClassName}.${upper ? functionName.toUpperCase() : functionName}`;
247
+ }
248
+ }
249
+ return null;
250
+ }
251
+ return `${doc}`
252
+ .replace(/[#]{0,1}([A-Z][A-z]+)\.([a-z_]+)\(\)/g, (original, identifier, member_name) => {
253
+ const resolved = formatReference(identifier, member_name, ".");
254
+ return resolved != null ? `${resolved}()` : original;
255
+ })
256
+ .replace(/#([A-Z][A-z]*)(([:]{1,2})([a-z\-]+)){0,1}/g, (original, identifier, _, punc, member_name) => {
257
+ const resolved = formatReference(identifier, member_name, punc);
258
+ return resolved != null ? resolved : original;
259
+ })
260
+ .replace(/([A-Z][A-z]*)(([:]{1,2})([a-z\-]+))/g, (original, identifier, _, punc, member_name) => {
261
+ const resolved = formatReference(identifier, member_name, punc);
262
+ return resolved != null ? resolved : original;
263
+ })
264
+ .replace(/(\s)([a-z_]+)\(\)/g, (original, w, func) => {
265
+ const resolved = formatFunctionReference(func);
266
+ return resolved != null ? `${w}${resolved}()` : original;
267
+ })
268
+ .replace(/%([A-Z_]+)/g, (original, identifier) => {
269
+ const resolved = formatFunctionReference(identifier.toLowerCase(), true);
270
+ return resolved != null ? `%${resolved}` : original;
271
+ })
272
+ .replace(/#([A-Z_]+)/g, (original, identifier) => {
273
+ const resolved = formatFunctionReference(identifier.toLowerCase(), true);
274
+ return resolved != null ? `#${resolved}` : original;
275
+ });
276
+ }
277
+ generateMetadata(metadata) {
278
+ return { ...metadata };
279
+ }
280
+ generateParameters(parameters) {
281
+ const { namespace, options } = this;
282
+ return parameters.map(p => ({
283
+ kind: "parameter" /* NodeKind.parameter */,
284
+ direction: p.direction,
285
+ optional: p.isOptional,
286
+ varargs: p.isVarArgs,
287
+ name: p.name,
288
+ resoleNames: p.resolve_names,
289
+ type: generateType(p.type.resolve(namespace, options)),
290
+ ...this._generateDocAndMetadata(p)
291
+ }));
292
+ }
293
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
294
+ generateCallbackType(node) {
295
+ return [{}, {}];
296
+ }
297
+ generateCallback(node) {
298
+ const { namespace, options } = this;
299
+ const parameters = this.generateParameters(node.parameters);
300
+ return {
301
+ kind: "callback" /* NodeKind.callback */,
302
+ name: node.name,
303
+ type: this.generateCallbackType(node),
304
+ parameters,
305
+ returnType: generateType(node.return().resolve(namespace, options)),
306
+ ...this._generateDocAndMetadata(node)
307
+ };
308
+ }
309
+ generateClassCallback(node) {
310
+ const { namespace, options } = this;
311
+ const parameters = this.generateParameters(node.parameters);
312
+ return {
313
+ kind: "callback" /* NodeKind.callback */,
314
+ name: node.name,
315
+ type: this.generateCallbackType(node),
316
+ parameters,
317
+ returnType: generateType(node.return().resolve(namespace, options)),
318
+ ...this._generateDocAndMetadata(node)
319
+ };
320
+ }
321
+ generateReturn(return_type, output_parameters) {
322
+ const { namespace, options } = this;
323
+ const type = return_type.resolve(namespace, options);
324
+ if (output_parameters.length > 0) {
325
+ const exclude_first = type.equals(VoidType);
326
+ const returns = [
327
+ ...(exclude_first ? [] : [type]),
328
+ ...output_parameters.map(op => op.type.resolve(namespace, options))
329
+ ];
330
+ return returns.map(r => generateType(r));
331
+ }
332
+ return generateType(type);
333
+ }
334
+ generateEnum(node) {
335
+ return {
336
+ kind: "enum" /* NodeKind.enum */,
337
+ name: node.name,
338
+ members: Array.from(node.members.values()).map(member => member.asString(this)),
339
+ ...this._generateDocAndMetadata(node)
340
+ };
341
+ }
342
+ generateError(node) {
343
+ const { namespace } = this;
344
+ const clazz = node.asClass();
345
+ clazz.members = [];
346
+ clazz.members.push(...Array.from(node.functions.values()));
347
+ const GLib = namespace.assertInstalledImport("GLib");
348
+ const GLibError = GLib.assertClass("Error");
349
+ clazz.superType = GLibError.getType();
350
+ // Manually construct a GLib.Error constructor.
351
+ clazz.mainConstructor = new IntrospectedConstructor({
352
+ name: "new",
353
+ parent: clazz,
354
+ parameters: [
355
+ new IntrospectedFunctionParameter({
356
+ name: "options",
357
+ type: NativeType.of("{ message: string, code: number}"),
358
+ direction: GirDirection.In
359
+ })
360
+ ],
361
+ return_type: clazz.getType()
362
+ });
363
+ return {
364
+ ...clazz.asString(this),
365
+ kind: "error" /* NodeKind.error */
366
+ };
367
+ }
368
+ _generateDocAndMetadata(node) {
369
+ const { options } = this;
370
+ if (!options.noComments) {
371
+ return {
372
+ private: node.isPrivate,
373
+ doc: this.generateDoc(node.doc ?? "") ?? null,
374
+ metadata: this.generateMetadata(node.metadata ?? {}) ?? null
375
+ };
376
+ }
377
+ return {
378
+ private: false,
379
+ doc: null,
380
+ metadata: null
381
+ };
382
+ }
383
+ generateConst(node) {
384
+ const { namespace, options } = this;
385
+ return {
386
+ kind: "constant" /* NodeKind.constant */,
387
+ name: node.name,
388
+ type: generateType(node.type.resolve(namespace, options)),
389
+ ...this._generateDocAndMetadata(node)
390
+ };
391
+ }
392
+ implements(node) {
393
+ const { namespace, options } = this;
394
+ if (node.interfaces.length > 0) {
395
+ return node.interfaces
396
+ .map(i => i.resolveIdentifier(namespace, options))
397
+ .filter((i) => i != null);
398
+ }
399
+ return [];
400
+ }
401
+ extends(node) {
402
+ const { namespace: ns, options } = this;
403
+ if (node.superType) {
404
+ return node.superType.resolveIdentifier(ns, options);
405
+ }
406
+ return null;
407
+ }
408
+ generateInterface(node) {
409
+ const { namespace } = this;
410
+ // If an interface does not list a prerequisite type, we fill it with GObject.Object
411
+ if (node.superType == null) {
412
+ const gobject = namespace.assertInstalledImport("GObject");
413
+ // TODO Optimize GObject.Object
414
+ if (!gobject) {
415
+ throw new Error("GObject not generated, all interfaces extend from GObject.Object!");
416
+ }
417
+ const GObject = gobject.getClass("Object");
418
+ if (!GObject) {
419
+ throw new Error(`GObject.Object could not be found while generating ${node.namespace.name}.${node.name}`);
420
+ }
421
+ node.superType = GObject.getType();
422
+ }
423
+ const { name } = node;
424
+ const Extends = this.extends(node);
425
+ const Properties = node.props.map(v => v && v.asString(this));
426
+ const Methods = node.members
427
+ .filter(m => !(m instanceof IntrospectedStaticClassFunction) && !(m instanceof IntrospectedVirtualClassFunction))
428
+ .map(v => v && v.asString(this));
429
+ const StaticMethods = node.members
430
+ .filter((m) => m instanceof IntrospectedStaticClassFunction)
431
+ .map(v => v && v.asString(this));
432
+ const VirtualMethods = node.members
433
+ .filter((m) => m instanceof IntrospectedVirtualClassFunction)
434
+ .map(v => v && v.asString(this));
435
+ return {
436
+ kind: "interface" /* NodeKind.interface */,
437
+ name,
438
+ type: generateType(node.getType()),
439
+ extends: Extends ? generateType(Extends) : null,
440
+ props: Properties,
441
+ methods: Methods,
442
+ staticMethods: StaticMethods,
443
+ virtualMethods: VirtualMethods,
444
+ ...this._generateDocAndMetadata(node)
445
+ };
446
+ }
447
+ generateRecord(node) {
448
+ const { name } = node;
449
+ const Extends = this.extends(node);
450
+ const Properties = node.props.map(v => v && v.asString(this));
451
+ const Fields = node.fields.map(v => v && v.asString(this));
452
+ const Constructors = node.constructors.map(v => v && this.generateConstructorFunction(v));
453
+ const Methods = node.members
454
+ .filter(m => !(m instanceof IntrospectedStaticClassFunction) && !(m instanceof IntrospectedVirtualClassFunction))
455
+ .map(v => v && v.asString(this));
456
+ const StaticMethods = node.members
457
+ .filter((m) => m instanceof IntrospectedStaticClassFunction)
458
+ .map(v => v && v.asString(this));
459
+ const VirtualMethods = node.members
460
+ .filter((m) => m instanceof IntrospectedVirtualClassFunction)
461
+ .map(v => v && v.asString(this));
462
+ const Callbacks = node.callbacks.map(c => c && c.asString(this));
463
+ return {
464
+ kind: "record" /* NodeKind.record */,
465
+ name,
466
+ type: generateType(node.getType()),
467
+ mainConstructor: node.mainConstructor?.asString(this) ?? null,
468
+ extends: Extends ? generateType(Extends) : null,
469
+ implements: [],
470
+ props: Properties,
471
+ fields: Fields,
472
+ constructors: Constructors,
473
+ methods: Methods,
474
+ staticMethods: StaticMethods,
475
+ virtualMethods: VirtualMethods,
476
+ callbacks: Callbacks,
477
+ ...this._generateDocAndMetadata(node)
478
+ };
479
+ }
480
+ generateClass(node) {
481
+ const Extends = this.extends(node);
482
+ const Implements = this.implements(node);
483
+ let MainConstructor = null;
484
+ const ConstructorProps = node.props.map(v => this.generateProperty(v, true));
485
+ if (node.mainConstructor) {
486
+ MainConstructor = this.generateConstructor(node.mainConstructor);
487
+ }
488
+ else {
489
+ MainConstructor = {
490
+ kind: "properties_constructor" /* NodeKind.propertiesConstructor */,
491
+ name: "new",
492
+ private: false,
493
+ properties: ConstructorProps.map(p => ({
494
+ kind: "parameter" /* NodeKind.parameter */,
495
+ private: p.private,
496
+ varargs: false,
497
+ name: p.name,
498
+ type: p.type,
499
+ doc: p.doc,
500
+ metadata: p.metadata,
501
+ optional: true
502
+ })),
503
+ doc: null,
504
+ metadata: null
505
+ };
506
+ }
507
+ const Properties = node.props.map(v => v.asString(this));
508
+ const Fields = node.fields.map(v => v.asString(this));
509
+ const Constructors = node.constructors.map(v => this.generateConstructorFunction(v));
510
+ const Methods = node.members
511
+ .filter(m => !(m instanceof IntrospectedStaticClassFunction) && !(m instanceof IntrospectedVirtualClassFunction))
512
+ .map(v => v && v.asString(this));
513
+ const StaticMethods = node.members
514
+ .filter((m) => m instanceof IntrospectedStaticClassFunction)
515
+ .map(v => v && v.asString(this));
516
+ const VirtualMethods = node.members
517
+ .filter((m) => m instanceof IntrospectedVirtualClassFunction)
518
+ .map(v => v && v.asString(this));
519
+ // TODO Move these to a cleaner place.
520
+ const Connect = new IntrospectedClassFunction({
521
+ name: "connect",
522
+ parent: node,
523
+ parameters: [
524
+ new IntrospectedFunctionParameter({
525
+ name: "id",
526
+ type: StringType,
527
+ direction: GirDirection.In
528
+ }),
529
+ new IntrospectedFunctionParameter({
530
+ name: "callback",
531
+ type: AnyFunctionType,
532
+ direction: GirDirection.In
533
+ })
534
+ ],
535
+ return_type: NumberType
536
+ });
537
+ const ConnectAfter = new IntrospectedClassFunction({
538
+ name: "connect_after",
539
+ parent: node,
540
+ parameters: [
541
+ new IntrospectedFunctionParameter({
542
+ name: "id",
543
+ type: StringType,
544
+ direction: GirDirection.In
545
+ }),
546
+ new IntrospectedFunctionParameter({
547
+ name: "callback",
548
+ type: AnyFunctionType,
549
+ direction: GirDirection.In
550
+ })
551
+ ],
552
+ return_type: NumberType
553
+ });
554
+ const Emit = new IntrospectedClassFunction({
555
+ name: "emit",
556
+ parent: node,
557
+ parameters: [
558
+ new IntrospectedFunctionParameter({
559
+ name: "id",
560
+ type: StringType,
561
+ direction: GirDirection.In
562
+ }),
563
+ new IntrospectedFunctionParameter({
564
+ name: "args",
565
+ isVarArgs: true,
566
+ type: new ArrayType(AnyType),
567
+ direction: GirDirection.In
568
+ })
569
+ ],
570
+ return_type: VoidType
571
+ });
572
+ const default_signals = [];
573
+ let hasConnect, hasConnectAfter, hasEmit;
574
+ if (node.signals.length > 0) {
575
+ hasConnect = node.members.some(m => m.name === "connect");
576
+ hasConnectAfter = node.members.some(m => m.name === "connect_after");
577
+ hasEmit = node.members.some(m => m.name === "emit");
578
+ if (!hasConnect) {
579
+ default_signals.push(Connect);
580
+ }
581
+ if (!hasConnectAfter) {
582
+ default_signals.push(ConnectAfter);
583
+ }
584
+ if (!hasEmit) {
585
+ default_signals.push(Emit);
586
+ }
587
+ hasConnect = !default_signals.some(s => s.name === "connect");
588
+ hasConnectAfter = !default_signals.some(s => s.name === "connect_after");
589
+ hasEmit = !default_signals.some(s => s.name === "emit");
590
+ }
591
+ const SignalsList = [
592
+ ...default_signals.map(s => s.asString(this)),
593
+ ...node.signals
594
+ .map(s => {
595
+ const methods = [];
596
+ if (!hasConnect)
597
+ methods.push(s.asString(this, IntrospectedSignalType.CONNECT));
598
+ if (!hasConnectAfter)
599
+ methods.push(s.asString(this, IntrospectedSignalType.CONNECT_AFTER));
600
+ if (!hasEmit)
601
+ methods.push(s.asString(this, IntrospectedSignalType.EMIT));
602
+ return methods;
603
+ })
604
+ .flat()
605
+ ];
606
+ const Signals = SignalsList;
607
+ return {
608
+ kind: "class" /* NodeKind.class */,
609
+ abstract: node.isAbstract,
610
+ type: generateType(node.getType()),
611
+ name: node.name,
612
+ mainConstructor: MainConstructor,
613
+ signals: Signals,
614
+ extends: Extends ? generateType(Extends) : null,
615
+ implements: Implements.map(i => generateType(i)),
616
+ props: Properties,
617
+ fields: Fields,
618
+ constructors: Constructors,
619
+ methods: Methods,
620
+ staticMethods: StaticMethods,
621
+ virtualMethods: VirtualMethods,
622
+ ...this._generateDocAndMetadata(node)
623
+ };
624
+ }
625
+ generateField(node) {
626
+ const { namespace, options } = this;
627
+ const { name, computed } = node;
628
+ const invalid = isInvalid(name);
629
+ const Static = node.isStatic;
630
+ const ReadOnly = node.writable;
631
+ return {
632
+ kind: "field" /* NodeKind.field */,
633
+ readonly: ReadOnly,
634
+ static: Static,
635
+ computed,
636
+ type: generateType(node.type.resolve(namespace, options)),
637
+ name: invalid ? `"${name}"` : name,
638
+ ...this._generateDocAndMetadata(node)
639
+ };
640
+ }
641
+ generateProperty(node, construct = false) {
642
+ const { namespace, options } = this;
643
+ const invalid = isInvalid(node.name);
644
+ const ReadOnly = node.writable || construct;
645
+ const Name = invalid ? `"${node.name}"` : node.name;
646
+ const Type = generateType(node.type.resolve(namespace, options));
647
+ return {
648
+ kind: "prop" /* NodeKind.prop */,
649
+ readonly: ReadOnly,
650
+ constructOnly: node.constructOnly,
651
+ readable: node.readable,
652
+ writable: node.writable,
653
+ static: false,
654
+ type: Type,
655
+ name: Name,
656
+ ...this._generateDocAndMetadata(node)
657
+ };
658
+ }
659
+ generateSignal(node, type = IntrospectedSignalType.CONNECT) {
660
+ switch (type) {
661
+ case IntrospectedSignalType.CONNECT:
662
+ return node.asConnect(false).asString(this);
663
+ case IntrospectedSignalType.CONNECT_AFTER:
664
+ return node.asConnect(true).asString(this);
665
+ case IntrospectedSignalType.EMIT:
666
+ return node.asEmit().asString(this);
667
+ }
668
+ }
669
+ generateEnumMember(node) {
670
+ const invalid = isInvalid(node.name);
671
+ let enumMember;
672
+ if (node.value != null &&
673
+ !Number.isNaN(Number.parseInt(node.value, 10)) &&
674
+ Number.isNaN(Number.parseInt(node.name, 10)) &&
675
+ node.name !== "NaN") {
676
+ enumMember = { name: invalid ? `"${node.name}"` : `${node.name}`, value: `${node.value}` };
677
+ }
678
+ else {
679
+ enumMember = { name: invalid ? `"${node.name}"` : `${node.name}`, value: null };
680
+ }
681
+ return {
682
+ kind: "enum_member" /* NodeKind.enumMember */,
683
+ ...enumMember,
684
+ ...this._generateDocAndMetadata(node)
685
+ };
686
+ }
687
+ generateParameter(node) {
688
+ const { namespace, options } = this;
689
+ const type = generateType(resolveDirectedType(node.type, node.direction)?.resolve(namespace, options) ??
690
+ node.type.resolve(namespace, options));
691
+ return {
692
+ kind: "parameter" /* NodeKind.parameter */,
693
+ name: node.name,
694
+ type,
695
+ varargs: node.isVarArgs,
696
+ optional: node.isOptional,
697
+ ...this._generateDocAndMetadata(node)
698
+ };
699
+ }
700
+ generateFunction(node) {
701
+ const { namespace } = this;
702
+ // Register our identifier with the sanitized identifiers.
703
+ // We avoid doing this in fromXML because other class-level function classes
704
+ // depends on that code.
705
+ sanitizeIdentifierName(namespace.name, node.raw_name);
706
+ const Parameters = this.generateParameters(node.parameters);
707
+ const ReturnType = this.generateReturn(node.return(), node.output_parameters);
708
+ return {
709
+ kind: "function" /* NodeKind.function */,
710
+ name: node.name,
711
+ parameters: Parameters,
712
+ returnType: ReturnType,
713
+ ...this._generateDocAndMetadata(node)
714
+ };
715
+ }
716
+ generateConstructorFunction(node) {
717
+ const { namespace, options } = this;
718
+ const Parameters = this.generateParameters(node.parameters);
719
+ return {
720
+ kind: "class_function" /* NodeKind.classFunction */,
721
+ static: true,
722
+ name: node.name,
723
+ parameters: Parameters,
724
+ returnType: generateType(node.return().resolve(namespace, options)),
725
+ ...this._generateDocAndMetadata(node)
726
+ };
727
+ }
728
+ generateConstructor(node) {
729
+ return {
730
+ name: node.name,
731
+ kind: "constructor" /* NodeKind.constructor */,
732
+ parameters: this.generateParameters(node.parameters),
733
+ ...this._generateDocAndMetadata(node)
734
+ };
735
+ }
736
+ generateDirectAllocationConstructor(node) {
737
+ return {
738
+ name: node.name,
739
+ kind: "constructor" /* NodeKind.constructor */,
740
+ parameters: this.generateParameters(node.parameters),
741
+ ...this._generateDocAndMetadata(node)
742
+ };
743
+ }
744
+ generateClassFunction(node) {
745
+ const parameters = node.parameters.map(p => this.generateParameter(p));
746
+ const output_parameters = node.output_parameters;
747
+ const return_type = node.return();
748
+ const ReturnType = this.generateReturn(return_type, output_parameters);
749
+ return {
750
+ kind: "class_function" /* NodeKind.classFunction */,
751
+ name: node.name,
752
+ parameters,
753
+ returnType: ReturnType,
754
+ ...this._generateDocAndMetadata(node)
755
+ };
756
+ }
757
+ generateStaticClassFunction(node) {
758
+ const parameters = node.parameters.map(p => this.generateParameter(p));
759
+ const output_parameters = node.output_parameters;
760
+ const return_type = node.return();
761
+ const ReturnType = this.generateReturn(return_type, output_parameters);
762
+ return {
763
+ kind: "static_class_function" /* NodeKind.staticClassFunction */,
764
+ name: node.name,
765
+ parameters,
766
+ returnType: ReturnType,
767
+ ...this._generateDocAndMetadata(node)
768
+ };
769
+ }
770
+ generateAlias(node) {
771
+ const { namespace, options } = this;
772
+ const type = node.type.resolve(namespace, options);
773
+ const { name } = node;
774
+ return {
775
+ kind: "alias" /* NodeKind.alias */,
776
+ name,
777
+ type: generateType(type.resolve(namespace, options)),
778
+ ...this._generateDocAndMetadata(node)
779
+ };
780
+ }
781
+ generateVirtualClassFunction(node) {
782
+ return {
783
+ ...this.generateClassFunction(node),
784
+ kind: "virtual_class_function" /* NodeKind.virtualClassFunction */
785
+ };
786
+ }
787
+ generateNamespace(node) {
788
+ function shouldGenerate(node) {
789
+ return node.emit;
790
+ }
791
+ const { name, version } = node;
792
+ const members = Array.from(node.members.values())
793
+ .flatMap(m => m)
794
+ .filter(shouldGenerate);
795
+ const classes = members
796
+ .filter((m) => m instanceof IntrospectedClass)
797
+ .map(m => m.asString(this));
798
+ const interfaces = members
799
+ .filter((m) => m instanceof IntrospectedInterface)
800
+ .map(m => m.asString(this));
801
+ const records = members
802
+ .filter((m) => m instanceof IntrospectedRecord)
803
+ .map(m => m.asString(this));
804
+ const constants = members
805
+ .filter((m) => m instanceof IntrospectedConstant)
806
+ .map(m => m.asString(this));
807
+ const callbacks = members
808
+ .filter((m) => m instanceof IntrospectedCallback)
809
+ .map(m => m.asString(this));
810
+ // Functions can have overrides.
811
+ const functions = [
812
+ ...members
813
+ .filter((m) => !(m instanceof IntrospectedCallback) && m instanceof IntrospectedFunction)
814
+ .reduce((prev, next) => {
815
+ if (!prev.has(next.name))
816
+ prev.set(next.name, next.asString(this));
817
+ return prev;
818
+ }, new Map())
819
+ .values()
820
+ ];
821
+ const errors = members
822
+ .filter((m) => m instanceof IntrospectedError)
823
+ .map(m => m.asString(this));
824
+ const enums = members
825
+ .filter((m) => !(m instanceof IntrospectedError) && m instanceof IntrospectedEnum)
826
+ .map(m => m.asString(this));
827
+ const alias = members
828
+ .filter((m) => m instanceof IntrospectedAlias)
829
+ .map(m => m.asString(this));
830
+ // Resolve imports after we stringify everything else, sometimes we have to ad-hoc add an import.
831
+ const imports = [];
832
+ return Promise.resolve({
833
+ kind: "namespace" /* NodeKind.namespace */,
834
+ name,
835
+ version,
836
+ imports: Object.fromEntries(imports),
837
+ classes,
838
+ interfaces,
839
+ records,
840
+ constants,
841
+ functions,
842
+ callbacks,
843
+ errors,
844
+ enums,
845
+ alias
846
+ });
847
+ }
848
+ async stringifyNamespace(node) {
849
+ const { namespace, options } = this;
850
+ if (options.verbose) {
851
+ console.debug(`Resolving the types of ${namespace.name}...`);
852
+ }
853
+ try {
854
+ const output = await this.generateNamespace(node);
855
+ if (options.verbose) {
856
+ console.debug(`Printing ${namespace.name}...`);
857
+ }
858
+ if (!output)
859
+ return null;
860
+ return JSON.stringify(output, null, 4);
861
+ }
862
+ catch (err) {
863
+ console.error(`Failed to generate namespace: ${node.name}`);
864
+ console.error(err);
865
+ return null;
866
+ }
867
+ }
868
+ }
869
+ //# sourceMappingURL=json.js.map