Skip to content

Commit

Permalink
Merge branch 'gi-ts-rework' into gi-ts-resolve-conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpLink committed Feb 27, 2024
2 parents a39a3f5 + ad22fa3 commit 9ad55eb
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,11 @@ class ModuleGenerator extends FormatGenerator<string[]> {
}
}

// TODO: Check if this is necessary anywhere else...
if (isInvalid(name) && !isGlobal) {
name = `["${name}"]`
}

const inParamsDef: string[] = this.generateInParameters(inParams)

def.push(
Expand All @@ -692,7 +697,7 @@ class ModuleGenerator extends FormatGenerator<string[]> {
)})${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) {
Expand Down
1 change: 0 additions & 1 deletion packages/lib/src/gir-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
10 changes: 10 additions & 0 deletions packages/lib/src/gir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions packages/lib/src/gir/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
Expand Down
32 changes: 31 additions & 1 deletion packages/lib/src/validators/class.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -139,6 +139,35 @@ const removeComplexFields = <T extends IntrospectedBaseClass>(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 = <T extends IntrospectedBaseClass>(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 = <T extends IntrospectedBaseClass>(node: T): T => {
node.fields = node.fields.filter(f => {
return !f.isPrivate && !f.name.startsWith("_");
Expand Down Expand Up @@ -232,6 +261,7 @@ export class ClassVisitor extends GirVisitor {
visitClass = (node: IntrospectedClass) =>
chainVisitors(
node,
removeReferencesToMissingLibraries,
fixMissingParent,
fixParamSpecSubtypes,
removeComplexFields,
Expand Down
38 changes: 38 additions & 0 deletions ts-for-gir.code-workspace
Original file line number Diff line number Diff line change
@@ -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,
},
},
}

0 comments on commit 9ad55eb

Please sign in to comment.