@ts-for-gir/lib 4.0.0-rc.15 → 4.0.0-rc.17

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/package.json CHANGED
@@ -1,60 +1,60 @@
1
1
  {
2
- "name": "@ts-for-gir/lib",
3
- "version": "4.0.0-rc.15",
4
- "description": "Typescript .d.ts generator from GIR for gjs",
5
- "main": "src/index.ts",
6
- "module": "src/index.ts",
7
- "type": "module",
8
- "engines": {
9
- "node": ">=18"
10
- },
11
- "exports": {
12
- ".": "./src/index.ts"
13
- },
14
- "scripts": {
15
- "check": "tsc --noEmit"
16
- },
17
- "repository": {
18
- "type": "git",
19
- "url": "git+https://github.com/gjsify/ts-for-gir.git"
20
- },
21
- "author": "Pascal Garber <pascal@mailfreun.de>",
22
- "files": [
23
- "src"
24
- ],
25
- "license": "Apache-2.0",
26
- "bugs": {
27
- "url": "https://github.com/gjsify/ts-for-gir/issues"
28
- },
29
- "homepage": "https://github.com/gjsify/ts-for-gir#readme",
30
- "keywords": [
31
- "gjs",
32
- "typescript",
33
- "generate",
34
- "gir",
35
- "gobject-introspection",
36
- "gnome",
37
- "gtk",
38
- "glib",
39
- "gobject",
40
- "dts",
41
- "type definitions"
42
- ],
43
- "devDependencies": {
44
- "@ts-for-gir/tsconfig": "^4.0.0-rc.15",
45
- "@types/ejs": "^3.1.5",
46
- "@types/lodash": "^4.17.24",
47
- "@types/node": "^25.6.2",
48
- "rimraf": "^6.1.3",
49
- "typescript": "^6.0.3"
50
- },
51
- "dependencies": {
52
- "@gi.ts/parser": "^4.0.0-rc.15",
53
- "@ts-for-gir/reporter": "^4.0.0-rc.15",
54
- "@ts-for-gir/templates": "^4.0.0-rc.15",
55
- "colorette": "^2.0.20",
56
- "ejs": "^5.0.2",
57
- "glob": "^13.0.6",
58
- "lodash": "4.18.1"
59
- }
60
- }
2
+ "name": "@ts-for-gir/lib",
3
+ "version": "4.0.0-rc.17",
4
+ "description": "Typescript .d.ts generator from GIR for gjs",
5
+ "main": "src/index.ts",
6
+ "module": "src/index.ts",
7
+ "type": "module",
8
+ "engines": {
9
+ "node": ">=18"
10
+ },
11
+ "exports": {
12
+ ".": "./src/index.ts"
13
+ },
14
+ "scripts": {
15
+ "check": "tsc --noEmit"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/gjsify/ts-for-gir.git"
20
+ },
21
+ "author": "Pascal Garber <pascal@mailfreun.de>",
22
+ "files": [
23
+ "src"
24
+ ],
25
+ "license": "Apache-2.0",
26
+ "bugs": {
27
+ "url": "https://github.com/gjsify/ts-for-gir/issues"
28
+ },
29
+ "homepage": "https://github.com/gjsify/ts-for-gir#readme",
30
+ "keywords": [
31
+ "gjs",
32
+ "typescript",
33
+ "generate",
34
+ "gir",
35
+ "gobject-introspection",
36
+ "gnome",
37
+ "gtk",
38
+ "glib",
39
+ "gobject",
40
+ "dts",
41
+ "type definitions"
42
+ ],
43
+ "devDependencies": {
44
+ "@ts-for-gir/tsconfig": "^4.0.0-rc.17",
45
+ "@types/ejs": "^3.1.5",
46
+ "@types/lodash": "^4.17.24",
47
+ "@types/node": "^25.6.2",
48
+ "rimraf": "^6.1.3",
49
+ "typescript": "^6.0.3"
50
+ },
51
+ "dependencies": {
52
+ "@gi.ts/parser": "^4.0.0-rc.17",
53
+ "@ts-for-gir/reporter": "^4.0.0-rc.17",
54
+ "@ts-for-gir/templates": "^4.0.0-rc.17",
55
+ "colorette": "^2.0.20",
56
+ "ejs": "^5.0.2",
57
+ "glob": "^13.0.6",
58
+ "lodash": "4.18.1"
59
+ }
60
+ }
@@ -1,4 +1,4 @@
1
- import { BinaryType, BooleanType, ClosureType, PromiseType, TupleType, TypeIdentifier, VoidType } from "../gir.ts";
1
+ import { BooleanType, ClosureType, makeUnion, PromiseType, TupleType, TypeIdentifier, VoidType } from "../gir.ts";
2
2
 
