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-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.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..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; @@ -412,14 +414,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(); } } } 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, 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, + }, + }, +}