Issues (204)

src/Reflection.js (30 issues)

1
/* Javascript Object Inheritance Implementation                ______  ________
2
 * (c) 2016 <[email protected]>                             __ / / __ \/  _/  _/
3
 * Licensed under MIT.                                    / // / /_/ // /_/ /
4
 * ------------------------------------------------------ \___/\____/___/__*/
5
6
// Register JOII 'namespace'.
7
JOII = typeof (JOII) !== 'undefined' ? JOII : {};
8
JOII.Reflection = {};
9
10
/**
11
 * ReflectionClass
12
 *
13
 * Retrieves and presents meta information about the given class.
14
 *
15
 * API / Usage:
16
 *      var r = new JOII.Reflection.Class(MyClass);
17
 *      r.getMethods();      - Returns array of JOII.Reflection.Method
18
 *      r.getMethod(name);   - Returns JOII.Reflection.Method
19
 *      r.getProperties();   - Returns array of JOII.Reflection.Property
20
 *      r.getProperty(name); - Returns JOII.Reflection.Property
21
 *      r.isFinal()          - Returns true if the class is final
22
 *      r.hasParent()        - Returns true if the class has a parent
23
 *      r.getParent()        - Returns JOII.Reflection.Class of the parent
24
 */
25
JOII.Reflection.Class = JOII.ClassBuilder({}, {
26
27
    /**
28
     * Contains the __joii__ metadata object.
29
     *
30
     * @var object
31
     */
32
    'protected immutable object meta': null,
33
34
    /**
35
     * Contains the prototype of the class.
36
     *
37
     * @var object
38
     */
39
    'protected immutable object proto': null,
40
41
    /**
42
     * Represents the Reflection.Class instance of the parent definition.
43
     *
44
     * @var JOII.Reflection.Class
45
     */
46
    'public immutable object parent': null,
47
48
    /**
49
     * Constructor
50
     *
51
     * @param {Function} definition
52
     */
53
    'protected __construct': function(definition) {
54
55
        if (typeof (definition) === 'function') {
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
56
            definition = definition.prototype;
57
        }
58
59
        // Is the passed argument an actual JOII class?
60
        if (typeof (definition) !== 'object' ||
61
            typeof (definition.__joii__) !== 'object') {
62
            throw 'Reflection.Class requires a JOII-created definition.';
63
        }
64
65
        this.proto = definition;
66
        this.meta = definition.__joii__;
67
68
        // Does the class definition have a parent?
69
        if (typeof (this.meta.parent) !== 'undefined') {
70
            this.parent = new JOII.Reflection.Class(this.meta.parent);
71
        }
72
    },
73
74
    /**
75
     * Returns the name of the class.
76
     *
77
     * @return string
78
     */
79
    'public getName': function() {
80
        return this.meta.name;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
81
    },
82
83
    /**
84
     * Returns true if the class is marked as abstract.
85
     *
86
     * @return bool
87
     */
88
    'public isAbstract': function() {
89
        return this.meta.is_abstract === true;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
90
    },
91
    
92
    /**
93
     * Returns true if the property is static.
94
     *
95
     * @return bool
96
     */
97
    'public isStatic': function() {
98
        return this.meta.is_static;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
99
    },
100
101
    /**
102
     * Returns true if a property by the given name exists.
103
     *
104
     * @return bool
105
     */
106
    'public hasProperty': function(name) {
107
        var list = this.getProperties();
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
108
        for (var i in list) {
109
            if (list[i].getName() === name) {
110
                return true;
111
            }
112
        }
113
        return false;
114
    },
115
116
    /**
117
     * Returns true if the class being reflected has a parent class.
118
     *
119
     * @return bool
120
     */
121
    'public hasParent': function() {
122
        return this.parent !== null;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
123
    },
124
125
    /**
126
     * Returns the reflector of the parent class.
127
     *
128
     * @return JOII.Reflection.Class
129
     */
130
    'public getParent': function() {
131
        return this.parent;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
132
    },
133
134
    /**
135
     * Returns an array of JOII.Reflection.Method based on the methods
136
     * defined in this class.
137
     *
138
     * @param  string filter Optional filter for 'private' or 'public'.
139
     * @return JOII.Reflection.Method[]
140
     */
141
    'public getMethods': function(filter) {
142
        var result = [];
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
143
        for (var i in this.proto) {
144
            if (typeof (this.proto[i]) === 'function' && JOII.Compat.indexOf(JOII.InternalPropertyNames, i) === -1) {
145
                result.push(new JOII.Reflection.Method(this, i));
146
            }
147
        }
148
        return result;
149
    },
150
151
    /**
152
     * Returns true if a method by the given name exists.
153
     *
154
     * @return bool
155
     */
156
    'public hasMethod': function(name) {
157
        var list = this.getMethods();
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
158
        for (var i in list) {
159
            if (list[i].getName() === name) {
160
                return true;
161
            }
162
        }
163
        return false;
164
    },
165
166
    /**
167
     * Returns an instance of JOII.Reflection.Method of a method by the
168
     * given name.
169
     *
170
     * @param  string name
171
     * @return JOII.Reflection.Method
172
     */
173
    'public getMethod': function(name) {
174
        var list = this.getMethods();
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
175
        for (var i in list) {
176
            if (list[i].getName() === name) {
177
                return list[i];
178
            }
179
        }
180
        throw 'Method "' + name + '" does not exist.';
181
    },
182
183
    /**
184
     * Returns an array of JOII.Reflection.Property based on the properties
185
     * defined in this class.
186
     *
187
     * @param  string filter Optional filter for 'private' or 'public'.
188
     * @return JOII.Reflection.Property[]
189
     */
190
    'public getProperties': function(filter) {
191
        var result = [];
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
192
        for (var i in this.proto) {
193
            if (typeof (this.proto[i]) !== 'function' && JOII.Compat.indexOf(JOII.InternalPropertyNames, i) === -1) {
194
                result.push(new JOII.Reflection.Property(this, i));
195
            }
196
        }
197
        return result;
198
    },
199
200
    /**
201
     * Returns an instance of JOII.Reflection.Property of a property by the
202
     * given name.
203
     *
204
     * @param  string name
205
     * @return JOII.Reflection.Property
206
     */
207
    'public getProperty': function(name) {
208
        var list = this.getProperties();
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
209
        for (var i in list) {
210
            if (list[i].getName() === name) {
211
                return list[i];
212
            }
213
        }
214
        throw 'Property "' + name + '" does not exist.';
215
    }
216
});
217
218
/**
219
 * Defines a property declared in a JOII class and provides meta
220
 * information about it.
221
 */