3
3
  import type { GirModule } from "../gir-module.ts";
4
4
  import type { IntrospectedAlias } from "./alias.ts";
@@ -65,7 +65,7 @@ export function promisifyNamespaceFunctions(namespace: GirModule) {
65
65
  parameters: sync_parameters,
66
66
  }),
67
67
  node.copy({
68
- return_type: new BinaryType(async_return, node.return()),
68
+ return_type: makeUnion(async_return, node.return()),
69
69
  }),
70
70
  ]);
71
71
  }
@@ -1,4 +1,4 @@
1
- import { BinaryType, BooleanType, ClosureType, PromiseType, TupleType, TypeIdentifier, VoidType } from "../gir.ts";
1
+ import { BooleanType, ClosureType, makeUnion, PromiseType, TupleType, TypeIdentifier, VoidType } from "../gir.ts";
2
2
  import { IntrospectedConstructor } from "./constructor.ts";
3
3
  import type { IntrospectedClassFunction } from "./introspected-classes.ts";
4
4
  import {
@@ -42,7 +42,7 @@ function generatePromisifyOverloadedSignatures(
42
42
  // Union overload (with optional callback)
43
43
  const unionOverload = node.copy({
44
44
  parameters: [...async_parameters, sync_parameters[sync_parameters.length - 1].copy({ isOptional: true })],
45
- returnType: new BinaryType(async_return, VoidType),
45
+ returnType: makeUnion(async_return, VoidType),
46
46
  });
47
47
 
48
48
  return [promiseOverload, callbackOverload, unionOverload];
package/src/gir.ts CHANGED
@@ -348,9 +348,9 @@ export class NativeType extends TypeExpression {
348
348
  export class OrType extends TypeExpression {
349
349
  readonly types: ReadonlyArray<TypeExpression>;
350
350
 
351
- constructor(type: TypeExpression, ...types: TypeExpression[]) {
351
+ constructor(...types: TypeExpression[]) {
352
352
  super();
353
- this.types = [type, ...types];
353
+ this.types = [...types];
354
354
  }
355
355
 
356
356
  rewrap(type: TypeExpression): TypeExpression {
@@ -362,17 +362,15 @@ export class OrType extends TypeExpression {
362
362
  }
363
363
 
364
364
  resolve(namespace: IntrospectedNamespace, options: OptionsGeneration): TypeExpression {
365
- const [type, ...types] = this.types;
366
-
367
- return new OrType(type.resolve(namespace, options), ...types.map((t) => t.resolve(namespace, options)));
365
+ return makeUnion(...this.types.map((t) => t.resolve(namespace, options)));
368
366
  }
369
367
 
370
368
  print(namespace: IntrospectedNamespace, options: OptionsGeneration): string {
371
- return `(${this.types.map((t) => t.print(namespace, options)).join(" | ")})`;
369
+ return `${this.types.map((t) => t.print(namespace, options)).join(" | ")}`;
372
370
  }
373
371
 
374
372
  rootPrint(namespace: IntrospectedNamespace, options: OptionsGeneration): string {
375
- return `${this.types.map((t) => t.print(namespace, options)).join(" | ")}`;
373
+ return this.print(namespace, options);
376
374
  }
377
375
 
378
376
  equals(type: TypeExpression) {
@@ -384,6 +382,36 @@ export class OrType extends TypeExpression {
384
382
  }
385
383
  }
386
384
 
385
+ export function makeUnion(...inputTypes: TypeExpression[]) {
386
+ const types: Set<TypeExpression> = new Set();
387
+ for (const type of inputTypes) {
388
+ if (type instanceof BinaryType) {
389
+ types.add(type.a);
390
+ types.add(type.b);
391
+ } else if (type instanceof OrType && !(type instanceof TupleType)) {
392
+ for (const t of type.types) {
393
+ types.add(t);
394
+ }
395
+ } else {
396
+ types.add(type);
397
+ }
398
+ }
399
+ if (types.size === 1) {
400
+ return [...types][0];
401
+ }
402
+ if (types.size === 2) {
403
+ const typesArray = [...types];
404
+ if (typesArray[0] === NullType) {
405
+ return new NullableType(typesArray[1]);
406
+ }
407
+ if (typesArray[1] === NullType) {
408
+ return new NullableType(typesArray[0]);
409
+ }
410
+ return new BinaryType(...typesArray);
411
+ }
412
+ return new OrType(...types);
413
+ }
414
+
387
415
  export class TupleType extends OrType {
388
416
  print(namespace: IntrospectedNamespace, options: OptionsGeneration): string {
389
417
  return `[${this.types.map((t) => t.print(namespace, options)).join(", ")}]`;
@@ -633,9 +661,10 @@ export class NullableType extends BinaryType {
633
661
  }
634
662
 
635
663
  export function makeNullable(type: TypeExpression) {
664
+ if (type instanceof NullableType) return type;
636
665
  if (type === RawPointerType) return NullType;
637
666
  if (type === AnyType) return AnyType;
638
- return new NullableType(type);
667
+ return makeUnion(type, NullType);
639
668
  }
640
669
 
641
670
  export class PromiseType extends TypeExpression {
@@ -834,6 +863,8 @@ export class ArrayType extends TypeExpression {
834
863
  typeSuffix = "".padStart(2 * depth, "[]");
835
864
  }
836
865
 
866
+ if (this.type instanceof OrType && !(this.type instanceof TupleType))
867
+ return `(${this.type.print(namespace, options)})${typeSuffix}`;
837
868
  return `${this.type.print(namespace, options)}${typeSuffix}`;
838
869
  }
839
870
 
@@ -1,6 +1,6 @@
1
1
  import type { IntrospectedNamespace } from "../gir/namespace.ts";
2
2
  import type { NSRegistry } from "../gir/registry.ts";
3
- import { NullableType, OrType, TypeIdentifier } from "../gir.ts";
3
+ import { makeUnion, NullType, TypeIdentifier } from "../gir.ts";
4
4
 
5
5
  const shellTemplate = (version: string) => ({
6
6
  namespace: "Shell",
@@ -24,8 +24,10 @@ const shellTemplate = (version: string) => ({
24
24
  if (addGlslSnippet) {
25
25
  // Create a new parameter with updated type using copy()
26
26
  const updatedParameter = addGlslSnippet.parameters[0].copy({
27
- type: new NullableType(
28
- new OrType(new TypeIdentifier("SnippetHook", "Shell"), new TypeIdentifier("SnippetHook", "Cogl")),
27
+ type: makeUnion(
28
+ new TypeIdentifier("SnippetHook", "Shell"),
29
+ new TypeIdentifier("SnippetHook", "Cogl"),
30
+ NullType,
29
31
  ),
30
32
  });
31
33
 
@@ -7,18 +7,17 @@ import {
7
7
  BigintOrNumberType,
8
8
  BinaryType,
9
9
  BooleanType,
10
+ makeUnion,
10
11
  NativeType,
11
12
  NeverType,
12
13
  NullableType,
13
14
  NullType,
14
15
  NumberType,
15
16
  ObjectType,
16
- OrType,
17
17
  PromiseType,
18
18
  RawPointerType,
19
19
  StringType,
20
20
  ThisType,
21
- TupleType,
22
21
  type TypeExpression,
23
22
  TypeIdentifier,
24
23
  Uint8ArrayType,
@@ -231,17 +230,17 @@ export function resolveDirectedType(type: TypeExpression, direction: GirDirectio
231
230
  type.type.equals(Uint8ArrayType) &&
232
231
  type.arrayDepth === 0
233
232
  ) {
234
- return new BinaryType(type, StringType);
233
+ return makeUnion(type, StringType);
235
234
  } else {
236
235
  // Rewrap arrays if they have directional types
237
236
  return type.rewrap(resolveDirectedType(type.type, direction) ?? type.type);
238
237
  }
239
238
  } else if (type instanceof TypeIdentifier) {
240
239
  if ((direction === GirDirection.In || direction === GirDirection.Inout) && type.is("GLib", "Bytes")) {
241
- return new BinaryType(type, Uint8ArrayType);
240
+ return makeUnion(type, Uint8ArrayType);
242
241
  } else if (type.is("GObject", "Value")) {
243
242
  if (direction === GirDirection.In || direction === GirDirection.Inout) {
244
- return new BinaryType(type, AnyType);
243
+ return makeUnion(type, AnyType);
245
244
  } else {
246
245
  // GJS converts GObject.Value out parameters to their unboxed type, which we don't know,
247
246
  // so type as `unknown`
@@ -273,12 +272,7 @@ export function resolveDirectedType(type: TypeExpression, direction: GirDirectio
273
272
  // NullableType is skipped to preserve its subclass behaviour.
274
273
  const a = resolveDirectedType(type.a, direction) ?? type.a;
275
274
  const b = resolveDirectedType(type.b, direction) ?? type.b;
276
- if (a !== type.a || b !== type.b) return new BinaryType(a, b);
277
- } else if (type instanceof OrType && !(type instanceof BinaryType || type instanceof TupleType)) {
278
- // flatten "bigint | number" out of another OR-type
279
- const types = type.types.map((t) => resolveDirectedType(t, direction) ?? t);
280
- if (types.length === 1) return types[0];
281
- return new OrType(types[0], ...types.slice(1));
275
+ if (a !== type.a || b !== type.b) return makeUnion(a, b);
282
276
  }
283
277
 
284
278
  return null;