object.js
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"use strict";
module.exports = ReflectionObject;
ReflectionObject.className = "ReflectionObject";
var util = require("./util");
var Root; // cyclic
/**
* Constructs a new reflection object instance.
* @classdesc Base class of all reflection objects.
* @constructor
* @param {string} name Object name
* @param {Object.<string,*>} [options] Declared options
* @abstract
*/
function ReflectionObject(name, options) {
if (!util.isString(name))
throw TypeError("name must be a string");
if (options && !util.isObject(options))
throw TypeError("options must be an object");
/**
* Options.
* @type {Object.<string,*>|undefined}
*/
this.options = options; // toJSON
/**
* Unique name within its namespace.
* @type {string}
*/
this.name = name;
/**
* Parent namespace.
* @type {Namespace|null}
*/
this.parent = null;
/**
* Whether already resolved or not.
* @type {boolean}
*/
this.resolved = false;
/**
* Comment text, if any.
* @type {string|null}
*/
this.comment = null;
/**
* Defining file name.
* @type {string|null}
*/
this.filename = null;
}
Object.defineProperties(ReflectionObject.prototype, {
/**
* Reference to the root namespace.
* @name ReflectionObject#root
* @type {Root}
* @readonly
*/
root: {
get: function() {
var ptr = this;
while (ptr.parent !== null)
ptr = ptr.parent;
return ptr;
}
},
/**
* Full name including leading dot.
* @name ReflectionObject#fullName
* @type {string}
* @readonly
*/
fullName: {
get: function() {
var path = [ this.name ],
ptr = this.parent;
while (ptr) {
path.unshift(ptr.name);
ptr = ptr.parent;
}
return path.join(".");
}
}
});
/**
* Converts this reflection object to its descriptor representation.
* @returns {Object.<string,*>} Descriptor
* @abstract
*/
ReflectionObject.prototype.toJSON = /* istanbul ignore next */ function toJSON() {
throw Error(); // not implemented, shouldn't happen
};
/**
* Called when this object is added to a parent.
* @param {ReflectionObject} parent Parent added to
* @returns {undefined}
*/
ReflectionObject.prototype.onAdd = function onAdd(parent) {
if (this.parent && this.parent !== parent)
this.parent.remove(this);
this.parent = parent;
this.resolved = false;
var root = parent.root;
if (root instanceof Root)
root._handleAdd(this);
};
/**
* Called when this object is removed from a parent.
* @param {ReflectionObject} parent Parent removed from
* @returns {undefined}
*/
ReflectionObject.prototype.onRemove = function onRemove(parent) {
var root = parent.root;
if (root instanceof Root)
root._handleRemove(this);
this.parent = null;
this.resolved = false;
};
/**
* Resolves this objects type references.
* @returns {ReflectionObject} `this`
*/
ReflectionObject.prototype.resolve = function resolve() {
if (this.resolved)
return this;
if (this.root instanceof Root)
this.resolved = true; // only if part of a root
return this;
};
/**
* Gets an option value.
* @param {string} name Option name
* @returns {*} Option value or `undefined` if not set
*/
ReflectionObject.prototype.getOption = function getOption(name) {
if (this.options)
return this.options[name];
return undefined;
};
/**
* Sets an option.
* @param {string} name Option name
* @param {*} value Option value
* @param {boolean} [ifNotSet] Sets the option only if it isn't currently set
* @returns {ReflectionObject} `this`
*/
ReflectionObject.prototype.setOption = function setOption(name, value, ifNotSet) {
if (!ifNotSet || !this.options || this.options[name] === undefined)
(this.options || (this.options = {}))[name] = value;
return this;
};
/**
* Sets multiple options.
* @param {Object.<string,*>} options Options to set
* @param {boolean} [ifNotSet] Sets an option only if it isn't currently set
* @returns {ReflectionObject} `this`
*/
ReflectionObject.prototype.setOptions = function setOptions(options, ifNotSet) {
if (options)
for (var keys = Object.keys(options), i = 0; i < keys.length; ++i)
this.setOption(keys[i], options[keys[i]], ifNotSet);
return this;
};
/**
* Converts this instance to its string representation.
* @returns {string} Class name[, space, full name]
*/
ReflectionObject.prototype.toString = function toString() {
var className = this.constructor.className,
fullName = this.fullName;
if (fullName.length)
return className + " " + fullName;
return className;
};
// Sets up cyclic dependencies (called in index-light)
ReflectionObject._configure = function(Root_) {
Root = Root_;
};