222
JOII.Reflection.Property = JOII.ClassBuilder({}, {
223
224
    /**
225
     * Represents the reflector of the owning class.
226
     *
227
     * @var JOII.Reflection.Class
228
     */
229
    'protected nullable object reflector': null,
230
231
    /**
232
     * Represents the metadata of this property.
233
     *
234
     * @var object
235
     */
236
    'protected nullable object meta': null,
237
238
    /**
239
     * Represents the name of the property.
240
     *
241
     * @var string
242
     */
243
    'public read string name': null,
244
245
    /**
246
     * Constructor.
247
     *
248
     * @param JOII.Reflection.Class reflector
249
     * @param string property_name
250
     */
251
    'protected __construct': function(reflector, property_name) {
252
        this.reflector = reflector;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
253
        this.name = property_name;
254
        this.meta = reflector.getMeta().metadata[property_name];
255
256
        // If we, for some strange reason don't have metadata, fill it in
257
        // with some default values.
258
        if (typeof (this.meta) === 'undefined') {
259
            this.meta = {
260
                name        : this.name,
261
                type        : null,
262
                visibility  : 'public',
263
                is_nullable : false,
264
                is_abstract : false,
265
                is_read_only: false,
266
                is_final    : false
267
            };
268
        }
269
270
        // Attempt to fetch the type by fetching the predefined value.
271
        // However, only do this for non-nullable types to avoid type
272
        // mismatching exceptions in setters.
273
        if (this.meta.type === null && this.meta.is_nullable === false) {
274
            this.meta.type = typeof (this.reflector.getProto()[this.meta.name]);
275
        }
276
    },
277
278
    /**
279
     * Returns the type of the property.
280
     *
281
     * @return string
282
     */
283
    'public getType': function() {
284
        return this.meta.type;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
285
    },
286
287
    /**
288
     * Returns true if the property is abstract.
289
     *
290
     * @return bool
291
     */
292
    'public isAbstract': function() {
293
        return this.meta.is_abstract;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
294
    },
295
296
    /**
297
     * Returns true if the property is static.
298
     *
299
     * @return bool
300
     */
301
    'public isStatic': function() {
302
        return this.meta.is_static;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
303
    },
304
305
    /**
306
     * Returns true if the property is nullable.
307
     *
308
     * @return bool
309
     */
310
    'public isNullable': function() {
311
        return this.meta.is_nullable;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
312
    },
313
314
    /**
315
     * Returns true if the property is final.
316
     *
317
     * @return bool
318
     */
319
    'public isFinal': function() {
320
        return this.meta.is_final;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
321
    },
322
323
    /**
324
     * Returns true if the property is private.
325
     *
326
     * @return bool
327
     */
328
    'public isPrivate': function() {
329
        return this.meta.visibility === 'private';
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
330
    },
331
332
    /**
333
     * Returns true if the property is protected.
334
     *
335
     * @return bool
336
     */
337
    'public isProtected': function() {
338
        return this.meta.visibility === 'protected';
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
339
    },
340
341
    /**
342
     * Returns true if the property is public.
343
     *
344
     * @return bool
345
     */
346
    'public isPublic': function() {
347
        return this.meta.visibility === 'public';
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
348
    },
349
350
    /**
351
     * Returns true if the property is public.
352
     *
353
     * @return bool
354
     */
355
    'public isImmutable': function() {
356
        return this.meta.is_read_only;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
357
    },
358
359
    /**
360
     * Returns true if the property is a constant.
361
     *
362
     * @return bool
363
     */
364
    'public isConstant': function() {
365
        return this.meta.is_constant;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
366
    },
367
368
    /**
369
     * Returns true if the given type matches the type of this property.
370
     *
371
     * @param string type
372
     * @return bool
373
     */
374
    'public isType': function(type) {
375
        return type === this.meta.type;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
376
    },
377
378
    /**
379
     * Returns the visibility of the property as a string.
380
     *
381
     * @return string
382
     */
383
    'public getVisibility': function() {
384
        return this.meta.visibility;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
385
    },
386
387
    /**
388
     * Returns a string representation of this object.
389
     *
390
     * @return string
391
     */
392
    toString: function() {
393
        var name_parts = [],
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
394
            proto_ref  = this.reflector.getProto()[this.name],
395
            name       = '',
396
            body       = '';
397
398
        if (this.meta.is_abstract) { name_parts.push('abstract'); }
399
        if (this.meta.is_final) { name_parts.push('final'); }
400
401
        name_parts.push(this.meta.visibility);
402
        
403
        if (this.meta.is_static) { name_parts.push('static'); }
404
405
        if (this.meta.is_nullable) { name_parts.push('nullable'); }
406
        if (this.meta.is_read_only) { name_parts.push('read'); }
407
408
        // If type === null, attempt to detect it by the predefined value.
409
        if (this.meta.type === null) {
410
            if (proto_ref === null) {
411
                name_parts.push('mixed');
412
            } else {
413
                name_parts.push(typeof (proto_ref));
414
            }
415
        } else {
416
            name_parts.push(this.meta.type);
417
        }
418
419
        name_parts.push('"' + this.meta.name + '"');
420
        name = name_parts.join(' ');
421
422
        if (typeof (proto_ref) === 'function') {
423
            body = '[Function]';
424
        } else if (typeof (proto_ref) === 'object' && proto_ref !== null) {
425
            body = '[Object (' + proto_ref.length + ')]';
426
        } else if (typeof (proto_ref) === 'string') {
427
            body = '"' + proto_ref + '"';
428
        } else {
429
            body = proto_ref;
430
        }
431
        return name + ': ' + body;
432
    }
433
});
434
435
JOII.Reflection.Method = JOII.ClassBuilder({ 'extends': JOII.Reflection.Property }, {
436
437
    /**
438
     * Returns an array of strings based on the parameters defined in
439
     * the declared function.
440
     *
441
     * @return string[]
442
     */
443
    'public getParameters': function() {
444
        var FN_ARGS        = /^function\s*[^\(]*\(\s*([^\)]*)\)/m,
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
445
            FN_ARG_SPLIT   = /,/,
446
            FN_ARG         = /^\s*(_?)(\S+?)\1\s*$/,
447
            STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg,
448
            getParams      = function(fn) {
449
                var fnText, argDecl;
450
                var args = [];
451
                fnText = fn.toString().replace(STRIP_COMMENTS, '');
452
                argDecl = fnText.match(FN_ARGS);
453
454
                var r = argDecl[1].split(FN_ARG_SPLIT), repl = function(all, underscore, name) {
455
                    args.push(name);
456
                };
457
                for (var a in r) {
458
                    var arg = r[a];
459
                    arg.replace(FN_ARG, repl);
460
                }
461
462
                return args;
463
            };
464
465
        var prototype = this.reflector.getProto();
466
        var overloads = prototype.__joii__.metadata[this.name].overloads;
467
468
        if (!overloads || overloads.length === 0) {
469
            // old method for BC (wasn't recognized as a function when prototyping)
470
            return getParams(this.reflector.getProto()[this.name]);
471
        } else if (overloads.length === 1 && overloads[0].parameters.length === 0) {
472
            // old method for BC (was recognized when prototyping, but old style)
473
            return getParams(overloads[0].fn);
474
        }
475
        else {
476
            var ret = [];
477
478
            for (var idx = 0; idx < overloads.length; idx++) {
479
                var fn_meta = [];
480
                var function_parameters_meta = overloads[idx];
481
                var parsed_params = getParams(function_parameters_meta.fn);
482
                for (var j = 0; j < function_parameters_meta.parameters.length; j++) {
483
                    var param = {
484
                        name: parsed_params.length > j ? parsed_params[j] : null,
485
                        type: function_parameters_meta.parameters[j]
486
                    };
487
                    fn_meta.push(param);
488
                }
489
                ret.push(fn_meta);
490
            }
491
            return ret;
492
        }
493
    },
494
495
    /**
496
     * Returns the body of this method as a string.
497
     *
498
     * @return string
499
     */
500
    'public getBodyAsString': function(f) {
501
        var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg,
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
502
            fn_text        = this.reflector.getProto()[this.name].toString().replace(STRIP_COMMENTS, '');
503
504
        return fn_text.substr(fn_text.indexOf('{') + 1, fn_text.lastIndexOf('}') - 4).replace(/}([^}]*)$/, '$1');
505
    },
