@gtkx/gir 0.10.5 → 0.11.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.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,27 @@
1
- export * from "./class-names.js";
2
- export * from "./doc-sanitizer.js";
3
- export * from "./naming.js";
4
- export * from "./parser.js";
5
- export * from "./types.js";
1
+ /**
2
+ * @gtkx/gir - GObject Introspection Repository
3
+ *
4
+ * Pure GIR data layer with normalized types and a queryable API.
5
+ * All type references use fully qualified names (Namespace.TypeName).
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { GirRepository } from "@gtkx/gir";
10
+ *
11
+ * const repo = new GirRepository();
12
+ * await repo.loadFromDirectory("./girs");
13
+ * repo.resolve();
14
+ *
15
+ * const button = repo.resolveClass("Gtk.Button" as QualifiedName);
16
+ * button.isSubclassOf("Gtk.Widget" as QualifiedName); // true
17
+ * button.getInheritanceChain();
18
+ * // ["Gtk.Button", "Gtk.Widget", "GObject.InitiallyUnowned", "GObject.Object"]
19
+ * ```
20
+ *
21
+ * @packageDocumentation
22
+ */
23
+ export { INTRINSIC_TYPES, isIntrinsicType, isNumericType, isStringType, isVoidType, NUMERIC_TYPES, STRING_TYPES, VOID_TYPES, } from "./intrinsics.js";
24
+ export { GirRepository } from "./repository.js";
25
+ export type { ContainerType, QualifiedName, TypeKind, } from "./types.js";
26
+ export { GirCallback, GirClass, GirConstant, GirConstructor, GirEnumeration, GirEnumerationMember, GirField, GirFunction, GirInterface, GirMethod, GirNamespace, GirParameter, GirProperty, GirRecord, GirSignal, GirType, parseQualifiedName, qualifiedName, } from "./types.js";
27
+ export { toCamelCase, toPascalCase } from "./utils.js";
package/dist/index.js CHANGED
@@ -1,5 +1,26 @@
1
- export * from "./class-names.js";
2
- export * from "./doc-sanitizer.js";
3
- export * from "./naming.js";
4
- export * from "./parser.js";
5
- export * from "./types.js";
1
+ /**
2
+ * @gtkx/gir - GObject Introspection Repository
3
+ *
4
+ * Pure GIR data layer with normalized types and a queryable API.
5
+ * All type references use fully qualified names (Namespace.TypeName).
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { GirRepository } from "@gtkx/gir";
10
+ *
11
+ * const repo = new GirRepository();
12
+ * await repo.loadFromDirectory("./girs");
13
+ * repo.resolve();
14
+ *
15
+ * const button = repo.resolveClass("Gtk.Button" as QualifiedName);
16
+ * button.isSubclassOf("Gtk.Widget" as QualifiedName); // true
17
+ * button.getInheritanceChain();
18
+ * // ["Gtk.Button", "Gtk.Widget", "GObject.InitiallyUnowned", "GObject.Object"]
19
+ * ```
20
+ *
21
+ * @packageDocumentation
22
+ */
23
+ export { INTRINSIC_TYPES, isIntrinsicType, isNumericType, isStringType, isVoidType, NUMERIC_TYPES, STRING_TYPES, VOID_TYPES, } from "./intrinsics.js";
24
+ export { GirRepository } from "./repository.js";
25
+ export { GirCallback, GirClass, GirConstant, GirConstructor, GirEnumeration, GirEnumerationMember, GirField, GirFunction, GirInterface, GirMethod, GirNamespace, GirParameter, GirProperty, GirRecord, GirSignal, GirType, parseQualifiedName, qualifiedName, } from "./types.js";
26
+ export { toCamelCase, toPascalCase } from "./utils.js";
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Normalizer that converts raw GIR types to normalized types.
3
+ *
4
+ * The key job is to qualify all type references with their namespace,
5
+ * except for intrinsic types which remain unqualified.
6
+ *
7
+ * @internal
8
+ */
9
+ import { GirNamespace } from "../types.js";
10
+ import type { RawNamespace } from "./raw-types.js";
11
+ /**
12
+ * Context for normalization - provides access to all loaded namespaces
13
+ * for resolving cross-namespace references.
14
+ */
15
+ export type NormalizerContext = {
16
+ /** All raw namespaces that have been loaded */
17
+ rawNamespaces: Map<string, RawNamespace>;
18
+ };
19
+ /**
20
+ * Normalizes a raw namespace into a normalized namespace.
21
+ */
22
+ export declare const normalizeNamespace: (raw: RawNamespace, ctx: NormalizerContext) => GirNamespace;
@@ -0,0 +1,398 @@
1
+ /**
2
+ * Normalizer that converts raw GIR types to normalized types.
3
+ *
4
+ * The key job is to qualify all type references with their namespace,
5
+ * except for intrinsic types which remain unqualified.
6
+ *
7
+ * @internal
8
+ */
9
+ import { isIntrinsicType } from "../intrinsics.js";
10
+ import { GirCallback, GirClass, GirConstant, GirConstructor, GirEnumeration, GirEnumerationMember, GirField, GirFunction, GirInterface, GirMethod, GirNamespace, GirParameter, GirProperty, GirRecord, GirSignal, GirType, qualifiedName, } from "../types.js";
11
+ /**
12
+ * Normalizes a raw namespace into a normalized namespace.
13
+ */
14
+ export const normalizeNamespace = (raw, ctx) => {
15
+ const nsName = raw.name;
16
+ const classes = new Map();
17
+ const interfaces = new Map();
18
+ const records = new Map();
19
+ const enumerations = new Map();
20
+ const bitfields = new Map();
21
+ const callbacks = new Map();
22
+ const functions = new Map();
23
+ const constants = new Map();
24
+ for (const rawClass of raw.classes) {
25
+ const normalized = normalizeClass(rawClass, nsName, ctx);
26
+ classes.set(normalized.name, normalized);
27
+ }
28
+ for (const rawInterface of raw.interfaces) {
29
+ const normalized = normalizeInterface(rawInterface, nsName, ctx);
30
+ interfaces.set(normalized.name, normalized);
31
+ }
32
+ for (const rawRecord of raw.records) {
33
+ const normalized = normalizeRecord(rawRecord, nsName, ctx);
34
+ records.set(normalized.name, normalized);
35
+ }
36
+ for (const rawEnum of raw.enumerations) {
37
+ const normalized = normalizeEnumeration(rawEnum, nsName);
38
+ enumerations.set(normalized.name, normalized);
39
+ }
40
+ for (const rawBitfield of raw.bitfields) {
41
+ const normalized = normalizeEnumeration(rawBitfield, nsName);
42
+ bitfields.set(normalized.name, normalized);
43
+ }
44
+ for (const rawCallback of raw.callbacks) {
45
+ const normalized = normalizeCallback(rawCallback, nsName, ctx);
46
+ callbacks.set(normalized.name, normalized);
47
+ }
48
+ for (const rawFunction of raw.functions) {
49
+ const normalized = normalizeFunction(rawFunction, nsName, ctx);
50
+ functions.set(normalized.name, normalized);
51
+ }
52
+ for (const rawConstant of raw.constants) {
53
+ const normalized = normalizeConstant(rawConstant, nsName, ctx);
54
+ constants.set(normalized.name, normalized);
55
+ }
56
+ return new GirNamespace({
57
+ name: raw.name,
58
+ version: raw.version,
59
+ sharedLibrary: raw.sharedLibrary,
60
+ cPrefix: raw.cPrefix,
61
+ classes,
62
+ interfaces,
63
+ records,
64
+ enumerations,
65
+ bitfields,
66
+ callbacks,
67
+ functions,
68
+ constants,
69
+ doc: raw.doc,
70
+ });
71
+ };
72
+ /**
73
+ * Normalizes a type reference, qualifying it with its namespace if not intrinsic.
74
+ */
75
+ const normalizeTypeName = (typeName, currentNamespace, ctx) => {
76
+ if (isIntrinsicType(typeName)) {
77
+ return typeName;
78
+ }
79
+ if (typeName.includes(".")) {
80
+ return typeName;
81
+ }
82
+ const currentNs = ctx.rawNamespaces.get(currentNamespace);
83
+ if (currentNs && typeExistsInNamespace(typeName, currentNs)) {
84
+ return qualifiedName(currentNamespace, typeName);
85
+ }
86
+ for (const [nsName, ns] of ctx.rawNamespaces) {
87
+ if (nsName !== currentNamespace && typeExistsInNamespace(typeName, ns)) {
88
+ return qualifiedName(nsName, typeName);
89
+ }
90
+ }
91
+ return qualifiedName(currentNamespace, typeName);
92
+ };
93
+ /**
94
+ * Checks if a type name exists in a namespace.
95
+ */
96
+ const typeExistsInNamespace = (typeName, ns) => {
97
+ return (ns.classes.some((c) => c.name === typeName) ||
98
+ ns.interfaces.some((i) => i.name === typeName) ||
99
+ ns.records.some((r) => r.name === typeName) ||
100
+ ns.enumerations.some((e) => e.name === typeName) ||
101
+ ns.bitfields.some((b) => b.name === typeName) ||
102
+ ns.callbacks.some((c) => c.name === typeName));
103
+ };
104
+ const buildContainerTypeBase = (raw) => ({
105
+ cType: raw.cType,
106
+ containerType: raw.containerType,
107
+ transferOwnership: raw.transferOwnership,
108
+ nullable: raw.nullable ?? false,
109
+ });
110
+ /**
111
+ * Normalizes a raw type to a normalized type.
112
+ */
113
+ const normalizeType = (raw, currentNamespace, ctx) => {
114
+ if (raw.containerType === "ghashtable") {
115
+ const typeParams = (raw.typeParameters ?? []).map((tp) => normalizeType(tp, currentNamespace, ctx));
116
+ return new GirType({
117
+ ...buildContainerTypeBase(raw),
118
+ name: qualifiedName("GLib", "HashTable"),
119
+ isArray: false,
120
+ elementType: typeParams[1] ?? null,
121
+ typeParameters: typeParams,
122
+ });
123
+ }
124
+ if (raw.containerType === "gptrarray" || raw.containerType === "garray") {
125
+ const typeParams = (raw.typeParameters ?? []).map((tp) => normalizeType(tp, currentNamespace, ctx));
126
+ const typeName = raw.containerType === "gptrarray" ? "PtrArray" : "Array";
127
+ return new GirType({
128
+ ...buildContainerTypeBase(raw),
129
+ name: qualifiedName("GLib", typeName),
130
+ isArray: true,
131
+ elementType: typeParams[0] ?? null,
132
+ typeParameters: typeParams,
133
+ });
134
+ }
135
+ if (raw.containerType === "glist" || raw.containerType === "gslist") {
136
+ const elementType = raw.elementType ? normalizeType(raw.elementType, currentNamespace, ctx) : null;
137
+ return new GirType({
138
+ ...buildContainerTypeBase(raw),
139
+ name: "array",
140
+ isArray: true,
141
+ elementType,
142
+ typeParameters: elementType ? [elementType] : [],
143
+ });
144
+ }
145
+ const isArray = raw.isArray === true || raw.name === "array";
146
+ if (isArray && raw.elementType) {
147
+ return new GirType({
148
+ name: "array",
149
+ cType: raw.cType,
150
+ isArray: true,
151
+ elementType: normalizeType(raw.elementType, currentNamespace, ctx),
152
+ transferOwnership: raw.transferOwnership,
153
+ nullable: raw.nullable ?? false,
154
+ });
155
+ }
156
+ if (isArray) {
157
+ return new GirType({
158
+ name: "array",
159
+ cType: raw.cType,
160
+ isArray: true,
161
+ elementType: null,
162
+ transferOwnership: raw.transferOwnership,
163
+ nullable: raw.nullable ?? false,
164
+ });
165
+ }
166
+ return new GirType({
167
+ name: normalizeTypeName(raw.name, currentNamespace, ctx),
168
+ cType: raw.cType,
169
+ isArray: false,
170
+ elementType: null,
171
+ transferOwnership: raw.transferOwnership,
172
+ nullable: raw.nullable ?? false,
173
+ });
174
+ };
175
+ /**
176
+ * Normalizes a raw class to a normalized class.
177
+ */
178
+ const normalizeClass = (raw, currentNamespace, ctx) => {
179
+ const qn = qualifiedName(currentNamespace, raw.name);
180
+ let parent = null;
181
+ if (raw.parent) {
182
+ const normalizedParent = normalizeTypeName(raw.parent, currentNamespace, ctx);
183
+ if (typeof normalizedParent !== "string" || normalizedParent.includes(".")) {
184
+ parent = normalizedParent;
185
+ }
186
+ }
187
+ const implementsRefs = raw.implements.map((impl) => {
188
+ const normalized = normalizeTypeName(impl, currentNamespace, ctx);
189
+ return normalized;
190
+ });
191
+ return new GirClass({
192
+ name: raw.name,
193
+ qualifiedName: qn,
194
+ cType: raw.cType,
195
+ parent,
196
+ abstract: raw.abstract ?? false,
197
+ glibTypeName: raw.glibTypeName,
198
+ glibGetType: raw.glibGetType,
199
+ cSymbolPrefix: raw.cSymbolPrefix,
200
+ implements: implementsRefs,
201
+ methods: raw.methods.map((m) => normalizeMethod(m, currentNamespace, ctx)),
202
+ constructors: raw.constructors.map((c) => normalizeConstructor(c, currentNamespace, ctx)),
203
+ staticFunctions: raw.functions.map((f) => normalizeFunction(f, currentNamespace, ctx)),
204
+ properties: raw.properties.map((p) => normalizeProperty(p, currentNamespace, ctx)),
205
+ signals: raw.signals.map((s) => normalizeSignal(s, currentNamespace, ctx)),
206
+ doc: raw.doc,
207
+ });
208
+ };
209
+ /**
210
+ * Normalizes a raw interface to a normalized interface.
211
+ */
212
+ const normalizeInterface = (raw, currentNamespace, ctx) => {
213
+ const qn = qualifiedName(currentNamespace, raw.name);
214
+ const prerequisites = raw.prerequisites.map((prereq) => {
215
+ const normalized = normalizeTypeName(prereq, currentNamespace, ctx);
216
+ return normalized;
217
+ });
218
+ return new GirInterface({
219
+ name: raw.name,
220
+ qualifiedName: qn,
221
+ cType: raw.cType,
222
+ glibTypeName: raw.glibTypeName,
223
+ prerequisites,
224
+ methods: raw.methods.map((m) => normalizeMethod(m, currentNamespace, ctx)),
225
+ properties: raw.properties.map((p) => normalizeProperty(p, currentNamespace, ctx)),
226
+ signals: raw.signals.map((s) => normalizeSignal(s, currentNamespace, ctx)),
227
+ doc: raw.doc,
228
+ });
229
+ };
230
+ /**
231
+ * Normalizes a raw record to a normalized record.
232
+ */
233
+ const normalizeRecord = (raw, currentNamespace, ctx) => {
234
+ const qn = qualifiedName(currentNamespace, raw.name);
235
+ return new GirRecord({
236
+ name: raw.name,
237
+ qualifiedName: qn,
238
+ cType: raw.cType,
239
+ opaque: raw.opaque ?? false,
240
+ disguised: raw.disguised ?? false,
241
+ glibTypeName: raw.glibTypeName,
242
+ glibGetType: raw.glibGetType,
243
+ isGtypeStructFor: raw.isGtypeStructFor,
244
+ fields: raw.fields.map((f) => normalizeField(f, currentNamespace, ctx)),
245
+ methods: raw.methods.map((m) => normalizeMethod(m, currentNamespace, ctx)),
246
+ constructors: raw.constructors.map((c) => normalizeConstructor(c, currentNamespace, ctx)),
247
+ staticFunctions: raw.functions.map((f) => normalizeFunction(f, currentNamespace, ctx)),
248
+ doc: raw.doc,
249
+ });
250
+ };
251
+ /**
252
+ * Normalizes a raw enumeration to a normalized enumeration.
253
+ */
254
+ const normalizeEnumeration = (raw, currentNamespace) => {
255
+ const qn = qualifiedName(currentNamespace, raw.name);
256
+ return new GirEnumeration({
257
+ name: raw.name,
258
+ qualifiedName: qn,
259
+ cType: raw.cType,
260
+ members: raw.members.map((m) => new GirEnumerationMember({
261
+ name: m.name,
262
+ value: m.value,
263
+ cIdentifier: m.cIdentifier,
264
+ doc: m.doc,
265
+ })),
266
+ doc: raw.doc,
267
+ });
268
+ };
269
+ /**
270
+ * Normalizes a raw callback to a normalized callback.
271
+ */
272
+ const normalizeCallback = (raw, currentNamespace, ctx) => {
273
+ const qn = qualifiedName(currentNamespace, raw.name);
274
+ return new GirCallback({
275
+ name: raw.name,
276
+ qualifiedName: qn,
277
+ cType: raw.cType,
278
+ returnType: normalizeType(raw.returnType, currentNamespace, ctx),
279
+ parameters: raw.parameters.map((p) => normalizeParameter(p, currentNamespace, ctx)),
280
+ doc: raw.doc,
281
+ });
282
+ };
283
+ /**
284
+ * Normalizes a raw constant to a normalized constant.
285
+ */
286
+ const normalizeConstant = (raw, currentNamespace, ctx) => {
287
+ const qn = qualifiedName(currentNamespace, raw.name);
288
+ return new GirConstant({
289
+ name: raw.name,
290
+ qualifiedName: qn,
291
+ cType: raw.cType,
292
+ value: raw.value,
293
+ type: normalizeType(raw.type, currentNamespace, ctx),
294
+ doc: raw.doc,
295
+ });
296
+ };
297
+ /**
298
+ * Normalizes a raw method to a normalized method.
299
+ */
300
+ const normalizeMethod = (raw, currentNamespace, ctx) => {
301
+ return new GirMethod({
302
+ name: raw.name,
303
+ cIdentifier: raw.cIdentifier,
304
+ returnType: normalizeType(raw.returnType, currentNamespace, ctx),
305
+ parameters: raw.parameters.map((p) => normalizeParameter(p, currentNamespace, ctx)),
306
+ throws: raw.throws ?? false,
307
+ doc: raw.doc,
308
+ returnDoc: raw.returnDoc,
309
+ finishFunc: raw.finishFunc,
310
+ });
311
+ };
312
+ /**
313
+ * Normalizes a raw constructor to a normalized constructor.
314
+ */
315
+ const normalizeConstructor = (raw, currentNamespace, ctx) => {
316
+ return new GirConstructor({
317
+ name: raw.name,
318
+ cIdentifier: raw.cIdentifier,
319
+ returnType: normalizeType(raw.returnType, currentNamespace, ctx),
320
+ parameters: raw.parameters.map((p) => normalizeParameter(p, currentNamespace, ctx)),
321
+ throws: raw.throws ?? false,
322
+ doc: raw.doc,
323
+ returnDoc: raw.returnDoc,
324
+ });
325
+ };
326
+ /**
327
+ * Normalizes a raw function to a normalized function.
328
+ */
329
+ const normalizeFunction = (raw, currentNamespace, ctx) => {
330
+ return new GirFunction({
331
+ name: raw.name,
332
+ cIdentifier: raw.cIdentifier,
333
+ returnType: normalizeType(raw.returnType, currentNamespace, ctx),
334
+ parameters: raw.parameters.map((p) => normalizeParameter(p, currentNamespace, ctx)),
335
+ throws: raw.throws ?? false,
336
+ doc: raw.doc,
337
+ returnDoc: raw.returnDoc,
338
+ });
339
+ };
340
+ /**
341
+ * Normalizes a raw parameter to a normalized parameter.
342
+ */
343
+ const normalizeParameter = (raw, currentNamespace, ctx) => {
344
+ return new GirParameter({
345
+ name: raw.name,
346
+ type: normalizeType(raw.type, currentNamespace, ctx),
347
+ direction: raw.direction ?? "in",
348
+ callerAllocates: raw.callerAllocates ?? false,
349
+ nullable: raw.nullable ?? false,
350
+ optional: raw.optional ?? false,
351
+ scope: raw.scope,
352
+ closure: raw.closure,
353
+ destroy: raw.destroy,
354
+ transferOwnership: raw.transferOwnership,
355
+ doc: raw.doc,
356
+ });
357
+ };
358
+ /**
359
+ * Normalizes a raw property to a normalized property.
360
+ */
361
+ const normalizeProperty = (raw, currentNamespace, ctx) => {
362
+ return new GirProperty({
363
+ name: raw.name,
364
+ type: normalizeType(raw.type, currentNamespace, ctx),
365
+ readable: raw.readable ?? true,
366
+ writable: raw.writable ?? false,
367
+ constructOnly: raw.constructOnly ?? false,
368
+ hasDefault: raw.hasDefault ?? false,
369
+ getter: raw.getter,
370
+ setter: raw.setter,
371
+ doc: raw.doc,
372
+ });
373
+ };
374
+ /**
375
+ * Normalizes a raw signal to a normalized signal.
376
+ */
377
+ const normalizeSignal = (raw, currentNamespace, ctx) => {
378
+ return new GirSignal({
379
+ name: raw.name,
380
+ when: raw.when ?? "last",
381
+ returnType: raw.returnType ? normalizeType(raw.returnType, currentNamespace, ctx) : null,
382
+ parameters: (raw.parameters ?? []).map((p) => normalizeParameter(p, currentNamespace, ctx)),
383
+ doc: raw.doc,
384
+ });
385
+ };
386
+ /**
387
+ * Normalizes a raw field to a normalized field.
388
+ */
389
+ const normalizeField = (raw, currentNamespace, ctx) => {
390
+ return new GirField({
391
+ name: raw.name,
392
+ type: normalizeType(raw.type, currentNamespace, ctx),
393
+ writable: raw.writable ?? false,
394
+ readable: raw.readable ?? true,
395
+ private: raw.private ?? false,
396
+ doc: raw.doc,
397
+ });
398
+ };
@@ -0,0 +1,42 @@
1
+ /**
2
+ * GObject Introspection XML (GIR) parser.
3
+ *
4
+ * Parses GIR files to extract type information for GTK/GLib libraries.
5
+ * Outputs raw GIR types that are then normalized by the normalizer.
6
+ *
7
+ * @internal
8
+ */
9
+ import type { RawNamespace } from "./raw-types.js";
10
+ /**
11
+ * Parser for GObject Introspection XML (GIR) files.
12
+ *
13
+ * Converts GIR XML into raw TypeScript objects.
14
+ */
15
+ export declare class RawGirParser {
16
+ private parser;
17
+ constructor();
18
+ /**
19
+ * Parses a GIR XML string into a raw namespace object.
20
+ */
21
+ parse(girXml: string): RawNamespace;
22
+ private parseCallbacks;
23
+ private parseClasses;
24
+ private parseImplements;
25
+ private parseInterfaces;
26
+ private parsePrerequisites;
27
+ private parseMethods;
28
+ private parseConstructors;
29
+ private parseFunctions;
30
+ private parseParameters;
31
+ private parseReturnType;
32
+ private parseType;
33
+ private extractTypeParameters;
34
+ private parseGLibContainerType;
35
+ private parseProperties;
36
+ private parseSignals;
37
+ private parseRecords;
38
+ private parseFields;
39
+ private parseEnumerations;
40
+ private parseEnumerationMembers;
41
+ private parseConstants;
42
+ }