From ed4038b1b6b28011f0cb75d84b052ec435eb6a99 Mon Sep 17 00:00:00 2001 From: Evan Welsh Date: Mon, 26 Feb 2024 17:05:43 -0800 Subject: [PATCH] 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,