From 6f71161a76c232b3f28740cad0b925526b15c7f8 Mon Sep 17 00:00:00 2001 From: Evan Welsh Date: Mon, 26 Feb 2024 17:03:33 -0800 Subject: [PATCH 1/4] vscode: Add a workspace definition file --- ts-for-gir.code-workspace | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 ts-for-gir.code-workspace diff --git a/ts-for-gir.code-workspace b/ts-for-gir.code-workspace new file mode 100644 index 000000000..a223454db --- /dev/null +++ b/ts-for-gir.code-workspace @@ -0,0 +1,38 @@ +{ + "folders": [ + { + "path": ".", + }, + { + "path": "packages/parser", + }, + { + "path": "packages/lib", + }, + { + "path": "packages/cli", + }, + { + "path": "packages/generator-base", + }, + { + "path": "packages/generator-typescript", + }, + { + "path": "packages/generator-html-doc", + }, + ], + "settings": { + "typescript.enablePromptUseWorkspaceTsdk": true, + "typescript.tsdk": ".yarn/sdks/typescript/lib", + "files.exclude": { + "**/.git": true, + "**/.svn": true, + "**/.hg": true, + "**/CVS": true, + "**/.DS_Store": true, + "**/Thumbs.db": true, + "packages/**": true, + }, + }, +} From 78a5a871c88788af5bc858083cc5adcb1cc7f222 Mon Sep 17 00:00:00 2001 From: Evan Welsh Date: Mon, 26 Feb 2024 17:05:14 -0800 Subject: [PATCH 2/4] typescript: Fix invalid identifier names in functions and interfaces --- .../src/type-definition-generator.ts | 7 ++++++- packages/lib/src/gir.ts | 10 ++++++++++ packages/lib/src/gir/util.ts | 4 ++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/generator-typescript/src/type-definition-generator.ts b/packages/generator-typescript/src/type-definition-generator.ts index 2e597473a..347183a95 100644 --- a/packages/generator-typescript/src/type-definition-generator.ts +++ b/packages/generator-typescript/src/type-definition-generator.ts @@ -684,6 +684,11 @@ class ModuleGenerator extends FormatGenerator { } } + // TODO: Check if this is necessary anywhere else... + if (isInvalid(name) && !isGlobal) { + name = `["${name}"]` + } + const inParamsDef: string[] = this.generateInParameters(inParams) def.push( @@ -692,7 +697,7 @@ class ModuleGenerator extends FormatGenerator { )})${retSep} ${returnType}`, ) - // Add overloaded methods + // TODO: Add overloaded methods // if (overloads && tsFunction.overloads.length > 0) { // def.push(...this.addInfoComment(`Overloads of ${name}`, indentCount)) // for (const func of tsFunction.overloads) { diff --git a/packages/lib/src/gir.ts b/packages/lib/src/gir.ts index b1c0ef116..3ebe3d12c 100644 --- a/packages/lib/src/gir.ts +++ b/packages/lib/src/gir.ts @@ -3,6 +3,8 @@ import { IntrospectedProperty, IntrospectedField } from './gir/property.js' import { GenerationOptions } from './types.js' import { sanitizeIdentifierName } from './gir/util.js' +export { sanitizeMemberName, isInvalid } from './gir/util.js' + export { IntrospectedBase, Options as IntrospectedOptions, @@ -57,6 +59,14 @@ export class TypeIdentifier extends TypeExpression { return type } + /** + * TODO: gi.ts didn't deal with sanitizing types but probably should have to avoid + * invalid names such as "3gppProfile" + */ + sanitize() { + return new TypeIdentifier(sanitizeIdentifierName(this.namespace, this.name), this.namespace) + } + protected _resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeIdentifier | null { const name: string = sanitizeIdentifierName(null, this.name) const ns_name = this.namespace diff --git a/packages/lib/src/gir/util.ts b/packages/lib/src/gir/util.ts index 04bcb0dee..0a399d717 100644 --- a/packages/lib/src/gir/util.ts +++ b/packages/lib/src/gir/util.ts @@ -412,14 +412,14 @@ export function parseTypeExpression(modName: string, type: string): TypeExpressi const baseType = parseTypeString(type); if (baseType.namespace) { - return new TypeIdentifier(baseType.name, baseType.namespace); + return new TypeIdentifier(baseType.name, baseType.namespace).sanitize(); } else { const primitiveType = resolvePrimitiveType(baseType.name); if (primitiveType !== null) { return primitiveType; } else { - return new TypeIdentifier(baseType.name, modName); + return new TypeIdentifier(baseType.name, modName).sanitize(); } } } From ed4038b1b6b28011f0cb75d84b052ec435eb6a99 Mon Sep 17 00:00:00 2001 From: Evan Welsh Date: Mon, 26 Feb 2024 17:05:43 -0800 Subject: [PATCH 3/4] lib: Any-ify references to libraries that are not found --- packages/lib/src/validators/class.ts | 32 +++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/lib/src/validators/class.ts b/packages/lib/src/validators/class.ts index 9c735c318..6f7b49eb4 100644 --- a/packages/lib/src/validators/class.ts +++ b/packages/lib/src/validators/class.ts @@ -1,4 +1,4 @@ -import { NativeType, TypeIdentifier } from "../gir.js"; +import { AnyType, NativeType, TypeIdentifier } from "../gir.js"; import { IntrospectedBaseClass, IntrospectedClass, IntrospectedInterface, IntrospectedRecord } from "../gir/class.js"; import { IntrospectedError } from "../gir/enum.js"; import { @@ -139,6 +139,35 @@ const removeComplexFields = (node: T): T => { return node; }; +/** + * TODO: Consider making this transformation optional. + * + * If we are referencing an unknown library, any-ify the type. + * + * @param node + */ +const removeReferencesToMissingLibraries = (node: T): T => { + const { namespace } = node; + + node.fields = node.fields.map(f => { + const type = f.type.deepUnwrap(); + + if (type instanceof TypeIdentifier) { + // Find the type for the identifier + const nsNode = namespace.getInstalledImport(type.namespace); + + // Don't allow private or disguised fields + if (!nsNode) { + return f.copy({ type: AnyType }); + } + } + + return f; + }); + + return node; +}; + const removePrivateFields = (node: T): T => { node.fields = node.fields.filter(f => { return !f.isPrivate && !f.name.startsWith("_"); @@ -232,6 +261,7 @@ export class ClassVisitor extends GirVisitor { visitClass = (node: IntrospectedClass) => chainVisitors( node, + removeReferencesToMissingLibraries, fixMissingParent, fixParamSpecSubtypes, removeComplexFields, From ad22fa32f05206ad48a6150b87faab03d424e884 Mon Sep 17 00:00:00 2001 From: Evan Welsh Date: Mon, 26 Feb 2024 17:06:59 -0800 Subject: [PATCH 4/4] lib: Fix parsing for GList and GList --- packages/lib/src/gir-module.ts | 1 - packages/lib/src/gir/util.ts | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/lib/src/gir-module.ts b/packages/lib/src/gir-module.ts index 29d0c49af..2c6edb637 100644 --- a/packages/lib/src/gir-module.ts +++ b/packages/lib/src/gir-module.ts @@ -1377,7 +1377,6 @@ export class GirModule { ?.filter(isIntrospectable) // Avoid attempting to alias non-introspectable symbols. .map((b) => { - console.debug('b', b) b.type = b.type ?.filter((t): t is NamedType => !!(t && t.$.name)) .map((t) => { diff --git a/packages/lib/src/gir/util.ts b/packages/lib/src/gir/util.ts index 0a399d717..b8d831051 100644 --- a/packages/lib/src/gir/util.ts +++ b/packages/lib/src/gir/util.ts @@ -249,7 +249,9 @@ export function getType( if (variableType instanceof TypeIdentifier) { if (variableType.is("GLib", "List") || variableType.is("GLib", "SList")) { - const listType = parameter?.type?.[0].type?.[0]?.$.name; + // TODO: $?.name was not necessary in gi.ts, but TelepathyLogger + // fails to generate now. + const listType = parameter?.type?.[0].type?.[0]?.$?.name; if (listType) { name = listType;