506
507
    /**
508
     * Returns true if the function body contains "arguments", making
509
     * a _guess_ the function uses variadic arguments.
510
     *
511
     * This is only used in toString() to show an indication of the
512
     * function signature. Do NOT rely on this in functional code!!
513
     *
514
     * @return bool
515
     */
516
    'public usesVariadicArguments': function() {
517
        var data = this.reflector.getProto()[this.name].toString();
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
518
        return data.match(/[\(|\.|\ ](arguments)[\)|\.|\,|\ |]/g);
519
    },
520
521
    /**
522
     * Returns a string representation of the method.
523
     *
524
     * @return string
525
     */
526
    toString: function() {
527
528
        // Get the "declaration" part of the method.
529
        var prefix = this['super']('toString').split(':')[0],
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
530
            body   = '[Function',
531
            args   = this.getParameters(),
532
            is_var = this.usesVariadicArguments();
533
534
        if (args.length > 0 && typeof (args[0]) === 'object') {
535
            // right now, this is spitting out every overload's signature one after another, each on a new line.
536
            // should probably find a better way to do this
537
            for (var idx = 0; idx < args.length; idx++) {
538
                var function_parameters_meta = args[idx];
539
540
                body += ' (';
541
542
                var first_time = true;
543
                for (var i = 0; i < function_parameters_meta.length; i++) {
544
                    if (!first_time) {
545
                        body += ', ';
546
                    }
547
                    first_time = false;
548
                    body += function_parameters_meta[i].type;
549
                    if (function_parameters_meta[i].name !== null) {
550
                        body += " " + function_parameters_meta[i].name;
551
                        is_var = true;
552
                    }
553
                }
554
555
556
                var data = this.reflector.getProto().__joii__.metadata[this.name].overloads[idx].fn.toString();
557
                is_var = data.match(/[\(|\.|\ ](arguments)[\)|\.|\,|\ |]/g);
558
559
                if (is_var) {
560
                    body += ', ...';
561
                }
562
                body += ')\n';
563
            }
564
        } else if (args.length > 0) {
565
            body += ' (' + args.join(', ');
566
            if (is_var) {
567
                body += ', ...';
568
            }
569
            body += ')';
570
        } else if (args.length === 0 && is_var) {
571
            body += ' (...)';
572
        }
573
574
        body += ']';
575
        return prefix + ': ' + body;
576
    }
577
});
578