@ts-for-gir/lib
Version:
Typescript .d.ts generator from GIR for gjs
128 lines • 4.27 kB
JavaScript
import { IntrospectedBase, IntrospectedClassMember } from "./base.js";
import { getType, parseDoc, parseMetadata } from "./util.js";
import { isIntrospectable } from "./namespace.js";
export class IntrospectedField extends IntrospectedClassMember {
type;
// TODO: Make these properties readonly
optional = false;
computed;
isStatic;
writable;
isNative = false;
copy(options) {
const { type, name, parent, optional, computed, isStatic, writable } = this;
return new IntrospectedField({
name,
parent,
type: options?.type ?? type,
optional,
computed,
isStatic: options?.isStatic ?? isStatic,
writable
})._copyBaseProperties(this);
}
constructor({ name, parent, type, computed = false, optional = false, isStatic = false, writable = true, ...args }) {
super(name, parent ?? null, { ...args });
this.type = type;
this.computed = computed;
this.optional = optional;
this.isStatic = isStatic;
this.writable = writable;
}
asString(generator) {
return generator.generateField(this);
}
accept(visitor) {
const node = this.copy({
type: visitor.visitType?.(this.type) ?? this.type
});
return visitor.visitField?.(node) ?? node;
}
static fromXML(field, parent) {
const namespace = parent.namespace;
const name = field.$["name"];
const _name = name.replace(/[-]/g, "_");
const f = new IntrospectedField({
name: _name,
parent,
type: getType(namespace, field),
isPrivate: field.$.private === "1",
isIntrospectable: isIntrospectable(field)
});
return f;
}
}
export class JSField extends IntrospectedField {
isNative = true;
}
export class IntrospectedProperty extends IntrospectedBase {
type;
writable = false;
readable = true;
constructOnly;
get namespace() {
return this.parent.namespace;
}
copy(options) {
const { name, writable, readable, type, constructOnly, parent } = this;
return new IntrospectedProperty({
name: options?.name ?? name,
writable,
readable,
type: options?.type ?? type,
constructOnly,
parent: options?.parent ?? parent
})._copyBaseProperties(this);
}
accept(visitor) {
const node = this.copy({
parent: this.parent,
type: visitor.visitType?.(this.type) ?? this.type
});
return visitor.visitProperty?.(node) ?? node;
}
constructor({ name, type, writable, readable, constructOnly, parent, ...args }) {
super(name, parent, { ...args });
this.type = type;
this.writable = writable;
this.readable = readable;
this.constructOnly = constructOnly;
}
asString(generator, construct) {
return generator.generateProperty(this, construct);
}
toCamelCase() {
const [part, ...parts] = this.name.split("_");
if (parts.length === 0) {
return this.copy({
name: part,
parent: this.parent
});
}
const camelCase = `${part}${parts.map(s => `${s[0].toUpperCase()}${s.slice(1)}`).join("")}`;
return this.copy({
name: camelCase,
parent: this.parent
});
}
static fromXML(element, parent, options) {
const ns = parent.namespace;
const name = element.$["name"];
const _name = name.replace(/[-]/g, "_");
const property = new IntrospectedProperty({
name: _name,
writable: element.$?.writable === "1",
readable: element.$?.readable !== "0",
constructOnly: element.$?.["construct-only"] === "1",
type: getType(ns, element),
parent,
isIntrospectable: isIntrospectable(element)
});
if (options.loadDocs) {
property.doc = parseDoc(element);
property.metadata = parseMetadata(element);
}
return property;
}
}
//# sourceMappingURL=property.js.map