classReflectionObject{ name: string // unique name within its namespace options: object parent: string | null = null// parent namespace resolved: boolean = false// whether already resolved or not comment: string | null = null// comment text, if any filename: string | null = null// defining file name
constructor(name: string, options: object) { if (!isString(name)) thrownewTypeError('name must be a string') this.name = name if (!isObject(options)) thrownewTypeError('options must be an object') this.options = options }
/** * @property fullName * @description full name including leading dot. */ fullName:{ get() { let path = [this.name] let ptr = this.parent while(ptr) { path.unshift(ptr.name) ptr = ptr.parent } return path.join('.') } }
/** * @method toJSON * @description Converts this reflection object to its descriptor representation */ toJSON() { throwError() // not implemented, shouldn't happen }
/** * @method onAdd * @description called when this object is added to a parent */ onAdd(parent) { if (this.parent && this.parent !== parent) { this.parent.remove(this) } this.parent = parent this.resolved = false let root = parent.root if (root instanceof Root) { root._handleAdd(this) } }
/** * @method onRemove * @description called when this object is removed from a parent */ onRemove(parent) { let root = parent.root if (root instanceof Root) { root._handleRemove(this) } this.parent = null this.resolved = false }
/** * @method resolve * @description resovles this objects type references */ resolve() { if (this.resolved) { returnthis } if (this.root instanceof Root) { this.resolved = true } returnthis }
/** * @method getOption * @description gets an option value */ getOption(name) { if (this.options) { returnthis.options[name] } returnundefined }
/** * @method set an option */ setOption(name, value, ifNotSet) { if (!ifNotSet || !this.options || this.options[name]===undefined) { (this.options || this.options = {})[name] = valeu } returnthis }
/** * @mehtod fromJSON * @description construct a namespace from JSON */ static fromJSON(name, json) { returnnew Namespace(name, json.options).addJSON(json.nested) }
/** * @mehtod isReservedId * @description test if the specified id is reserved */ static isReservedId(reserved, id) { if (reserved) { for (let i = 0; i < reserved.length; ++i) { if (typeof reserved[i] !== 'string' && reserved[i][0] <= id && reserved[i][1] > id) { returntrue } } } returnfalse }
/** * @method isReservedName * @description test if the specified name is reserved */ isReservedName(reserved, name) { if (reserved) { for (let i = 0; i < reserved.length; ++i) { if (reserved[i] === name) { returntrue } } } returnfalse }